| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Expiring_Store\User_Interface; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 6 |
use Yoast\WP\SEO\Expiring_Store\Application\Expiring_Store; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Runs expiring store cleanup on a recurring weekly WP-Cron schedule. |
| 11 |
*/ |
| 12 |
class Expiring_Store_Cleanup_Integration implements Integration_Interface { |
| 13 |
|
| 14 |
use No_Conditionals; |
| 15 |
|
| 16 |
public const CRON_HOOK = 'wpseo_expiring_store_cleanup'; |
| 17 |
|
| 18 |
/** |
| 19 |
* The expiring store. |
| 20 |
* |
| 21 |
* @var Expiring_Store |
| 22 |
*/ |
| 23 |
private $expiring_store; |
| 24 |
|
| 25 |
/** |
| 26 |
* The constructor. |
| 27 |
* |
| 28 |
* @param Expiring_Store $expiring_store The expiring store. |
| 29 |
*/ |
| 30 |
public function __construct( Expiring_Store $expiring_store ) { |
| 31 |
$this->expiring_store = $expiring_store; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Registers action hooks. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_hooks(): void { |
| 40 |
\add_action( 'admin_init', [ $this, 'schedule_cleanup' ] ); |
| 41 |
\add_action( self::CRON_HOOK, [ $this, 'run_cleanup' ] ); |
| 42 |
\add_action( 'wpseo_deactivate', [ $this, 'unschedule_cleanup' ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Schedules the weekly cleanup cron if not already scheduled. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function schedule_cleanup(): void { |
| 51 |
if ( ! \wp_next_scheduled( self::CRON_HOOK ) ) { |
| 52 |
\wp_schedule_event( \time(), 'weekly', self::CRON_HOOK ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Clears all scheduled cleanup events. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function unschedule_cleanup(): void { |
| 62 |
\wp_clear_scheduled_hook( self::CRON_HOOK ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Runs the expiring store cleanup. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function run_cleanup(): void { |
| 71 |
$this->expiring_store->cleanup_expired(); |
| 72 |
} |
| 73 |
} |
| 74 |
|