diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index 6428a76..281943f 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -21,6 +21,11 @@ */ class Event_Subscriber extends Abstract_Subscriber { + /** + * @var array + */ + private static $events = []; + /** * @inheritDoc * @@ -46,19 +51,11 @@ public function register() { * @return void */ public function cache_event( $name, $data ) { - $events = []; - - if ( $this->container->has( 'events' ) ) { - $events = $this->container->get( 'events' ); - } - - $events[] = [ + self::$events[] = [ 'name' => $name, 'data' => wp_json_encode( $data ), 'stellar_slug' => Config::get_stellar_slug(), ]; - - $this->container->bind( 'events', $events ); } /** @@ -69,7 +66,7 @@ public function cache_event( $name, $data ) { * @return void */ public function send_cached_events() { - if ( ! $this->container->has( 'events' ) ) { + if ( empty( self::$events ) ) { return; } @@ -82,12 +79,12 @@ public function send_cached_events() { 'sslverify' => false, 'body' => [ 'action' => Event::AJAX_ACTION, - 'events' => $this->container->get( 'events' ), + 'events' => self::$events, ], ] ); - $this->container->bind( 'events', [] ); + self::$events = []; } /** @@ -101,6 +98,10 @@ public function send_events() { // Get the passed event array. $events = filter_input( INPUT_POST, 'events', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); // phpcs:ignore WordPressVIPMinimum.Security.PHPFilterFunctions.RestrictedFilter - $this->container->get( Event::class )->send_batch( $events ); + if ( empty( $events ) ) { + return; + } + + $this->container->get( Event::class )->send_batch( (array) $events ); } }