PluginProbe
Parse.ly / 3.13.2
Parse.ly v3.13.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / RemoteAPI / class-validate-api.php

class-validate-api.php in Parse.ly 3.13.2, at src/RemoteAPI/class-validate-api.php

114 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Parsely;
14 use WP_Error;
15
16 use function Parsely\Utils\convert_to_associative_array;
17
18 /**
19 * Class for credentials validation API (`/validate`).
20 *
21 * @since 3.11.0
22 *
23 * @phpstan-import-type WP_HTTP_Request_Args from Parsely
24 */
25 class Validate_API extends Base_Endpoint_Remote {
26 protected const API_BASE_URL = Parsely::PUBLIC_API_BASE_URL;
27 protected const ENDPOINT = '/validate/secret';
28 protected const QUERY_FILTER = 'wp_parsely_validate_secret_endpoint_args';
29
30 /**
31 * Indicates whether the endpoint is public or protected behind permissions.
32 *
33 * @since 3.11.0
34 *
35 * @var bool
36 */
37 protected $is_public_endpoint = false;
38
39 /**
40 * Gets the URL for the Parse.ly API credentials validation endpoint.
41 *
42 * @since 3.11.0
43 *
44 * @param array<string, mixed> $query The query arguments to send to the remote API.
45 * @return string
46 */
47 public function get_api_url( array $query ): string {
48 $query = array(
49 'apikey' => $query['apikey'],
50 'secret' => $query['secret'],
51 );
52
53 return add_query_arg( $query, static::API_BASE_URL . static::ENDPOINT );
54 }
55
56 /**
57 * Queries the Parse.ly API credentials validation endpoint.
58 * The API will return a 200 response if the credentials are valid and a 401 response if they are not.
59 *
60 * @param array<string, mixed> $query The query arguments to send to the remote API.
61 * @return object|WP_Error The response from the remote API, or a WP_Error object if the response is an error.
62 */
63 private function api_validate_credentials( array $query ) {
64 /**
65 * GET request options.
66 *
67 * @var WP_HTTP_Request_Args $options
68 */
69 $options = $this->get_request_options();
70 $response = wp_safe_remote_get( $this->get_api_url( $query ), $options );
71
72 if ( is_wp_error( $response ) ) {
73 return $response;
74 }
75
76 $body = wp_remote_retrieve_body( $response );
77 $decoded = json_decode( $body );
78
79 if ( ! is_object( $decoded ) ) {
80 return new WP_Error(
81 400,
82 __(
83 'Unable to decode upstream API response',
84 'wp-parsely'
85 )
86 );
87 }
88
89 if ( ! property_exists( $decoded, 'success' ) || false === $decoded->success ) {
90 return new WP_Error(
91 $decoded->code ?? 400,
92 $decoded->message ?? __( 'Unable to read data from upstream API', 'wp-parsely' )
93 );
94 }
95
96 return $decoded;
97 }
98
99 /**
100 * Returns the response from the Parse.ly API credentials validation endpoint.
101 *
102 * @since 3.11.0
103 *
104 * @param array<string, mixed> $query The query arguments to send to the remote API.
105 * @param bool $associative (optional) When TRUE, returned objects will be converted into
106 * associative arrays.
107 * @return array<string, mixed>|object|WP_Error
108 */
109 public function get_items( array $query, bool $associative = false ) {
110 $api_request = $this->api_validate_credentials( $query );
111 return $associative ? convert_to_associative_array( $api_request ) : $api_request;
112 }
113 }
114