$url, 'environment' => $environment ); } /** * Atomically initialise on an explicit caller action. Never silently replace * an existing identity after a clone, domain change or damaged option. */ public static function get_or_create() { $context = self::context(); if ( is_wp_error( $context ) ) { return $context; } $stored = get_option( self::OPTION, null ); if ( null === $stored ) { $id = ABTestKit_Connected_Event::new_id(); if ( is_wp_error( $id ) ) { return $id; } $candidate = array_merge( $context, array( 'schema_version' => 1, 'installation_id' => $id, 'created_at' => time() ) ); // Unique option_name selects one winner if setup requests overlap. add_option( self::OPTION, $candidate, '', 'no' ); $stored = get_option( self::OPTION, null ); } if ( ! is_array( $stored ) || 1 !== ( $stored['schema_version'] ?? null ) || ! ABTestKit_Connected_Event::valid_id( $stored['installation_id'] ?? null ) || ! is_int( $stored['created_at'] ?? null ) || $stored['created_at'] <= 0 ) { return new WP_Error( 'abtestkit_connected_identity_unavailable' ); } if ( $context['home_url'] !== ( $stored['home_url'] ?? null ) || $context['environment'] !== ( $stored['environment'] ?? null ) ) { return new WP_Error( 'abtestkit_connected_identity_mismatch' ); } return $stored; } }