Files
josh d15dce1e84
All checks were successful
WordPress release / release (push) Successful in 5s
Loop Builder release / release (push) Successful in 4s
Add Elementor event loop preset library and separate releases
2026-11-13 11:00:14 -05:00

307 lines
14 KiB
PHP

<?php
namespace EventLabs\LoopBuilder;
use BurnEvents\Events;
use Elementor\Core\Base\Document;
use Elementor\Plugin as Elementor;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Templates {
public const POST_TYPE = 'eventlabs_loop_item';
public const CAPABILITY = 'edit_theme_options';
public const LAYOUT_META = '_eventlabs_loop_layout';
public const STARTER_ACTION = 'eventlabs_loop_create_starter';
private const PREVIEW_META = '_eventlabs_loop_preview_event';
private const PREVIEW_NONCE = 'eventlabs_loop_preview_nonce';
private const PREVIEW_ACTION = 'eventlabs_loop_save_preview';
private static ?Card $card = null;
private static array $preview_cards = [];
public static function register(): void {
add_action( 'init', [ self::class, 'register_type' ], 11 );
add_action( 'template_redirect', [ self::class, 'protect_preview' ], 0 );
add_action( 'add_meta_boxes_' . self::POST_TYPE, [ self::class, 'add_preview_box' ] );
add_action( 'save_post_' . self::POST_TYPE, [ self::class, 'save_preview' ] );
add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', [ self::class, 'layout_column' ], 10, 2 );
add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', [ self::class, 'columns' ] );
add_filter( 'post_row_actions', [ self::class, 'row_actions' ], 10, 2 );
add_action( 'admin_post_' . self::STARTER_ACTION, [ self::class, 'create_starter' ] );
add_action( 'admin_post_eventlabs_loop_duplicate', [ self::class, 'duplicate' ] );
add_filter( 'plugin_action_links_' . plugin_basename( FILE ), [ self::class, 'links' ] );
}
public static function register_type(): void {
register_post_type( self::POST_TYPE, [
'labels' => [
'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[] = '<a href="' . esc_url( admin_url( 'edit.php?post_type=' . self::POST_TYPE ) ) . '">'
. esc_html__( 'Loop Templates', 'eventlabs-loop-builder' ) . '</a>';
}
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 );
?>
<p>
<label for="eventlabs-loop-preview-event"><?php esc_html_e( 'Preview event ID', 'eventlabs-loop-builder' ); ?></label>
<input class="widefat" id="eventlabs-loop-preview-event" name="eventlabs_loop_preview_event" type="text" inputmode="numeric"
pattern="[0-9]+" list="eventlabs-loop-recent-events" aria-describedby="eventlabs-loop-preview-help" value="<?php echo $id ? esc_attr( (string) $id ) : ''; ?>">
<datalist id="eventlabs-loop-recent-events">
<?php foreach ( self::recent_events( 60 ) as $event ) : ?>
<option value="<?php echo esc_attr( (string) $event->ID ); ?>"><?php echo esc_html( get_the_title( $event ) ); ?></option>
<?php endforeach; ?>
</datalist>
</p>
<p class="description" id="eventlabs-loop-preview-help"><?php esc_html_e( 'Choose a recent event or enter the ID of any published event. Leave blank to preview the next event automatically.', 'eventlabs-loop-builder' ); ?></p>
<?php
}
public static function save_preview( int $template_id ): void {
$nonce = $_POST[ self::PREVIEW_NONCE ] ?? '';
if ( ! is_string( $nonce ) || ! wp_verify_nonce( wp_unslash( $nonce ), self::PREVIEW_ACTION )
|| ! current_user_can( 'edit_post', $template_id ) || wp_is_post_autosave( $template_id ) || wp_is_post_revision( $template_id ) ) {
return;
}
$input = $_POST['eventlabs_loop_preview_event'] ?? '';
$id = is_string( $input ) && ctype_digit( $input ) ? (int) $input : 0;
if ( $id && self::is_preview_event( get_post( $id ) ) ) {
update_post_meta( $template_id, self::PREVIEW_META, $id );
} elseif ( '' === $input || '0' === $input ) {
delete_post_meta( $template_id, self::PREVIEW_META );
}
}
public static function columns( array $columns ): array {
return array_slice( $columns, 0, 2, true ) + [ 'ell_layout' => __( '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'] = '<a href="' . esc_url( wp_nonce_url( $url, 'eventlabs_loop_duplicate_' . $post->ID ) ) . '">' . esc_html__( 'Duplicate', 'eventlabs-loop-builder' ) . '</a>';
}
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;
}
}
}