| 1 |
<?php |
| 2 |
/** |
| 3 |
* Clear the cached pattern library on demand. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\BlockPatterns\Cleanup; |
| 9 |
|
| 10 |
use Templately\Modules\BlockPatterns\PatternSync; |
| 11 |
use Templately\Modules\Utilities\Cleanup\CleanupTask; |
| 12 |
use Templately\Modules\Utilities\Cleanup\Context; |
| 13 |
use Templately\Modules\Utilities\Cleanup\Remover; |
| 14 |
use Templately\Modules\Utilities\Cleanup\RetentionKind; |
| 15 |
use Templately\Modules\Utilities\Cleanup\TaskRegistry; |
| 16 |
use Templately\Modules\Utilities\Cleanup\TaskResult; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
/** |
| 20 |
* MANUAL ONLY — `schedulable` is false, and that is the whole point. |
| 21 |
* |
| 22 |
* This module already prunes its own orphaned content files at sync time, and |
| 23 |
* the cache is keyed by plan, so there is no leak here to fix on a schedule. |
| 24 |
* What this adds is an on-demand "rebuild the library" button for the Utilities |
| 25 |
* tab. Running it unattended would be actively harmful: every cleared pattern |
| 26 |
* is re-fetched from the cloud, so a nightly sweep would turn a working cache |
| 27 |
* into a nightly download. |
| 28 |
*/ |
| 29 |
class PatternCacheTask implements CleanupTask { |
| 30 |
|
| 31 |
public static function register(): void { |
| 32 |
add_action( |
| 33 |
TaskRegistry::COLLECT_ACTION, |
| 34 |
static function ( TaskRegistry $registry ) { |
| 35 |
$registry->register_classes( [ self::class ] ); |
| 36 |
} |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function descriptor(): array { |
| 41 |
return [ |
| 42 |
'id' => 'block-patterns-cache', |
| 43 |
'label' => __( 'Cached pattern library', 'templately' ), |
| 44 |
'group' => 'cache', |
| 45 |
'scope' => 'both', |
| 46 |
'retention_kind' => RetentionKind::INTRINSIC, |
| 47 |
'destructive' => true, |
| 48 |
'schedulable' => false, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
public function estimate( Context $context ): TaskResult { |
| 53 |
return $this->clear( $context->with_dry_run( true ) ); |
| 54 |
} |
| 55 |
|
| 56 |
public function run( Context $context ): TaskResult { |
| 57 |
return $this->clear( $context ); |
| 58 |
} |
| 59 |
|
| 60 |
private function clear( Context $context ): TaskResult { |
| 61 |
$sync = PatternSync::get_instance(); |
| 62 |
$dir = $sync->content_dir(); |
| 63 |
|
| 64 |
$result = TaskResult::empty(); |
| 65 |
|
| 66 |
$files = glob( rtrim( $dir, '/\\' ) . '/*.php' ); |
| 67 |
foreach ( (array) $files as $file ) { |
| 68 |
if ( 'index' === basename( $file, '.php' ) ) { |
| 69 |
// The directory's own guard file — never a candidate. |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$outcome = Remover::delete_file( $file, $context ); |
| 74 |
|
| 75 |
if ( $outcome instanceof WP_Error ) { |
| 76 |
$result->fail( sprintf( '%s: %s', basename( $file ), $outcome->get_error_message() ) ); |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
$result->merge( $outcome ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! $context->is_dry_run() ) { |
| 84 |
$sync->invalidate_all(); |
| 85 |
} |
| 86 |
|
| 87 |
return $result; |
| 88 |
} |
| 89 |
} |
| 90 |
|