[ 'name' => __( 'Loop Templates', 'eventlabs-loop-builder' ), 'singular_name' => __( 'Loop Template', 'eventlabs-loop-builder' ), 'add_new' => __( 'Add Loop Template', 'eventlabs-loop-builder' ), 'add_new_item' => __( 'Add Loop Template', 'eventlabs-loop-builder' ), 'edit_item' => __( 'Edit Loop Template', 'eventlabs-loop-builder' ), 'view_item' => __( 'Preview Loop Template', 'eventlabs-loop-builder' ), 'search_items' => __( 'Search loop templates', 'eventlabs-loop-builder' ), 'not_found' => __( 'No loop templates found.', 'eventlabs-loop-builder' ), ], 'public' => false, 'publicly_queryable' => true, 'exclude_from_search' => true, 'show_ui' => true, 'show_in_menu' => 'edit.php?post_type=' . Events::EVENT, 'show_in_nav_menus' => false, 'show_in_admin_bar' => false, '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', 'edit_private_posts', 'edit_published_posts', 'delete_posts', 'delete_private_posts', 'delete_others_posts', 'delete_published_posts', ], self::CAPABILITY ), 'supports' => [ 'title', 'editor', 'revisions', 'elementor' ], ] ); } public static function protect_preview(): void { $singular = is_singular( self::POST_TYPE ); if ( ! $singular && ! in_array( self::POST_TYPE, (array) get_query_var( 'post_type' ), true ) ) { return; } nocache_headers(); if ( ! $singular || ! current_user_can( 'edit_post', get_queried_object_id() ) ) { wp_die( esc_html__( 'Page not found.', 'eventlabs-loop-builder' ), '', [ 'response' => 404 ] ); } } public static function links( array $links ): array { if ( current_user_can( self::CAPABILITY ) ) { $links[] = '' . esc_html__( 'Loop Templates', 'eventlabs-loop-builder' ) . ''; } return $links; } public static function is_editing(): bool { $elementor = Elementor::instance(); return $elementor->editor->is_edit_mode() || $elementor->preview->is_preview_mode(); } private static function is_template( ?WP_Post $post ): bool { return $post && self::POST_TYPE === $post->post_type; } public static function current_template(): ?WP_Post { $post = get_post(); if ( self::is_template( $post ) ) { return $post; } $document = Elementor::instance()->documents->get_current(); $post = $document ? $document->get_main_post() : null; return self::is_template( $post ) ? $post : null; } public static function active_card(): ?Card { if ( self::$card ) { return self::$card; } $template = self::current_template(); if ( ! $template || ! current_user_can( 'edit_post', $template->ID ) ) { return null; } if ( ! array_key_exists( $template->ID, self::$preview_cards ) ) { self::$preview_cards[ $template->ID ] = self::preview_card( $template->ID ); } return self::$preview_cards[ $template->ID ]; } public static function is_rendering_card(): bool { return null !== self::$card; } private static function preview_card( int $template_id ): ?Card { $id = (int) get_post_meta( $template_id, self::PREVIEW_META, true ); $event = $id ? get_post( $id ) : null; if ( self::is_preview_event( $event ) ) { return new Card( $event, Events::occurrence( $id ) ); } $row = Events::query( [ 'from' => gmdate( 'Y-m-d H:i:s' ), 'limit' => 1 ] )[0] ?? null; $event = $row ? get_post( (int) $row['event_id'] ) : null; if ( self::is_preview_event( $event ) ) { return new Card( $event, $row ); } $event = self::recent_events( 1 )[0] ?? null; return $event ? new Card( $event, Events::occurrence( $event->ID ) ) : null; } private static function is_preview_event( ?WP_Post $event ): bool { return $event && Events::EVENT === $event->post_type && 'publish' === $event->post_status && '' === $event->post_password; } private static function recent_events( int $limit ): array { return get_posts( [ 'post_type' => Events::EVENT, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => $limit, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ] ); } public static function add_preview_box(): void { add_meta_box( 'eventlabs-loop-preview', __( 'Preview event', 'eventlabs-loop-builder' ), [ self::class, 'preview_box' ], self::POST_TYPE, 'side' ); } public static function preview_box( WP_Post $post ): void { $id = (int) get_post_meta( $post->ID, self::PREVIEW_META, true ); wp_nonce_field( self::PREVIEW_ACTION, self::PREVIEW_NONCE ); ?>

__( 'Layout', 'eventlabs-loop-builder' ) ] + $columns; } public static function layout_column( string $column, int $id ): void { if ( 'ell_layout' === $column ) { $layout = (string) get_post_meta( $id, self::LAYOUT_META, true ); echo esc_html( Presets::layouts()[ $layout ] ?? __( 'Any layout', 'eventlabs-loop-builder' ) ); } } public static function row_actions( array $actions, WP_Post $post ): array { if ( self::is_template( $post ) && current_user_can( self::CAPABILITY ) && current_user_can( 'edit_post', $post->ID ) && 'trash' !== $post->post_status ) { $url = add_query_arg( [ 'action' => 'eventlabs_loop_duplicate', 'template_id' => $post->ID ], admin_url( 'admin-post.php' ) ); $actions['ell_duplicate'] = '' . esc_html__( 'Duplicate', 'eventlabs-loop-builder' ) . ''; } return $actions; } public static function create_starter(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You cannot create a loop template.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] ); } check_admin_referer( self::STARTER_ACTION ); $preset = isset( $_POST['preset'] ) && is_string( $_POST['preset'] ) ? sanitize_key( wp_unslash( $_POST['preset'] ) ) : ''; $style = Presets::catalog()[ $preset ] ?? null; if ( $style ) { $title = self::preset_title( $style ); $elements = Presets::elements( $preset ); $meta = [ self::LAYOUT_META => $style['layout'] ]; } elseif ( in_array( $preset, [ 'photo', 'compact' ], true ) ) { $title = 'compact' === $preset ? __( 'Compact event card', 'eventlabs-loop-builder' ) : __( 'Event photo card', 'eventlabs-loop-builder' ); $elements = require __DIR__ . '/starter-layout.php'; $meta = []; } else { wp_die( esc_html__( 'Choose a loop preset from the library.', 'eventlabs-loop-builder' ), '', [ 'response' => 400, 'back_link' => true ] ); } self::create_draft( $title, wp_json_encode( $elements, JSON_THROW_ON_ERROR ), $meta ); } public static function duplicate(): void { $id = isset( $_GET['template_id'] ) && is_string( $_GET['template_id'] ) ? absint( $_GET['template_id'] ) : 0; $post = $id ? get_post( $id ) : null; if ( ! self::is_template( $post ) || 'trash' === $post->post_status || ! current_user_can( self::CAPABILITY ) || ! current_user_can( 'edit_post', $id ) ) { wp_die( esc_html__( 'You cannot duplicate this loop template.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] ); } check_admin_referer( 'eventlabs_loop_duplicate_' . $id ); $meta = array_intersect_key( get_post_meta( $id ), array_flip( [ self::LAYOUT_META, self::PREVIEW_META, '_elementor_page_settings', '_wp_page_template' ] ) ); /* translators: %s: loop template title. */ $title = sprintf( __( '%s (copy)', 'eventlabs-loop-builder' ), $post->post_title ); $settings = array_map( static fn( array $values ) => maybe_unserialize( $values[0] ), $meta ); self::create_draft( $title, (string) get_post_meta( $id, Document::ELEMENTOR_DATA_META_KEY, true ) ?: '[]', $settings ); } private static function preset_title( array $style ): string { /* translators: 1: style family, 2: layout, 3: preset name. */ return sprintf( __( '%1$s %2$s ยท %3$s', 'eventlabs-loop-builder' ), Presets::families()[ $style['family'] ], Presets::layouts()[ $style['layout'] ], $style['title'] ); } private static function create_draft( string $title, string $elements, array $meta ): void { $id = wp_insert_post( [ 'post_type' => self::POST_TYPE, 'post_status' => 'draft', 'post_title' => wp_slash( $title ), 'meta_input' => wp_slash( [ Document::ELEMENTOR_DATA_META_KEY => $elements ] + $meta ), ], true ); if ( is_wp_error( $id ) ) { wp_die( esc_html( $id->get_error_message() ), '', [ 'response' => 500, 'back_link' => true ] ); } $document = Elementor::instance()->documents->get( $id ); if ( ! $document || get_post_meta( $id, Document::ELEMENTOR_DATA_META_KEY, true ) !== $elements ) { wp_die( esc_html__( 'WordPress could not save the loop layout.', 'eventlabs-loop-builder' ), '', [ 'response' => 500, 'back_link' => true ] ); } $document->set_is_built_with_elementor( true ); wp_safe_redirect( $document->get_edit_url() ); exit; } public static function published_layout( int $id ): ?WP_Post { $post = $id ? get_post( $id ) : null; if ( ! self::is_template( $post ) || 'publish' !== $post->post_status || '' !== $post->post_password ) { return null; } $document = Elementor::instance()->documents->get( $id ); return $document && $document->is_built_with_elementor() ? $post : null; } public static function options(): array { $templates = get_posts( [ 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'update_post_term_cache' => false, ] ); $options = [ '' => __( 'Built-in event card', 'eventlabs-loop-builder' ) ]; foreach ( Presets::catalog() as $slug => $style ) { $options[ $slug ] = self::preset_title( $style ); } foreach ( $templates as $template ) { if ( self::published_layout( $template->ID ) ) { $options[ $template->ID ] = get_the_title( $template ) ?: __( '(Untitled template)', 'eventlabs-loop-builder' ); } } return $options; } public static function render( WP_Post $template, Card $card ): string { $previous = self::$card; self::$card = $card; try { // Render only the saved layout. The card context supplies the event and occurrence to the dynamic fields. return Elementor::instance()->frontend->get_builder_content_for_display( $template->ID ); } finally { self::$card = $previous; } } }