| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Application\File; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Responsible for scheduling and unscheduling the cron. |
| 10 |
*/ |
| 11 |
class Llms_Txt_Cron_Scheduler { |
| 12 |
|
| 13 |
/** |
| 14 |
* The name of the cron job. |
| 15 |
*/ |
| 16 |
public const LLMS_TXT_POPULATION = 'wpseo_llms_txt_population'; |
| 17 |
|
| 18 |
/** |
| 19 |
* The options helper. |
| 20 |
* |
| 21 |
* @var Options_Helper |
| 22 |
*/ |
| 23 |
private $options_helper; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @param Options_Helper $options_helper The options helper. |
| 29 |
*/ |
| 30 |
public function __construct( |
| 31 |
Options_Helper $options_helper |
| 32 |
) { |
| 33 |
$this->options_helper = $options_helper; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Schedules the llms txt population cron a week from now. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function schedule_weekly_llms_txt_population(): void { |
| 42 |
if ( $this->options_helper->get( 'enable_llms_txt', false ) !== true ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if ( ! \wp_next_scheduled( self::LLMS_TXT_POPULATION ) ) { |
| 47 |
\wp_schedule_event( ( \time() + \WEEK_IN_SECONDS ), 'weekly', self::LLMS_TXT_POPULATION ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Schedules the llms txt population cron 5 minutes from now. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function schedule_quick_llms_txt_population(): void { |
| 57 |
if ( $this->options_helper->get( 'enable_llms_txt', false ) !== true ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if ( \wp_next_scheduled( self::LLMS_TXT_POPULATION ) ) { |
| 62 |
$this->unschedule_llms_txt_population(); |
| 63 |
} |
| 64 |
|
| 65 |
\wp_schedule_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), 'weekly', self::LLMS_TXT_POPULATION ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Unschedules the llms txt population cron. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function unschedule_llms_txt_population() { |
| 74 |
$scheduled = \wp_next_scheduled( self::LLMS_TXT_POPULATION ); |
| 75 |
if ( $scheduled ) { |
| 76 |
\wp_unschedule_event( $scheduled, self::LLMS_TXT_POPULATION ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|