|null */ function abtestkit_connected_deactivation_prepare() : ?array { if ( ! abtestkit_connected_platform_can_create_or_launch() ) { return null; } $connection = abtestkit_hosted_get_connection(); if ( ! is_array( $connection ) ) { return null; } $site_ref = (string) $connection['site_ref']; $stored = get_option( ABTESTKIT_CONNECTED_DEACTIVATION_OPTION, null ); if ( is_array( $stored ) && is_string( $stored['site_ref'] ?? null ) && hash_equals( $site_ref, $stored['site_ref'] ) && is_array( $stored['event'] ?? null ) && ! is_wp_error( ABTestKit_Connected_Event::validate( $stored['event'], time() ) ) ) { return $stored; } $installation = ABTestKit_Connected_Installation::get_or_create(); $event_id = ABTestKit_Connected_Event::new_id(); if ( is_wp_error( $installation ) || is_wp_error( $event_id ) ) { return null; } $event = array( 'schema_version' => 1, 'event_id' => $event_id, 'installation_id' => (string) $installation['installation_id'], 'event_name' => 'plugin_deactivated', 'occurred_at' => time(), 'plugin_version' => abtestkit_hosted_sync_plugin_version(), 'test_ref' => null, 'payload' => array( 'evidence' => 'observed' ), ); if ( is_wp_error( ABTestKit_Connected_Event::validate( $event, time() ) ) ) { return null; } $marker = array( 'event' => $event, 'site_ref' => $site_ref, 'attempted_at' => 0, 'accepted' => false, ); update_option( ABTESTKIT_CONNECTED_DEACTIVATION_OPTION, $marker, false ); $stored = get_option( ABTESTKIT_CONNECTED_DEACTIVATION_OPTION, null ); return is_array( $stored ) ? $stored : null; } /** * Send the minimal event directly because cron stops when the plugin becomes * inactive. A failed or ambiguous attempt is queued for later reactivation. */ function abtestkit_connected_deactivation_send() : bool { $connection = abtestkit_hosted_get_connection(); $marker = abtestkit_connected_deactivation_prepare(); if ( ! is_array( $connection ) || ! is_array( $marker ) || ! is_array( $marker['event'] ?? null ) ) { return false; } $event = $marker['event']; $installation = ABTestKit_Connected_Installation::get_or_create(); if ( is_wp_error( $installation ) || is_wp_error( ABTestKit_Connected_Event::validate( $event, time() ) ) || ! is_string( $marker['site_ref'] ?? null ) || ! hash_equals( (string) $connection['site_ref'], $marker['site_ref'] ) || ! hash_equals( (string) $installation['installation_id'], (string) ( $event['installation_id'] ?? '' ) ) ) { return false; } if ( ! empty( $marker['accepted'] ) ) { return true; } $marker['attempted_at'] = time(); update_option( ABTESTKIT_CONNECTED_DEACTIVATION_OPTION, $marker, false ); $response = abtestkit_hosted_service_request( 'POST', '/sites/' . (string) $connection['site_ref'] . '/events', array( 'schema_version' => 1, 'installation' => array( 'installation_id' => (string) $installation['installation_id'], 'home_url' => (string) $installation['home_url'], 'environment' => (string) $installation['environment'], 'created_at' => (int) $installation['created_at'], ), 'events' => array( $event ), ), (string) $connection['credential'], 2 ); if ( is_wp_error( $response ) || ! in_array( (int) $response['status'], array( 200, 202 ), true ) ) { if ( 1 === (int) get_option( ABTestKit_Connected_Outbox::SCHEMA_OPTION, 0 ) ) { ABTestKit_Connected_Outbox::enqueue( $event, time() ); } return false; } $data = $response['data']; $accepted = is_int( $data['accepted'] ?? null ) ? $data['accepted'] : -1; $duplicates = is_int( $data['duplicates'] ?? null ) ? $data['duplicates'] : -1; if ( 1 !== $accepted + $duplicates ) { if ( 1 === (int) get_option( ABTestKit_Connected_Outbox::SCHEMA_OPTION, 0 ) ) { ABTestKit_Connected_Outbox::enqueue( $event, time() ); } return false; } $marker['accepted'] = true; update_option( ABTESTKIT_CONNECTED_DEACTIVATION_OPTION, $marker, false ); return true; }