| 1 |
<?php |
| 2 |
/** |
| 3 |
* Anonymous analytics identity for the wp-admin landing page. |
| 4 |
* |
| 5 |
* Owns the `wcpos_anon_id` option: a random v4 UUID that keys experiment |
| 6 |
* assignment for sites that have NOT consented to profile tracking. It is |
| 7 |
* never derived from the URL or site data (landing-experiments spec §5.1). |
| 8 |
* |
| 9 |
* @package WCPOS\WooCommercePOS\Services |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WCPOS\WooCommercePOS\Services; |
| 13 |
|
| 14 |
/** |
| 15 |
* Anon_ID service. |
| 16 |
*/ |
| 17 |
class Anon_ID { |
| 18 |
/** |
| 19 |
* Option name. Program-wide identifier — the wcpos.com reconciler and the |
| 20 |
* landing bootstrap both refer to it by this name; do not rename. |
| 21 |
*/ |
| 22 |
const OPTION = 'wcpos_anon_id'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns the stored anon id, creating one on first use. |
| 26 |
* |
| 27 |
* @return string v4 UUID. |
| 28 |
*/ |
| 29 |
public function get(): string { |
| 30 |
$id = get_option( self::OPTION ); |
| 31 |
|
| 32 |
if ( ! \is_string( $id ) || '' === $id ) { |
| 33 |
$id = wp_generate_uuid4(); |
| 34 |
// Autoload off: only the landing page (and WP-CLI) ever reads it. |
| 35 |
if ( ! add_option( self::OPTION, $id, '', false ) ) { |
| 36 |
// Lost a first-load race — return the persisted winner, never a stray id. |
| 37 |
$id = (string) get_option( self::OPTION ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return $id; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Replaces the stored id with a fresh UUID (privacy affordance). |
| 46 |
* |
| 47 |
* @return string The new v4 UUID. |
| 48 |
*/ |
| 49 |
public function rotate(): string { |
| 50 |
$id = wp_generate_uuid4(); |
| 51 |
update_option( self::OPTION, $id, false ); |
| 52 |
|
| 53 |
return $id; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Deletes the stored id (privacy affordance; also run on uninstall). |
| 58 |
*/ |
| 59 |
public function delete(): void { |
| 60 |
delete_option( self::OPTION ); |
| 61 |
} |
| 62 |
} |
| 63 |
|