| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse.ly Suggestions API Endpoint: Check Auth |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.19.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Services\Suggestions_API\Endpoints; |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
use Parsely\Services\Base_Service_Endpoint; |
| 15 |
|
| 16 |
/** |
| 17 |
* The endpoint for the /check-auth API request. |
| 18 |
* |
| 19 |
* @since 3.19.0 |
| 20 |
* |
| 21 |
* @phpstan-import-type WP_HTTP_Request_Args from Base_Service_Endpoint |
| 22 |
* @phpstan-import-type WP_HTTP_Response from Base_Service_Endpoint |
| 23 |
*/ |
| 24 |
class Endpoint_Check_Auth extends Suggestions_API_Base_Endpoint { |
| 25 |
/** |
| 26 |
* Returns the endpoint for the API request. |
| 27 |
* |
| 28 |
* @since 3.19.0 |
| 29 |
* |
| 30 |
* @return string The endpoint for the API request. |
| 31 |
*/ |
| 32 |
public function get_endpoint(): string { |
| 33 |
return '/check-auth'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns whether the Site ID has the authorization to use the Suggestions |
| 38 |
* API or Suggestions API feature. |
| 39 |
* |
| 40 |
* @since 3.19.0 |
| 41 |
* |
| 42 |
* @param array<mixed> $options The options to pass to the API request. |
| 43 |
* @return array<mixed>|WP_Error The response from the remote API, or a WP_Error |
| 44 |
* object if the response is an error. |
| 45 |
*/ |
| 46 |
public function get_check_auth_result( array $options ) { |
| 47 |
/** @var array<mixed>|WP_Error $response */ |
| 48 |
$response = $this->request( 'GET', $options ); |
| 49 |
|
| 50 |
return $response; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the request options for the remote API request. |
| 55 |
* |
| 56 |
* @since 3.24.3 |
| 57 |
* |
| 58 |
* @param string $method The HTTP method to use for the request. |
| 59 |
* @return WP_HTTP_Request_Args The request options for the remote API request. |
| 60 |
*/ |
| 61 |
protected function get_request_options( string $method ): array { |
| 62 |
$options = parent::get_request_options( $method ); |
| 63 |
|
| 64 |
// Set the body to an empty array, as leaving it to '{}' causes errors. |
| 65 |
$options['body'] = array(); |
| 66 |
|
| 67 |
return $options; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Processes the response from the remote API. |
| 72 |
* |
| 73 |
* @since 3.19.0 |
| 74 |
* |
| 75 |
* @param WP_HTTP_Response|WP_Error $response The response from the remote API. |
| 76 |
* @return array<mixed>|WP_Error The processed response. |
| 77 |
*/ |
| 78 |
protected function process_response( $response ) { |
| 79 |
if ( is_wp_error( $response ) ) { |
| 80 |
/** @var WP_Error $response */ |
| 81 |
return $response; |
| 82 |
} |
| 83 |
|
| 84 |
return $response['response']; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Executes the API request. |
| 89 |
* |
| 90 |
* @since 3.19.0 |
| 91 |
* |
| 92 |
* @param array<mixed> $args The arguments to pass to the API request. |
| 93 |
* @return WP_Error|array<mixed> The response from the API. |
| 94 |
*/ |
| 95 |
public function call( array $args = array() ) { |
| 96 |
return $this->get_check_auth_result( $args ); |
| 97 |
} |
| 98 |
} |
| 99 |
|