'Insufficient permissions.' ) ); } $nonce = isset( $_POST['prv_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['prv_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, PRV_Settings_Page::NONCE_TEST ) ) { wp_send_json_error( array( 'message' => 'Security check failed.' ) ); } $model_id = isset( $_POST['model_id'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['model_id'] ) ) : ''; $model = PRV_Model_Registry::find_by_id( $model_id ); if ( null === $model ) { wp_send_json_error( array( 'message' => 'Model not found.' ) ); } $slug = (string) $model['slug']; $lock_key = 'prv_test_lock_' . md5( $slug ); if ( false !== get_transient( $lock_key ) ) { wp_send_json_error( array( 'message' => 'Rate limited -- wait 30 s between tests.' ) ); } set_transient( $lock_key, 1, self::RATE_LIMIT_SECONDS ); // Resolve key through the store — never bypass to a direct constant read. $api_key = PRV_Key_Store::get_key(); if ( '' === $api_key ) { wp_send_json_error( array( 'message' => 'API key not configured — set it in wp-config.php or via Settings → Provider API Key.' ) ); } $start = microtime( true ); $provider = ( 'perplexity/sonar' === $slug ) ? new PRV_Perplexity_Provider() : new PRV_OpenRouter_Provider( $slug ); try { if ( $provider instanceof PRV_OpenRouter_Provider ) { $provider->probe_with_key( 'what is BPC-157', $api_key ); } else { $provider->probe( 'what is BPC-157' ); } $ms = (int) round( ( microtime( true ) - $start ) * 1000 ); wp_send_json_success( array( 'resolved' => true, 'latency_ms' => $ms, 'slug' => $slug, 'message' => sprintf( 'Resolved · %d ms', $ms ), ) ); } catch ( \Exception $e ) { wp_send_json_error( array( 'resolved' => false, 'slug' => $slug, 'message' => 'Not resolved -- provider returned error (slug retired?)', ) ); } } }