['hits' => 2, 'passed' => 0], ... ] */ private static $runtime_buffer = []; public function __construct(ADVAIPBL_Main $plugin_instance) { $this->plugin = $plugin_instance; add_action('shutdown', [$this, 'save_runtime_buffer']); add_action(self::CRON_HOOK, [$this, 'aggregate_metrics']); } /** * Increments the hit or pass counter for a specific rule. * * @param string $rule_id * @param string $type 'hits' or 'passed' */ public function increment($rule_id, $type = 'hits') { if (!isset(self::$runtime_buffer[$rule_id])) { self::$runtime_buffer[$rule_id] = ['hits' => 0, 'passed' => 0]; } if ($type === 'hits' || $type === 'passed') { self::$runtime_buffer[$rule_id][$type]++; } } /** * Called at the end of the PHP request to flush the runtime buffer. */ public function save_runtime_buffer() { if (empty(self::$runtime_buffer)) { return; } $existing_buffer = get_transient(self::TRANSIENT_BUFFER); if (!is_array($existing_buffer)) { $existing_buffer = []; } foreach (self::$runtime_buffer as $rule_id => $counts) { if (!isset($existing_buffer[$rule_id])) { $existing_buffer[$rule_id] = ['hits' => 0, 'passed' => 0]; } $existing_buffer[$rule_id]['hits'] += $counts['hits']; $existing_buffer[$rule_id]['passed'] += $counts['passed']; } set_transient(self::TRANSIENT_BUFFER, $existing_buffer, DAY_IN_SECONDS); } /** * Cron job callback to aggregate the transient buffer into the permanent storage. */ public function aggregate_metrics() { $buffer = get_transient(self::TRANSIENT_BUFFER); if (empty($buffer) || !is_array($buffer)) { return; } delete_transient(self::TRANSIENT_BUFFER); $metrics = get_option(self::OPTION_METRICS, []); if (!is_array($metrics)) { $metrics = []; } foreach ($buffer as $rule_id => $counts) { if (!isset($metrics[$rule_id])) { $metrics[$rule_id] = ['hits' => 0, 'passed' => 0]; } $metrics[$rule_id]['hits'] += $counts['hits']; $metrics[$rule_id]['passed'] += $counts['passed']; } update_option(self::OPTION_METRICS, $metrics); } /** * Retrieves the aggregated metrics. * * @return array */ public function get_metrics() { return get_option(self::OPTION_METRICS, []); } /** * Gets metrics for a specific rule. * * @param string $rule_id * @return array ['hits' => 0, 'passed' => 0] */ public function get_rule_metrics($rule_id) { $metrics = $this->get_metrics(); return $metrics[$rule_id] ?? ['hits' => 0, 'passed' => 0]; } }