block. * * The HEX flags render any <, >, &, ' and " inside strings as \uXXXX so a * value can never break out of the script tag into the surrounding HTML. * * @param mixed $value * @return string|false */ function ridoway_pixel_json_encode($value) { return wp_json_encode( $value, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); } function ridoway_pixel_option_enabled($key, $default = '0') { return ridoway_pixel_option($key, $default) === '1'; } /** * Number of minor units in one major unit for a currency, per ISO 4217. * * OpenAI expects amounts in integer minor units. WooCommerce can display any * number of decimals, so the display setting is deliberately ignored here: the * currency's real exponent is what the Conversions API expects. * * @param string $currency Three-letter currency code. * @return int 0, 2 or 3. */ function ridoway_pixel_currency_exponent($currency) { $currency = strtoupper(trim((string) $currency)); // Currencies with no minor unit: the amount is already an integer. $zero_decimal = array( 'BIF', 'CLP', 'DJF', 'GNF', 'ISK', 'JPY', 'KMF', 'KRW', 'PYG', 'RWF', 'UGX', 'UYI', 'VND', 'VUV', 'XAF', 'XOF', 'XPF', ); // Currencies with 1000 minor units per major unit. $three_decimal = array( 'BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND' ); if (in_array($currency, $zero_decimal, true)) { $exponent = 0; } elseif (in_array($currency, $three_decimal, true)) { $exponent = 3; } else { $exponent = 2; } return (int) apply_filters('ridoway_pixel_currency_exponent', $exponent, $currency); } /** * Convert a decimal amount into OpenAI's integer minor units. * * @param mixed $amount Decimal amount as float, int or numeric string. * @param string $currency Three-letter currency code. * @return int|null Integer minor units, or null when there is no usable amount. */ function ridoway_pixel_to_minor_units($amount, $currency = '') { if ($amount === null || $amount === '' || is_bool($amount) || is_array($amount)) { return null; } if (!is_numeric($amount)) { return null; } $value = (float) $amount; if (!is_finite($value)) { return null; } return (int) round($value * pow(10, ridoway_pixel_currency_exponent($currency))); } /** * Whether the plugin may track the current visitor. * * GDPR suites hook `ridoway_pixel_consent` and return false until the visitor * grants consent. The browser side additionally honours the * ridoway_pixel_respect_consent setting. * * @return bool */ function ridoway_pixel_consent_rule() { return (bool) apply_filters('ridoway_pixel_consent', true); } /** * OpenAI's click identifier for the current visitor, for server events. * * @return string Empty string when the visitor has no click id. */ function ridoway_pixel_capi_get_oppref() { if (!empty($_COOKIE['__oppref'])) { return sanitize_text_field(wp_unslash($_COOKIE['__oppref'])); } if (!empty($_GET['oppref'])) { return sanitize_text_field(wp_unslash($_GET['oppref'])); } return ''; } /** * The OpenAI browser reference cookie, reported inside the user object. * * @return string */ function ridoway_pixel_capi_get_obref() { if (!empty($_COOKIE['__obref'])) { return sanitize_text_field(wp_unslash($_COOKIE['__obref'])); } return ''; }