0 ? $price : null; } private static function dropped($rows, $multiplier, $arm_key, $subs_by) { $out = array(); foreach ((array) $rows as $r) { $snapshot = (float) $r['snapshot']; $current = self::stored_price($r['price']); if ($snapshot <= 0 || $current === null || $current > $snapshot * $multiplier) { continue; } $key = 'user_id' === $arm_key ? (int) $r['user_id'] : (string) $r['session_id']; $out[] = array( 'sub_rows' => isset($subs_by[$key]) ? $subs_by[$key] : array(), 'item_id' => (int) $r['item_id'], 'snapshot' => $snapshot, 'current' => $current, 'drop_pct' => (int) round(100 * ($snapshot - $current) / $snapshot), 'wid' => (int) $r['wid'], ); } return $out; } public function get_threshold() { $opt = (float) get_option(self::OPTION_THRESHOLD, 0.05); if ($opt <= 0 || $opt > 0.9) { return 0.05; } return $opt; } public function register_hooks() { add_action('plugins_loaded', array($this, 'maybe_backfill_snapshots'), 28); } public function maybe_backfill_snapshots() { if (get_option(self::BACKFILL_OPTION, '') === self::BACKFILL_VERSION) { return; } if (get_transient(self::BACKFILL_OPTION . '_retry')) { return; } set_transient(self::BACKFILL_OPTION . '_retry', 1, HOUR_IN_SECONDS); global $wpdb; $tbl = $wpdb->prefix . 'aieo_wishlist'; $col_exists = (string) $wpdb->get_var( "SHOW COLUMNS FROM {$tbl} LIKE 'price_at_wishlist'" ); if ($col_exists === '') { return; } $missing = $wpdb->get_results( "SELECT w.id, pm.meta_value FROM {$tbl} w JOIN {$wpdb->postmeta} pm ON pm.post_id = w.item_id AND pm.meta_key = '_price' WHERE w.price_at_wishlist IS NULL AND w.item_type = 'product' AND pm.meta_value <> ''", ARRAY_A ); $snapshot = array(); foreach ((array) $missing as $row) { $price = self::stored_price($row['meta_value']); $id = (int) $row['id']; if ($price !== null && (!isset($snapshot[$id]) || $price < $snapshot[$id])) { $snapshot[$id] = $price; } } foreach ($snapshot as $id => $price) { $wpdb->query($wpdb->prepare( "UPDATE {$tbl} SET price_at_wishlist = %s WHERE id = %d AND price_at_wishlist IS NULL", number_format($price, 2, '.', ''), $id )); } update_option(self::BACKFILL_OPTION, self::BACKFILL_VERSION, true); delete_transient(self::BACKFILL_OPTION . '_retry'); } public function produce_candidates($limit = 200) { global $wpdb; $tbl_w = $wpdb->prefix . 'aieo_wishlist'; $tbl_sub = $wpdb->prefix . 'aieo_push_subscriptions'; $tbl_pm = $wpdb->postmeta; $tbl_p = $wpdb->posts; $limit = max(1, (int) $limit); $threshold = $this->get_threshold(); $multiplier = (float) (1.0 - $threshold); $subs = $wpdb->get_results( "SELECT subscription_id, user_id, session_token FROM {$tbl_sub} WHERE status = 'active' LIMIT 5000", ARRAY_A ); if (empty($subs)) { return array(); } $subs_by_user = array(); $subs_by_token = array(); foreach ($subs as $s) { $uid = (int) $s['user_id']; $tok = (string) $s['session_token']; $sub_row = array('subscription_id' => (int) $s['subscription_id'], 'user_id' => $uid); if ($uid > 0) { $subs_by_user[$uid][] = $sub_row; } elseif ($tok !== '') { $subs_by_token[$tok][] = $sub_row; } } if (empty($subs_by_user) && empty($subs_by_token)) { return array(); } $candidates = array(); if (!empty($subs_by_user)) { $user_ids = array_keys($subs_by_user); $in_users = implode(',', array_map('intval', $user_ids)); $logged_in = $wpdb->get_results( "SELECT w.user_id, w.id AS wid, w.item_id, w.price_at_wishlist AS snapshot, pm.meta_value AS price FROM {$tbl_w} w JOIN {$tbl_pm} pm ON pm.post_id = w.item_id AND pm.meta_key = '_price' WHERE w.user_id IN ({$in_users}) AND w.item_type = 'product' AND w.price_at_wishlist IS NOT NULL AND w.price_at_wishlist > 0 AND pm.meta_value <> ''", ARRAY_A ); $candidates = array_merge($candidates, self::dropped($logged_in, $multiplier, 'user_id', $subs_by_user)); } if (!empty($subs_by_token)) { $tokens = array_keys($subs_by_token); $placeholders = implode(',', array_fill(0, count($tokens), '%s')); $anon = $wpdb->get_results($wpdb->prepare( "SELECT w.session_id, w.id AS wid, w.item_id, w.price_at_wishlist AS snapshot, pm.meta_value AS price FROM {$tbl_w} w JOIN {$tbl_pm} pm ON pm.post_id = w.item_id AND pm.meta_key = '_price' WHERE w.user_id = 0 AND w.session_id IN ({$placeholders}) AND w.item_type = 'product' AND w.price_at_wishlist IS NOT NULL AND w.price_at_wishlist > 0 AND pm.meta_value <> ''", $tokens ), ARRAY_A); $candidates = array_merge($candidates, self::dropped($anon, $multiplier, 'session_id', $subs_by_token)); } if (empty($candidates)) { return array(); } $pids = array_values(array_unique(array_map( static function ($c) { return (int) $c['item_id']; }, $candidates ))); if (empty($pids)) { return array(); } $in_pids = implode(',', $pids); $titles = $wpdb->get_results( "SELECT ID, post_title FROM {$tbl_p} WHERE post_status = 'publish' AND post_type IN ('product', 'product_variation') AND ID IN ({$in_pids})", ARRAY_A ); $title_by_pid = array(); foreach ($titles as $t) { $title_by_pid[(int) $t['ID']] = (string) $t['post_title']; } usort($candidates, static function ($a, $b) { if ($a['drop_pct'] !== $b['drop_pct']) { return $b['drop_pct'] <=> $a['drop_pct']; } return $a['wid'] <=> $b['wid']; }); $out = array(); $seen = array(); foreach ($candidates as $c) { $pid = $c['item_id']; if (!isset($title_by_pid[$pid])) { continue; } foreach ($c['sub_rows'] as $sub_row) { $key = $sub_row['subscription_id'] . ':' . $pid; if (isset($seen[$key])) { continue; } $seen[$key] = true; $url = (string) get_permalink($pid); $out[] = array( 'subscription_id' => $sub_row['subscription_id'], 'user_id' => $sub_row['user_id'], 'target_product_id' => $pid, 'target_url' => $url ?: home_url('/'), 'payload_data' => array( 'product_name' => (string) $title_by_pid[$pid], 'snapshot_price'=> self::price_text($c['snapshot']), 'current_price' => self::price_text($c['current']), 'drop_pct' => $c['drop_pct'], ), ); if (count($out) >= $limit) { break 2; } } } return $out; } }