| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse.ly Content API Endpoint: Validate |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.17.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Services\Content_API\Endpoints; |
| 12 |
|
| 13 |
use Parsely\Services\Base_Service_Endpoint; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* The endpoint for credentials validation. |
| 18 |
* |
| 19 |
* @since 3.17.0 |
| 20 |
* |
| 21 |
* @phpstan-import-type WP_HTTP_Response from Base_Service_Endpoint |
| 22 |
*/ |
| 23 |
class Endpoint_Validate extends Content_API_Base_Endpoint { |
| 24 |
/** |
| 25 |
* Returns the endpoint for the API request. |
| 26 |
* |
| 27 |
* @since 3.17.0 |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public function get_endpoint(): string { |
| 32 |
return '/validate/secret'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns the query arguments for the API request. |
| 37 |
* |
| 38 |
* We want to validate the API key and secret, so we don't need to send any |
| 39 |
* query arguments. |
| 40 |
* |
| 41 |
* @since 3.17.0 |
| 42 |
* |
| 43 |
* @param array<mixed> $args The query arguments to send to the remote API. |
| 44 |
* @return array<mixed> The query arguments for the API request. |
| 45 |
*/ |
| 46 |
public function get_query_args( array $args = array() ): array { |
| 47 |
return $args; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Queries the Parse.ly API credentials validation endpoint. |
| 52 |
* |
| 53 |
* The API will return a 200 response if the credentials are valid and a 403 |
| 54 |
* response if they are not. |
| 55 |
* |
| 56 |
* @since 3.17.0 |
| 57 |
* |
| 58 |
* @param string $api_key The API key to validate. |
| 59 |
* @param string $secret_key The secret key to validate. |
| 60 |
* @return array<mixed>|WP_Error The response from the remote API, or a WP_Error object if the response is an error. |
| 61 |
*/ |
| 62 |
private function api_validate_credentials( string $api_key, string $secret_key ) { |
| 63 |
$query = array( |
| 64 |
'apikey' => $api_key, |
| 65 |
'secret' => $secret_key, |
| 66 |
); |
| 67 |
|
| 68 |
$response = $this->request( 'GET', $query ); |
| 69 |
|
| 70 |
if ( is_wp_error( $response ) ) { |
| 71 |
return $response; |
| 72 |
} |
| 73 |
|
| 74 |
if ( false === $response['success'] ) { |
| 75 |
// Built from a successful response, so request() didn't strip it. |
| 76 |
return new WP_Error( |
| 77 |
$response['code'] ?? 403, |
| 78 |
$this->strip_credentials( |
| 79 |
(string) ( $response['message'] ?? |
| 80 |
__( 'Unable to validate the API credentials', 'wp-parsely' ) ) |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
return $response; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Processes the response from the remote API. |
| 90 |
* |
| 91 |
* @since 3.17.0 |
| 92 |
* |
| 93 |
* @param WP_HTTP_Response|WP_Error $response The response from the remote API. |
| 94 |
* @return array<mixed>|WP_Error The processed response. |
| 95 |
*/ |
| 96 |
protected function process_response( $response ) { |
| 97 |
return Base_Service_Endpoint::process_response( $response ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Executes the API request. |
| 102 |
* |
| 103 |
* @since 3.17.0 |
| 104 |
* |
| 105 |
* @param array<mixed> $args The arguments to pass to the API request. |
| 106 |
* @return WP_Error|array<mixed> The response from the API request. |
| 107 |
*/ |
| 108 |
public function call( array $args = array() ) { |
| 109 |
/** @var string $api_key */ |
| 110 |
$api_key = $args['apikey']; |
| 111 |
/** @var string $secret */ |
| 112 |
$secret = $args['secret']; |
| 113 |
|
| 114 |
return $this->api_validate_credentials( $api_key, $secret ); |
| 115 |
} |
| 116 |
} |
| 117 |
|