| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Migration\Handlers; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Async_Operation\{ |
| 6 |
Async_Operation, |
| 7 |
Async_Operation_Hook, |
| 8 |
Queries\Image_Optimization_Operation_Query |
| 9 |
}; |
| 10 |
use ImageOptimization\Classes\Logger; |
| 11 |
use ImageOptimization\Classes\Migration\Migration; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Cleanup_Legacy_Bulk_Operations extends Migration { |
| 18 |
public static function get_name(): string { |
| 19 |
return 'cleanup_legacy_bulk_operations'; |
| 20 |
} |
| 21 |
|
| 22 |
public static function run(): bool { |
| 23 |
self::cleanup_operations( Async_Operation_Hook::OPTIMIZE_BULK ); |
| 24 |
self::cleanup_operations( Async_Operation_Hook::REOPTIMIZE_BULK ); |
| 25 |
|
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
private static function cleanup_operations( string $hook ): void { |
| 30 |
$query = ( new Image_Optimization_Operation_Query() ) |
| 31 |
->set_hook( $hook ) |
| 32 |
->set_status( [ |
| 33 |
Async_Operation::OPERATION_STATUS_PENDING, |
| 34 |
Async_Operation::OPERATION_STATUS_RUNNING, |
| 35 |
] ) |
| 36 |
->set_limit( -1 ); |
| 37 |
|
| 38 |
$operations = Async_Operation::get( $query ); |
| 39 |
|
| 40 |
if ( empty( $operations ) ) { |
| 41 |
Logger::info( sprintf( |
| 42 |
'No legacy operations found for hook %s', |
| 43 |
$hook |
| 44 |
) ); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$operation_ids = array_map( |
| 49 |
function( $operation ) { |
| 50 |
return $operation->get_id(); |
| 51 |
}, |
| 52 |
$operations |
| 53 |
); |
| 54 |
|
| 55 |
Async_Operation::remove( $operation_ids ); |
| 56 |
|
| 57 |
Logger::info( sprintf( |
| 58 |
'Removed %d legacy operations for hook %s', |
| 59 |
count( $operation_ids ), |
| 60 |
$hook |
| 61 |
) ); |
| 62 |
} |
| 63 |
} |
| 64 |
|