PluginProbe
Parse.ly / 3.2.0
Parse.ly v3.2.0
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-base-proxy.php

class-base-proxy.php in Parse.ly 3.2.0, at src/RemoteAPI/class-base-proxy.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base Proxy for all Parse.ly API endpoints.
4 *
5 * @package Parsely
6 * @since 3.2.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\RemoteAPI;
12
13 use Parsely\Parsely;
14 use RuntimeException;
15 use WP_Error;
16
17 /**
18 * Base Proxy for all Parse.ly API endpoints.
19 *
20 * Child classes must add a protected `ENDPOINT` constant, and a protected QUERY_FILTER constant.
21 *
22 * @since 3.2.0
23 */
24 abstract class Base_Proxy implements Proxy {
25 protected const ENDPOINT = '';
26 protected const QUERY_FILTER = '';
27
28 /**
29 * Parsely Instance.
30 *
31 * @var Parsely
32 */
33 private $parsely;
34
35 /**
36 * Constructor.
37 *
38 * @param Parsely $parsely Parsely instance.
39 * @since 3.2.0
40 */
41 public function __construct( Parsely $parsely ) {
42 $this->parsely = $parsely;
43 }
44
45 /**
46 * Get URL for a particular Parse.ly API endpoint.
47 *
48 * @since 3.2.0
49 *
50 * @throws RuntimeException If the endpoint constant is not defined.
51 * @throws RuntimeException If the query filter constant is not defined.
52 *
53 * @param array<string, mixed> $query The query arguments to send to the remote API.
54 * @return string
55 */
56 public function get_api_url( array $query ): string {
57 if ( static::ENDPOINT === '' ) {
58 throw new RuntimeException( 'ENDPOINT constant must be defined in child class.' );
59 }
60 if ( static::QUERY_FILTER === '' ) {
61 throw new RuntimeException( 'QUERY_FILTER constant must be defined in child class.' );
62 }
63
64 $query['apikey'] = $this->parsely->get_api_key();
65 $query = array_filter( $query );
66
67 // Sort by key so the query args are in alphabetical order.
68 ksort( $query );
69
70 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Hook names are defined in child classes.
71 $query = apply_filters( static::QUERY_FILTER, $query );
72 return add_query_arg( $query, static::ENDPOINT );
73 }
74
75 /**
76 * Implements the fetcher for the proxy interface.
77 *
78 * @since 3.2.0
79 *
80 * @param array<string, mixed> $query The query arguments to send to the remote API.
81 * @return WP_Error|array<string, mixed>
82 */
83 public function get_items( array $query ) {
84 $full_api_url = $this->get_api_url( $query );
85
86 $result = wp_safe_remote_get( $full_api_url, array() );
87
88 if ( is_wp_error( $result ) ) {
89 return $result;
90 }
91
92 $body = wp_remote_retrieve_body( $result );
93 $decoded = json_decode( $body );
94
95 if ( ! is_object( $decoded ) ) {
96 return new WP_Error( 400, __( 'Unable to decode upstream API response', 'wp-parsely' ) );
97 }
98
99 if ( ! property_exists( $decoded, 'data' ) ) {
100 return new WP_Error( $decoded->code ?? 400, $decoded->message ?? __( 'Unable to read data from upstream API', 'wp-parsely' ) );
101 }
102
103 if ( ! is_array( $decoded->data ) ) {
104 return new WP_Error( 400, __( 'Unable to parse data from upstream API', 'wp-parsely' ) );
105 }
106
107 return $decoded->data;
108 }
109 }
110