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( $message ) . '
' . 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 ); } }