get_var($wpdb->prepare( "SELECT eligible FROM {$table} WHERE surface = %s AND audience_key = %s AND product_id = %d LIMIT 1", $surface, $audience_key, $product_id )); return $eligible !== null ? (bool) $eligible : false; } public static function get_for($surface, $audience_key, $product_id = 0) { global $wpdb; if (!in_array($surface, self::VALID_SURFACES, true)) { return null; } $product_id = (int) $product_id; $memo_key = $surface . ':' . $audience_key . ':' . $product_id; if (array_key_exists($memo_key, self::$memo)) { return self::$memo[$memo_key]; } if (!self::table_exists()) { return null; } $table = self::table_name(); $row = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$table} WHERE surface = %s AND audience_key = %s AND product_id = %d LIMIT 1", $surface, $audience_key, $product_id ), ARRAY_A); return self::$memo[$memo_key] = self::decoded($row); } private static $memo = array(); public static function forget_request() { self::$memo = array(); } private static function decoded($row) { if (!is_array($row)) { return null; } if (in_array((string) ($row['surface'] ?? ''), array(self::SURFACE_BRAND_GIFT, self::SURFACE_FREE_SHIPPING_BAR), true) && class_exists('AIEO_DMM_Offer_Facts') && AIEO_DMM_Offer_Facts::left_behind((string) ($row['computed_at'] ?? ''))) { return null; } if (!empty($row['payload'])) { $decoded = json_decode((string) $row['payload'], true); $row['payload'] = is_array($decoded) ? $decoded : array(); } else { $row['payload'] = array(); } return $row; } public static function prime($surface, $audience_key, array $product_ids) { global $wpdb; if (!in_array($surface, self::VALID_SURFACES, true) || !is_string($audience_key) || '' === $audience_key) { return; } $missing = array(); foreach (array_unique(array_filter(array_map('intval', $product_ids))) as $id) { if (!array_key_exists($surface . ':' . $audience_key . ':' . $id, self::$memo)) { $missing[] = $id; } } if (!$missing || !self::table_exists()) { return; } $table = self::table_name(); $placeholders = implode(',', array_fill(0, count($missing), '%d')); $rows = $wpdb->get_results($wpdb->prepare( "SELECT * FROM {$table} WHERE surface = %s AND audience_key = %s AND product_id IN ({$placeholders})", array_merge(array($surface, $audience_key), $missing) ), ARRAY_A); $by = array(); foreach ((array) $rows as $r) { $by[(int) $r['product_id']] = $r; } foreach ($missing as $id) { self::$memo[$surface . ':' . $audience_key . ':' . $id] = self::decoded($by[$id] ?? null); } } public static function set($surface, $audience_key, $product_id, $eligible, $offer_id = null, ?array $payload = null) { global $wpdb; if (!in_array($surface, self::VALID_SURFACES, true)) { return false; } if (!self::table_exists()) { return false; } $row = array( 'surface' => (string) $surface, 'audience_key' => (string) $audience_key, 'product_id' => (int) $product_id, 'eligible' => $eligible ? 1 : 0, 'offer_id' => $offer_id !== null ? (int) $offer_id : null, 'payload' => $payload !== null ? wp_json_encode($payload) : null, 'computed_at' => current_time('mysql', true), ); $formats = array('%s', '%s', '%d', '%d', '%d', '%s', '%s'); unset(self::$memo[$surface . ':' . $audience_key . ':' . (int) $product_id]); return false !== $wpdb->replace(self::table_name(), $row, $formats); } public static function delete_for_offer($offer_id, $surface = self::SURFACE_BRAND_GIFT) { global $wpdb; $offer_id = (int) $offer_id; if ($offer_id <= 0 || !in_array($surface, self::VALID_SURFACES, true) || !self::table_exists()) { return 0; } $table = self::table_name(); $deleted = $wpdb->query($wpdb->prepare("DELETE FROM {$table} WHERE offer_id = %d AND surface = %s", $offer_id, $surface)); self::$memo = array(); return (int) $deleted; } public static function prune($surface, array $running_offer_ids, $and_ineligible = false) { global $wpdb; if (!in_array($surface, self::VALID_SURFACES, true) || !self::table_exists()) { return 0; } $ids = array_values(array_unique(array_filter(array_map('intval', $running_offer_ids)))); $stale = $ids ? 'eligible = 1 AND (offer_id IS NULL OR offer_id NOT IN (' . implode(',', $ids) . '))' : 'eligible = 1'; if ($and_ineligible) { $stale = '(' . $stale . ') OR eligible = 0'; } $table = self::table_name(); $deleted = $wpdb->query($wpdb->prepare("DELETE FROM {$table} WHERE surface = %s AND ({$stale})", $surface)); self::$memo = array(); return (int) $deleted; } public static function recompute($product_id, $audience_key) { $product_id = (int) $product_id; $count = 0; if ($product_id === 0) { self::set( self::SURFACE_WEB_PUSH, $audience_key, 0, $audience_key !== 'guest' ); $count++; $rows = apply_filters('aieo_eligibility_compute', array(), 0, $audience_key); if (is_array($rows)) { foreach ($rows as $surface => $row) { if (!in_array($surface, self::VALID_SURFACES, true)) { continue; } $eligible = !empty($row['eligible']); $offer_id = isset($row['offer_id']) ? (int) $row['offer_id'] : null; $payload = isset($row['payload']) && is_array($row['payload']) ? $row['payload'] : null; self::set($surface, $audience_key, 0, $eligible, $offer_id, $payload); $count++; } } return $count; } if (!function_exists('wc_get_product')) { return 0; } $product = wc_get_product($product_id); if (!$product) { return 0; } self::set(self::SURFACE_STICKY_ATC, $audience_key, $product_id, true); $count++; $rows = apply_filters('aieo_eligibility_compute', array(), $product_id, $audience_key); if (is_array($rows)) { foreach ($rows as $surface => $row) { if (!in_array($surface, self::VALID_SURFACES, true)) { continue; } $eligible = !empty($row['eligible']); $offer_id = isset($row['offer_id']) ? (int) $row['offer_id'] : null; $payload = isset($row['payload']) && is_array($row['payload']) ? $row['payload'] : null; self::set($surface, $audience_key, $product_id, $eligible, $offer_id, $payload); $count++; } } return $count; } public static function recompute_all_audiences($product_id) { $total = 0; $audiences = class_exists('AIEO_Identity_Bridge') ? AIEO_Identity_Bridge::known_audience_keys() : array('guest'); foreach ($audiences as $key) { $total += self::recompute($product_id, $key); } return $total; } public static function table_name() { global $wpdb; return $wpdb->prefix . self::TABLE_NAME; } public static function table_exists() { static $cached = null; if ($cached !== null) { return $cached; } global $wpdb; $found = (int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s", self::table_name() )); return $cached = ($found > 0); } public static function maybe_install() { $current = (int) get_option(self::SCHEMA_OPTION, 0); if ($current >= self::SCHEMA_VERSION) { if (!is_admin() && !wp_doing_cron()) { return; } if (self::table_exists()) { return; } } if (!AIEO_Schema_Lock::acquire(self::SCHEMA_OPTION)) { return; } global $wpdb; $table = self::table_name(); $charset = aieo_get_charset_collate(); $sql = "CREATE TABLE {$table} ( surface VARCHAR(32) NOT NULL, product_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, audience_key VARCHAR(64) NOT NULL, eligible TINYINT(1) NOT NULL DEFAULT 0, offer_id BIGINT(20) UNSIGNED DEFAULT NULL, payload LONGTEXT DEFAULT NULL, computed_at DATETIME NOT NULL, PRIMARY KEY (surface, audience_key, product_id), KEY ix_audience (audience_key), KEY ix_offer (offer_id) ) {$charset};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); update_option(self::SCHEMA_OPTION, self::SCHEMA_VERSION, 'no'); } }