Choose an Elementor event template during or after migration
All checks were successful
WordPress release / release (push) Successful in 7s

This commit is contained in:
josh
2026-11-12 15:34:43 -05:00
parent 6df287d530
commit db4747d85f
8 changed files with 242 additions and 47 deletions

View File

@@ -47,7 +47,7 @@ final class Api {
}
}
private static function table(): string {
public static function table(): string {
global $wpdb;
return $wpdb->prefix . 'eventlabs_external_ids';
}
@@ -309,6 +309,9 @@ final class Api {
if ( is_wp_error( $result ) ) {
return $result;
}
if ( ! $id && 'events' === $resource && Legacy_Migration::SOURCE === ( $identity[0] ?? '' ) && ! empty( $payload['migration_template_id'] ) ) {
Single_Event_Templates::assign( $saved_id, (int) $payload['migration_template_id'] );
}
if ( $identity && ! $id && ! $wpdb->insert( self::table(), [
'sync_key' => $sync_key, 'post_id' => $saved_id, 'source' => $identity[0], 'external_id' => $identity[1],
], [ '%s', '%d', '%s', '%s' ] ) ) {

View File

@@ -16,6 +16,7 @@ final class Legacy_Migration {
private const EVENT = 'tribe_events';
private const CATEGORY = 'tribe_events_cat';
private const BATCH = 8;
private const TEMPLATE_BATCH = 100;
private const ACTION = 'burn_events_legacy_migrate';
private const TYPES = [ 'venue' => 'tribe_venue', 'organizer' => 'tribe_organizer', 'series' => 'tribe_event_series' ];
@@ -50,24 +51,60 @@ final class Legacy_Migration {
return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status NOT IN ('trash', 'auto-draft', 'inherit')", self::EVENT ) );
}
private static function copied_count(): int {
global $wpdb;
$table = Api::table();
return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table x JOIN $wpdb->posts p ON p.ID = x.post_id WHERE x.source = %s AND p.post_type = %s AND p.post_status NOT IN ('trash', 'auto-draft', 'inherit')", self::SOURCE, Events::EVENT ) );
}
public static function section(): void {
$count = self::count();
if ( ! $count && ! self::installed() ) {
$copies = self::copied_count();
if ( ! $count && ! $copies && ! self::installed() ) {
return;
}
$templates = $count || $copies ? Single_Event_Templates::migration_templates() : [];
?>
<section id="eventlabs-migration">
<h2><?php esc_html_e( 'Migrate from The Events Calendar', 'burn-events' ); ?></h2>
<p><a href="<?php echo esc_url( 'https://git.labs.casa/josh/eventLabs/src/branch/main/docs/MIGRATION.md' ); ?>"><?php esc_html_e( 'Read the migration guide', 'burn-events' ); ?></a></p>
<?php if ( ! $count ) : ?>
<p><?php esc_html_e( 'The plugin is installed. No events are available to migrate.', 'burn-events' ); ?></p>
<p><?php esc_html_e( 'No source events are available to copy.', 'burn-events' ); ?></p>
<?php else : ?>
<p><?php printf( esc_html( _n( '%s source event found.', '%s source events found.', $count, 'burn-events' ) ), esc_html( number_format_i18n( $count ) ) ); ?></p>
<p><?php esc_html_e( 'Copy events as drafts with their saved dates, linked venues, organizers and series, categories, tags, and featured images. The source events stay available for review.', 'burn-events' ); ?></p>
<p><?php esc_html_e( 'Imported recurring events use the dates already saved in the source calendar. Set a new repeat rule to generate new dates.', 'burn-events' ); ?></p>
<form data-be-migrate action="<?php echo esc_url( admin_url( 'admin-ajax.php', 'relative' ) ); ?>" method="post">
<?php endif; ?>
<?php if ( $copies ) : ?>
<p><?php printf( esc_html( _n( '%s event already copied.', '%s events already copied.', $copies, 'burn-events' ) ), esc_html( number_format_i18n( $copies ) ) ); ?></p>
<?php endif; ?>
<?php if ( ( $count || $copies ) && ! $templates ) : ?>
<p><?php esc_html_e( 'To choose an Elementor layout here, activate Elementor and publish a layout under', 'burn-events' ); ?> <a href="<?php echo esc_url( Single_Event_Templates::list_url() ); ?>"><?php esc_html_e( 'Single Event Templates', 'burn-events' ); ?></a>.</p>
<?php endif; ?>
<?php if ( $count || ( $copies && $templates ) ) : ?>
<form data-be-migrate data-has-copies="<?php echo $copies ? '1' : '0'; ?>" action="<?php echo esc_url( admin_url( 'admin-ajax.php', 'relative' ) ); ?>" method="post">
<input type="hidden" name="action" value="<?php echo esc_attr( self::ACTION ); ?>">
<?php wp_nonce_field( self::ACTION ); ?>
<button type="submit" class="button button-primary" disabled><?php esc_html_e( 'Copy events as drafts', 'burn-events' ); ?></button>
<?php if ( $templates ) : ?>
<p>
<label for="be-migration-template"><?php esc_html_e( 'Single event template', 'burn-events' ); ?></label>
<select id="be-migration-template" name="template_id" aria-describedby="be-migration-template-help">
<option value="0"><?php esc_html_e( 'Inherit site default', 'burn-events' ); ?></option>
<?php foreach ( $templates as $template ) : ?>
<option value="<?php echo esc_attr( (string) $template->ID ); ?>"><?php echo esc_html( get_the_title( $template ) ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p class="description" id="be-migration-template-help"><?php esc_html_e( 'Choose a template for new copies. Applying it to copied events replaces their current template, including on published events.', 'burn-events' ); ?></p>
<?php endif; ?>
<p>
<?php if ( $count ) : ?>
<button type="submit" class="button button-primary" data-be-operation value="copy" disabled><?php esc_html_e( 'Copy events as drafts', 'burn-events' ); ?></button>
<?php endif; ?>
<?php if ( $templates ) : ?>
<button type="submit" class="button" data-be-operation value="apply" disabled><?php esc_html_e( 'Apply template to copied events', 'burn-events' ); ?></button>
<?php endif; ?>
</p>
<p data-be-migrate-status role="status"></p>
<ul data-be-migrate-errors></ul>
</form>
@@ -83,11 +120,20 @@ final class Legacy_Migration {
wp_send_json_error( __( 'Refresh the migration page and sign in as an administrator.', 'burn-events' ), $signed_in ? 403 : 401 );
}
$after = is_scalar( $_POST['after'] ?? null ) ? absint( $_POST['after'] ) : 0;
$operation = $_POST['operation'] ?? 'copy';
$template_id = $_POST['template_id'] ?? '0';
if ( ! in_array( $operation, [ 'copy', 'apply' ], true ) || ! is_scalar( $template_id ) || ! ctype_digit( (string) $template_id ) ) {
wp_send_json_error( __( 'Choose a valid migration action and template.', 'burn-events' ), 400 );
}
$template_id = (int) $template_id;
// Other plugins may print during post creation. Keep that output out of the JSON response.
$buffer_level = ob_get_level();
ob_start();
try {
$result = self::batch( $after );
$result = 'apply' === $operation ? self::apply_template_batch( $after, $template_id ) : self::batch( $after, $template_id );
} catch ( InvalidArgumentException $error ) {
$validation_error = $error->getMessage();
$result = null;
} catch ( \Throwable ) {
$result = null;
} finally {
@@ -95,14 +141,21 @@ final class Legacy_Migration {
// Close only buffers opened while copying this batch.
}
}
if ( isset( $validation_error ) ) {
wp_send_json_error( $validation_error, 400 );
}
if ( null === $result ) {
wp_send_json_error( __( 'The source calendar could not be read. You can retry the migration.', 'burn-events' ), 500 );
$message = 'apply' === $operation ? __( 'The copied events could not be updated. You can retry the template assignment.', 'burn-events' ) : __( 'The source calendar could not be read. You can retry the migration.', 'burn-events' );
wp_send_json_error( $message, 500 );
}
wp_send_json_success( $result );
}
public static function batch( int $after ): array {
public static function batch( int $after, int $template_id = 0 ): array {
global $wpdb;
if ( $template_id ) {
self::require_template( $template_id );
}
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND ID > %d AND post_status NOT IN ('trash', 'auto-draft', 'inherit') ORDER BY ID LIMIT %d", self::EVENT, $after, self::BATCH + 1 ) );
if ( $wpdb->last_error ) {
throw new RuntimeException( 'Source event query failed.' );
@@ -112,7 +165,7 @@ final class Legacy_Migration {
foreach ( array_slice( $ids, 0, self::BATCH ) as $id ) {
$after = (int) $id;
try {
++$result[ self::event( $after ) ];
++$result[ self::event( $after, $template_id ) ];
} catch ( InvalidArgumentException $error ) {
$result['errors'][] = sprintf( __( 'Event #%1$d — %2$s', 'burn-events' ), $after, $error->getMessage() );
} catch ( \Throwable $error ) {
@@ -123,7 +176,43 @@ final class Legacy_Migration {
return $result;
}
private static function event( int $id ): string {
private static function require_template( int $template_id ): void {
if ( ! Single_Event_Templates::can_use_for_migration( $template_id ) ) {
throw new InvalidArgumentException( __( 'Choose a published template built with Elementor. Refresh the migration page if the template changed.', 'burn-events' ) );
}
}
public static function apply_template_batch( int $after, int $template_id ): array {
global $wpdb;
self::require_template( $template_id );
$table = Api::table();
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM $table x JOIN $wpdb->posts p ON p.ID = x.post_id WHERE x.source = %s AND p.post_type = %s AND p.ID > %d AND p.post_status NOT IN ('trash', 'auto-draft', 'inherit') ORDER BY p.ID LIMIT %d", self::SOURCE, Events::EVENT, $after, self::TEMPLATE_BATCH + 1 ) );
if ( $wpdb->last_error ) {
throw new RuntimeException( 'Copied event query failed.' );
}
$result = [ 'applied' => 0, 'skipped' => 0, 'errors' => [], 'next' => 0 ];
foreach ( array_slice( $ids, 0, self::TEMPLATE_BATCH ) as $id ) {
$after = (int) $id;
if ( ! current_user_can( 'edit_post', $after ) ) {
$result['errors'][] = sprintf( __( 'You cannot edit event #%d.', 'burn-events' ), $after );
continue;
}
try {
if ( Single_Event_Templates::assign( $after, $template_id ) ) {
clean_post_cache( $after );
++$result['applied'];
} else {
++$result['skipped'];
}
} catch ( \Throwable ) {
$result['errors'][] = sprintf( __( 'The template could not be assigned to event #%d.', 'burn-events' ), $after );
}
}
$result['next'] = count( $ids ) > self::TEMPLATE_BATCH ? $after : 0;
return $result;
}
private static function event( int $id, int $template_id ): string {
$source = get_post( $id );
if ( ! $source || self::EVENT !== $source->post_type || 'trash' === $source->post_status ) {
throw new InvalidArgumentException( __( 'The source event is unavailable.', 'burn-events' ) );
@@ -146,6 +235,9 @@ final class Legacy_Migration {
'hidden' => self::flag( $id, '_EventHideFromUpcoming' ),
'metadata' => [ 'migration' => [ 'source_post_id' => $id, 'source_status' => $source->post_status, 'saved_dates' => count( $dates ) ] ],
];
if ( $template_id ) {
$payload['migration_template_id'] = $template_id;
}
foreach ( self::TYPES as $type => $post_type ) {
$payload[ $type ] = [];
$links = 'series' === $type ? self::series_ids( $id ) : get_post_meta( $id, '_Event' . ucfirst( $type ) . 'ID', false );

View File

@@ -4,6 +4,7 @@ namespace BurnEvents;
use Elementor\Core\Base\Document;
use Elementor\Plugin as Elementor;
use InvalidArgumentException;
use WP_Post;
defined( 'ABSPATH' ) || exit;
@@ -151,6 +152,41 @@ final class Single_Event_Templates {
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 '';
@@ -216,16 +252,7 @@ final class Single_Event_Templates {
public static function assignment_box( WP_Post $post ): void {
$selected = self::get_assignment( $post->ID );
$templates = get_posts(
[
'post_type' => self::POST_TYPE,
'post_status' => 'publish',
'has_password' => false,
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
]
);
$templates = self::published_templates();
wp_nonce_field( 'burn_events_save_assignment', 'burn_events_assignment_nonce' );
?>
<label class="screen-reader-text" for="burn-events-assignment"><?php esc_html_e( 'Single Event Template', 'burn-events' ); ?></label>