Skip to content

Commit

Permalink
Merge pull request #74 from stellarwp/fix/status-get-logic
Browse files Browse the repository at this point in the history
Correct the logic in Status->get()
  • Loading branch information
Camwyn authored Jun 1, 2023
2 parents c38f38b + 5246247 commit cccdf75
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/Telemetry/Opt_In/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,37 @@ public function get_option() {
* 3 = Mixed
*
* @since 1.0.0
* @since 2.0.1 Correct logic so it is not subject to the order of the plugins.
*
* @return integer The status value.
*/
public function get() {
$status = self::STATUS_ACTIVE;
$option = $this->get_option();

// If the status option is not an option, default to inactive.
if ( ! isset( $option['plugins'] ) ) {
return self::STATUS_INACTIVE;
}

foreach ( $option['plugins'] as $plugin ) {
$status = array_reduce(
$option['plugins'],
function( $carry, $item ) {
// First run, ignore the default STATUS_ACTIVE.
if ( empty( $carry ) ) {
return (int) $item['optin'];
}

// If a plugin's status is false, we set the status as inactive.
if ( false === (bool) $plugin['optin'] ) {
$status = self::STATUS_INACTIVE;
continue;
}
// As long as they are the same, we keep returning the same.
if ( $carry === $item['optin'] ) {
return (int) $item['optin'];
}

// If another plugin's status is true and the status is already inactive, we set the status as mixed.
if ( true === $plugin['optin'] && self::STATUS_INACTIVE === $status ) {
$status = self::STATUS_MIXED;
break;
return self::STATUS_MIXED;
}
);

if ( 0 === $status ) {
$status = self::STATUS_INACTIVE;
}

/**
Expand Down

0 comments on commit cccdf75

Please sign in to comment.