15, 'headers' => [ 'Content-Type' => 'application/json', 'x-api-key' => $api_key, ], 'body' => wp_json_encode( [ 'domain' => home_url() ] ), ] ); if ( is_wp_error( $response ) ) { return new WP_Error( 'request_failed', sprintf( /* translators: %s: error message */ __( 'Could not reach Agentyme platform: %s', 'agentyme' ), $response->get_error_message() ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); // The API returns { valid: bool, reason?: string } in the body — trust that // over the HTTP status code, which may not always reflect the actual outcome. if ( is_array( $body ) && array_key_exists( 'valid', $body ) ) { if ( true === $body['valid'] ) { return true; } return new WP_Error( 'invalid_key', ! empty( $body['reason'] ) ? $body['reason'] : __( 'API key validation failed.', 'agentyme' ) ); } // Fallback for unexpected responses with no parseable body. $status = wp_remote_retrieve_response_code( $response ); return new WP_Error( 'unexpected_response', sprintf( /* translators: %d: HTTP status code */ __( 'Unexpected response from Agentyme platform (HTTP %d).', 'agentyme' ), $status ) ); } }