| 1 |
<?php |
| 2 |
/** |
| 3 |
* Explicit product consent state. |
| 4 |
* |
| 5 |
* @package signls-sdk |
| 6 |
* @license GPL-3.0-or-later |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Signls\Sdk\V1; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
final class Consent { |
| 14 |
|
| 15 |
const VERSION = 1; |
| 16 |
|
| 17 |
private $state; |
| 18 |
|
| 19 |
public function __construct( StateStore $state ) { |
| 20 |
$this->state = $state; |
| 21 |
} |
| 22 |
|
| 23 |
public function status(): string { |
| 24 |
$status = (string) $this->state->get( 'consent_status', 'unset' ); |
| 25 |
return in_array( $status, array( 'unset', 'enabled', 'disabled' ), true ) ? $status : 'unset'; |
| 26 |
} |
| 27 |
|
| 28 |
public function enabled(): bool { |
| 29 |
return 'enabled' === $this->status(); |
| 30 |
} |
| 31 |
|
| 32 |
public function enable( string $source, int $notice_version ): bool { |
| 33 |
$now = time(); |
| 34 |
$first_enabled = (int) $this->state->get( 'consent_first_enabled_at', 0 ); |
| 35 |
if ( $first_enabled <= 0 ) { |
| 36 |
$first_enabled = $now; |
| 37 |
} |
| 38 |
|
| 39 |
return $this->state->set_many( |
| 40 |
array( |
| 41 |
'consent_status' => 'enabled', |
| 42 |
'consent_version' => self::VERSION, |
| 43 |
'consent_first_enabled_at' => $first_enabled, |
| 44 |
'consent_last_changed_at' => $now, |
| 45 |
'consent_source' => Sanitizer::slug( $source, 48, 'unknown' ), |
| 46 |
'consent_notice_version' => max( 1, $notice_version ), |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function disable( string $source, int $notice_version ): bool { |
| 52 |
return $this->state->set_many( |
| 53 |
array( |
| 54 |
'consent_status' => 'disabled', |
| 55 |
'consent_version' => self::VERSION, |
| 56 |
'consent_last_changed_at' => time(), |
| 57 |
'consent_source' => Sanitizer::slug( $source, 48, 'unknown' ), |
| 58 |
'consent_notice_version' => max( 1, $notice_version ), |
| 59 |
) |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|