Skip to content

Commit

Permalink
Keep di52 compatibility
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Camwyn committed Nov 9, 2023
1 parent c311c04 commit 9616f4b
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/Telemetry/Events/Event_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class Event_Subscriber extends Abstract_Subscriber {

private static $events = [];

/**
* @inheritDoc
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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 = [];
}

/**
Expand All @@ -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 ?: [] );
}
}

0 comments on commit 9616f4b

Please sign in to comment.