| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Tools; |
| 5 |
|
| 6 |
/** |
| 7 |
* Resets Imagify internal state (bulk transients, process locks, scheduled jobs). |
| 8 |
* |
| 9 |
* This service clears only optimization/job state. It intentionally does NOT |
| 10 |
* touch user-data caches, settings, API key, or DB tables. |
| 11 |
*/ |
| 12 |
class ResetInternalState { |
| 13 |
|
| 14 |
/** |
| 15 |
* Performs the full internal-state reset. |
| 16 |
* |
| 17 |
* Actions performed in order: |
| 18 |
* 1. Delete each bulk-running-state transient by name. |
| 19 |
* 2. Delete all process-lock / RPC transients via LIKE patterns (options table). |
| 20 |
* 3. On multisite: also delete site-transient process locks (sitemeta table). |
| 21 |
* 4. Unschedule all ActionScheduler actions for each registered hook. |
| 22 |
* |
| 23 |
* All DB queries use $wpdb->prepare() + $wpdb->esc_like() — no raw interpolation. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function reset(): void { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
// 1. Delete named bulk transients. |
| 31 |
foreach ( InternalStateList::get_bulk_transients() as $transient ) { |
| 32 |
delete_transient( $transient ); |
| 33 |
} |
| 34 |
|
| 35 |
// 2. Delete process-lock and legacy RPC transients via LIKE patterns in wp_options. |
| 36 |
foreach ( InternalStateList::get_locked_transient_patterns() as $raw_pattern ) { |
| 37 |
// Build a safe LIKE pattern: esc_like() each literal segment, preserve % wildcards. |
| 38 |
$like_pattern = implode( '%', array_map( [ $wpdb, 'esc_like' ], explode( '%', $raw_pattern ) ) ); |
| 39 |
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 40 |
$wpdb->prepare( |
| 41 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery |
| 42 |
$like_pattern |
| 43 |
) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
// 3. On multisite, also clean up sitemeta process locks. |
| 48 |
if ( is_multisite() ) { |
| 49 |
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 50 |
$wpdb->prepare( |
| 51 |
"DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery |
| 52 |
$wpdb->esc_like( '_site_transient_imagify_' ) . '%_process_lock%' |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
// 4. Unschedule ActionScheduler jobs. |
| 58 |
foreach ( InternalStateList::get_scheduler_hooks() as $hook ) { |
| 59 |
if ( function_exists( 'as_unschedule_all_actions' ) ) { |
| 60 |
as_unschedule_all_actions( $hook ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
do_action( 'imagify_after_reset_internal_state' ); |
| 65 |
} |
| 66 |
} |
| 67 |
|