Fix: Growth Alert (Inline) N+1 database queries on WooCommerce shop/archive pages#133
Conversation
Growth Alert (inline) hooks fire once per product on WooCommerce shop/archive pages, and each run re-queried the database for data that only varies by source. Restore request-level memoization in Inline::get_notifications_data(), keyed by source so different inline sources cannot receive each other's cached data. Measured on an 11-product archive with one active Growth Alert: Database->get_posts calls drop from 137 to 11 per page load with identical rendered output. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
✅ Found okay — reviewed the fix strictly; it's correct and low-risk. What it fixesGrowth Alert (inline) hooks Correctness (verified)
|
Problem
Growth Alert (inline) registers its render callback on
woocommerce_after_shop_loop_itemandwoocommerce_after_shop_loop_item_title, which fire once per product in shop/archive loops. Every callback run calledInline::get_notifications_data(), whose request-level cache was commented out — so the same product-independent data was re-queried from the database for every product on the page (classic N+1). Reported by a client whose shop pages slowed down, withNotificationX\Core\Database->get_postsdominating Query Monitor.This affects every site running NotificationX Pro with the WooCommerce module enabled — the queries fire even with zero Growth Alert campaigns created.
Fix
Restore request-level memoization in
Inline::get_notifications_data()(includes/Features/Inline.php), keyed by$sourceso different inline sources cannot receive each other's cached data (the likely reason the previous un-keyed cache was disabled). The computed result only varies by$source—$id/$settingsare only passed to thenx_inline_notifications_datashort-circuit filter — so per-request caching is safe. Per-product filtering still happens afterward in the extension render loop.The old public
$notifications_dataproperty keeps its previous shape/behavior for backward compatibility.Verification (Docker sandbox, WP + WooCommerce 10.9.1, NX 3.2.10 + Pro 3.1.2, 11 products, one active Growth Alert stock campaign)
Database->get_poststotalRendered Growth Alert markup is byte-identical before/after (verified by diffing the archive page HTML). With no inline campaign, per-page calls drop from 49 to 7.
Card
https://projects.startise.com/wp-admin/admin.php?page=fluent-boards#/boards/19/tasks/83239-Bug-Fix-%7C-Growth-Alert-(Inline
🤖 Generated with Claude Code