Keep calendar migration retryable when requests fail
All checks were successful
WordPress release / release (push) Successful in 5s

This commit is contained in:
josh
2026-11-12 13:14:26 -05:00
parent 91a81e761b
commit c3db26ecf9
3 changed files with 78 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ final class Legacy_Migration {
public static function register(): void {
add_action( 'admin_notices', [ self::class, 'notice' ] );
add_action( 'wp_ajax_' . self::ACTION, [ self::class, 'ajax' ] );
add_action( 'wp_ajax_nopriv_' . self::ACTION, [ self::class, 'ajax' ] );
}
public static function notice(): void {
@@ -63,7 +64,7 @@ final class Legacy_Migration {
<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' ) ); ?>" method="post">
<form data-be-migrate 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>
@@ -77,15 +78,27 @@ final class Legacy_Migration {
}
public static function ajax(): void {
if ( ! current_user_can( 'manage_options' ) || ! check_ajax_referer( self::ACTION, '_wpnonce', false ) ) {
wp_send_json_error( __( 'Refresh the migration page and sign in as an administrator.', 'burn-events' ), 403 );
$signed_in = is_user_logged_in();
if ( ! $signed_in || ! current_user_can( 'manage_options' ) || ! check_ajax_referer( self::ACTION, '_wpnonce', false ) ) {
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;
// Other plugins may print during post creation. Keep that output out of the JSON response.
$buffer_level = ob_get_level();
ob_start();
try {
wp_send_json_success( self::batch( $after ) );
} catch ( \Throwable $error ) {
$result = self::batch( $after );
} catch ( \Throwable ) {
$result = null;
} finally {
while ( ob_get_level() > $buffer_level && ob_end_clean() ) {
// Close only buffers opened while copying this batch.
}
}
if ( null === $result ) {
wp_send_json_error( __( 'The source calendar could not be read. You can retry the migration.', 'burn-events' ), 500 );
}
wp_send_json_success( $result );
}
public static function batch( int $after ): array {