| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registers custom cron schedules and scheduled tasks related functionality in the container. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Cron; |
| 11 |
|
| 12 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 13 |
use SolidWP\Performance\Cron\Tasks\Clean_Expired_Cache_Files_Task; |
| 14 |
|
| 15 |
/** |
| 16 |
* Registers custom cron schedules and scheduled tasks related functionality in the container. |
| 17 |
* |
| 18 |
* @package SolidWP\Performance |
| 19 |
*/ |
| 20 |
final class Provider extends Service_Provider { |
| 21 |
|
| 22 |
public const SCHEDULE_EVERY_30_MINUTES = 'solid.performance.cron.every_30_minutes'; |
| 23 |
public const SCHEDULE_KEY_EVERY_30_MINUTES = 'swpsp_every_30_minutes'; |
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritDoc |
| 27 |
*/ |
| 28 |
public function register(): void { |
| 29 |
$this->register_schedules(); |
| 30 |
$this->register_tasks(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register custom cron schedules. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
private function register_schedules(): void { |
| 39 |
$this->container->singleton( |
| 40 |
self::SCHEDULE_EVERY_30_MINUTES, |
| 41 |
static fn() => new Schedule( |
| 42 |
self::SCHEDULE_KEY_EVERY_30_MINUTES, |
| 43 |
HOUR_IN_SECONDS / 2, |
| 44 |
__( 'Every 30 minutes', 'solid-performance' ) |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
$this->container->singleton( |
| 49 |
Schedule_Collection::class, |
| 50 |
fn() => new Schedule_Collection( |
| 51 |
// Register custom cron schedules here. |
| 52 |
$this->container->get( self::SCHEDULE_EVERY_30_MINUTES ), |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
add_filter( |
| 57 |
'cron_schedules', |
| 58 |
function ( array $schedules ) { |
| 59 |
$custom_schedules = $this->container->get( Schedule_Collection::class )->to_array(); |
| 60 |
|
| 61 |
return array_merge( $schedules, $custom_schedules ); |
| 62 |
}, |
| 63 |
10, |
| 64 |
1 |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register scheduled tasks. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
private function register_tasks(): void { |
| 74 |
$this->container->when( Clean_Expired_Cache_Files_Task::class ) |
| 75 |
->needs( Schedule::class ) |
| 76 |
->give( fn() => $this->container->get( self::SCHEDULE_EVERY_30_MINUTES ) ); |
| 77 |
|
| 78 |
$this->container->singleton( |
| 79 |
Registry::class, |
| 80 |
fn() => new Registry( |
| 81 |
// Register your cronjob tasks here. |
| 82 |
$this->container->get( Clean_Expired_Cache_Files_Task::class ), |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
// Register all the scheduled task hooks with WordPress. |
| 87 |
add_action( 'init', fn() => $this->container->get( Scheduler::class )->register_task_hooks() ); |
| 88 |
} |
| 89 |
} |
| 90 |
|