3, 'edition' => $edition, 'sources' => array_values( $sources ), 'options' => $options ) ), wp_salt( 'auth' ) ); } /** Prime missing/non-autoloaded options in one bounded query; get_option filters still apply. */ private static function prime_context_options( $prefix, $names ) { if ( ! function_exists( 'wp_load_alloptions' ) ) { return; } global $wpdb; $loaded = wp_load_alloptions(); $missing = wp_cache_get( 'notoptions', 'options' ); $missing = is_array( $missing ) ? $missing : array(); $pending = array(); foreach ( $names as $name ) { $key = $prefix . '_shopping_' . $name; if ( ! isset( $loaded[ $key ] ) && ! isset( $missing[ $key ] ) && false === wp_cache_get( $key, 'options' ) ) { $pending[] = $key; } } if ( ! $pending ) { return; } $placeholders = implode( ',', array_fill( 0, count( $pending ), '%s' ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name IN ($placeholders)", $pending ) ); $found = array(); foreach ( $rows as $row ) { wp_cache_add( $row->option_name, $row->option_value, 'options' ); $found[ $row->option_name ] = true; } foreach ( $pending as $key ) { if ( ! isset( $found[ $key ] ) ) { $missing[ $key ] = true; } } wp_cache_set( 'notoptions', $missing, 'options' ); } /** A negative result is context-qualified outcome data, never a buying offer. */ public static function negative_cache_valid( $products, $context ) { return is_array( $products ) && $context && ( $products['_affiai_empty_context'] ?? null ) === $context && isset( $products['type'], $products['search'] ) && null !== self::cache_time( $products ) && ! array_filter( $products, 'is_array' ) && ( ! self::unavailable_product( $products ) || time() - self::cache_time( $products ) <= 86400 ); } public static function cache_time( $products ) { // Legacy frames used a site-local pseudo timestamp; invalidate once rather // than guessing its timezone. All new frames use an explicit UTC clock. if ( ! is_array( $products ) || ( $products['_affiai_cache_clock'] ?? null ) !== 'utc-v1' ) { return null; } $value = $products['date'] ?? null; return ( is_int( $value ) || ( is_string( $value ) && ctype_digit( $value ) ) ) && (int) $value > 0 && (int) $value <= time() ? (int) $value : null; } public static function cache_valid( $products, $context ) { if ( ! $context || ! is_array( $products ) || null === self::cache_time( $products ) ) { return false; } if ( self::negative_cache_valid( $products, $context ) ) { return true; } $seen = false; foreach ( $products as $product ) { if ( ! is_array( $product ) ) { continue; } $seen = true; if ( ( $product['_affiai_integrity']['context'] ?? null ) !== $context ) { return false; } } return $seen; } public static function stamp_cache( $products, $context, $market = null, $allow_negative = false ) { if ( $allow_negative && ! self::usable_products( $products ) ) { return array( 'type' => $products['type'] ?? '', 'search' => $products['search'] ?? '', 'date' => time(), '_affiai_cache_clock' => 'utc-v1', '_affiai_empty_context' => $context, '_afx_empty_product' => json_encode( self::unavailable_product( $products ) ) ); } $products['date'] = time(); $products['_affiai_cache_clock'] = 'utc-v1'; foreach ( $products as &$product ) { if ( is_array( $product ) && isset( $product['merchant'] ) ) { $product['_affiai_integrity'] = array( 'v' => 1, 'context' => $context, 'source_market' => $market ); } } unset( $product ); return $products; } /** Validate commercial fields even on cached/manual payloads; never display malformed prices. */ public static function clean_prices( $products ) { foreach ( $products as &$product ) { if ( ! is_array( $product ) || ! is_array( $product['merchant'] ?? null ) ) { continue; } foreach ( $product['merchant'] as &$offer ) { if ( ! is_array( $offer ) ) { continue; } $money = self::money( $offer['price'] ?? null, $offer['currency'] ?? null ); $old = self::money( $offer['discount_price'] ?? null, $offer['currency'] ?? null ); $offer['currency'] = self::currency( $offer['currency'] ?? null ); $offer['price'] = $money['amount'] ?? null; $offer['discount_price'] = $money && $old && self::compare( $old['minor'], $money['minor'] ) > 0 ? $old['amount'] : null; if ( ! $money || ! $old ) { $offer['discount'] = null; } } unset( $offer ); } unset( $product ); return $products; } /** Count usable purchase links, not the presence of an arbitrary product array. */ public static function usable_products( $products ) { $out = array(); foreach ( $products as $key => $product ) { if ( ! is_array( $product ) || ! is_array( $product['merchant'] ?? null ) ) { continue; } $product['merchant'] = array_filter( $product['merchant'], array( __CLASS__, 'usable' ) ); if ( $product['merchant'] ) { $out[ $key ] = $product; } } return $out; } /** Bounded latest operational counts, not visitor events or successful comparisons. */ public static function health_details( $products, $context, $post_id ) { $counts = array( 'usable_offers' => 0, 'unknown_money' => 0, 'unverified_identity' => 0, 'unknown_source_or_fetch' => 0 ); foreach ( self::usable_products( $products ) as $product ) { foreach ( $product['merchant'] as $offer ) { $counts['usable_offers']++; if ( ! self::money( $offer['price'] ?? null, $offer['currency'] ?? null ) ) { $counts['unknown_money']++; } $market = $product['_affiai_integrity']['source_market'] ?? null; $fetched = $offer['source_fetched_at'] ?? null; if ( ! $market || ! is_int( $fetched ) || $fetched < 1 || $fetched > time() ) { $counts['unknown_source_or_fetch']++; } } } foreach ( $counts as &$count ) { $count = min( 10000, $count ); } unset( $count ); return array( 'context' => $context, 'post_id' => max( 0, (int) $post_id ), 'counts' => $counts ); } /** Plain text can pass through WordPress's later shortcode filter without execution. */ public static function plain_html( $value ) { return str_replace( array( '[', ']' ), array( '[', ']' ), esc_html( $value ) ); } /** Keep only the permitted identity fields in a context-bound negative frame. */ public static function unavailable_product( $products ) { if ( is_string( $products['_afx_empty_product'] ?? null ) ) { $decoded = json_decode( $products['_afx_empty_product'], true ); return is_array( $decoded ) ? $decoded : array(); } foreach ( $products as $product ) { if ( is_array( $product ) && isset( $product['_afx_unavailable_asin'] ) ) { return array_intersect_key( $product, array_flip( array( '_afx_unavailable_asin', 'title', 'image' ) ) ); } } return array(); } /** Opt-in empty presentation is assembled outside the commercial product templates. */ public static function no_offers( $atts, $id_attribute, $template, $domain, $amp = false, $source = array() ) { $message = is_string( $atts['no_offers_message'] ?? null ) ? trim( html_entity_decode( $atts['no_offers_message'], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ) : ''; $keep = in_array( $atts['no_offers_keep_product'] ?? null, array( true, 'yes', '1', 1 ), true ); if ( '' === $message && $keep ) { $message = __( 'Product offers are temporarily unavailable.', $domain ); } $ids = $atts['asin'] ?: ( $atts[ $id_attribute ] ?? '' ); if ( '' === $message || strlen( $message ) > 2000 || ! is_string( $ids ) || ! trim( $ids ) || ! empty( $atts['keyword'] ) || ! empty( $atts['auto'] ) || ! in_array( $template, array( 'box', 'grid', 'list', 'carousel' ), true ) ) { return null; } $shell = ''; if ( $keep && 1 === count( explode( ',', $ids ) ) ) { $title = is_string( $atts['title'] ?? null ) ? html_entity_decode( $atts['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : ''; $image = is_string( $atts['image'] ?? null ) ? html_entity_decode( $atts['image'], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : ''; // Delimited publisher fields do not identify a single product shell. if ( false !== strpos( $title . $image, '|' ) ) { $title = $image = ''; } $source_id = $source['_afx_unavailable_asin'] ?? null; if ( is_string( $source_id ) && preg_match( '/^[A-Z0-9]{10}$/D', $source_id ) && in_array( trim( $ids ), array( $source_id, 'AZ' . $source_id ), true ) ) { if ( '' === $title && is_string( $source['title'] ?? null ) ) { $title = $source['title']; } if ( '' === $image && is_string( $source['image'] ?? null ) ) { $image = $source['image']; } } if ( $title ) { $shell .= '

' . self::plain_html( $title ) . '

'; } $width = $height = 250; // Fixed contain-fit image slot; attachment dimensions are used when available. if ( $image && ctype_digit( $image ) ) { $attachment = wp_get_attachment_image_src( (int) $image, 'large' ); $image = $attachment[0] ?? ''; $width = max( 1, (int) ( $attachment[1] ?? 250 ) ); $height = max( 1, (int) ( $attachment[2] ?? 250 ) ); } if ( $image && preg_match( '#^https?://#i', $image ) && esc_url( $image ) ) { $image_url = str_replace( array( '[', ']' ), array( '[', ']' ), esc_url( $image ) ); $shell .= $amp ? '' : '' . esc_attr( $title ) . ''; } } return '
' . $shell . '

' . self::plain_html( $message ) . '

'; } /** Entity encoding protects a block message from shortcode quote/bracket injection. */ public static function block_options( $attributes ) { $message = $attributes['noOffersMessage'] ?? ''; if ( ! is_string( $message ) || strlen( $message ) > 2000 || ( '' === trim( $message ) && true !== ( $attributes['noOffersKeepProduct'] ?? false ) ) ) { return ''; } $message = str_replace( array( '[', ']' ), array( '[', ']' ), htmlspecialchars( $message, ENT_QUOTES, 'UTF-8' ) ); return ' no_offers_message="' . $message . '" no_offers_keep_product="' . ( true === ( $attributes['noOffersKeepProduct'] ?? false ) ? 'yes' : 'no' ) . '"'; } /** ISO 4217 monetary codes and precision, maintained by SIX; snapshot 2026-10-13. */ public static function currencies() { return array( 'AED' => 2, 'AFN' => 2, 'ALL' => 2, 'AMD' => 2, 'AOA' => 2, 'ARS' => 2, 'AUD' => 2, 'AWG' => 2, 'AZN' => 2, 'BAM' => 2, 'BBD' => 2, 'BDT' => 2, 'BHD' => 3, 'BIF' => 0, 'BMD' => 2, 'BND' => 2, 'BOB' => 2, 'BOV' => 2, 'BRL' => 2, 'BSD' => 2, 'BTN' => 2, 'BWP' => 2, 'BYN' => 2, 'BZD' => 2, 'CAD' => 2, 'CDF' => 2, 'CHE' => 2, 'CHF' => 2, 'CHW' => 2, 'CLF' => 4, 'CLP' => 0, 'CNY' => 2, 'COP' => 2, 'COU' => 2, 'CRC' => 2, 'CUP' => 2, 'CVE' => 2, 'CZK' => 2, 'DJF' => 0, 'DKK' => 2, 'DOP' => 2, 'DZD' => 2, 'EGP' => 2, 'ERN' => 2, 'ETB' => 2, 'EUR' => 2, 'FJD' => 2, 'FKP' => 2, 'GBP' => 2, 'GEL' => 2, 'GHS' => 2, 'GIP' => 2, 'GMD' => 2, 'GNF' => 0, 'GTQ' => 2, 'GYD' => 2, 'HKD' => 2, 'HNL' => 2, 'HTG' => 2, 'HUF' => 2, 'IDR' => 2, 'ILS' => 2, 'INR' => 2, 'IQD' => 3, 'IRR' => 2, 'ISK' => 0, 'JMD' => 2, 'JOD' => 3, 'JPY' => 0, 'KES' => 2, 'KGS' => 2, 'KHR' => 2, 'KMF' => 0, 'KPW' => 2, 'KRW' => 0, 'KWD' => 3, 'KYD' => 2, 'KZT' => 2, 'LAK' => 2, 'LBP' => 2, 'LKR' => 2, 'LRD' => 2, 'LSL' => 2, 'LYD' => 3, 'MAD' => 2, 'MDL' => 2, 'MGA' => 2, 'MKD' => 2, 'MMK' => 2, 'MNT' => 2, 'MOP' => 2, 'MRU' => 2, 'MUR' => 2, 'MVR' => 2, 'MWK' => 2, 'MXN' => 2, 'MXV' => 2, 'MYR' => 2, 'MZN' => 2, 'NAD' => 2, 'NGN' => 2, 'NIO' => 2, 'NOK' => 2, 'NPR' => 2, 'NZD' => 2, 'OMR' => 3, 'PAB' => 2, 'PEN' => 2, 'PGK' => 2, 'PHP' => 2, 'PKR' => 2, 'PLN' => 2, 'PYG' => 0, 'QAR' => 2, 'RON' => 2, 'RSD' => 2, 'RUB' => 2, 'RWF' => 0, 'SAR' => 2, 'SBD' => 2, 'SCR' => 2, 'SDG' => 2, 'SEK' => 2, 'SGD' => 2, 'SHP' => 2, 'SLE' => 2, 'SOS' => 2, 'SRD' => 2, 'SSP' => 2, 'STN' => 2, 'SVC' => 2, 'SYP' => 2, 'SZL' => 2, 'THB' => 2, 'TJS' => 2, 'TMT' => 2, 'TND' => 3, 'TOP' => 2, 'TRY' => 2, 'TTD' => 2, 'TWD' => 2, 'TZS' => 2, 'UAH' => 2, 'UGX' => 0, 'USD' => 2, 'USN' => 2, 'UYI' => 0, 'UYU' => 2, 'UYW' => 4, 'UZS' => 2, 'VED' => 2, 'VES' => 2, 'VND' => 0, 'VUV' => 0, 'WST' => 2, 'XAD' => 2, 'XAF' => 0, 'XCD' => 2, 'XCG' => 2, 'XOF' => 0, 'XPF' => 0, 'YER' => 2, 'ZAR' => 2, 'ZMW' => 2, 'ZWG' => 2 ); } /** Ambiguous symbols have no currency; display codes keep dollar currencies distinct. */ public static function currency( $value ) { if ( ! is_string( $value ) ) { return null; } $symbols = array( '€' => 'EUR', '£' => 'GBP', '₹' => 'INR', 'R$' => 'BRL' ); $value = trim( $value ); $code = $symbols[ $value ] ?? strtoupper( $value ); return array_key_exists( $code, self::currencies() ) ? $code : null; } /** Canonical decimal plus integer minor-unit string; never guess formatted locale text. */ public static function money( $value, $currency ) { $currency = self::currency( $currency ); if ( null === $currency || ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) ) { return null; } if ( is_float( $value ) ) { if ( ! is_finite( $value ) ) { return null; } $value = json_encode( $value, JSON_PRESERVE_ZERO_FRACTION ); } $value = (string) $value; if ( ! preg_match( '/^(0|[1-9][0-9]{0,9})(?:\.([0-9]{1,8}))?$/D', $value, $parts ) ) { return null; } $precision = self::currencies()[ $currency ]; $fraction = rtrim( $parts[2] ?? '', '0' ); if ( strlen( $fraction ) > $precision ) { return null; } $fraction = str_pad( $fraction, $precision, '0' ); return array( 'amount' => $parts[1] . ( $precision ? '.' . $fraction : '' ), 'minor' => ltrim( $parts[1] . $fraction, '0' ) ?: '0', 'currency' => $currency ); } /** Compare unsigned integer strings without float conversion or platform integer overflow. */ public static function compare( $a, $b ) { $a = ltrim( $a, '0' ) ?: '0'; $b = ltrim( $b, '0' ) ?: '0'; return strlen( $a ) === strlen( $b ) ? strcmp( $a, $b ) : ( strlen( $a ) < strlen( $b ) ? -1 : 1 ); } /** Display-only discount; canonical money is validated before division. */ public static function discount( $price, $old, $currency ) { $current = self::money( $price, $currency ); $previous = self::money( $old, $currency ); if ( ! $current || ! $previous || '0' === $previous['minor'] || self::compare( $previous['minor'], $current['minor'] ) <= 0 ) { return null; } return round( 100 - ( (float) $current['amount'] * 100 / (float) $previous['amount'] ), 2 ); } /** Preserve raw zeros; canonical padding is permitted only after check-digit validation. */ public static function gtin( $value ) { if ( ! is_string( $value ) || ! preg_match( '/^(?:[0-9]{8}|[0-9]{12}|[0-9]{13}|[0-9]{14})$/D', $value ) ) { return null; } $sum = 0; $weight = 3; for ( $i = strlen( $value ) - 2; $i >= 0; $i-- ) { $sum += (int) $value[ $i ] * $weight; $weight = 4 - $weight; } return ( 10 - $sum % 10 ) % 10 === (int) substr( $value, -1 ) ? str_pad( $value, 14, '0', STR_PAD_LEFT ) : null; } /** Source price context stays explicit even when regional links are routed elsewhere. */ public static function context_html( $offer, $product ) { $market = $product['_affiai_integrity']['source_market'] ?? null; $currency = self::currency( $offer['currency'] ?? null ); /* translators: %s: source currency, marketplace, or UTC fetch time as specified in the sentence. */ $parts = array( $currency ? sprintf( __( 'Item price in %s; shipping, tax and stock may be unknown.', 'affiliate-ai-lite' ), $currency ) : __( 'Currency unknown; price unavailable.', 'affiliate-ai-lite' ) ); /* translators: %s: source currency, marketplace, or UTC fetch time as specified in the sentence. */ $parts[] = $market ? sprintf( __( 'Source market: %s.', 'affiliate-ai-lite' ), $market ) : __( 'Source market unknown.', 'affiliate-ai-lite' ); $fetched = $offer['source_fetched_at'] ?? null; /* translators: %s: source currency, marketplace, or UTC fetch time as specified in the sentence. */ $parts[] = is_int( $fetched ) && $fetched > 0 && $fetched <= time() ? sprintf( __( 'Fetched: %s UTC.', 'affiliate-ai-lite' ), gmdate( 'Y-m-d H:i', $fetched ) ) : __( 'Fetch time unknown.', 'affiliate-ai-lite' ); return '

' . esc_html( implode( ' ', $parts ) ) . '

'; } /** A permitted HTTP(S) purchase link remains useful without a known price. */ public static function usable( $offer ) { if ( ! is_array( $offer ) || ! is_string( $offer['url'] ?? null ) || 'absent' === ( $offer['offer_state'] ?? null ) ) { return false; } $url = wp_parse_url( $offer['url'] ); return is_array( $url ) && ! empty( $url['host'] ) && in_array( strtolower( $url['scheme'] ?? '' ), array( 'http', 'https' ), true ) && empty( $url['user'] ) && empty( $url['pass'] ) && ! preg_match( '/[\x00-\x20]/', $offer['url'] ) && ! in_array( $offer['availability'] ?? '', array( 'OutOfStock', 'Discontinued' ), true ); } }