| 1 |
<?php |
| 2 |
/** |
| 3 |
* Script-Blocking Datasets Cron. |
| 4 |
* |
| 5 |
* Warms the unified remote-served dataset (services.json) off the request path |
| 6 |
* and reconciles declared cookies against the refreshed catalog. The dataset is |
| 7 |
* always available from the bundled floor, so this only keeps it current - a |
| 8 |
* failed run never leaves blocking without data. |
| 9 |
* |
| 10 |
* @package SureCookie\Inc\Modules\Services |
| 11 |
* @since 1.2.5 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SureCookie\Inc\Modules\Services; |
| 15 |
|
| 16 |
use SureCookie\Inc\Traits\GetInstance; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Cron |
| 24 |
* |
| 25 |
* Schedules and runs the daily dataset refresh. |
| 26 |
* |
| 27 |
* @since 1.2.5 |
| 28 |
*/ |
| 29 |
class Cron { |
| 30 |
use GetInstance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Action hook that refreshes the datasets. Also used by Known_Scripts to |
| 34 |
* schedule a one-off near-immediate warm when its cache is cold. |
| 35 |
* |
| 36 |
* @since 1.2.5 |
| 37 |
*/ |
| 38 |
public const REFRESH_HOOK = 'surecookie_refresh_datasets'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor. |
| 42 |
* |
| 43 |
* @since 1.2.5 |
| 44 |
*/ |
| 45 |
private function __construct() { |
| 46 |
add_action( self::REFRESH_HOOK, [ $this, 'refresh' ] ); |
| 47 |
|
| 48 |
// Cron::get_instance() is instantiated from the module bootstrap on the |
| 49 |
// `init` hook at priority 999, so an add_action( 'init', ... ) here would |
| 50 |
// register a callback for a priority that has already run and never fire. |
| 51 |
// Schedule directly instead - wp_schedule_event() is available this late, |
| 52 |
// and schedule() is idempotent (a no-op once the event is registered). |
| 53 |
$this->schedule(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Schedule the daily refresh if it is not already scheduled. |
| 58 |
* |
| 59 |
* @since 1.2.5 |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function schedule(): void { |
| 63 |
if ( ! wp_next_scheduled( self::REFRESH_HOOK ) ) { |
| 64 |
wp_schedule_event( time(), 'daily', self::REFRESH_HOOK ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Refresh the unified remote catalog and reconcile declared cookies. |
| 70 |
* |
| 71 |
* One fetch warms Services_Source (which feeds both the blocking view and the |
| 72 |
* declared-cookie view); then stale declared cookies are reconciled against it. |
| 73 |
* |
| 74 |
* @since 1.2.5 |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function refresh(): void { |
| 78 |
Services_Source::get_instance()->refresh_from_remote(); |
| 79 |
Known_Scripts::get_instance()->load_data(); |
| 80 |
Declared_Cookies::get_instance()->reconcile_declared_cookies(); |
| 81 |
} |
| 82 |
} |
| 83 |
|