array( 'name' => __( 'Events', 'eventwright' ), 'singular_name' => __( 'Event', 'eventwright' ), 'add_new' => __( 'Add event', 'eventwright' ), 'add_new_item' => __( 'Add new event', 'eventwright' ), 'edit_item' => __( 'Edit event', 'eventwright' ), 'new_item' => __( 'New event', 'eventwright' ), 'view_item' => __( 'View event', 'eventwright' ), 'view_items' => __( 'View events', 'eventwright' ), 'search_items' => __( 'Search events', 'eventwright' ), 'not_found' => __( 'No events found.', 'eventwright' ), 'not_found_in_trash' => __( 'No events found in Trash.', 'eventwright' ), 'all_items' => __( 'All events', 'eventwright' ), 'archives' => __( 'Event archives', 'eventwright' ), 'insert_into_item' => __( 'Insert into event', 'eventwright' ), 'uploaded_to_this_item' => __( 'Uploaded to this event', 'eventwright' ), 'filter_items_list' => __( 'Filter events list', 'eventwright' ), 'items_list' => __( 'Events list', 'eventwright' ), 'item_published' => __( 'Event published.', 'eventwright' ), 'item_updated' => __( 'Event updated.', 'eventwright' ), 'menu_name' => __( 'Events', 'eventwright' ), ), 'public' => true, 'show_in_rest' => true, 'has_archive' => false, 'menu_icon' => 'dashicons-calendar-alt', 'menu_position' => 26, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'custom-fields', 'revisions', 'comments' ), 'taxonomies' => array( 'post_tag' ), 'rewrite' => array( 'slug' => $s['event_slug'], 'with_front' => false, ), 'capability_type' => 'post', 'map_meta_cap' => true, ) ); register_post_type( self::VENUE, array( 'labels' => array( 'name' => __( 'Venues', 'eventwright' ), 'singular_name' => __( 'Venue', 'eventwright' ), 'add_new' => __( 'Add venue', 'eventwright' ), 'add_new_item' => __( 'Add new venue', 'eventwright' ), 'edit_item' => __( 'Edit venue', 'eventwright' ), 'view_item' => __( 'View venue', 'eventwright' ), 'search_items' => __( 'Search venues', 'eventwright' ), 'not_found' => __( 'No venues found.', 'eventwright' ), 'not_found_in_trash' => __( 'No venues found in Trash.', 'eventwright' ), 'all_items' => __( 'Venues', 'eventwright' ), ), 'public' => true, 'show_in_menu' => 'edit.php?post_type=' . self::EVENT, 'show_in_rest' => true, 'has_archive' => false, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), 'rewrite' => array( 'slug' => $s['venue_slug'], 'with_front' => false, ), 'capability_type' => 'post', 'map_meta_cap' => true, ) ); register_post_type( self::ORGANIZER, array( 'labels' => array( 'name' => __( 'Organizers', 'eventwright' ), 'singular_name' => __( 'Organizer', 'eventwright' ), 'add_new' => __( 'Add organizer', 'eventwright' ), 'add_new_item' => __( 'Add new organizer', 'eventwright' ), 'edit_item' => __( 'Edit organizer', 'eventwright' ), 'view_item' => __( 'View organizer', 'eventwright' ), 'search_items' => __( 'Search organizers', 'eventwright' ), 'not_found' => __( 'No organizers found.', 'eventwright' ), 'not_found_in_trash' => __( 'No organizers found in Trash.', 'eventwright' ), 'all_items' => __( 'Organizers', 'eventwright' ), ), 'public' => true, 'show_in_menu' => 'edit.php?post_type=' . self::EVENT, 'show_in_rest' => true, 'has_archive' => false, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), 'rewrite' => array( 'slug' => $s['organizer_slug'], 'with_front' => false, ), 'capability_type' => 'post', 'map_meta_cap' => true, ) ); register_post_type( self::ATTENDEE, array( 'labels' => array( 'name' => __( 'Attendees', 'eventwright' ), 'singular_name' => __( 'Attendee', 'eventwright' ), 'all_items' => __( 'Attendees', 'eventwright' ), 'edit_item' => __( 'Edit attendee', 'eventwright' ), 'search_items' => __( 'Search attendees', 'eventwright' ), 'not_found' => __( 'No attendees yet.', 'eventwright' ), ), 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => true, 'show_in_menu' => 'edit.php?post_type=' . self::EVENT, 'show_in_rest' => false, 'show_in_nav_menus' => false, 'supports' => array( 'title' ), 'rewrite' => false, 'query_var' => false, 'capability_type' => 'post', 'capabilities' => array( 'create_posts' => 'do_not_allow' ), 'map_meta_cap' => true, ) ); register_taxonomy( self::CATEGORY, array( self::EVENT ), array( 'labels' => array( 'name' => __( 'Event categories', 'eventwright' ), 'singular_name' => __( 'Event category', 'eventwright' ), 'menu_name' => __( 'Categories', 'eventwright' ), 'all_items' => __( 'All categories', 'eventwright' ), 'edit_item' => __( 'Edit category', 'eventwright' ), 'add_new_item' => __( 'Add new category', 'eventwright' ), 'search_items' => __( 'Search categories', 'eventwright' ), 'not_found' => __( 'No categories found.', 'eventwright' ), ), 'hierarchical' => true, 'public' => true, 'show_in_rest' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => $s['category_base'], 'with_front' => false, 'hierarchical' => true, ), ) ); register_taxonomy_for_object_type( 'post_tag', self::EVENT ); self::register_meta(); } /** * Venues, organisers and attendees use the classic editor screen. * * @param bool $use Whether to use the block editor. * @param string $post_type Post type. * @return bool */ public static function block_editor_for_type( $use, $post_type ) { if ( in_array( $post_type, array( self::VENUE, self::ORGANIZER, self::ATTENDEE ), true ) ) { return false; } return $use; } /** * Meta fields for each type: key => [type, default, sanitize]. * * @return array */ public static function event_meta_fields() { return array( '_evwr_start' => array( 'string', '' ), '_evwr_end' => array( 'string', '' ), '_evwr_all_day' => array( 'boolean', false ), '_evwr_timezone' => array( 'string', '' ), '_evwr_venue_id' => array( 'integer', 0 ), '_evwr_organizer_ids' => array( 'array', array() ), '_evwr_cost' => array( 'string', '' ), '_evwr_website' => array( 'string', '' ), '_evwr_online_url' => array( 'string', '' ), '_evwr_status' => array( 'string', 'scheduled' ), '_evwr_recurrence' => array( 'string', '' ), '_evwr_hide_from_lists' => array( 'boolean', false ), '_evwr_rsvp_enabled' => array( 'boolean', false ), '_evwr_rsvp_waitlist' => array( 'boolean', false ), '_evwr_rsvp_capacity' => array( 'integer', 0 ), '_evwr_rsvp_max_per' => array( 'integer', 10 ), '_evwr_rsvp_closes' => array( 'string', '' ), ); } /** * Venue meta fields. * * @return array */ public static function venue_meta_fields() { return array( '_evwr_address' => array( 'string', '' ), '_evwr_city' => array( 'string', '' ), '_evwr_state' => array( 'string', '' ), '_evwr_zip' => array( 'string', '' ), '_evwr_country' => array( 'string', '' ), '_evwr_phone' => array( 'string', '' ), '_evwr_website' => array( 'string', '' ), '_evwr_lat' => array( 'number', 0 ), '_evwr_lng' => array( 'number', 0 ), ); } /** * Organizer meta fields. * * @return array */ public static function organizer_meta_fields() { return array( '_evwr_phone' => array( 'string', '' ), '_evwr_website' => array( 'string', '' ), '_evwr_email' => array( 'string', '' ), ); } /** * Register meta so the block editor can read and write it. */ private static function register_meta() { $groups = array( self::EVENT => self::event_meta_fields(), self::VENUE => self::venue_meta_fields(), self::ORGANIZER => self::organizer_meta_fields(), ); foreach ( $groups as $post_type => $fields ) { foreach ( $fields as $key => $def ) { list( $type, $default ) = $def; $args = array( 'type' => $type, 'single' => true, 'default' => $default, 'sanitize_callback' => array( __CLASS__, 'sanitize_meta' ), 'auth_callback' => array( __CLASS__, 'auth_meta' ), 'show_in_rest' => true, ); if ( 'array' === $type ) { $args['show_in_rest'] = array( 'schema' => array( 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ); } register_post_meta( $post_type, $key, $args ); } } } /** * Only people who can edit the post may change its meta. * * @param bool $allowed Allowed. * @param string $meta_key Key. * @param int $post_id Post. * @return bool */ public static function auth_meta( $allowed, $meta_key, $post_id ) { return current_user_can( 'edit_post', $post_id ); } /** * Sanitise meta by key. * * @param mixed $value Value. * @param string $meta_key Key. * @return mixed */ public static function sanitize_meta( $value, $meta_key ) { switch ( $meta_key ) { case '_evwr_start': case '_evwr_end': case '_evwr_rsvp_closes': return eventwright_sanitize_local_datetime( $value ); case '_evwr_all_day': case '_evwr_rsvp_enabled': case '_evwr_rsvp_waitlist': case '_evwr_hide_from_lists': return (bool) $value; case '_evwr_timezone': return eventwright_sanitize_timezone( $value ); case '_evwr_venue_id': case '_evwr_rsvp_capacity': return absint( $value ); case '_evwr_rsvp_max_per': return max( 1, min( 100, absint( $value ) ) ); case '_evwr_organizer_ids': $ids = is_array( $value ) ? $value : ( '' === $value || null === $value ? array() : array( $value ) ); return array_values( array_unique( array_filter( array_map( 'absint', $ids ) ) ) ); case '_evwr_website': case '_evwr_online_url': return esc_url_raw( (string) $value ); case '_evwr_email': return sanitize_email( (string) $value ); case '_evwr_status': return in_array( $value, array( 'scheduled', 'cancelled', 'postponed', 'moved_online' ), true ) ? $value : 'scheduled'; case '_evwr_recurrence': return Recurrence::sanitize_json( $value ); case '_evwr_lat': case '_evwr_lng': return is_numeric( $value ) ? (float) $value : 0; default: return sanitize_text_field( (string) $value ); } } }