Skip to content

Commit

Permalink
Merge pull request #375 from humanmade/backport-374-to-v10-branch
Browse files Browse the repository at this point in the history
[Backport v10-branch] Ensure segment init has run
  • Loading branch information
roborourke authored Jan 19, 2022
2 parents 62d921d + 4f16713 commit 452d1d8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions inc/telemetry/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ function bootstrap() {

/**
* Initialize segment.io.
*
* @return bool True if Segment has initialised successfully.
*/
function initialize() {
function initialize() : bool {
static $initialized;
if ( $initialized ) {
return;
if ( is_bool( $initialized ) ) {
return $initialized;
}

if ( ! is_user_logged_in() ) {
return;
$initialized = false;
return $initialized;
}

// Connect.
Expand Down Expand Up @@ -81,6 +84,7 @@ function initialize() {
}

$initialized = true;
return $initialized;
}

/**
Expand All @@ -90,7 +94,9 @@ function initialize() {
* @return void
*/
function track( array $message ) {
initialize();
if ( ! initialize() ) {
return;
}

// Add user ID if missing.
if ( ! isset( $message['userId'] ) ) {
Expand Down

0 comments on commit 452d1d8

Please sign in to comment.