Skip to content

Commit

Permalink
Merge pull request #102 from stellarwp/fix/di52-compatible-events
Browse files Browse the repository at this point in the history
Keep di52 compatibility
  • Loading branch information
Camwyn authored Nov 9, 2023
2 parents c311c04 + 3ef4050 commit 79f7a04
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Telemetry/Events/Event_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
class Event_Subscriber extends Abstract_Subscriber {

/**
* @var array
*/
private static $events = [];

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

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

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

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

0 comments on commit 79f7a04

Please sign in to comment.