Skip to content

Commit b550389

Browse files
committed
add helper function to utilize vip remote get
1 parent f9e56ee commit b550389

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

includes/Classifai/Helpers.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,27 @@ function get_classification_mode(): string {
661661

662662
return $value;
663663
}
664+
665+
666+
/**
667+
* Use VIP's `vip_safe_wp_remote_get` if available, otherwise use `wp_remote_get`.
668+
*
669+
* @param string $url URL to fetch.
670+
* @param array $args Optional. Request arguments.
671+
* @return WP_Error|array The response or WP_Error on failure.
672+
*/
673+
function safe_wp_remote_get( string $url, array $args = [] ) {
674+
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
675+
return vip_safe_wp_remote_get( $url, $args );
676+
}
677+
678+
wp_parse_args(
679+
$args,
680+
[
681+
'timeout' => 20, // phpcs:ignore
682+
683+
]
684+
);
685+
686+
return wp_remote_get( $url, $args ); // phpcs:ignore
687+
}

includes/Classifai/Providers/Azure/Language.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Classifai\Providers\Provider;
1111
use Classifai\Features\ExcerptGeneration;
1212
use WP_Error;
13+
use function Classifai\safe_wp_remote_get;
14+
1315

1416
class Language extends Provider {
1517
/**
@@ -152,7 +154,7 @@ protected function authenticate_credentials( string $url, string $api_key ) {
152154
$endpoint = trailingslashit( $url ) . '/text/analytics/v3.1/languages';
153155
$endpoint = add_query_arg( 'api-version', static::API_VERSION, $endpoint );
154156

155-
$request = wp_remote_post(
157+
$request = safe_wp_remote_get(
156158
$endpoint,
157159
[
158160
'headers' => [
@@ -322,7 +324,7 @@ public function request_summary( $endpoint_url, $api_key, $post_content, $post_i
322324
private function retrieve_summary( $url ) {
323325
$api_key = $this->feature_instance->get_settings( static::ID )['api_key'];
324326

325-
$request = wp_remote_get(
327+
$request = safe_wp_remote_get(
326328
$url,
327329
[
328330
'headers' => [
@@ -342,7 +344,7 @@ private function retrieve_summary( $url ) {
342344

343345
while ( 'succeeded' !== $response->status ) {
344346
sleep( .5 );
345-
$request = wp_remote_get(
347+
$request = safe_wp_remote_get(
346348
$url,
347349
[
348350
'headers' => [

0 commit comments

Comments
 (0)