0, 'skipped' => 0, 'redirects' => 0, 'db_restored' => 0, 'warnings' => array( __( 'No backup rows found for this batch.', 'ailinkmaster-internal-auto-manager' ) ), ); } // Disable Reconciler + bulk mode during rollback. if ( class_exists( 'AILM_Reconciler' ) ) { AILM_Reconciler::disable(); } if ( class_exists( 'AILM_Link_Repository' ) ) { AILM_Link_Repository::set_bulk_mode( true ); } try { // --- Step 1: Restore content (posts, postmeta, nav menus) --- foreach ( $backups as $b ) { // Skip pure log rows (no old_value AND no new value) and // link_rules rows (handled in step 2). if ( 'link_rules' === $b->object_type ) { continue; } // Skip option rows (permalink_structure is handled in step 4). if ( 'option' === $b->object_type ) { continue; } // Skip pure log rows that have neither old nor new value. if ( null === $b->old_value && null === $b->new_value ) { continue; } $restored_result = self::restore_backup_row( $b, $migration_batch ); if ( 'restored' === $restored_result['status'] ) { $restored++; } elseif ( 'skipped' === $restored_result['status'] ) { $skipped++; $warnings[] = $restored_result['message']; } } // --- Step 2: Restore plugin DB (link rules/links target_url) --- $db_restored = self::restore_plugin_db( $backups, $migration_batch ); // --- Step 3: Archive the batch's redirects --- $redirects_archived = AILM_URL_Redirect_DB::archive_batch( $migration_batch ); // --- Step 4: Restore old permalink structure --- // The migration backed up the old structure in a backup row with // field_name='permalink_structure'. Restore it so the site returns // to the pre-migration URL structure. $structure_restored = self::restore_permalink_structure( $backups ); AILM_URL_Redirect_DB::add_backup_row( $migration_batch, 'option', 0, 'ailm_permalink_snapshot', null, null, 'info', 'Rollback completed for batch ' . $migration_batch ); // --- Step 5: Rescan affected posts --- self::rescan_affected_posts( $backups ); } finally { // Re-enable Reconciler + bulk mode. if ( class_exists( 'AILM_Reconciler' ) ) { AILM_Reconciler::enable(); } if ( class_exists( 'AILM_Link_Repository' ) ) { AILM_Link_Repository::set_bulk_mode( false ); AILM_Link_Repository::invalidate_cache(); } } // Purge caches. self::purge_caches(); do_action( 'ailm_url_rollback_completed', $migration_batch ); return array( 'restored' => $restored, 'skipped' => $skipped, 'redirects' => $redirects_archived, 'db_restored' => $db_restored, 'warnings' => $warnings, ); } /** * Restore a single backup row, with the new_value_hash safety check. * * @param object $b Backup row object. * @param string $migration_batch Batch ID (for logging). * @return array { * @type string $status 'restored' | 'skipped' | 'noop'. * @type string $message Human-readable message (for skipped). * } */ private static function restore_backup_row( $b, $migration_batch ) { // Nav menu backups store a JSON summary of changes in new_value // (with old + new URLs per item). There is no single field to hash, // so we skip the safety check and restore directly from new_value. if ( 'nav_menu' === $b->object_type ) { self::restore_value( 'nav_menu', (int) $b->object_id, $b->field_name, $b->new_value ); return array( 'status' => 'restored', 'message' => '' ); } // Get the current value of the field. $current = AILM_URL_Redirect_DB::get_current_value( $b->object_type, (int) $b->object_id, $b->field_name ); // SAFETY CHECK (remark B): if the current content differs from what we // wrote during migration, the user has edited it since — do NOT overwrite. if ( null !== $b->new_value_hash && is_string( $current ) ) { $current_hash = md5( $current ); if ( $current_hash !== $b->new_value_hash ) { $msg = sprintf( /* translators: 1: object type, 2: object ID, 3: field name */ __( 'Skipped rollback of %1$s #%2$d (%3$s): content changed after migration.', 'ailinkmaster-internal-auto-manager' ), $b->object_type, (int) $b->object_id, $b->field_name ); AILM_URL_Redirect_DB::add_backup_row( $migration_batch, $b->object_type, (int) $b->object_id, $b->field_name, null, null, 'warning', $msg ); return array( 'status' => 'skipped', 'message' => $msg ); } } // Restore the old value. self::restore_value( $b->object_type, (int) $b->object_id, $b->field_name, $b->old_value ); return array( 'status' => 'restored', 'message' => '' ); } /** * Restore an old value to its storage location. * * @param string $object_type 'post' | 'term' | 'nav_menu'. * @param int $object_id Object ID. * @param string $field_name Field name. * @param string $old_value The old value to restore. * @return void */ private static function restore_value( $object_type, $object_id, $field_name, $old_value ) { global $wpdb; switch ( $object_type ) { case 'post': if ( 'post_content' === $field_name ) { // Direct SQL (no wp_update_post storm). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_content = %s WHERE ID = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $old_value, $object_id ) ); clean_post_cache( $object_id ); } else { // postmeta field. update_post_meta( $object_id, $field_name, $old_value ); } break; case 'nav_menu': // The backup new_value is a JSON summary of changes; to rollback // we restore each item's old _menu_item_url. $changes = json_decode( $old_value, true ); if ( is_array( $changes ) ) { foreach ( $changes as $change ) { if ( isset( $change['item_id'], $change['old'] ) ) { update_post_meta( (int) $change['item_id'], '_menu_item_url', $change['old'] ); } } } break; case 'term': if ( 'description' === $field_name ) { // wp_update_term() requires the taxonomy — resolve it from the // term. Passing an empty string (the previous code) triggers a // WP_Error ("Invalid taxonomy"). $term = get_term( $object_id ); if ( $term && ! is_wp_error( $term ) && taxonomy_exists( $term->taxonomy ) ) { wp_update_term( $object_id, $term->taxonomy, array( 'description' => $old_value ) ); } } else { update_term_meta( $object_id, $field_name, $old_value ); } break; } } /** * Restore old target_url values in the plugin's link rules/links tables. * * @param array $backups Backup rows for the batch. * @param string $migration_batch Batch ID (for logging). * @return int Number of rows restored. */ private static function restore_plugin_db( $backups, $migration_batch ) { if ( ! class_exists( 'AILM_Link_DB' ) ) { return 0; } global $wpdb; $rules_table = AILM_Link_DB::get_rules_table(); $links_table = AILM_Link_DB::get_links_table(); $restored = 0; foreach ( $backups as $b ) { if ( 'link_rules' !== $b->object_type ) { continue; } if ( null === $b->old_value || null === $b->new_value ) { continue; } // Restore old target_url in rules. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $r = $wpdb->query( $wpdb->prepare( "UPDATE {$rules_table} SET target_url = %s WHERE id = %d AND target_url = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $b->old_value, (int) $b->object_id, $b->new_value ) ); if ( false !== $r ) { $restored += $r; } // Restore old url in links (by matching the new value). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $l = $wpdb->query( $wpdb->prepare( "UPDATE {$links_table} SET url = %s WHERE url = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $b->old_value, $b->new_value ) ); if ( false !== $l ) { $restored += $l; } } return $restored; } /** * Restore the old permalink structure from the backup rows. * * The migration stored the old structure (as JSON) in a backup row with * field_name='permalink_structure'. We decode it, restore the WP options, * flush rewrite rules, and re-capture the snapshot. * * @param array $backups Backup rows for the batch. * @return bool True if the structure was restored. */ private static function restore_permalink_structure( $backups ) { foreach ( $backups as $b ) { if ( 'option' !== $b->object_type || 'permalink_structure' !== $b->field_name ) { continue; } if ( null === $b->old_value ) { continue; } $old = json_decode( $b->old_value, true ); if ( ! is_array( $old ) || ! isset( $old['structure'] ) ) { continue; } // Restore the WP permalink options. update_option( 'permalink_structure', $old['structure'] ); if ( isset( $old['category_base'] ) ) { update_option( 'category_base', $old['category_base'] ); } if ( isset( $old['tag_base'] ) ) { update_option( 'tag_base', $old['tag_base'] ); } // Flush rewrite rules so WP uses the restored structure. flush_rewrite_rules( true ); // Re-capture the snapshot to match the restored structure. AILM_URL_Snapshot::capture_current(); return true; } return false; } /** * Rescan affected posts after rollback so wp_ailm_links reflects reality. * * @param array $backups Backup rows. * @return void */ private static function rescan_affected_posts( $backups ) { $scanned = array(); foreach ( $backups as $b ) { if ( 'post' !== $b->object_type || ! $b->object_id ) { continue; } $pid = (int) $b->object_id; if ( isset( $scanned[ $pid ] ) ) { continue; } $scanned[ $pid ] = true; if ( class_exists( 'AILM_Scout_Scanner' ) ) { AILM_Scout_Scanner::process_post( $pid ); } } } /** * Purge page caches after rollback. * * @return void */ private static function purge_caches() { if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } if ( did_action( 'litespeed_init' ) || class_exists( 'LiteSpeed\Purge' ) ) { do_action( 'litespeed_purge_all' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound } if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } } }