PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.3 3.24.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 All 107 releases
wp-parsely / src / services / suggestions-api / endpoints / class-endpoint-check-auth.php

class-endpoint-check-auth.php in Parse.ly 3.24.2, at src/services/suggestions-api/endpoints/class-endpoint-check-auth.php

113 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 * 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_Response from Base_Service_Endpoint
22 */
23 class Endpoint_Check_Auth extends Suggestions_API_Base_Endpoint {
24 /**
25 * Returns the endpoint for the API request.
26 *
27 * @since 3.19.0
28 *
29 * @return string The endpoint for the API request.
30 */
31 public function get_endpoint(): string {
32 return '/check-auth';
33 }
34
35 /**
36 * Returns whether the Site ID has the authorization to use the Suggestions
37 * API or Suggestions API feature.
38 *
39 * @since 3.19.0
40 *
41 * @param array<mixed> $options The options to pass to the API request.
42 * @return array<mixed>|WP_Error The response from the remote API, or a WP_Error
43 * object if the response is an error.
44 */
45 public function get_check_auth_result( array $options ) {
46 /** @var array<mixed>|WP_Error $response */
47 $response = $this->request( 'GET', $options );
48
49 return $response;
50 }
51
52 /**
53 * Sends a request to the remote API.
54 *
55 * @since 3.19.0
56 * @since 3.24.2 Strips the credentials from errors.
57 *
58 * @param string $method The HTTP method to use for the request.
59 * @param array<mixed> $query_args The query arguments to send to the remote API.
60 * @param array<mixed> $data The data to send in the request body.
61 * @return WP_Error|array<mixed> The response from the remote API.
62 */
63 protected function request( string $method, array $query_args = array(), array $data = array() ) {
64 // Get the URL to send the request to.
65 $request_url = $this->get_endpoint_url( $query_args );
66
67 // Build the request options.
68 $request_options = $this->get_request_options( $method );
69 // Set the body to an empty array, as leaving it to '{}' causes errors.
70 $request_options['body'] = array();
71
72 /** @var WP_HTTP_Response|WP_Error $response */
73 $response = wp_safe_remote_request( $request_url, $request_options );
74
75 if ( is_wp_error( $response ) ) {
76 return $this->get_relayable_transport_error( $response );
77 }
78
79 $result = $this->process_response( $response );
80
81 return is_wp_error( $result ) ? $this->strip_credentials_from_error( $result ) : $result;
82 }
83
84 /**
85 * Processes the response from the remote API.
86 *
87 * @since 3.19.0
88 *
89 * @param WP_HTTP_Response|WP_Error $response The response from the remote API.
90 * @return array<mixed>|WP_Error The processed response.
91 */
92 protected function process_response( $response ) {
93 if ( is_wp_error( $response ) ) {
94 /** @var WP_Error $response */
95 return $response;
96 }
97
98 return $response['response'];
99 }
100
101 /**
102 * Executes the API request.
103 *
104 * @since 3.19.0
105 *
106 * @param array<mixed> $args The arguments to pass to the API request.
107 * @return WP_Error|array<mixed> The response from the API.
108 */
109 public function call( array $args = array() ) {
110 return $this->get_check_auth_result( $args );
111 }
112 }
113