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 / content-api / endpoints / class-content-api-base-endpoint.php

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

90 lines 2.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 Content API Base Endpoint
4 *
5 * @package Parsely
6 * @since 3.17.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Services\Content_API\Endpoints;
12
13 use Parsely\Services\Base_Service_Endpoint;
14 use WP_Error;
15
16 /**
17 * The base class for the Content API service endpoints.
18 *
19 * @since 3.17.0
20 *
21 * @phpstan-type Content_API_Valid_Response array{
22 * data: array<mixed>,
23 * }
24 *
25 * @phpstan-type Content_API_Error_Response array{
26 * code?: int,
27 * message?: string,
28 * }
29 *
30 * @phpstan-type Content_API_Response = Content_API_Valid_Response|Content_API_Error_Response
31 *
32 * @phpstan-import-type WP_HTTP_Response from Base_Service_Endpoint
33 */
34 abstract class Content_API_Base_Endpoint extends Base_Service_Endpoint {
35 /**
36 * Returns the common query arguments to send to the remote API.
37 *
38 * This method append the API key and secret to the query arguments.
39 *
40 * @since 3.17.0
41 *
42 * @param array<mixed> $args Additional query arguments to send to the remote API.
43 * @return array<mixed> The query arguments to send to the remote API.
44 */
45 protected function get_query_args( array $args = array() ): array {
46 $query_args = parent::get_query_args( $args );
47
48 // Set up the API key and secret.
49 $query_args['apikey'] = $this->get_parsely()->get_site_id();
50 if ( $this->get_parsely()->api_secret_is_set() ) {
51 $query_args['secret'] = $this->get_parsely()->get_api_secret();
52 }
53
54 return $query_args;
55 }
56
57 /**
58 * Processes the response from the remote API.
59 *
60 * @since 3.17.0
61 *
62 * @param WP_HTTP_Response|WP_Error $response The response from the remote API.
63 * @return array<mixed>|WP_Error The processed response.
64 */
65 protected function process_response( $response ) {
66 $response = parent::process_response( $response );
67
68 if ( is_wp_error( $response ) ) {
69 /** @var WP_Error $response */
70 return $response;
71 }
72
73 if ( ! isset( $response['data'] ) ) {
74 /** @var Content_API_Error_Response $response */
75 return new WP_Error(
76 $response['code'] ?? 400,
77 $response['message'] ?? __( 'Unable to read data from upstream API', 'wp-parsely' ),
78 array( 'status' => $response['code'] ?? 400 )
79 );
80 }
81
82 if ( ! is_array( $response['data'] ) ) {
83 return new WP_Error( 400, __( 'Unable to parse data from upstream API', 'wp-parsely' ) );
84 }
85
86 /** @var Content_API_Valid_Response $response */
87 return $response['data'];
88 }
89 }
90