84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Action Scheduler wrapper.
|
|
*
|
|
* @package MediaRightsAudit\Core\Queue
|
|
*/
|
|
|
|
namespace MediaRightsAudit\Core\Queue;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Thin wrapper around Action Scheduler that namespaces all jobs under a single group.
|
|
*
|
|
* Action Scheduler is bundled via Composer (woocommerce/action-scheduler) and
|
|
* loaded from the main plugin file, so its functions are always available at
|
|
* runtime. The is_available() guard is kept for defensive unit-test scenarios
|
|
* only.
|
|
*/
|
|
class Scheduler {
|
|
|
|
/**
|
|
* Action Scheduler group for all plugin jobs.
|
|
*/
|
|
const GROUP = 'robotstxt-mediaaudit';
|
|
|
|
/**
|
|
* Returns true when Action Scheduler functions are available.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function is_available(): bool {
|
|
return function_exists( 'as_schedule_single_action' )
|
|
&& function_exists( 'as_has_scheduled_action' )
|
|
&& function_exists( 'as_unschedule_all_actions' );
|
|
}
|
|
|
|
/**
|
|
* Schedules a single action to run after the given delay.
|
|
*
|
|
* @param string $hook Action hook name.
|
|
* @param array<mixed> $args Arguments passed to the hook.
|
|
* @param int $delay Seconds from now (default 0 = immediate).
|
|
*
|
|
* @return int|null Action Scheduler action ID, or null if unavailable.
|
|
*/
|
|
public static function schedule_single( string $hook, array $args = array(), int $delay = 0 ): ?int {
|
|
if ( ! self::is_available() ) {
|
|
return null;
|
|
}
|
|
return as_schedule_single_action( time() + $delay, $hook, $args, self::GROUP );
|
|
}
|
|
|
|
/**
|
|
* Returns true if a pending (not-yet-run) action exists for this hook.
|
|
*
|
|
* @param string $hook Action hook name.
|
|
* @param array<mixed> $args Arguments to match.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function has_pending( string $hook, array $args = array() ): bool {
|
|
if ( ! self::is_available() ) {
|
|
return false;
|
|
}
|
|
return as_has_scheduled_action( $hook, $args, self::GROUP );
|
|
}
|
|
|
|
/**
|
|
* Cancels all pending plugin actions across all hooks.
|
|
*
|
|
* Called on plugin deactivation.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function cancel_all(): void {
|
|
if ( ! self::is_available() ) {
|
|
return;
|
|
}
|
|
as_unschedule_all_actions( '', array(), self::GROUP );
|
|
}
|
|
}
|