Skip to content

Commit

Permalink
add helper function to utilize vip remote get
Browse files Browse the repository at this point in the history
  • Loading branch information
psorensen committed Aug 6, 2024
1 parent f9e56ee commit b550389
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
24 changes: 24 additions & 0 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,27 @@ function get_classification_mode(): string {

return $value;
}


/**
* Use VIP's `vip_safe_wp_remote_get` if available, otherwise use `wp_remote_get`.
*
* @param string $url URL to fetch.
* @param array $args Optional. Request arguments.
* @return WP_Error|array The response or WP_Error on failure.
*/
function safe_wp_remote_get( string $url, array $args = [] ) {
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
return vip_safe_wp_remote_get( $url, $args );
}

wp_parse_args(
$args,
[
'timeout' => 20, // phpcs:ignore

]
);

return wp_remote_get( $url, $args ); // phpcs:ignore
}
8 changes: 5 additions & 3 deletions includes/Classifai/Providers/Azure/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Classifai\Providers\Provider;
use Classifai\Features\ExcerptGeneration;
use WP_Error;
use function Classifai\safe_wp_remote_get;


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

$request = wp_remote_post(
$request = safe_wp_remote_get(
$endpoint,
[
'headers' => [
Expand Down Expand Up @@ -322,7 +324,7 @@ public function request_summary( $endpoint_url, $api_key, $post_content, $post_i
private function retrieve_summary( $url ) {
$api_key = $this->feature_instance->get_settings( static::ID )['api_key'];

$request = wp_remote_get(
$request = safe_wp_remote_get(
$url,
[
'headers' => [
Expand All @@ -342,7 +344,7 @@ private function retrieve_summary( $url ) {

while ( 'succeeded' !== $response->status ) {
sleep( .5 );
$request = wp_remote_get(
$request = safe_wp_remote_get(
$url,
[
'headers' => [
Expand Down

0 comments on commit b550389

Please sign in to comment.