OpenStreetMap contributors'; const DEFAULT_PLACE_NAMES = 'https://nominatim.openstreetmap.org/reverse'; function setting_defaults(): array { return array( 'recipient' => '', 'categories' => array( __( 'Litter', 'spotsend' ), __( 'Damage', 'spotsend' ), __( 'Hazard', 'spotsend' ), __( 'Other', 'spotsend' ), ), 'map' => 'openstreetmap', 'max_mb' => 8, 'max_photos' => 3, 'per_hour' => 5, 'picker_center' => '', 'picker_zoom' => 12, 'tiles' => DEFAULT_TILES, 'tiles_attribution' => DEFAULT_ATTRIBUTION, 'place_names' => false, 'place_names_url' => DEFAULT_PLACE_NAMES, 'action_label' => '', 'done_label' => '', 'map_layers' => array(), 'confirm' => true, 'confirm_subject' => '', 'confirm_message' => '', ); } /** * Subject of the confirmation a visitor gets: the setting, or "Your report to ". */ function confirm_subject(): string { $subject = (string) setting( 'confirm_subject' ); /* translators: %s: the site's name */ return '' !== $subject ? $subject : sprintf( __( 'Your report to %s', 'spotsend' ), wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ); } function confirm_message(): string { $message = (string) setting( 'confirm_message' ); return '' !== $message ? $message : __( 'Thank you for your report. It has arrived, and we will look at it soon.', 'spotsend' ); } function setting( string $key ) { $saved = get_option( OPTION, array() ); return $saved[ $key ] ?? setting_defaults()[ $key ]; } /** * Where notifications go: the setting, or the site admin's address when it is empty. */ function recipient(): string { $email = (string) setting( 'recipient' ); return is_email( $email ) ? $email : (string) get_option( 'admin_email' ); } /** * @return string[] */ function categories(): array { return array_values( array_filter( array_map( 'trim', (array) setting( 'categories' ) ) ) ); } /** * What a resolved report is called, for example "Resolved" or "Visited". */ function done_label(): string { $label = (string) setting( 'done_label' ); return '' !== $label ? $label : __( 'Resolved', 'spotsend' ); } function max_bytes(): int { return min( (int) setting( 'max_mb' ) * MB_IN_BYTES, (int) wp_max_upload_size() ); } /** * How many photos a report takes: the setting, capped at the files PHP accepts in one request. */ function max_photos(): int { return min( (int) setting( 'max_photos' ), (int) ini_get( 'max_file_uploads' ) ); } /** * The largest request PHP accepts, in bytes; 0 when there is no limit. Above it PHP drops the * whole form, so the photos together must stay under it. */ function max_request_bytes(): int { return wp_convert_hr_to_bytes( (string) ini_get( 'post_max_size' ) ); } add_action( 'admin_init', static function () { register_setting( 'spotsend', OPTION, array( 'type' => 'array', 'sanitize_callback' => 'CommerceConsulting\SpotSend\sanitize_settings', 'default' => setting_defaults(), ) ); } ); /** * Ready-made categories with the wording for closing a report, offered under Categories. Empty * labels fall back to "Mark as resolved" and "Resolved". * * @return array */ function category_sets(): array { return array( 'problems' => array( 'name' => __( 'Problems to deal with', 'spotsend' ), 'categories' => array( __( 'Litter', 'spotsend' ), __( 'Pothole', 'spotsend' ), /* translators: a street light or lamp that does not work */ __( 'Broken light', 'spotsend' ), __( 'Fallen tree', 'spotsend' ), __( 'Other', 'spotsend' ), ), 'action_label' => '', 'done_label' => '', ), 'places' => array( 'name' => __( 'Places worth a visit', 'spotsend' ), 'categories' => array( __( 'Waterfall', 'spotsend' ), /* translators: a notable rock or boulder in the landscape, as a category of places worth a visit */ __( 'Rock', 'spotsend' ), __( 'Old building', 'spotsend' ), __( 'Viewpoint', 'spotsend' ), __( 'Other', 'spotsend' ), ), 'action_label' => __( 'Mark as visited', 'spotsend' ), /* translators: what a closed report about a place worth a visit is called */ 'done_label' => __( 'Visited', 'spotsend' ), ), 'sightings' => array( 'name' => __( 'Sightings and finds', 'spotsend' ), 'categories' => array( /* translators: wild animals seen somewhere, as a category of sightings */ __( 'Wildlife', 'spotsend' ), __( 'Rare plant', 'spotsend' ), __( 'Lost property', 'spotsend' ), __( 'Other', 'spotsend' ), ), 'action_label' => __( 'Mark as checked', 'spotsend' ), /* translators: what a closed report about a sighting or find is called */ 'done_label' => __( 'Checked', 'spotsend' ), ), ); } /** * Map services a report's coordinates can open in: key => name. */ function map_services(): array { return array( 'openstreetmap' => 'OpenStreetMap', 'google' => 'Google Maps', 'apple' => 'Apple Maps', ); } /** * The settings tabs in the order they are shown, each with the option keys it saves. * * @return array */ function settings_tabs(): array { return array( 'reports' => array( 'label' => __( 'Reports', 'spotsend' ), 'keys' => array( 'recipient', 'categories', 'action_label', 'done_label', 'map_layers' ), ), 'texts' => array( 'label' => __( 'Texts', 'spotsend' ), 'keys' => array( 'confirm', 'confirm_subject', 'confirm_message' ), ), 'map' => array( 'label' => __( 'Map', 'spotsend' ), 'keys' => array( 'map', 'picker_center', 'picker_zoom', 'tiles', 'tiles_attribution', 'place_names', 'place_names_url' ), ), 'limits' => array( 'label' => __( 'Limits', 'spotsend' ), 'keys' => array( 'max_mb', 'max_photos', 'per_hour' ), ), ); } /** * Merges what was submitted over the stored option. A tab form sends its name in "_tab", and then * all of that tab's keys count as submitted, so an unticked checkbox is off. Without it, only the * keys present in the input are changed. Keys of other tabs keep their stored value. */ function sanitize_settings( $input ): array { $input = is_array( $input ) ? $input : array(); $stored = get_option( OPTION, array() ); $stored = is_array( $stored ) ? $stored : array(); $tabs = settings_tabs(); $tab = (string) ( $input['_tab'] ?? '' ); $keys = isset( $tabs[ $tab ] ) ? $tabs[ $tab ]['keys'] : array_keys( $input ); // Without the layer selects (KML Places Map inactive) nothing was submitted for them. if ( ! isset( $input['map_layers'] ) ) { $keys = array_diff( $keys, array( 'map_layers' ) ); } // A newline-separated string from the settings form, or an array from code. $lines = $input['categories'] ?? array(); $lines = is_array( $lines ) ? $lines : preg_split( '/\R/', (string) $lines ); // A chosen set replaces the categories and the closing wording; the choice itself is not kept. $set = category_sets()[ (string) ( $input['category_set'] ?? '' ) ] ?? null; $categories = $set ? $set['categories'] : array_values( array_filter( array_map( 'sanitize_text_field', $lines ) ) ); $center = parse_coords( (string) ( $input['picker_center'] ?? '' ) ); $place_names = esc_url_raw( (string) ( $input['place_names_url'] ?? '' ), array( 'https' ) ); $submitted = array( 'recipient' => sanitize_email( $input['recipient'] ?? '' ), 'categories' => $categories, 'max_mb' => min( 50, max( 1, absint( $input['max_mb'] ?? 8 ) ) ), 'max_photos' => max( 1, absint( $input['max_photos'] ?? 3 ) ), 'per_hour' => min( 100, max( 1, absint( $input['per_hour'] ?? 5 ) ) ), 'map' => array_key_exists( $input['map'] ?? '', map_services() ) ? $input['map'] : 'openstreetmap', 'picker_center' => $center ? implode( ', ', $center ) : '', 'picker_zoom' => min( 19, max( 1, absint( $input['picker_zoom'] ?? 12 ) ) ), 'tiles' => sanitize_tiles( (string) ( $input['tiles'] ?? '' ) ), 'tiles_attribution' => wp_kses( (string) ( $input['tiles_attribution'] ?? '' ), array( 'a' => array( 'href' => true ) ) ), 'place_names' => ! empty( $input['place_names'] ), 'place_names_url' => '' !== $place_names ? $place_names : DEFAULT_PLACE_NAMES, 'action_label' => $set ? $set['action_label'] : sanitize_text_field( (string) ( $input['action_label'] ?? '' ) ), 'done_label' => $set ? $set['done_label'] : sanitize_text_field( (string) ( $input['done_label'] ?? '' ) ), 'map_layers' => sanitize_map_layers( (array) ( $input['map_layers'] ?? array() ) ), 'confirm' => ! empty( $input['confirm'] ), 'confirm_subject' => sanitize_text_field( (string) ( $input['confirm_subject'] ?? '' ) ), 'confirm_message' => sanitize_textarea_field( (string) ( $input['confirm_message'] ?? '' ) ), ); $merged = array_merge( $stored, array_intersect_key( $submitted, array_flip( $keys ) ) ); // Layers only for categories that are still there, all but "no category" when a set replaces them. if ( isset( $merged['map_layers'] ) ) { $kept = $set ? array() : (array) ( $merged['categories'] ?? setting_defaults()['categories'] ); $merged['map_layers'] = array_intersect_key( (array) $merged['map_layers'], array_flip( array_merge( $kept, array( NO_CATEGORY ) ) ) ); } return $merged; } /** * Category => layer slug, dropping categories that are not added to the map. */ function sanitize_map_layers( array $layers ): array { $out = array(); foreach ( $layers as $category => $layer ) { $layer = sanitize_title( (string) $layer ); if ( '' !== $layer ) { $out[ sanitize_text_field( (string) $category ) ] = $layer; } } return $out; } /** * An https tile URL template with {z}, {x} and {y}, or the OpenStreetMap default. */ function sanitize_tiles( string $url ): string { $url = trim( sanitize_text_field( $url ) ); foreach ( array( '{z}', '{x}', '{y}' ) as $part ) { if ( ! str_contains( $url, $part ) ) { return DEFAULT_TILES; } } return preg_match( '#^https://[^\\s"\'<>]+$#', $url ) ? $url : DEFAULT_TILES; } /** * Where the map for choosing a place opens: the configured point, or the whole world. * * @return array{center: float[], zoom: int} */ function picker_view(): array { $center = parse_coords( (string) setting( 'picker_center' ) ); return $center ? array( 'center' => $center, 'zoom' => (int) setting( 'picker_zoom' ), ) : array( 'center' => array( 20, 0 ), 'zoom' => 2, ); } add_action( 'admin_menu', static function () { add_submenu_page( 'edit.php?post_type=' . POST_TYPE, __( 'Report settings', 'spotsend' ), __( 'Settings', 'spotsend' ), 'manage_options', 'spotsend', __NAMESPACE__ . '\render_settings_page' ); } ); add_action( 'admin_enqueue_scripts', static function ( string $hook ) { if ( ! str_ends_with( $hook, '_page_spotsend' ) ) { return; } wp_enqueue_script( 'spotsend-settings', RAPLACE_URL . 'assets/settings.js', array(), asset_version( 'assets/settings.js' ), array( 'in_footer' => true ) ); } ); function render_settings_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $tabs = settings_tabs(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- selects which tab to show only. $tab = sanitize_key( wp_unslash( $_GET['tab'] ?? '' ) ); $tab = isset( $tabs[ $tab ] ) ? $tab : (string) array_key_first( $tabs ); $name = static fn( string $key ): string => OPTION . '[' . $key . ']'; ?>

> > >