enabled()) { return null; } $result = apcu_fetch($cacheKey); if (!$result) { return null; } return $result; } public function store(string $cacheKey, $data): bool { if (!$this->enabled()) { return false; } return apcu_store($cacheKey, $data, 0); } } class AdmiralAdBlockAnalytics { /** * Default duration (1 day) (24 * 60 * 60) to store the embed for before requesting another script * @var int */ const DEFAULT_EMBED_EXPIRATION = 86400; /** * Option keys for admiral plugin settings and other configuration data */ const PROPERTY_OPTION_ID_KEY = "admiral_property_id"; const PROPERTY_PROMISE_OPTION_ID_KEY = "admiral_property_promise_id"; const PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY = "admiral_property_promise_property_id"; const EMBED_OPTION_KEY = "admiral_embed"; // ends in "2" to ignore old expiration option set before the fix for multiplciation vs addition of expiration const EMBED_EXPIRATION_OPTION_KEY = "admiral_embed_expiration2"; const PROTECT_OPTION_KEY = "admiral_protect"; const FIRST_PARTY_MODE_OPTION_KEY = "admiral_first_party_mode"; /** * The option key First-Party Mode used when it was called "Proxy". Sites * configured before the rename still store their setting here, so reads * fall back to it and writes keep it in sync. */ const LEGACY_FIRST_PARTY_MODE_OPTION_KEY = "admiral_proxy"; /** * @deprecated Use LEGACY_FIRST_PARTY_MODE_OPTION_KEY, or * FIRST_PARTY_MODE_OPTION_KEY for the current key */ const PROXY_ADMIRAL_OPTION_KEY = self::LEGACY_FIRST_PARTY_MODE_OPTION_KEY; const EMBED_ADDITIONAL_OPTIONS_KEY = "admiral_embed_additional_options"; /** * Suffix to append to the user-agent when forwarding First-Party Mode requests * @var string */ public static $UASuffix = "ADMIRALWP/uninit"; /** * Admiral PropertyID you get when signing up for Admiral * @var string */ private static $propertyID = ""; /** * Whether the propertyID was configured via an environment variable * @var bool */ private static $envConfiguredPropertyID = false; /** * Admiral PropertyPromiseID you get when signing up for Admiral * @var string */ private static $propertyPromiseID = ""; /** * Admiral Embed script you get when signing up for Admiral * @var string */ private static $embed = ""; /** * Endpoint to partner API */ private static $partnerApiEndpoint = "//partner.api.getadmiral.com/"; /** * Cache store to use for caching embeds and First-Party Mode data * * @var AdmiralCacheStore */ private static $store = null; /** * Whether or not Protect is on * @var bool */ private static $protect = null; /** * Whether First-Party Mode is on, i.e. whether requests are routed through * this WordPress server before being sent to Admiral * @var bool */ private static $firstPartyMode = null; private static $clientID = ""; private static $clientSecret = ""; private static $pluginCode = ""; /** * The environment to use for the plugin * @var string */ private static $environment = "production"; /** * An instance of AdmiralHttpHandler for making and forwarding requests to * Admiral's servers * @var AdmiralHttpHandler */ private static $httpHandlerInstance = null; /** * Additional options to send when requesting an embed * @var string */ private static $embedAdditionalOptions = null; /** * Whether or not the plugin has embedded for this pageview * @var bool */ private static $hasEmbedded = false; /** * Reset the plugin options, mostly for testing */ public static function reset() { self::$propertyID = ""; self::$envConfiguredPropertyID = false; self::$propertyPromiseID = ""; self::$embed = ""; self::$store = null; self::$httpHandlerInstance = null; self::$protect = false; self::$firstPartyMode = false; } /** * Get the configured propertyID from either an environment variable * or from the WordPress option. * * @return string the configured propertyID (or empty string) */ public static function getPropertyID() { if (empty(self::$propertyID) && defined("ADMIRAL_PROPERTY_ID")) { self::$propertyID = ADMIRAL_PROPERTY_ID; if (!empty(self::$propertyID)) { self::$envConfiguredPropertyID = true; } } if (empty(self::$propertyID)){ self::$propertyID = get_option(self::PROPERTY_OPTION_ID_KEY, ""); } return self::$propertyID; } /** * Getter for self::$envConfiguredPropertyID * * @return bool whether an environment var configured the PROPERTY_ID */ public static function getEnvConfiguredPropertyID() { return self::$envConfiguredPropertyID; } /** * Completes a POST submission by reading from $_POST for the * PROPERTY_OPTION_ID_KEY and saving it in the WordPress options. * * @return bool whether the new value was saved */ public static function updatePropertyIDByPOST() { $propertyID = trim($_POST[self::PROPERTY_OPTION_ID_KEY]); if ($propertyID !== self::getPropertyID()) { if (strpos($propertyID, 'A-') !== 0) { return false; } self::$propertyID = $propertyID; update_option(self::PROPERTY_OPTION_ID_KEY, $propertyID, true); // delete the promise since this is a user provided propertyID delete_option(self::PROPERTY_PROMISE_OPTION_ID_KEY); delete_option(self::PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY); // recreate the http since it has a propertyID dependency self::$httpHandlerInstance = null; // if the propertyID updated, the embed should too $script = self::requestEmbedScript(true); // even if this is empty, we should still set it // since the property changed self::setEmbed($script); return true; } return false; } /** * Sets the propertyID, not touching the DB * * @param string $propertyID the propertyID to set */ private static function setPropertyID($propertyID) { if (empty($propertyID)) { throw new \Exception("PropertyID cannot be empty"); } self::$propertyID = $propertyID; } private static function setPropertyPromiseID($propertyPromiseID) { if (empty($propertyPromiseID)) { throw new \Exception("propertyPromiseID cannot be empty"); } self::$propertyPromiseID = $propertyPromiseID; } /** * Getter for self::$propertyPromiseID * * @return string the propertyPromiseID (or empty string) */ public static function getPropertyPromiseID() { if (empty(self::$propertyPromiseID)){ self::$propertyPromiseID = get_option(self::PROPERTY_PROMISE_OPTION_ID_KEY, ""); } return self::$propertyPromiseID; } /** * Checks if the property is an orphan property * * @param string $propertyID the propertyID to check * @return bool whether the property is an orphan property */ public static function isPropertyOrphanProperty($propertyID) { $promiseProperty = get_option(self::PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY, ""); return $propertyID === $promiseProperty; } /** * Gets the protect option from the DB * * @return string|bool whether protect is on, unconfigured, or off */ public static function getProtect() { if (self::$protect === null) { self::$protect = get_option(self::PROTECT_OPTION_KEY, "unconfigured"); } return self::$protect; } /** * Gets whether protect is enabled * * @return bool whether protect is enabled */ public static function isProtectEnabled() { $protect = self::getProtect(); return $protect === "unconfigured" || !!$protect; } /** * Sets the protect option in the DB and updates self::$protect * * @param bool $protect whether protect is on or off */ public static function setProtect($protect) { $updated = update_option(self::PROTECT_OPTION_KEY, $protect, true); if ($updated) { self::$protect = $protect; } } /** * Reads an option, normalizing "not set" to null so an unset option can be * told apart from one holding a falsy value * * @param string $key the option to read * @return string|bool|null the stored value, or null if the option is unset */ private static function readOptionOrNull($key) { $value = get_option($key, null); return ($value === null || $value === false) ? null : $value; } /** * Retrieves the First-Party Mode option from the DB. * * Two keys are consulted: the current "admiral_first_party_mode" and the * legacy "admiral_proxy" used before the rename. setFirstPartyMode() always * writes both, so the two can only disagree when something outside this * plugin wrote the legacy key — support tooling, wp-cli, or an older plugin * version after a downgrade. In that case the legacy value is the newer * intent and wins; the next save re-syncs both keys. * * @return string|bool whether First-Party Mode is on, unconfigured, or off */ public static function getFirstPartyMode() { if (self::$firstPartyMode === null) { $value = self::readOptionOrNull(self::FIRST_PARTY_MODE_OPTION_KEY); $legacy = self::readOptionOrNull(self::LEGACY_FIRST_PARTY_MODE_OPTION_KEY); if ($value === null || ($legacy !== null && $legacy !== $value)) { $value = $legacy; } self::$firstPartyMode = $value === null ? "unconfigured" : $value; } return self::$firstPartyMode; } /** * Gets whether First-Party Mode is enabled * * @return bool whether First-Party Mode is enabled */ public static function isFirstPartyModeEnabled() { $firstPartyMode = self::getFirstPartyMode(); return $firstPartyMode !== "unconfigured" && !!$firstPartyMode; } /** * Sets the First-Party Mode option in the DB and updates * self::$firstPartyMode. The legacy "admiral_proxy" option is written too * so that downgrading to a plugin version from before the rename keeps the * publisher's setting. * * @param bool $firstPartyMode whether First-Party Mode is on or off */ public static function setFirstPartyMode($firstPartyMode) { update_option(self::FIRST_PARTY_MODE_OPTION_KEY, $firstPartyMode, true); update_option(self::LEGACY_FIRST_PARTY_MODE_OPTION_KEY, $firstPartyMode, true); // drop the cached value rather than assuming what was stored. // update_option() returns false both when a write fails and when the // value was already there, so its return value can't tell us which // happened, and either key may have been the one that changed. the next // read re-derives the value from the DB. self::$firstPartyMode = null; } /** * @deprecated Use getFirstPartyMode() * @return string|bool whether First-Party Mode is on, unconfigured, or off */ public static function getProxyAdmiral() { return self::getFirstPartyMode(); } /** * @deprecated Use isFirstPartyModeEnabled() * @return bool whether First-Party Mode is enabled */ public static function isProxyAdmiralEnabled() { return self::isFirstPartyModeEnabled(); } /** * @deprecated Use setFirstPartyMode() * @param bool $proxyAdmiral whether First-Party Mode is on or off */ public static function setProxyAdmiral($proxyAdmiral) { self::setFirstPartyMode($proxyAdmiral); } /** * Gets whether the site has permalinks set up * * @return bool whether permalinks are set up */ public static function hasPermalinks() { return !empty(get_option('permalink_structure')); } /** * Sets $embed and updates the DB with the new embed * * @param string $embed the embed to set */ private static function setEmbed($embed) { update_option(self::EMBED_EXPIRATION_OPTION_KEY, time() + self::DEFAULT_EMBED_EXPIRATION, true); if (update_option(self::EMBED_OPTION_KEY, $embed, true)) { self::$embed = $embed; } } /** * Retrieves the embed from the DB. If it doesn't exist or * has expired, retrieves an embed from kikis and returns it * * @return string the embed or empty string */ public static function getEmbed() { $propertyID = self::getPropertyID(); if (empty(self::$embed) && !empty($propertyID)) { self::$embed = get_option(self::EMBED_OPTION_KEY, ""); $embedTimeout = get_option(self::EMBED_EXPIRATION_OPTION_KEY, 0); // If the no embed was stored in the DB, or the current embed is timed out // we should request a new embed script $isExpired = $embedTimeout < time(); if ($isExpired || empty(self::$embed)) { // This also has an optional cache (APCu) that it uses, so call with forceRefresh // if we already have an embed $script = self::requestEmbedScript(!empty(self::$embed)); if (!empty($script)) { self::setEmbed($script); } } } return self::$embed; } public static function setHasEmbedded() { self::$hasEmbedded = true; } public static function getHasEmbedded() { return self::$hasEmbedded; } private static function returnBody($res) { if (empty($res["source"])) { return false; } $body = json_decode($res["source"], true); if (empty($body["result"])) { return false; } return $body; } /** * Get the additional options to send when requesting an embed. * * @return string the additional options (or empty string) */ public static function getEmbedAdditionalOptions() { if (empty(self::$embedAdditionalOptions)) { self::$embedAdditionalOptions = get_option(self::EMBED_ADDITIONAL_OPTIONS_KEY, ""); } return self::$embedAdditionalOptions; } /** * Retrieves protect candidates * * @return array of candidates */ public static function getProtectCandidates($forceRefresh = false) { if (self::isProtectEnabled()) { $hasAPCu = function_exists('apcu_enabled') && apcu_enabled(); $candidateKey = "admiral_protect_candidates"; $lockKey = "admiral_protect_candidates_lock"; // the amount of time in seconds that needs to elapse before we try to replace the cache $regen = 3600; $cachedCandidatesRes = $hasAPCu ? apcu_fetch($candidateKey) : get_option($candidateKey, []); $savedTime = isset($cachedCandidatesRes["time"]) ? $cachedCandidatesRes["time"] : 0; $cachedCandidates = isset($cachedCandidatesRes["candidates"]) ? $cachedCandidatesRes["candidates"] : []; $rightNow = time(); $timeDiff = $rightNow - $savedTime; if (!$savedTime || $timeDiff >= $regen || ($timeDiff >= 30 && $forceRefresh)) { // Use APCu for locking if available, otherwise fall back to WordPress options $gotLock = $hasAPCu ? apcu_add($lockKey, true, 10) : add_option($lockKey, true, "", true); if (!$gotLock) { return $cachedCandidates; } $secretPostData = array( "method" => "Partner.CreateSecretPromise", "jsonrpc" => "2.0", "params" => array( "apiKey" => "wordpressplugin-" . self::$clientID, "apiKeySecret" => self::$clientSecret, ) ); $headers = [ "Content-Type" => "application/json", "User-Agent" => self::$UASuffix, ]; $res = self::getHttpHandlerInstance()->httpCall(self::$partnerApiEndpoint, $headers, json_encode($secretPostData)); $body = self::returnBody($res); if (!$body) { error_log("error occurred obtaining secret"); // Clean up lock using the same method that was used to create it if ($hasAPCu) { apcu_delete($lockKey); } else { delete_option($lockKey); } return []; } $postData = [ "method" => "Partner.GetProtectCandidates", "jsonrpc" => "2.0", "params" => [ "propertyID" => self::getPropertyID(), "apiKey" => "wordpressplugin-" . self::$clientID, "apiKeySecretPromise" => $body["result"]["secretPromise"], ] ]; $headers = [ "Content-Type" => "application/json", "User-Agent" => self::$UASuffix, ]; $res = self::getHttpHandlerInstance()->httpCall(self::$partnerApiEndpoint, $headers, json_encode($postData)); $body = self::returnBody($res); if (!$body) { error_log("error obtaining candidates"); // Clean up lock using the same method that was used to create it if ($hasAPCu) { apcu_delete($lockKey); } else { delete_option($lockKey); } return []; } if (!isset($body["result"]["candidates"])) { // set the time to recheck in a minute $recheckTime = $rightNow - $regen + 60; // store an empty array, we'll check for protect candidates again later // Clean up lock using the same method that was used to create it $storeValue = ["time" => $recheckTime, "candidates" => []]; if ($hasAPCu) { apcu_store($candidateKey, $storeValue); apcu_delete($lockKey); } else { update_option($candidateKey, $storeValue, true); delete_option($lockKey); } return []; } $parsedCandidates = $body["result"]["candidates"]; $candidates = []; foreach ($parsedCandidates as $pCand) { // check the things that matter: candidateID, payload, protect stuff if (isset($pCand["candidateID"]) && isset($pCand["payload"]) && isset($pCand["payload"]["protect"]) && isset($pCand["payload"]["protect"]["requirement"])) { array_push($candidates, $pCand); } } $storeValue = ["time" => $rightNow, "candidates" => $candidates]; $stored = $hasAPCu ? apcu_store($candidateKey, $storeValue) : update_option($candidateKey, $storeValue, true); if (!$stored) { error_log("Error saving candidates to cache"); } // Clean up lock using the same method that was used to create it if ($hasAPCu) { apcu_delete($lockKey); } else { delete_option($lockKey); } return $candidates; } return $cachedCandidates; } return []; } /** * Sets the clientID and clientSecret * * @param string $clientID the clientID * @param string $clientSecret the clientSecret */ public static function setClientIDSecret($clientID, $clientSecret) { self::$clientID = $clientID; self::$clientSecret = $clientSecret; } /** * Retrieves the property ID and returns true or false respectively if it is set or not * Also allows for setting up the plugin code/version */ public static function initialize($pluginCode, $pluginVersion, $env) { if (empty(self::$pluginCode)) { self::$pluginCode = $pluginCode; self::$environment = $env; self::$UASuffix = "ADMIRALWP/" . $pluginVersion . " " . $pluginCode; add_action('admin_post_activate_admiral_adblocks_analytics_' . $pluginCode, function() { // Call redirect before all so that the user isn't sent to a blank page in case of handled error. $referer = wp_get_referer(); if (array_key_exists("accept", $_POST)) { AdmiralAdBlockAnalytics::createNewProperty(""); } if (!empty($referer)) { wp_redirect($referer); } else { wp_redirect('index.php'); } }); } if (empty(self::$store)) { self::$store = new AdmiralCacheAPC(); } $propertyID = self::getPropertyID(); if (empty($propertyID)) { return false; } return true; } public static function getPluginCode() { return self::$pluginCode; } public static function getBaseSignupLink() { return 'https://app.getadmiral.com/signup'; } public static function getClaimPropertyLink() { $link = self::getBaseSignupLink(); $token = self::getPropertyClaimToken(); $url = get_site_url(); $pid = self::getPropertyID(); if (empty($token)) { return ''; } $qs = http_build_query([ 'i' => 'claim-property', 't' => $token, 'd' => $url, 'p' => $pid, 'aid' => self::$clientID, ]); return $link . '?' . $qs; } private static function getSecretPromiseCall() { $postData = [ "method" => "Partner.CreateSecretPromise", "jsonrpc" => "2.0", "params" => [ "apiKey" => "wordpressplugin-" . self::$clientID, "apiKeySecret" => self::$clientSecret ] ]; $headers = [ "Content-Type" => "application/json", "User-Agent" => self::$UASuffix, ]; $res = self::getHttpHandlerInstance()->httpCall(self::$partnerApiEndpoint, $headers, json_encode($postData)); if (empty($res["source"])) { error_log("Partner.CreateSecretPromise returned empty"); return ""; } $body = json_decode($res["source"], true); if (empty($body["result"])) { error_log("Partner.CreateSecretPromise returned empty result"); return ""; } return $body["result"]["secretPromise"]; } private static function createPropertyCall($secretPromise, $domain) { $postData = [ "method" => "Partner.NewOrphanProperty", "jsonrpc" => "2.0", "params" => [ "apiKey" => "wordpressplugin-" . self::$clientID, "apiKeySecretPromise" => $secretPromise, "domain" => $domain, "withEmbed" => true ] ]; $headers = [ "Content-Type" => "application/json", "User-Agent" => self::$UASuffix, ]; $res = self::getHttpHandlerInstance()->httpCall(self::$partnerApiEndpoint, $headers, json_encode($postData)); if (empty($res["source"])) { error_log("Partner.NewOrphanProperty returned empty"); return ""; } $body = json_decode($res["source"], true); if (empty($body["result"])) { error_log("Partner.NewOrphanProperty returned empty result"); return ""; } return $body["result"]; } /** * Function to create new anonymous property * * @return bool whether the property was created and setup */ public static function createNewProperty($domain) { $secretPromise = self::getSecretPromiseCall(); if (empty($domain)) { $domain = get_site_url(); } if (!empty($secretPromise)) { $property = self::createPropertyCall($secretPromise, $domain); if (!empty($property)) { self::setPropertyID($property['propertyID']); self::setPropertyPromiseID($property['propertyPromiseID']); update_option(self::PROPERTY_OPTION_ID_KEY, self::getPropertyID(), true); update_option(self::PROPERTY_PROMISE_OPTION_ID_KEY, self::getPropertyPromiseID(), true); update_option(self::PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY, self::getPropertyID(), true); // Ensure this is set to avoid it trying to hit the db update_option(self::EMBED_ADDITIONAL_OPTIONS_KEY, self::getEmbedAdditionalOptions(), true); // Get embed will handle any potential fetching of embed code at this point self::setEmbed($property['embed']); return true; } return false; } } /** * Touch the options to ensure they are updated with autoload=true * This is used after an update to the plugin to ensure that the options are updated * with autoload=true so that they are loaded on every page load. * * @return void */ public static function touchOptions() { $pidValue = self::getPropertyID(); if (!empty($pidValue)) { update_option(self::PROPERTY_OPTION_ID_KEY, self::getPropertyID(), true); update_option(self::PROPERTY_PROMISE_OPTION_ID_KEY, self::getPropertyPromiseID(), true); if (self::isPropertyOrphanProperty($pidValue)) { update_option(self::PROPERTY_PROMISE_PROPERTY_OPTION_ID_KEY, self::getPropertyID(), true); } update_option(self::EMBED_ADDITIONAL_OPTIONS_KEY, self::getEmbedAdditionalOptions(), true); } } /** * Make a request to the delivery service to get a script for this property * * @return string the token or an empty string */ private static function getPropertyClaimToken() { $secretPromise = self::getSecretPromiseCall(); $postData = [ "method" => "Partner.GetClaimPropertyToken", "jsonrpc" => "2.0", "params" => [ "apiKey" => "wordpressplugin-" . self::$clientID, "apiKeySecretPromise" => $secretPromise, "propertyID" => self::getPropertyID(), "propertyPromiseID" => self::getPropertyPromiseID() ] ]; $headers = [ "Content-Type" => "application/json", "User-Agent" => self::$UASuffix, ]; $res = self::getHttpHandlerInstance()->httpCall(self::$partnerApiEndpoint, $headers, json_encode($postData)); if (empty($res["source"])) { error_log("Partner.GetClaimPropertyToken returned empty"); return ""; } $body = json_decode($res["source"], true); if (empty($body["result"])) { error_log("Partner.GetClaimPropertyToken returned empty result"); return ""; } if (empty($body["result"]["claimPropertyToken"])) { error_log("Partner.GetClaimPropertyToken returned empty claimPropertyToken"); return ""; } return $body["result"]["claimPropertyToken"]; } /** * Make a request to the delivery service to get a script for this property * * @return string the embed or an empty string */ private static function requestEmbedScript($forceRefresh = false) { // we can't call getHttpHandlerInstance without a store if (empty(self::$store)) { return ""; } $queryParams = array( "cacheable" => "1", "strategy" => "wordpress", ); if (self::isFirstPartyModeEnabled()) { $queryParams = array_merge($queryParams, self::getHttpHandlerInstance()->getEmbedQueryParams()); } $endpoint = "//delivery.api.getadmiral.com/script/"; $additionalOptions = self::getEmbedAdditionalOptions(); if (!empty($additionalOptions)) { $options = json_decode($additionalOptions, true); if (!empty($options['queryString'])) { $queryParams = array_merge($queryParams, $options['queryString']); } if (!empty($options['endpoint'])) { $endpoint = $options['endpoint']; } } $userAgent = self::$UASuffix; $target = "bootstrap"; $queryString = http_build_query($queryParams); $script = self::getHttpHandlerInstance()->fetchEmbed($endpoint, $userAgent, $target, $queryString, $forceRefresh); if (empty($script)) { return ""; } // Split by newlines and wrap each non-empty line in its own script tag $lines = explode("\n", $script); $scriptTags = array(); foreach ($lines as $line) { $trimmed = trim($line); if (!empty($trimmed)) { $scriptTags[] = ''; } } return implode("\n", $scriptTags); } /** * Returns the http handler instance * * @return AdmiralHttpHandler */ public static function getHttpHandlerInstance() { if (self::$httpHandlerInstance === null) { self::$httpHandlerInstance = new AdmiralHttpHandler(self::$store, self::getPropertyID()); } return self::$httpHandlerInstance; } /** * Force refresh the embed script from Admiral's servers * * @return bool whether the embed was successfully refreshed */ public static function refreshEmbed() { $propertyID = self::getPropertyID(); if (empty($propertyID)) { return false; } $script = self::requestEmbedScript(true); if (!empty($script)) { self::setEmbed($script); return true; } return false; } } /* EOF */