PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
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 3.20.2 All 106 releases
wp-parsely / src / services / suggestions-api / endpoints / class-suggestions-api-base-endpoint.php

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

125 lines 3.3 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 base class.
4 *
5 * @package Parsely
6 * @since 3.17.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Services\Suggestions_API\Endpoints;
12
13 use Parsely\Services\Base_Service_Endpoint;
14 use WP_Error;
15
16 /**
17 * The base class for the suggestions API endpoints.
18 *
19 * @since 3.17.0
20 *
21 * @phpstan-import-type WP_HTTP_Response from Base_Service_Endpoint
22 * @phpstan-import-type WP_HTTP_Request_Args from Base_Service_Endpoint
23 */
24 abstract class Suggestions_API_Base_Endpoint extends Base_Service_Endpoint {
25 /**
26 * Flag to truncate the content of the request body.
27 *
28 * By setting it to true, the content of the request body will be truncated to a maximum length.
29 *
30 * @since 3.17.0
31 *
32 * @var bool
33 */
34 protected const TRUNCATE_CONTENT = true;
35
36 /**
37 * Returns the request options for the remote API request.
38 *
39 * @since 3.17.0
40 *
41 * @param string $method The HTTP method to use for the request.
42 * @return WP_HTTP_Request_Args The request options for the remote API request.
43 */
44 protected function get_request_options( string $method ): array {
45 $options = array(
46 'method' => $method,
47 'headers' => array(
48 'Content-Type' => 'application/json; charset=utf-8',
49 'X-INTERNAL-SERVICE' => 'content-helper',
50 ),
51 'data_format' => 'body',
52 'timeout' => 90, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
53 'body' => '{}',
54 );
55
56 // Add API key to request headers.
57 if ( $this->get_parsely()->api_secret_is_set() ) {
58 $options['headers']['X-APIKEY-SECRET'] = $this->get_parsely()->get_api_secret();
59 }
60
61 return $options;
62 }
63
64 /**
65 * Returns the common query arguments to send to the remote API.
66 *
67 * This method appends the API key and secret to the query arguments.
68 *
69 * @since 3.17.0
70 *
71 * @param array<mixed> $args Additional query arguments to send to the remote API.
72 * @return array<mixed> The query arguments to send to the remote API.
73 */
74 protected function get_query_args( array $args = array() ): array {
75 $query_args = parent::get_query_args( $args );
76
77 // Set up the API key and secret.
78 $query_args['apikey'] = $this->get_parsely()->get_site_id();
79
80 return $query_args;
81 }
82
83 /**
84 * Processes the response from the remote API.
85 *
86 * @since 3.17.0
87 *
88 * @param WP_HTTP_Response|WP_Error $response The response from the remote API.
89 * @return array<mixed>|WP_Error The processed response.
90 */
91 protected function process_response( $response ) {
92 if ( is_wp_error( $response ) ) {
93 /** @var WP_Error $response */
94 return $response;
95 }
96
97 // Handle any errors returned by the API.
98 if ( 200 !== $response['response']['code'] ) {
99 $error = json_decode( wp_remote_retrieve_body( $response ), true );
100
101 if ( ! is_array( $error ) ) {
102 return new WP_Error(
103 400,
104 __( 'Unable to decode upstream API error', 'wp-parsely' )
105 );
106 }
107
108 return new WP_Error( $error['error'], $error['detail'] );
109 }
110
111 $body = wp_remote_retrieve_body( $response );
112 $decoded = json_decode( $body, true );
113
114 if ( ! is_array( $decoded ) ) {
115 return new WP_Error( 400, __( 'Unable to decode upstream API response', 'wp-parsely' ) );
116 }
117
118 if ( ! is_array( $decoded['result'] ) ) {
119 return new WP_Error( 400, __( 'Unable to parse data from upstream API', 'wp-parsely' ) );
120 }
121
122 return $decoded['result'];
123 }
124 }
125