From 9616f4b4113f52a04c97db377b118d4e0f21fb2c Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Thu, 9 Nov 2023 11:04:14 -0500 Subject: [PATCH 1/6] Keep di52 compatibility This removes the use of `bind()` in a way that is not compatible with di52 containers. It also adds a safety net on `send_events()` in the case that `$events` is null --- src/Telemetry/Events/Event_Subscriber.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index 6428a76..45d97b8 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -21,6 +21,8 @@ */ class Event_Subscriber extends Abstract_Subscriber { + private static $events = []; + /** * @inheritDoc * @@ -48,17 +50,13 @@ public function register() { 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 ); + self::$events = $events; } /** @@ -69,7 +67,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 +80,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 +99,6 @@ 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 ); + $this->container->get( Event::class )->send_batch( $events ?: [] ); } } From 45d76f134bd281d48258a42b26ad5ff1a5838cdd Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Thu, 9 Nov 2023 11:17:31 -0500 Subject: [PATCH 2/6] Add var doc --- src/Telemetry/Events/Event_Subscriber.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index 45d97b8..c8babd1 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -21,6 +21,13 @@ */ class Event_Subscriber extends Abstract_Subscriber { + /** + * Holds the events in a non-persistent way. + * + * @since 2.3.1 + * + * @var array + */ private static $events = []; /** From 75ff5158d98682081c73e6d289c457a67e025d2e Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Thu, 9 Nov 2023 11:18:31 -0500 Subject: [PATCH 3/6] Update src/Telemetry/Events/Event_Subscriber.php alter the logic in send_events() Co-authored-by: Matthew Batchelder --- src/Telemetry/Events/Event_Subscriber.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index c8babd1..e09c165 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -106,6 +106,14 @@ 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; + } + + if ( ! is_array( $events ) ) { + $events = (array) $events; + } + + $this->container->get( Event::class )->send_batch( $events ); } } From 84d5a7883fd55012a5c1750a14c483250a1466b0 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Thu, 9 Nov 2023 11:23:03 -0500 Subject: [PATCH 4/6] Minimalist docblock and fix some spacing issues --- src/Telemetry/Events/Event_Subscriber.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index e09c165..1f702eb 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -22,10 +22,6 @@ class Event_Subscriber extends Abstract_Subscriber { /** - * Holds the events in a non-persistent way. - * - * @since 2.3.1 - * * @var array */ private static $events = []; @@ -107,11 +103,11 @@ public function send_events() { $events = filter_input( INPUT_POST, 'events', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); // phpcs:ignore WordPressVIPMinimum.Security.PHPFilterFunctions.RestrictedFilter if ( empty( $events ) ) { - return; + return; } - + if ( ! is_array( $events ) ) { - $events = (array) $events; + $events = (array) $events; } $this->container->get( Event::class )->send_batch( $events ); From c9a21db80062e715838145440461a8d660474274 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Thu, 9 Nov 2023 11:31:24 -0500 Subject: [PATCH 5/6] Just cast --- src/Telemetry/Events/Event_Subscriber.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index 1f702eb..d1d81e9 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -106,10 +106,6 @@ public function send_events() { return; } - if ( ! is_array( $events ) ) { - $events = (array) $events; - } - - $this->container->get( Event::class )->send_batch( $events ); + $this->container->get( Event::class )->send_batch( (array) $events ); } } From 3ef4050047bde9679afd9a857152ed996bd8f449 Mon Sep 17 00:00:00 2001 From: Matthew Batchelder Date: Thu, 9 Nov 2023 12:03:15 -0500 Subject: [PATCH 6/6] Avoid overriding the events that are already cached. --- src/Telemetry/Events/Event_Subscriber.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Telemetry/Events/Event_Subscriber.php b/src/Telemetry/Events/Event_Subscriber.php index d1d81e9..281943f 100644 --- a/src/Telemetry/Events/Event_Subscriber.php +++ b/src/Telemetry/Events/Event_Subscriber.php @@ -51,15 +51,11 @@ public function register() { * @return void */ public function cache_event( $name, $data ) { - $events = []; - self::$events[] = [ 'name' => $name, 'data' => wp_json_encode( $data ), 'stellar_slug' => Config::get_stellar_slug(), ]; - - self::$events = $events; } /**