← Back
Editing: class-cal-aiv-bot-list.php
<?php if (!defined('ABSPATH')) { exit; } class CAL_AIV_Bot_List { // The live list has about 60 entries; far more means a bad response, and it is checked on every page view. const MAX_ENTRIES = 1000; public static function fetch_and_cache() { $publisher_id = cal_aiv_publisher_id(); // Turned off means no call to Awin for AI visibility, not just no reporting. if (!$publisher_id || !cal_aiv_is_enabled()) { return false; } $url = sprintf('https://awin1.com/ai-bot-useragents.json?publisherId=%d', $publisher_id); $response = wp_remote_get( $url, array( 'timeout' => 10, 'sslverify' => true, ) ); if (is_wp_error($response)) { self::log_error('Bot list fetch error: ' . $response->get_error_message()); self::maybe_schedule_retry(); return false; } $code = (int) wp_remote_retrieve_response_code($response); if ($code !== 200) { self::log_error("Bot list fetch HTTP {$code}"); self::maybe_schedule_retry(); return false; } $data = json_decode(wp_remote_retrieve_body($response), true); if (!isset($data['aiBotUserAgents']) || !is_array($data['aiBotUserAgents'])) { self::log_error('Bot list: invalid JSON structure'); self::maybe_schedule_retry(); return false; } $bot_list = self::normalise($data['aiBotUserAgents']); if (empty($bot_list) || count($bot_list) > self::MAX_ENTRIES) { // Treated as a failure so a bad 200 can't wipe the last good list. self::log_error('Bot list: ' . (empty($bot_list) ? 'no usable entries' : 'too many entries (' . count($bot_list) . ')')); self::maybe_schedule_retry(); return false; } // A plain autoloaded option: no extra query per page view, and a failed refresh keeps the last good list. update_option(CAL_AIV_OPT_BOT_LIST, $bot_list); update_option(CAL_AIV_OPT_STATUS, array( 'last_fetch' => current_time('mysql'), 'bot_count' => count($bot_list), )); delete_transient(CAL_AIV_TRANSIENT_RETRY_COUNT); wp_clear_scheduled_hook('cal_aiv_retry_fetch'); return true; } // Stored as signature => bot name, so the detector only ever sees non-empty strings. // A true or 123 entry cast to "1" or "123" would match almost every browser's User-Agent. private static function normalise($raw) { $list = array(); foreach ($raw as $key => $value) { if (is_string($key) && $key !== '' && !is_numeric($key)) { $list[$key] = $key; } elseif (is_array($value) && isset($value['userAgent']) && is_string($value['userAgent']) && $value['userAgent'] !== '') { $name = isset($value['name']) && is_string($value['name']) && $value['name'] !== '' ? $value['name'] : $value['userAgent']; $list[$value['userAgent']] = $name; } elseif (is_string($value) && $value !== '') { $list[$value] = $value; } } foreach (array_keys($list) as $signature) { if (!self::is_safe_signature((string) $signature)) { unset($list[$signature]); } } return $list; } // A bad list entry such as "Mozilla" would report every human visitor, so drop anything a normal browser would match. private static function is_safe_signature($signature) { if (strlen($signature) < 4) { return false; } $browsers = array( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1', 'Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0', 'Mozilla/5.0 (Linux; Android 14; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/114.0.0.0', 'Mozilla/5.0 (iPad; CPU OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/133.0 Mobile/15E148 Safari/605.1.15', 'Mozilla/5.0 (Linux; Android 14; SAMSUNG SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/27.0 Chrome/125.0.0.0 Mobile Safari/537.36', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0', 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 YaBrowser/24.12.0.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Vivaldi/7.0.3495.18', ); foreach ($browsers as $browser) { if (stripos($browser, $signature) !== false) { return false; } } return true; } public static function get_list() { $list = get_option(CAL_AIV_OPT_BOT_LIST); return is_array($list) ? $list : array(); } public static function manual_refresh() { check_admin_referer('cal_aiv_refresh_nonce'); if (!current_user_can('manage_options')) { wp_die(esc_html('Unauthorized'), '', array('response' => 403)); } $ok = self::fetch_and_cache(); wp_safe_redirect(add_query_arg(array( 'page' => PUBLISHER_MASTERTAG_ADMIN_SLUG, 'refresh' => $ok ? 'ok' : 'failed', ), admin_url('admin.php'))); exit; } private static function maybe_schedule_retry() { $attempt = absint(get_transient(CAL_AIV_TRANSIENT_RETRY_COUNT)); if ($attempt >= 5 || wp_next_scheduled('cal_aiv_retry_fetch')) { return; } $delay = (int) pow(4, $attempt) * 30; wp_schedule_single_event(time() + $delay, 'cal_aiv_retry_fetch'); set_transient(CAL_AIV_TRANSIENT_RETRY_COUNT, $attempt + 1, DAY_IN_SECONDS); } private static function log_error($message) { // Shown on the settings page; a successful fetch overwrites the status and clears it. $status = get_option(CAL_AIV_OPT_STATUS, array()); $status = is_array($status) ? $status : array(); $status['last_error'] = $message; $status['last_attempt'] = current_time('mysql'); update_option(CAL_AIV_OPT_STATUS, $status); if (defined('WP_DEBUG') && WP_DEBUG) { error_log('[Awin Publisher MasterTag] ' . $message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- WP_DEBUG only } } }
Save File
Cancel