| @@ -23,8 +23,17 @@ | ||
| 23 | 23 | |
| 24 | 24 | public $notifications_data = []; |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | + * Request-level cache of notifications data, keyed by source. | |
| 28 | + * Inline hooks fire once per product in shop/archive loops; the | |
| 29 | + * result only varies by $source, so compute it once per request. | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + */ | |
| 33 | + protected $notifications_cache = []; | |
| 34 | + | |
| 35 | + /** | |
| 27 | 36 | * __construct__ is for revoke first time to get ready |
| 28 | 37 | * |
| 29 | 38 | * @return void |
| 30 | 39 | */ |
| @@ -35,16 +44,18 @@ | ||
| 35 | 44 | |
| 36 | 45 | |
| 37 | 46 | public function get_notifications_data( $source, $id = null, $settings = [] ) { |
| 38 | 47 | |
| 48 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 49 | + | |
| 39 | 50 | $exit = apply_filters('nx_inline_notifications_data', null, $source, $id, $settings); |
| 40 | 51 | if($exit){ |
| 41 | 52 | return $exit; |
| 42 | 53 | } |
| 43 | 54 | |
| 44 | - // if ( empty( $this->notifications_data ) ) { | |
| 45 | - $this->notifications_data = array( 'shortcode' => array() ); | |
| 46 | - $notifications = PostType::get_instance()->get_posts( | |
| 55 | + if ( ! isset( $this->notifications_cache[ $source ] ) ) { | |
| 56 | + $data = array( 'shortcode' => array() ); | |
| 57 | + $notifications = PostType::get_instance()->get_posts( | |
| 47 | 58 | array( |
| 48 | 59 | 'source' => $source, |
| 49 | 60 | 'enabled' => true, |
| 50 | 61 | 'is_inline' => true, |
| @@ -50,12 +61,13 @@ | ||
| 50 | 61 | 'is_inline' => true, |
| 51 | 62 | ) |
| 52 | 63 | ); |
| 53 | 64 | |
| 65 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 54 | 66 | do_action( 'nx_inline' ); |
| 55 | 67 | |
| 56 | 68 | if ( ! empty( $notifications ) ) { |
| 57 | - $this->notifications_data = FrontEnd::get_instance()->get_notifications_data( | |
| 69 | + $data = FrontEnd::get_instance()->get_notifications_data( | |
| 58 | 70 | array( |
| 59 | 71 | 'shortcode' => array_column( $notifications, 'nx_id' ), |
| 60 | 72 | 'inline_shortcode' => true, |
| 61 | 73 | ) |
| @@ -60,9 +72,13 @@ | ||
| 60 | 72 | 'inline_shortcode' => true, |
| 61 | 73 | ) |
| 62 | 74 | ); |
| 63 | 75 | } |
| 64 | - // } | |
| 76 | + | |
| 77 | + $this->notifications_cache[ $source ] = $data; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $this->notifications_data = $this->notifications_cache[ $source ]; | |
| 65 | 81 | return $this->notifications_data; |
| 66 | 82 | } |
| 67 | 83 | |
| 68 | 84 | |
| @@ -271,14 +287,14 @@ | ||
| 271 | 287 | // Determine the random number |
| 272 | 288 | if ($cookieValue !== null) { |
| 273 | 289 | $randomNumber = $cookieValue; |
| 274 | 290 | } else { |
| 275 | - $randomNumber = rand($min, $max); | |
| 291 | + $randomNumber = wp_rand($min, $max); | |
| 276 | 292 | |
| 277 | 293 | // If cookieValue existed, ensure new number is within the allowed range |
| 278 | 294 | if ($cookieValue !== null) { |
| 279 | 295 | while (abs($randomNumber - $cookieValue) > $range) { |
| 280 | - $randomNumber = rand($min, $max); | |
| 296 | + $randomNumber = wp_rand($min, $max); | |
| 281 | 297 | } |
| 282 | 298 | } |
| 283 | 299 | |
| 284 | 300 | // Set the cookie for this product |