128 ) { return array( 'ok' => false, 'error' => 'invalid_token_format' ); } if ( $api_url !== null && $api_url !== '' ) { $this->plugin->settings()->set( 'api_url', rtrim( $api_url, '/' ) ); } $res = $this->plugin->client()->post_unsigned( '/v1/agent/pair', array( 'pairing_token' => $token, 'site_url' => home_url( '/' ), 'agent_version' => OPERANSE_AGENT_VERSION, 'wp_version' => get_bloginfo( 'version' ), 'php_version' => PHP_VERSION, 'inbound_capable' => $this->plugin->settings()->connection_mode() === Config\Settings::MODE_DIRECT_INBOUND, ) ); if ( ! $res['ok'] || ! is_array( $res['body'] ) ) { return array( 'ok' => false, 'error' => (string) ( $res['error'] ?? 'pairing_failed' ) ); } $b = $res['body']; if ( ! isset( $b['site_id'], $b['environment_id'], $b['key_id'], $b['secret'] ) ) { return array( 'ok' => false, 'error' => 'malformed_response' ); } // Nothing recorded before an administrator paired is sent after (ADR-072 ยง8, Q1): the spool starts empty. \Operanse\Agent\Telemetry\Spool\SpoolDrain::discard( $this->plugin->spool() ); $this->plugin->credentials()->store( (string) $b['site_id'], (string) $b['environment_id'], (string) $b['key_id'], (string) $b['secret'] ); if ( isset( $b['api_base_url'] ) && is_string( $b['api_base_url'] ) && $b['api_base_url'] !== '' ) { $this->plugin->settings()->set( 'api_url', rtrim( $b['api_base_url'], '/' ) ); } if ( isset( $b['config'] ) && is_array( $b['config'] ) ) { $this->plugin->settings()->apply_remote_config( $b['config'] ); } ( new Scheduler( $this->plugin->settings() ) )->schedule_all(); wp_schedule_single_event( time() + 5, Scheduler::HOOK_DISCOVER, array( 'pairing' ) ); return array( 'ok' => true, 'error' => null ); } }