| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for Validate API (`/validate`) |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.11.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\RemoteAPI; |
| 12 |
|
| 13 |
use Parsely\Endpoints\Base_Endpoint; |
| 14 |
use Parsely\Parsely; |
| 15 |
use WP_Error; |
| 16 |
|
| 17 |
use function Parsely\Utils\convert_to_associative_array; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class for credentials validation API (`/validate`). |
| 21 |
* |
| 22 |
* @since 3.11.0 |
| 23 |
* |
| 24 |
* @phpstan-import-type WP_HTTP_Request_Args from Parsely |
| 25 |
*/ |
| 26 |
class Validate_API extends Base_Endpoint_Remote { |
| 27 |
protected const API_BASE_URL = Parsely::PUBLIC_API_BASE_URL; |
| 28 |
protected const ENDPOINT = '/validate/secret'; |
| 29 |
protected const QUERY_FILTER = 'wp_parsely_validate_secret_endpoint_args'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns whether the endpoint is available for access by the current |
| 33 |
* user. |
| 34 |
* |
| 35 |
* @since 3.14.0 |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public function is_available_to_current_user(): bool { |
| 40 |
return current_user_can( |
| 41 |
// phpcs:ignore WordPress.WP.Capabilities.Undetermined |
| 42 |
$this->apply_capability_filters( |
| 43 |
Base_Endpoint::DEFAULT_ACCESS_CAPABILITY |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Gets the URL for the Parse.ly API credentials validation endpoint. |
| 50 |
* |
| 51 |
* @since 3.11.0 |
| 52 |
* |
| 53 |
* @param array<string, mixed> $query The query arguments to send to the remote API. |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function get_api_url( array $query ): string { |
| 57 |
$query = array( |
| 58 |
'apikey' => $query['apikey'], |
| 59 |
'secret' => $query['secret'], |
| 60 |
); |
| 61 |
|
| 62 |
return add_query_arg( $query, static::API_BASE_URL . static::ENDPOINT ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Queries the Parse.ly API credentials validation endpoint. |
| 67 |
* The API will return a 200 response if the credentials are valid and a 401 response if they are not. |
| 68 |
* |
| 69 |
* @param array<string, mixed> $query The query arguments to send to the remote API. |
| 70 |
* @return object|WP_Error The response from the remote API, or a WP_Error object if the response is an error. |
| 71 |
*/ |
| 72 |
private function api_validate_credentials( array $query ) { |
| 73 |
/** |
| 74 |
* GET request options. |
| 75 |
* |
| 76 |
* @var WP_HTTP_Request_Args $options |
| 77 |
*/ |
| 78 |
$options = $this->get_request_options(); |
| 79 |
$response = wp_safe_remote_get( $this->get_api_url( $query ), $options ); |
| 80 |
|
| 81 |
if ( is_wp_error( $response ) ) { |
| 82 |
return $response; |
| 83 |
} |
| 84 |
|
| 85 |
$body = wp_remote_retrieve_body( $response ); |
| 86 |
$decoded = json_decode( $body ); |
| 87 |
|
| 88 |
if ( ! is_object( $decoded ) ) { |
| 89 |
return new WP_Error( |
| 90 |
400, |
| 91 |
__( |
| 92 |
'Unable to decode upstream API response', |
| 93 |
'wp-parsely' |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! property_exists( $decoded, 'success' ) || false === $decoded->success ) { |
| 99 |
return new WP_Error( |
| 100 |
$decoded->code ?? 400, |
| 101 |
$decoded->message ?? __( 'Unable to read data from upstream API', 'wp-parsely' ) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return $decoded; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the response from the Parse.ly API credentials validation endpoint. |
| 110 |
* |
| 111 |
* @since 3.11.0 |
| 112 |
* |
| 113 |
* @param array<string, mixed> $query The query arguments to send to the remote API. |
| 114 |
* @param bool $associative (optional) When TRUE, returned objects will be converted into |
| 115 |
* associative arrays. |
| 116 |
* @return array<string, mixed>|object|WP_Error |
| 117 |
*/ |
| 118 |
public function get_items( array $query, bool $associative = false ) { |
| 119 |
$api_request = $this->api_validate_credentials( $query ); |
| 120 |
return $associative ? convert_to_associative_array( $api_request ) : $api_request; |
| 121 |
} |
| 122 |
} |
| 123 |
|