* * @phpstan-import-type PresizedDivSlot from PresizedDiv */ final class Cron { /** * Schedule cron jobs * * @since 2.0.0 * @return void */ public function schedule() { if ( ACTIRISE_CRON === 'true' ) { 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_debug_token' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_update_debug_token' ); $this->refresh_token(); } if ( ! wp_next_scheduled( 'actirise_cron_update_fast_cmp' ) ) { wp_schedule_event( time(), 'hourly', 'actirise_cron_update_fast_cmp' ); $this->refresh_token(); } } } /** * Update ads.txt file * * @since 2.0.0 * @return void */ public function update_adstxt() { if ( get_option( 'actirise-settings-uuid' ) ) { $adsTxt = AdsTxt::get_from_api(); if ( $adsTxt !== false ) { if ( get_option( 'actirise-adstxt-actirise' ) !== $adsTxt ) { update_option( 'actirise-adstxt-actirise', $adsTxt ); update_option( 'actirise-adstxt-update', 'true' ); } update_option( 'actirise-adstxt-lastupdate', time() ); } } } /** * Check for presized div * * @since 2.0.0 * @return void */ public function check_presized_div() { if ( get_option( 'actirise-settings-uuid' ) ) { /** @var array | bool $presizedDiv */ $presizedDiv = PresizedDiv::get_from_api(); if ( $presizedDiv !== false ) { /** @var array $presizedDivActive */ $presizedDivActive = get_option( 'actirise-presizeddiv-selected', array() ); /** @var array<\stdclass> $presizedDivNotif */ $presizedDivNotif = get_option( 'actirise-presizeddiv-notif', array() ); if ( ! is_array( $presizedDivActive ) ) { $presizedDivActive = array(); } /** @var array $presizedDiv */ foreach ( $presizedDiv as $presizedDivItem ) { /** @var string $slotName */ $slotName = $presizedDivItem['slotName']; if ( ! in_array( $slotName, array_column( $presizedDivActive, 'slotName' ), true ) ) { $presizedDivActive[] = array( 'slotName' => $slotName, 'active' => false, ); $presizedDivNotif[] = $slotName; } } /** @var array{slotName: string, active: bool} $presizedDivActiveItem */ foreach ( $presizedDivActive as $key => $presizedDivActiveItem ) { if ( ! in_array( $presizedDivActiveItem['slotName'], array_column( $presizedDiv, 'slotName' ), true ) ) { unset( $presizedDivActive[ $key ] ); } } update_option( 'actirise-presizeddiv-selected', $presizedDivActive ); update_option( 'actirise-presizeddiv-actirise', $presizedDiv ); update_option( 'actirise-presizeddiv-notif', $presizedDivNotif ); } else { update_option( 'actirise-presizeddiv-actirise', array() ); } update_option( 'actirise-presizeddiv-lastupdate', time() ); } } /** * Refresh debug token * * @since 2.0.0 * @return void */ public function refresh_token() { /** @var string $last_token */ $last_token = get_option( 'actirise-debug-token' ); $enabled = get_option( 'actirise-debug-enabled', '1' ) === '1'; if ( $enabled === false ) { return; } $new_token = $this->generate_token(); $debugUpdated = Debug::update_to_api( $new_token, $last_token ); if ( $debugUpdated !== false ) { update_option( 'actirise-debug-token', $new_token ); } } /** * Get FastCmp stub light * * @since 2.3.0 * @return void */ public function get_fast_cmp() { $this->get_fastcmp_stublight(); $this->get_fastcmp_vendors(); if ( get_option( 'actirise-fastcmp-uuid', '' ) === '' ) { $this->get_fastcmp_uuid(); } } /** * Generate a random token in uuid v4 format * * @since 2.0.0 * @return string */ private function generate_token() { $uid = uniqid( '', true ); $uid = str_replace( '.', '', $uid ); $uid = str_shuffle( $uid ); $uuid = sprintf( '%05s-%05s-%05d-%05d', substr( $uid, 0, 5 ), substr( $uid, 5, 5 ), ( hexdec( substr( $uid, 10, 5 ) ) & 0x0fff ) | 0x5000, ( hexdec( substr( $uid, 15, 5 ) ) & 0x3fff ) | 0x5000 ); return $uuid; } /** * Get FastCmp stub light * * @since 2.2.0 * @return void */ private function get_fastcmp_stublight() { $stubLightUrl = 'https://static.fastcmp.com/fast-cmp-stub-light.js'; $response = wp_remote_request( $stubLightUrl, array( 'method' => 'GET', 'timeout' => 10, 'sslverify' => false, ) ); if ( is_wp_error( $response ) ) { return; } /** @var string|boolean $stubLight */ $stubLight = wp_remote_retrieve_body( $response ); if ( $stubLight === false ) { return; } update_option( 'actirise-fastcmp-stub-light', $stubLight ); } /** * Get FastCmp vendors * * @since 2.2.0 * @return void */ private function get_fastcmp_vendors() { $vendorsApiUrl = 'https://eu.fastcmp.com/wp/vendor-list'; $response = wp_remote_request( $vendorsApiUrl, array( 'method' => 'GET', 'timeout' => 10, 'sslverify' => false, ) ); if ( is_wp_error( $response ) ) { return; } /** @var string|boolean $vendors */ $vendors = wp_remote_retrieve_body( $response ); if ( $vendors === false ) { return; } /** @var string $vendors */ $vendorsJson = json_decode( $vendors, true ); if ( $vendorsJson === null ) { return; } update_option( 'actirise-fastcmp-vendors', $vendorsJson ); } /** * Get FastCmp uuid * * @since 2.2.0 * @return void */ private function get_fastcmp_uuid() { $fastCMPUrl = 'https://eu.fastcmp.com/wp/domain-uid?domain='; $domain = rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ); $domain = str_replace( 'www.', '', $domain ); $response = wp_remote_request( $fastCMPUrl . $domain, array( 'method' => 'GET', 'timeout' => 10, 'sslverify' => false, ) ); if ( is_wp_error( $response ) ) { return; } /** @var string|boolean $uuid */ $uuid = wp_remote_retrieve_body( $response ); if ( $uuid === false ) { return; } /** @var string $uuid */ $uuidJson = json_decode( $uuid, true ); /** @var null|array{domain?: string, domainUid?: string, error?: string} $uuidJson */ if ( $uuidJson === null ) { return; } /** @var array{domain?: string, domainUid?: string, error?: string} $uuidJson */ if ( isset( $uuidJson['error'] ) || ! isset( $uuidJson['domainUid'] ) ) { return; } update_option( 'actirise-fastcmp-uuid', $uuidJson['domainUid'] ); } }