array( 'name' => __( 'Reports', 'spotsend' ), 'singular_name' => __( 'Report', 'spotsend' ), 'edit_item' => __( 'Report', 'spotsend' ), 'all_items' => __( 'All reports', 'spotsend' ), 'search_items' => __( 'Search reports', 'spotsend' ), 'not_found' => __( 'No reports yet.', 'spotsend' ), 'not_found_in_trash' => __( 'No reports in the Trash.', 'spotsend' ), 'menu_name' => __( 'Reports', 'spotsend' ), ), 'public' => false, 'show_ui' => true, 'show_in_rest' => false, 'menu_icon' => 'dashicons-flag', 'menu_position' => 22, 'supports' => array( 'title' ), 'capability_type' => 'post', 'capabilities' => array( 'create_posts' => 'do_not_allow' ), 'map_meta_cap' => true, ) ); } ); /** * Edit screen notices that speak of reports. A published report is a resolved one, and reports * have no public page, so no "View" links. */ add_filter( 'post_updated_messages', static function ( array $messages ): array { $updated = __( 'Report updated.', 'spotsend' ); /* translators: %s: what a resolved report is called, e.g. "Resolved" */ $resolved = sprintf( __( 'Report status changed to "%s".', 'spotsend' ), done_label() ); $messages[ POST_TYPE ] = array_replace( $messages['post'] ?? array(), array( 1 => $updated, 4 => $updated, 6 => $resolved, 7 => __( 'Report saved.', 'spotsend' ), 8 => $updated, 9 => $updated, 10 => $updated, ) ); return $messages; } ); /** * List screen notices after bulk actions, with the count of reports. */ add_filter( 'bulk_post_updated_messages', static function ( array $messages, array $counts ): array { $messages[ POST_TYPE ] = array( /* translators: %s: number of reports */ 'updated' => _n( '%s report updated.', '%s reports updated.', $counts['updated'], 'spotsend' ), /* translators: %s: number of reports */ 'locked' => _n( '%s report not updated, somebody is editing it.', '%s reports not updated, somebody is editing them.', $counts['locked'], 'spotsend' ), /* translators: %s: number of reports */ 'deleted' => _n( '%s report permanently deleted.', '%s reports permanently deleted.', $counts['deleted'], 'spotsend' ), /* translators: %s: number of reports */ 'trashed' => _n( '%s report moved to the Trash.', '%s reports moved to the Trash.', $counts['trashed'], 'spotsend' ), /* translators: %s: number of reports */ 'untrashed' => _n( '%s report restored from the Trash.', '%s reports restored from the Trash.', $counts['untrashed'], 'spotsend' ), ); return $messages; }, 10, 2 ); /** * Report fields as stored in post meta. * * `photos` are attachment IDs in the order they were added, one `raplace_photo` row each; * `photo` is the first of them, 0 when there is none. `resolved_on` is the "Y-m-d" day it was * resolved, empty while it waits. * * @return array{categories:string[], description:string, reporter:string, email:string, lat:string, lng:string, photo:int, photos:int[], resolved:bool, resolved_on:string} */ function get_report( int $post_id ): array { $photos = array_values( array_filter( array_map( 'intval', get_post_meta( $post_id, 'raplace_photo', false ) ) ) ); return array( 'categories' => array_filter( (array) get_post_meta( $post_id, 'raplace_categories', true ) ), 'description' => (string) get_post_meta( $post_id, 'raplace_description', true ), 'reporter' => (string) get_post_meta( $post_id, 'raplace_reporter', true ), 'email' => (string) get_post_meta( $post_id, 'raplace_email', true ), 'lat' => (string) get_post_meta( $post_id, 'raplace_lat', true ), 'lng' => (string) get_post_meta( $post_id, 'raplace_lng', true ), 'photo' => $photos[0] ?? 0, 'photos' => $photos, 'resolved' => 'publish' === get_post_status( $post_id ), 'resolved_on' => (string) get_post_meta( $post_id, 'raplace_resolved', true ), ); } // The day a report is resolved, however it gets there: the resolve button, or Publish on its screen. add_action( 'transition_post_status', static function ( string $new_status, string $old_status, \WP_Post $post ) { if ( POST_TYPE !== $post->post_type ) { return; } if ( 'publish' === $new_status && 'publish' !== $old_status ) { update_post_meta( $post->ID, 'raplace_resolved', current_time( 'Y-m-d' ) ); } elseif ( 'publish' !== $new_status && 'publish' === $old_status ) { delete_post_meta( $post->ID, 'raplace_resolved' ); } }, 10, 3 ); function map_link( string $lat, string $lng ): string { switch ( setting( 'map' ) ) { case 'google': return "https://www.google.com/maps/search/?api=1&query=$lat,$lng"; case 'apple': return "https://maps.apple.com/?q=$lat,$lng&ll=$lat,$lng"; default: return "https://www.openstreetmap.org/?mlat=$lat&mlon=$lng#map=16/$lat/$lng"; } }