[ 'name' => __( 'Single Event Templates', 'burn-events' ), 'singular_name' => __( 'Single Event Template', 'burn-events' ), 'add_new_item' => __( 'Add Single Event Template', 'burn-events' ), 'edit_item' => __( 'Edit Single Event Template', 'burn-events' ), ], 'public' => false, 'publicly_queryable' => true, 'exclude_from_search' => true, 'show_ui' => true, 'show_in_menu' => 'edit.php?post_type=' . self::EVENT, 'rewrite' => false, 'map_meta_cap' => false, 'capabilities' => array_fill_keys( [ 'edit_post', 'read_post', 'delete_post', 'edit_posts', 'edit_others_posts', 'publish_posts', 'read_private_posts', 'create_posts' ], self::CAPABILITY ), 'supports' => [ 'title', 'editor', 'revisions', 'elementor' ], ] ); } public static function list_url(): string { return admin_url( 'edit.php?post_type=' . self::POST_TYPE ); } public static function protect_template_preview(): void { if ( is_singular( self::POST_TYPE ) ) { nocache_headers(); if ( ! current_user_can( 'edit_post', get_queried_object_id() ) ) { wp_die( esc_html__( 'Page not found.', 'burn-events' ), '', [ 'response' => 404 ] ); } } } public static function register_widget_category( $manager ): void { $manager->add_category( 'burn-events', [ 'title' => 'eventLabs', 'icon' => 'eicon-calendar' ] ); } public static function order_widget_category( array $settings ): array { $categories = $settings['initial_document']['panel']['elements_categories'] ?? []; if ( isset( $categories['burn-events'] ) ) { $favorites = array_intersect_key( $categories, [ 'favorites' => true ] ); $settings['initial_document']['panel']['elements_categories'] = $favorites + [ 'burn-events' => $categories['burn-events'] ] + $categories; } return $settings; } public static function register_widgets( $manager ): void { require_once __DIR__ . '/class-elementor-widgets.php'; $widgets = [ new Calendar_Widget(), new Event_List_Widget(), new Event_Details_Widget(), new Event_Title_Widget(), new Event_Date_Widget(), new Event_Image_Widget(), new Event_Description_Widget(), new Event_Venue_Widget(), new Event_Organizer_Widget(), new Event_Cost_Widget(), new Event_Calendar_Links_Widget(), new Event_Related_Widget(), new Countdown_Widget(), new Community_Widget(), new Event_Field_Widget(), ]; foreach ( $widgets as $widget ) { $manager->register( $widget ); } } private static function sample_event( int $template_id ): ?WP_Post { $sample_id = (int) get_post_meta( $template_id, self::SAMPLE_META, true ); $sample = $sample_id ? get_post( $sample_id ) : null; if ( $sample && self::EVENT === $sample->post_type && 'publish' === $sample->post_status && '' === $sample->post_password ) { return $sample; } $events = get_posts( [ 'post_type' => self::EVENT, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => 1, ] ); return $events[0] ?? null; } public static function widget_context( int $id = 0 ): array { if ( ! $id && self::$rendering_event ) { return [ self::$rendering_event, self::$rendering_occurrence ]; } $post = $id ? get_post( $id ) : get_post(); if ( $post && ! $id && self::POST_TYPE === $post->post_type && current_user_can( 'edit_post', $post->ID ) ) { $post = self::sample_event( $post->ID ); } if ( ! $post || self::EVENT !== $post->post_type || post_password_required( $post ) || ( 'publish' !== $post->post_status && ! current_user_can( 'read_post', $post->ID ) ) ) { return [ null, null ]; } return [ $post, Events::occurrence( $post->ID, Calendar::request( 'be_occ' ) ) ]; } private static function is_layout( ?WP_Post $template ): bool { return $template && self::POST_TYPE === $template->post_type && 'publish' === $template->post_status && '' === $template->post_password; } private static function is_elementor_ready(): bool { return did_action( 'elementor/loaded' ) && class_exists( Elementor::class ); } private static function get_assignment( int $event_id ): int { return (int) get_post_meta( $event_id, self::EVENT_TEMPLATE_META, true ); } private static function published_templates(): array { return get_posts( [ 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', ] ); } public static function migration_templates(): array { if ( ! self::is_elementor_ready() ) { return []; } return array_filter( self::published_templates(), static fn( WP_Post $post ): bool => self::can_use_for_migration( $post->ID ) ); } public static function can_use_for_migration( int $template_id ): bool { if ( $template_id < 1 || ! self::is_elementor_ready() || ! self::is_layout( get_post( $template_id ) ) ) { return false; } $document = Elementor::instance()->documents->get( $template_id ); $data = get_post_meta( $template_id, Document::ELEMENTOR_DATA_META_KEY, true ); $elements = is_string( $data ) ? json_decode( $data, true ) : null; return $document instanceof Document && $document->is_built_with_elementor() && is_array( $elements ) && $elements && array_is_list( $elements ); } public static function assign( int $event_id, int $template_id ): bool { if ( $event_id < 1 || self::EVENT !== get_post_type( $event_id ) || $template_id < 1 || ! self::is_layout( get_post( $template_id ) ) ) { throw new InvalidArgumentException( __( 'The event or published template is unavailable.', 'burn-events' ) ); } if ( self::get_assignment( $event_id ) === $template_id ) { return false; } Events::write_meta( $event_id, self::EVENT_TEMPLATE_META, (string) $template_id ); return true; } public static function render( WP_Post $event, ?array $row ): string { if ( ! self::is_elementor_ready() || Elementor::instance()->preview->is_preview_mode() ) { return ''; } $assignment = self::get_assignment( $event->ID ); if ( -1 === $assignment ) { return ''; } $default = (int) get_option( self::DEFAULT_OPTION, 0 ); $frontend = Elementor::instance()->frontend; foreach ( array_unique( array_filter( [ $assignment, $default ] ) ) as $template_id ) { if ( ! self::is_layout( get_post( $template_id ) ) ) { continue; } self::$rendering_event = $event; self::$rendering_occurrence = $row; try { $markup = $frontend->get_builder_content_for_display( $template_id ); } finally { self::$rendering_event = null; self::$rendering_occurrence = null; } if ( trim( $markup ) ) { add_action( 'wp_enqueue_scripts', [ $frontend, 'enqueue_styles' ] ); return $markup; } } return ''; } public static function add_preview_box(): void { add_meta_box( 'burn-events-preview', __( 'Preview event', 'burn-events' ), [ self::class, 'preview_box' ], self::POST_TYPE, 'side' ); } public static function preview_box( WP_Post $post ): void { $sample = (int) get_post_meta( $post->ID, self::SAMPLE_META, true ); wp_nonce_field( 'burn_events_save_preview', 'burn_events_preview_nonce' ); ?>
post_type && 'publish' === $sample->post_status && '' === $sample->post_password ) { update_post_meta( $post_id, self::SAMPLE_META, $sample_id ); } else { delete_post_meta( $post_id, self::SAMPLE_META ); } } public static function add_assignment_box(): void { add_meta_box( 'burn-events-template', __( 'Single Event Template', 'burn-events' ), [ self::class, 'assignment_box' ], self::EVENT, 'side' ); } public static function assignment_box( WP_Post $post ): void { $selected = self::get_assignment( $post->ID ); $templates = self::published_templates(); wp_nonce_field( 'burn_events_save_assignment', 'burn_events_assignment_nonce' ); ?> 0 && self::is_layout( get_post( $value ) ) ) ) { update_post_meta( $post_id, self::EVENT_TEMPLATE_META, $value ); } else { delete_post_meta( $post_id, self::EVENT_TEMPLATE_META ); } } private static function can_save( int $id, string $field, string $action ): bool { return ! wp_is_post_autosave( $id ) && ! wp_is_post_revision( $id ) && current_user_can( 'edit_post', $id ) && isset( $_POST[ $field ] ) && is_string( $_POST[ $field ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $field ] ) ), $action ); } public static function builder_notice(): void { $screen = get_current_screen(); if ( ! $screen || self::POST_TYPE !== $screen->post_type || ! current_user_can( self::CAPABILITY ) ) { return; } if ( ! self::is_elementor_ready() ) { echo '' . esc_html__( 'Activate Elementor to design and publish Single Event templates.', 'burn-events' ) . '