Skip to content

Commit e5f564c

Browse files
authored
Merge pull request #1022 from 10up/fix/vip-timeouts
Fix timeouts that can happen for long running tasks on a WP VIP environment
2 parents ade0090 + 8125fed commit e5f564c

File tree

14 files changed

+35
-17
lines changed

14 files changed

+35
-17
lines changed

includes/Classifai/Helpers.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,13 @@ function is_local_path( string $resource_ref ): bool {
832832
/**
833833
* Safe generic HTTP wrapper compatible with WP VIP.
834834
*
835-
* - Use vip_safe_wp_remote_request() when available.
836-
* - Fall back to wp_remote_request() on other scenarios.
837-
* - Respect all call args (timeout, headers, method, etc).
835+
* By default this function uses the WordPress HTTP API for requests,
836+
* to use vip_safe_wp_remote_request() (if available) specify
837+
* `$args['use_vip'] = true`.
838+
*
839+
* As a general guide, prompt requests should use the WordPress HTTP API;
840+
* administrative requests (API authentication, scope requests) should use the
841+
* WordPress VIP API if available.
838842
*
839843
* @since 3.6.0
840844
*
@@ -846,6 +850,9 @@ function is_local_path( string $resource_ref ): bool {
846850
function safe_wp_remote_request( string $method, string $url, array $args = [] ) {
847851
$method = strtoupper( $method );
848852
$args['method'] = $method;
853+
$use_vip = $args['use_vip'] ?? false;
854+
855+
unset( $args['use_vip'] );
849856

850857
// Respect timeout if caller set.
851858
$timeout = isset( $args['timeout'] ) ? (int) $args['timeout'] : 20;
@@ -860,7 +867,8 @@ function safe_wp_remote_request( string $method, string $url, array $args = [] )
860867
$args['headers']['User-Agent'] = $cached_user_agent;
861868
}
862869

863-
if ( function_exists( 'vip_safe_wp_remote_request' ) ) {
870+
// Use VIP-safe wrapper if available and wanted. Some requests need a longer timeout than VIP allows.
871+
if ( function_exists( 'vip_safe_wp_remote_request' ) && (bool) $use_vip ) {
864872
$fallback = '';
865873
$threshold = 3;
866874
$retry = 20;
@@ -925,9 +933,10 @@ function safe_file_get_contents( string $file_path, array $args = [] ) {
925933
/**
926934
* Safe GET wrapper compatible with WP VIP.
927935
*
928-
* - Use vip_safe_wp_remote_get() when available.
929-
* - Fall back to wp_remote_get() on other scenarios.
930-
* - Respect all call args (timeout, headers, etc).
936+
* - By default will use the WordPress HTTP API for requests.
937+
* - If `$args['use_vip'] = true` and vip_safe_wp_remote_get() is available,
938+
* then vip_safe_wp_remote_get() will be used instead.
939+
* - Respects all call args (timeout, headers, etc).
931940
*
932941
* @since 3.6.0
933942
*
@@ -942,9 +951,10 @@ function safe_wp_remote_get( string $url, array $args = [] ) {
942951
/**
943952
* Safe POST wrapper compatible with WP VIP.
944953
*
945-
* - Use vip_safe_wp_remote_post() when available.
946-
* - Fall back to wp_remote_post() on other scenarios.
947-
* - Respect all call args (timeout, headers, etc).
954+
* - By default will use the WordPress HTTP API for requests.
955+
* - If `$args['use_vip'] = true` and vip_safe_wp_remote_post() is available,
956+
* then vip_safe_wp_remote_post() will be used instead.
957+
* - Respects all call args (timeout, headers, etc).
948958
*
949959
* @since 3.6.0
950960
*

includes/Classifai/Providers/Azure/ComputerVision.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ protected function authenticate_credentials( string $url, string $api_key ) {
763763
'Content-Type' => 'application/json',
764764
],
765765
'body' => '{"url":"https://classifaiplugin.com/wp-content/themes/fse-classifai-theme/assets/img/header.png"}',
766+
'use_vip' => true,
766767
]
767768
);
768769

includes/Classifai/Providers/Azure/Embeddings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ protected function authenticate_credentials( string $url, string $api_key, strin
296296
'dimensions' => $this->get_dimensions(),
297297
]
298298
),
299+
'use_vip' => true,
299300
]
300301
);
301302

includes/Classifai/Providers/Azure/OpenAI.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ protected function authenticate_credentials( string $url, string $api_key, strin
314314
'max_tokens' => 5,
315315
]
316316
),
317+
'use_vip' => true,
317318
]
318319
);
319320

includes/Classifai/Providers/Azure/Speech.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function connect_to_service( array $args = array() ): array {
207207
'Ocp-Apim-Subscription-Key' => $default['api_key'],
208208
'Content-Type' => 'application/json',
209209
),
210+
'use_vip' => true,
210211
);
211212

212213
// Create request URL.

includes/Classifai/Providers/ElevenLabs/ElevenLabs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected function get_models( string $api_key = '' ) {
219219
return new WP_Error( 'auth', esc_html__( 'Please enter your ElevenLabs API key.', 'classifai' ) );
220220
}
221221

222-
$response = $this->request( $this->get_api_url( $this->model_path ), $api_key, 'get' );
222+
$response = $this->request( $this->get_api_url( $this->model_path ), $api_key, 'get', [ 'use_vip' => true ] );
223223

224224
if ( is_wp_error( $response ) ) {
225225
return $response;
@@ -260,7 +260,7 @@ protected function get_voices( string $api_key = '' ) {
260260
return new WP_Error( 'auth', esc_html__( 'Please enter your ElevenLabs API key.', 'classifai' ) );
261261
}
262262

263-
$response = $this->request( $this->get_api_url( 'voices?per_page=100' ), $api_key, 'get' );
263+
$response = $this->request( $this->get_api_url( 'voices?per_page=100' ), $api_key, 'get', [ 'use_vip' => true ] );
264264

265265
if ( is_wp_error( $response ) ) {
266266
return $response;

includes/Classifai/Providers/GoogleAI/GoogleAI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function get_models( string $api_key = '' ) {
6565

6666
// Make request to ensure credentials work.
6767
$request = new APIRequest( $api_key );
68-
$response = $request->get( $this->model_url );
68+
$response = $request->get( $this->model_url, [ 'use_vip' => true ] );
6969

7070
if ( is_wp_error( $response ) ) {
7171
return $response;

includes/Classifai/Providers/Localhost/Ollama.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function get_models( array $args = [] ): array {
149149
$this->get_api_model_url( $default['endpoint_url'] ),
150150
[
151151
'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
152+
'use_vip' => true,
152153
]
153154
);
154155

includes/Classifai/Providers/Localhost/StableDiffusion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public function get_models( array $args = [] ): array {
229229
$this->get_api_model_url( $default['endpoint_url'] ),
230230
[
231231
'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
232+
'use_vip' => true,
232233
]
233234
);
234235

includes/Classifai/Providers/OpenAI/OpenAI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function authenticate_credentials( string $api_key = '' ) {
7272

7373
// Make request to ensure credentials work.
7474
$request = new APIRequest( $api_key );
75-
$response = $request->get( $this->model_url );
75+
$response = $request->get( $this->model_url, [ 'use_vip' => true ] );
7676

7777
return ! is_wp_error( $response ) ? true : $response;
7878
}

0 commit comments

Comments
 (0)