* * @phpstan-import-type PresizedDivSlot from PresizedDiv */ final class Cron { /** * Transient key tracking the ads.txt follow-up polling attempt count. */ const ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT = 'actirise_adstxt_followup_attempt'; /** * Delay before the next ads.txt follow-up poll, in seconds. */ const ADSTXT_FOLLOWUP_DELAY = 10; /** * Maximum number of ads.txt follow-up polls before giving up. */ const ADSTXT_FOLLOWUP_MAX_ATTEMPTS = 10; /** * Lifetime of the ads.txt follow-up attempt transient, in seconds. */ const ADSTXT_FOLLOWUP_TRANSIENT_TTL = 1800; /** * Schedule cron jobs * * @since 2.0.0 * @return void */ public function schedule() { if ( ACTIRISE_CRON === 'true' ) { add_filter( 'cron_schedules', function ( $schedules ) { // wp_schedule_event() below applies this filter while the plugin // file is still being included. Translating there loads the // translations too early, which WordPress 6.7 and later flag: // the label stays in English until after_setup_theme. $schedules['five_minutes'] = array( 'interval' => 300, 'display' => did_action( 'after_setup_theme' ) ? esc_html__( 'Every Five Minutes', 'actirise' ) : 'Every Five Minutes', ); return $schedules; } ); if ( ! wp_next_scheduled( 'actirise_cron_update_adstxt' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_update_adstxt' ); $this->update_adstxt(); } if ( ! wp_next_scheduled( 'actirise_cron_update_presized_div' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_update_presized_div' ); $this->check_presized_div(); } if ( ! wp_next_scheduled( 'actirise_cron_update_fast_cmp' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_update_fast_cmp' ); $this->get_fast_cmp(); } if ( ! wp_next_scheduled( 'actirise_cron_custom_variables_sync' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_custom_variables_sync' ); $this->custom_variables_sync(); } if ( ! wp_next_scheduled( 'actirise_cron_check_adstxt' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_check_adstxt' ); $this->check_adstxt(); } if ( ! wp_next_scheduled( 'actirise_cron_check_slots' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_check_slots' ); $this->check_slots(); } if ( ! wp_next_scheduled( 'actirise_cron_event_annotation' ) ) { wp_schedule_event( time(), 'five_minutes', 'actirise_cron_event_annotation' ); $this->upload_event_annotation(); } if ( Options::get( 'contract-type' ) === 'essential' && ! wp_next_scheduled( 'actirise_cron_check_optimization' ) ) { wp_schedule_event( time(), 'five_minutes', 'actirise_cron_check_optimization' ); } // Never run inline, unlike the polls above. schedule() executes while // the plugin file is still being included, long before WordPress has // created $wp_rewrite, and rest_url() dereferences that global on any // site using pretty permalinks — a fatal that takes the whole request // down, wp-admin/upgrade.php included. An event due at time() is // picked up by the cron spawn of this very request, once WordPress is // fully loaded, exactly as queue_rest_selftest() relies on. if ( ! wp_next_scheduled( 'actirise_cron_rest_selftest' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_rest_selftest' ); } // Never run inline either: migrations write files and call the API, // and schedule() runs while the plugin file is still being included. if ( ! wp_next_scheduled( 'actirise_cron_migrations' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_migrations' ); } } } /** * Run the migrations an update left pending. * * Migrations run on `admin_init`, so an install that auto-updated and whose * dashboard nobody opens would otherwise never play them: a whole release's * one-off work, the FastCMP first publication among it, silently skipped. * This hourly fallback plays them without an admin visit. It is cron-only * on purpose: an install with WP-Cron disabled keeps waiting for its * dashboard, rather than running migrations inside a visitor's request. * * Costs two option reads when nothing is pending: the gate and the lock * shared with the admin path live in {@see Migrations::migrate()}. * * @since 4.3.2 * @return void */ public function run_migrations() { $migrations = new Migrations( 'actirise', ACTIRISE_VERSION ); $migrations->migrate(); } /** * Queue the one-off FastCMP first publication a moment from now. * * Deferred rather than run inline for the same reason as the self-test: the * callers are an update or a sign-up request, and a slow API must not hang * them. The two-minute delay leaves the requests that follow a sign-up time * to store the credentials the publication needs. * * @since 4.3.2 * @return void */ public function queue_fastcmp_initial_sync() { if ( ! wp_next_scheduled( 'actirise_cron_fastcmp_initial_sync' ) ) { wp_schedule_single_event( time() + 120, 'actirise_cron_fastcmp_initial_sync' ); } } /** * Queue the one-off custom variables first send a moment from now. * * Deferred rather than run inline for the same reason as the FastCMP * publication above: the callers are an update or a sign-up request, and a * slow API must not hang them. The two-minute delay also leaves the * self-test the same callers queue time to report the capability first, * the order of contract § 8.3. * * @since 4.4.0 * @return void */ public function queue_customvars_initial_sync() { if ( ! wp_next_scheduled( 'actirise_cron_custom_variables_initial_sync' ) ) { wp_schedule_single_event( time() + 120, 'actirise_cron_custom_variables_initial_sync' ); } } /** * Run the plugin's REST API self-test. * * Probes the loopback ping route, persists the reachability verdict, and * reports `rest_api_enabled` / `rest_url` and the served-events claim to the * Actirise API. Fired hourly via `actirise_cron_rest_selftest` and once on * activation. * * @since 4.3.0 * @return void */ public function rest_selftest() { $self_test = new RestSelfTest(); $self_test->run(); } /** * Queue the REST API self-test to run on the next cron spawn. * * The self-test has nothing to report until the site is known to the API * (bearer token + website uuid), which only happens once the publisher has * signed up or logged in — long after the activation run. Left to the hourly * schedule alone, the API would stay unaware the plugin can receive pushes * for up to an hour and keep the site on the polling fallback. Callers signal * the prerequisites just became available; the run is deferred to a one-off * event so the loopback probe (up to RestSelfTest::PROBE_TIMEOUT seconds on * a host that blocks it) never blocks the request that triggered it. * WordPress drops a single event queued on the same hook within ten * minutes, so repeated calls still yield one run. * * @since 4.3.0 * @return void */ public function queue_rest_selftest() { if ( ACTIRISE_CRON !== 'true' ) { // No cron on this install: nothing would ever pick the event up. $this->rest_selftest(); return; } wp_schedule_single_event( time(), 'actirise_cron_rest_selftest' ); } /** * Update ads.txt file * * @since 2.0.0 * @return void */ public function update_adstxt() { if ( ! Options::get( 'settings-uuid' ) ) { return; } $ads_txt = AdsTxt::get_from_api(); if ( ! is_string( $ads_txt ) ) { Logger::add_log( 'adstxt get_from_api is false', 'include/cron', 'error' ); return; } // check_adstxt() polls the live ads.txt monitoring endpoint; it reads // neither the adstxt-actirise option nor the freshly written file, so it // keeps its long-standing position ahead of the file rewrite performed by // AdsTxt::apply_pushed_content(). Ordering it ahead of the option write is // behaviour-preserving and keeps the cron path identical after extraction. $this->check_adstxt(); AdsTxt::apply_pushed_content( $ads_txt ); } /** * Read the current ads.txt monitoring verdict and refresh the health flag. * * Background paths (the hourly crons, login, onboarding, feature toggles) * only READ the monitoring: the POST trigger is rate limited API-side to * one check per website per hour (SPA-27538) and that quota belongs to the * user's Validate button (the Ajax `check_adstxt` action). Fresh verdicts * come from the API's own scheduled checks and, when active, arrive by * push; this read keeps the health badge in sync in between. * * @since 3.0.0 * @return bool|array|\WP_Error */ public function check_adstxt() { if ( ! Helpers::is_user_authenticated() ) { return false; } // Hosted mode: ads.txt is served by Actirise, no monitoring API call needed. if ( Options::get( 'hosted-adstxt-enabled', false ) === true ) { $this->check_hosted_adstxt_redirect(); return false; } $adstxt_status = AdsTxt::check_from_api( 'false' ); if ( is_array( $adstxt_status ) && isset( $adstxt_status['state'] ) && $adstxt_status['state'] === 'pending' ) { // With push active the API delivers the final verdict directly // (contract §5.2), so the plugin skips its sub-hour "pending -> // result" follow-up poll; the hourly cron stays as the fallback. // When push is off the follow-up loop is unchanged. if ( ! self::is_push_active() ) { $this->schedule_adstxt_followup( 1 ); } return $adstxt_status; } self::finalize_adstxt_check( $adstxt_status ); return $adstxt_status; } /** * Follow-up cron event for ads.txt monitoring polling. * * Fired once a read found the monitoring in `pending` (a check is running, * whoever triggered it). Polls the adstxt monitoring endpoint again and * either reschedules itself (until ADSTXT_FOLLOWUP_MAX_ATTEMPTS is * reached) or finalizes the check when a definitive state is returned. * * @since 4.2.0 * @return void */ public function check_adstxt_followup() { if ( ! Helpers::is_user_authenticated() ) { delete_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT ); return; } // Push may have become active mid-chain; the API now pushes the final // verdict directly (contract §5.2), so abort the redundant follow-up poll. if ( self::is_push_active() ) { delete_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT ); return; } // Hosted mode may have been enabled mid-chain; abort the API polling. if ( Options::get( 'hosted-adstxt-enabled', false ) === true ) { delete_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT ); return; } /** @var mixed $stored_attempt */ $stored_attempt = get_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT ); $attempt = is_numeric( $stored_attempt ) ? (int) $stored_attempt : 0; $adstxt_status = AdsTxt::check_from_api( 'false' ); $still_pending = is_array( $adstxt_status ) && isset( $adstxt_status['state'] ) && $adstxt_status['state'] === 'pending'; if ( $still_pending && $attempt < self::ADSTXT_FOLLOWUP_MAX_ATTEMPTS ) { $this->schedule_adstxt_followup( $attempt + 1 ); return; } delete_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT ); if ( $still_pending ) { Logger::add_log( 'check_adstxt_followup max attempts reached, state still pending', 'include/cron', 'error' ); return; } self::finalize_adstxt_check( $adstxt_status ); } /** * Schedule a single follow-up event to re-poll the ads.txt monitoring * endpoint. * * @since 4.2.0 * @param int $attempt The 1-based attempt counter to persist for the next run. * @return void */ private function schedule_adstxt_followup( $attempt ) { set_transient( self::ADSTXT_FOLLOWUP_ATTEMPT_TRANSIENT, $attempt, self::ADSTXT_FOLLOWUP_TRANSIENT_TTL ); wp_schedule_single_event( time() + self::ADSTXT_FOLLOWUP_DELAY, 'actirise_cron_check_adstxt_followup' ); } /** * Persist the ads.txt validation verdict's `adstxt-update` flag. * * Single source of the health-badge flag shared by the polling fallback * (`check_adstxt()` / `check_adstxt_followup()`) and the push handler * (`Actirise\Includes\Push\Handler\AdstxtMonitoringPushHandler`), so both * paths derive the flag identically from the final `state`: `ok` clears it, * any other state raises it. Static and side-effect-only on the option store. * * @since 4.2.0 * @param bool|array|\WP_Error $adstxt_status Final verdict (polling response or push payload); a WP_Error is ignored. * @return void */ public static function finalize_adstxt_check( $adstxt_status ) { if ( is_array( $adstxt_status ) && isset( $adstxt_status['state'] ) ) { if ( $adstxt_status['state'] === 'ok' ) { Options::update( 'adstxt-update', false ); } else { Options::update( 'adstxt-update', true ); } } } /** * Whether the plugin's push (REST) channel is active. * * Two conditions, both required. The self-test must have positively * confirmed the plugin's REST API is reachable (`rest-api-enabled` === * true), and the capability must have reached the API * (`rest-api-reported` === true): a reachable endpoint the API was never * told about receives nothing, so treating it as active would switch the * polling fallback off while no channel is live at all — which on a * freshly registered site would also stop the very page-load GET that * stores the website uuid the report needs. Any other value on either * option — never tested, unreachable, or reported without success — keeps * push inactive so the polling fallback stays in charge. * * This is the single reader of the self-test state, shared by the ads.txt * follow-up poll suppression above and the admin page-load GET gating in * {@see \ActiriseAdmin\Includes\Core::maybe_check_website_status()}. * * @since 4.3.0 * @return bool */ public static function is_push_active() { return Options::get( RestSelfTest::OPTION_ENABLED, false ) === true && Options::get( RestSelfTest::OPTION_REPORTED, false ) === true; } /** * Check hosted ads.txt redirect * * @since 3.3.5 * @return void */ public function check_hosted_adstxt_redirect() { $is_hosted = Options::get( 'hosted-adstxt-enabled', false ) === true; if ( ! $is_hosted ) { // Not in hosted mode, nothing to check Options::delete( 'hosted-adstxt-redirect-error' ); return; } $uuid = Options::get( 'website-uuid' ); if ( ! $uuid ) { return; } $site_ads_txt_url = get_site_url() . '/ads.txt'; $flashbid_url = 'https://gateway.actirise.com/services/adstxt/' . $uuid; $response = wp_remote_get( $site_ads_txt_url, array( 'timeout' => 10, 'redirection' => 0, // Don't follow redirects ) ); if ( is_wp_error( $response ) ) { // Network error, mark as error Options::update( 'hosted-adstxt-redirect-error', array( 'error_type' => 'manual_required', 'flashbid_url' => $flashbid_url, ) ); return; } $status_code = wp_remote_retrieve_response_code( $response ); if ( $status_code === 302 || $status_code === 301 ) { $redirect_location = wp_remote_retrieve_header( $response, 'location' ); // Ignore WordPress trailing slash redirects (ads.txt -> ads.txt/) $is_trailing_slash_redirect = ( $redirect_location === trailingslashit( $site_ads_txt_url ) ); if ( $redirect_location === $flashbid_url ) { // Redirect is correct (don't check destination status), clear error Options::delete( 'hosted-adstxt-redirect-error' ); } elseif ( $is_trailing_slash_redirect ) { // WordPress trailing slash redirect - not an error, clear any existing error Options::delete( 'hosted-adstxt-redirect-error' ); } else { // Wrong redirect URL Options::update( 'hosted-adstxt-redirect-error', array( 'error_type' => 'wrong_url', 'flashbid_url' => $flashbid_url, ) ); } } elseif ( $status_code === 200 ) { // File is being served directly instead of redirected // Check if it's a physical file or generated by another plugin/system $path_ads_txt = ABSPATH . 'ads.txt'; $wp_fs = Helpers::get_wp_fs(); $file_exists = ! is_wp_error( $wp_fs ) && $wp_fs->exists( $path_ads_txt ); if ( $file_exists ) { // Physical file exists Options::update( 'hosted-adstxt-redirect-error', array( 'error_type' => 'file_exists', 'flashbid_url' => $flashbid_url, ) ); } else { // File is generated by third-party plugin/system Options::update( 'hosted-adstxt-redirect-error', array( 'error_type' => 'third_party', 'flashbid_url' => $flashbid_url, ) ); } } else { // Other error Options::update( 'hosted-adstxt-redirect-error', array( 'error_type' => 'manual_required', 'flashbid_url' => $flashbid_url, ) ); } } /** * Check for presized div * * @since 2.0.0 * @return void */ public function check_presized_div() { if ( ! Helpers::is_user_authenticated() ) { return; } if ( Options::get( 'settings-uuid' ) ) { /** @var array | bool $presized_div */ $presized_div = PresizedDiv::get_from_api(); if ( $presized_div !== false ) { /** @var array $presized_div */ self::merge_presized_div_config( $presized_div ); } else { Options::update( 'presizeddiv-actirise', array() ); } } } /** * Merge a full presized-div configuration into local storage, preserving * publisher slot preferences. * * Single source of the presized-div apply, shared by the hourly polling * fallback ({@see check_presized_div()}) and the push handler * (`Actirise\Includes\Push\Handler\PresizedDivPushHandler`), so both paths * write identical state. Every incoming slot first passes * {@see validate_presized_div_slots()}: a rejected slot keeps its last valid * stored value or, when none exists, stays out of the mirror. The * publisher's per-slot `active` toggles in `presizeddiv-selected` are never * overwritten, a validator rejection included: a slot unknown to the local * selection is added as inactive and queued into `presizeddiv-notif` for the * admin notice, and only a slot absent from the incoming payload itself is * dropped from the selection. Writes `presizeddiv-selected`, * `presizeddiv-actirise`, `presizeddiv-notif`, `presizeddiv-lastupdate` and, * on a new rejection, `presizeddiv-rejected`. Static and side-effect-only on * the option store. * * @since 4.3.0 * @param array $incoming The full presized-div configuration (polling response or push payload). * @return void */ public static function merge_presized_div_config( $incoming ) { // The selection drop below works on the names the payload actually // carried: a slot removed by the validator is not a slot the API // dropped, so its publisher toggle must survive. $incoming_names = array(); foreach ( $incoming as $incoming_slot ) { if ( is_array( $incoming_slot ) && isset( $incoming_slot['slotName'] ) && is_string( $incoming_slot['slotName'] ) ) { $incoming_names[] = $incoming_slot['slotName']; } } /** @var array $incoming */ $incoming = self::validate_presized_div_slots( $incoming ); /** @var mixed $presized_div_active */ $presized_div_active = Options::get( 'presizeddiv-selected', array() ); /** @var mixed $presized_div_notif */ $presized_div_notif = Options::get( 'presizeddiv-notif', array() ); if ( ! is_array( $presized_div_active ) ) { $presized_div_active = array(); } /** @var array $presized_div_active */ if ( ! is_array( $presized_div_notif ) ) { $presized_div_notif = array(); } foreach ( $incoming as $presized_div_item ) { /** @var string $slot_name */ $slot_name = $presized_div_item['slotName']; if ( ! in_array( $slot_name, array_column( $presized_div_active, 'slotName' ), true ) ) { $presized_div_active[] = array( 'slotName' => $slot_name, 'active' => false, ); /** @var array<\stdclass> $presized_div_notif */ $presized_div_notif[] = $slot_name; } } /** @var array{slotName: string, active: bool} $presized_div_active_item */ foreach ( $presized_div_active as $key => $presized_div_active_item ) { $slot_name = $presized_div_active_item['slotName']; if ( ! in_array( $slot_name, $incoming_names, true ) ) { unset( $presized_div_active[ $key ] ); } } Options::update( 'presizeddiv-selected', $presized_div_active ); Options::update( 'presizeddiv-actirise', $incoming ); Options::update( 'presizeddiv-notif', $presized_div_notif ); Options::update( 'presizeddiv-lastupdate', time() ); } /** * Validate every incoming pre-sized div slot against the allow-list. * * Per-slot rejection, shared by the cron poll and the push handler through * {@see merge_presized_div_config()}: a slot whose template, xpathConfig * override or stylesheet fails {@see PresizedDivValidator} never replaces * its stored value. The last valid stored value takes its place when one * exists, so the page keeps rendering what it rendered before; without one * the slot is dropped. A rejection is logged with a bounded excerpt of the * slot name and the reason, never the payload body, and recorded in the * `presizeddiv-rejected` option surfaced by Site Health. Deduplication is * backed by `presizeddiv-lastrejections`, a per-slot map of the previous * run's rejections kept separate from the capped journal: a slot rejected * for the same reason as last run is neither logged nor journaled, however * many slots the payload rejects, so a persistently invalid payload cannot * flood the capped log through the hourly poll even when its rejections * outnumber the journal's twenty entries. The other slots of the payload * still apply. * * @since 4.4.0 * @param array $incoming The slots from the configuration payload. * @return array The slots safe to persist. */ private static function validate_presized_div_slots( $incoming ) { /** @var mixed $stored */ $stored = Options::get( 'presizeddiv-actirise', array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $validated = array(); /** @var mixed $previous_rejections */ $previous_rejections = Options::get( 'presizeddiv-lastrejections', array() ); if ( ! is_array( $previous_rejections ) ) { $previous_rejections = array(); } /** @var array $current_rejections */ $current_rejections = array(); /** @var array $journal_entries */ $journal_entries = array(); foreach ( $incoming as $slot ) { $result = PresizedDivValidator::validate_slot( $slot ); if ( $result['valid'] ) { $validated[] = $slot; continue; } $slot_name = is_array( $slot ) && isset( $slot['slotName'] ) && is_string( $slot['slotName'] ) ? $slot['slotName'] : '(unknown)'; $display_name = $slot_name === '(unknown)' ? $slot_name : PresizedDivValidator::excerpt( $slot_name, 100 ); $current_rejections[ $display_name ] = $result['reason']; $is_repeat = isset( $previous_rejections[ $display_name ] ) && $previous_rejections[ $display_name ] === $result['reason']; if ( ! $is_repeat ) { $journal_entries[] = array( 'slotName' => $display_name, 'reason' => $result['reason'], 'time' => time(), ); Logger::add_log( 'presized-div slot rejected: ' . $display_name . ' (' . $result['reason'] . ')', 'include/cron', 'error' ); } foreach ( $stored as $stored_slot ) { if ( ! is_array( $stored_slot ) || ! isset( $stored_slot['slotName'] ) || $stored_slot['slotName'] !== $slot_name ) { continue; } $stored_result = PresizedDivValidator::validate_slot( $stored_slot ); if ( $stored_result['valid'] ) { $validated[] = $stored_slot; } break; } } if ( count( $journal_entries ) > 0 ) { self::record_presized_div_rejections( $journal_entries ); } if ( $current_rejections !== $previous_rejections ) { Options::update( 'presizeddiv-lastrejections', $current_rejections ); } return $validated; } /** * Record a pre-sized div slot rejection for Site Health. * * Keeps the last twenty rejections in the `presizeddiv-rejected` option: * slot name, reason and timestamp, never the payload body. One read and * one write per merge run, whatever the payload rejects. Deduplication * does not live here: the caller skips a slot rejected for the same reason * as the previous run through the `presizeddiv-lastrejections` map, which * eviction from this capped journal cannot erase. * * @since 4.4.0 * @param array $entries The rejections of this run, names already bounded by the caller. * @return void */ private static function record_presized_div_rejections( $entries ) { /** @var mixed $rejections */ $rejections = Options::get( 'presizeddiv-rejected', array() ); if ( ! is_array( $rejections ) ) { $rejections = array(); } $rejections = array_merge( array_values( $rejections ), $entries ); $rejections = array_slice( $rejections, -20 ); Options::update( 'presizeddiv-rejected', $rejections ); } /** * Get FastCmp stub light * * @since 2.3.0 * @return void */ public function get_fast_cmp() { $this->get_fastcmp_stublight(); $this->get_fastcmp_vendors(); // Keeps the IAB Framework select's list warm, so an admin page load // rarely has to fetch it. Issues nothing without an Actirise account. TcfFrameworks::refresh(); if ( Options::get( 'fastcmp-uuid', '' ) === '' ) { $this->get_fastcmp_uuid(); } // Converge on Actirise when the push channel cannot reach us. Independent // of the domain uid resolved above: that one identifies the domain to // FastCMP for the tag, while the sync addresses its resource by the // Actirise website uuid. // // A pending first publication takes this run's place: it reads before it // writes, on the same resource, so polling as well would spend a second // request to learn what it has just been told. $sync = new ConfigSync(); if ( ! $sync->run_seed() && ConfigSync::should_poll() ) { $sync->pull(); } Options::update( 'fastcmp-lastupdate', time() ); } /** * Perform the one-off FastCMP first publication, if one is still owed. * * A three-line adapter for `actirise_cron_fastcmp_initial_sync`: the decision * table lives with the version and the transport, in ConfigSync. * * @since 4.3.0 * @return void */ public function fastcmp_initial_sync() { $sync = new ConfigSync(); $sync->run_seed(); } /** * Converge the custom variables mapping on the Platform. * * The hourly fallback of the `custom-variables` push (SPA-27015): while * push is active an armed site already receives every change, so nothing * is polled there. An unarmed site runs the GET even while push is live, * because that GET is the only thing that arms the sync on the websites * the rollout flag reaches (a push is refused with a `400` until then); * the gate lives in {@see CustomVariablesSync::should_poll()}. * * A pending first send runs first, whatever the push state: it reads, * may then write the local mapping once, and is bounded by * {@see CustomVariablesSync::run_seed()} (SPA-27016). * * @since 4.4.0 * @return void */ public function custom_variables_sync() { $sync = new CustomVariablesSync(); // A first send that concludes takes this run's place: it read before // it wrote, on the same resource, so polling as well would spend a // second request to learn what it has just been told (the // get_fast_cmp() precedent). A deferred attempt falls through to the // poll like any other run. if ( ! $sync->run_seed() && CustomVariablesSync::should_poll() ) { $sync->pull(); } } /** * Perform the one-off custom variables first send, if one is still owed. * * A three-line adapter for `actirise_cron_custom_variables_initial_sync`: * the decision table lives with the version and the transport, in * CustomVariablesSync. * * @since 4.4.0 * @return void */ public function custom_variables_initial_sync() { $sync = new CustomVariablesSync(); $sync->run_seed(); } /** * Set auto update * * @since 2.4.0 * @param bool $enabled * @return void */ public function set_auto_update( $enabled ) { if ( ACTIRISE_CRON !== 'true' ) { return; } if ( $enabled === Helpers::has_auto_update() ) { return; } /** @var string $plugin_file */ $plugin_file = plugin_basename( ACTIRISE_FILE ); /** @var array $auto_updates */ $auto_updates = get_site_option( 'auto_update_plugins', array() ); if ( $enabled ) { $plugins = array_unique( array_merge( $auto_updates, array( plugin_basename( ACTIRISE_FILE ) ) ) ); } else { $plugins = array_values( array_diff( $auto_updates, array( plugin_basename( ACTIRISE_FILE ) ) ) ); } update_site_option( 'auto_update_plugins', $plugins ); } /** * Check if the slots XPath are present and valid in website pages * * @since 3.2.0 * @return void */ public function check_slots() { if ( ! Helpers::is_user_authenticated() ) { return; } if ( Options::get( 'contract-type' ) !== 'essential' ) { return; } $ad_format = new AdFormat(); $ad_format->validate_xpath_and_notify(); } /** * Uploads event annotations via cron task. * * This method is called by the cron system to trigger the event * annotation upload process. It delegates the actual work to the * EventAnnotation class. * * @return void * * @since 3.2.0 */ public function upload_event_annotation() { EventAnnotation::send_events(); } /** * Get FastCmp stub light * * @since 2.2.0 * @return void */ private function get_fastcmp_stublight() { $stub_light_url = ACTIRISE_URL_FASTCMP_STATIC . 'fast-cmp-stub-light.js'; $response = wp_remote_request( $stub_light_url, array( 'method' => 'GET', 'timeout' => 10, ) ); if ( is_wp_error( $response ) ) { Logger::add_log( 'get_fastcmp_stublight error ' . $response->get_error_code(), 'include/cron', 'error' ); return; } /** @var string|boolean $stub_light */ $stub_light = wp_remote_retrieve_body( $response ); if ( $stub_light === false ) { return; } Options::update( 'fastcmp-stub-light', $stub_light ); } /** * Get FastCmp vendors * * @since 2.2.0 * @return void */ private function get_fastcmp_vendors() { $vendors_api_url = 'https://eu.fastcmp.com/wp/vendor-list'; $response = wp_remote_request( $vendors_api_url, array( 'method' => 'GET', 'timeout' => 10, ) ); if ( is_wp_error( $response ) ) { return; } /** @var string|boolean $vendors */ $vendors = wp_remote_retrieve_body( $response ); if ( $vendors === false ) { return; } /** @var string $vendors */ $vendors_json = json_decode( $vendors, true ); if ( $vendors_json === null ) { return; } Options::update( 'fastcmp-vendors', $vendors_json ); } /** * Get FastCmp uuid * * @since 2.2.0 * @return void */ private function get_fastcmp_uuid() { $fast_cmp_url = 'https://eu.fastcmp.com/wp/domain-uid?domain='; $domain = rawurlencode( Helpers::get_server_details()['host'] ); $domain = str_replace( 'www.', '', $domain ); $response = wp_remote_request( $fast_cmp_url . $domain, array( 'method' => 'GET', 'timeout' => 10, ) ); if ( is_wp_error( $response ) ) { return; } /** @var string|boolean $uuid */ $uuid = wp_remote_retrieve_body( $response ); if ( $uuid === false ) { return; } /** @var string $uuid */ $uuid_json = json_decode( $uuid, true ); /** @var null|array{domain?: string, domainUid?: string, error?: string} $uuid_json */ if ( $uuid_json === null ) { return; } /** @var array{domain?: string, domainUid?: string, error?: string} $uuid_json */ if ( isset( $uuid_json['error'] ) || ! isset( $uuid_json['domainUid'] ) ) { return; } Options::update( 'fastcmp-uuid', $uuid_json['domainUid'] ); } /** * Check optimization checklist and cache health issues. * * @since 4.1.0 * @return void */ public function check_optimization() { $health_check = new HealthCheck(); $health_check->refresh_cache(); } /** * Checks if a scheduled task needs to be executed based on transient expiration. * * This method uses WordPress transients to determine if a task should run. * If the transient doesn't exist (expired or never set), it creates a new one * with the specified duration and executes the callback function. * * @param string $transient_name The name of the transient to check. * @param callable $callback The function to execute if transient has expired. * @param int $duration Duration in seconds for the transient (default: 3600). * * @return bool True if the task was executed, false if it was skipped. * * @since 3.2.0 */ public function check_scheduled_task_with_transient( $transient_name, callable $callback, $duration = 3600 ) { if ( false === get_transient( $transient_name ) ) { set_transient( $transient_name, true, $duration ); call_user_func( $callback ); return true; } return false; } }