PluginProbe
Parse.ly / 3.16.1
Parse.ly v3.16.1
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.16.1, at src/RemoteAPI/class-validate-api.php

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