PluginProbe
Parse.ly / 3.8.4
Parse.ly v3.8.4
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 / Endpoints / class-base-api-proxy.php

class-base-api-proxy.php in Parse.ly 3.8.4, at src/Endpoints/class-base-api-proxy.php

165 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Endpoints: Base API proxy endpoint class for all API proxy endpoints
4 *
5 * @package Parsely
6 * @since 3.4.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Endpoints;
12
13 use Parsely\Parsely;
14 use Parsely\RemoteAPI\Remote_API_Interface;
15 use stdClass;
16 use WP_Error;
17 use WP_REST_Request;
18 use WP_REST_Server;
19
20 use function Parsely\Utils\convert_endpoint_to_filter_key;
21
22 /**
23 * Configures a REST API endpoint for use.
24 */
25 abstract class Base_API_Proxy {
26 /**
27 * Parsely object instance.
28 *
29 * @var Parsely
30 */
31 protected $parsely;
32
33 /**
34 * Proxy object which does the actual calls to the Parse.ly API.
35 *
36 * @var Remote_API_Interface
37 */
38 private $api;
39
40 /**
41 * Registers the endpoint's WP REST route.
42 */
43 abstract public function run(): void;
44
45 /**
46 * Generates the final data from the passed response.
47 *
48 * @param array<stdClass> $response The response received by the proxy.
49 * @return array<stdClass> The generated data.
50 */
51 abstract protected function generate_data( $response ): array;
52
53 /**
54 * Cached "proxy" to the Parse.ly API endpoint.
55 *
56 * @param WP_REST_Request $request The request object.
57 *
58 * @return stdClass|WP_Error stdClass containing the data or a WP_Error object on failure.
59 */
60 abstract public function get_items( WP_REST_Request $request );
61
62 /**
63 * Determines if there are enough permissions to call the endpoint.
64 *
65 * @return bool
66 */
67 public function permission_callback(): bool {
68 return $this->api->is_user_allowed_to_make_api_call();
69 }
70
71 /**
72 * Constructor.
73 *
74 * @param Parsely $parsely Instance of Parsely class.
75 * @param Remote_API_Interface $api API object which does the actual calls to the Parse.ly API.
76 */
77 public function __construct( Parsely $parsely, Remote_API_Interface $api ) {
78 $this->parsely = $parsely;
79 $this->api = $api;
80 }
81
82 /**
83 * Registers the endpoint's WP REST route.
84 *
85 * @param string $endpoint The endpoint's route (e.g. /stats/posts).
86 */
87 protected function register_endpoint( string $endpoint ): void {
88 if ( ! apply_filters( 'wp_parsely_enable_' . convert_endpoint_to_filter_key( $endpoint ) . '_api_proxy', true ) ) {
89 return;
90 }
91
92 $get_items_args = array(
93 'query' => array(
94 'default' => array(),
95 'sanitize_callback' => function ( array $query ) {
96 $sanitized_query = array();
97 foreach ( $query as $key => $value ) {
98 $sanitized_query[ sanitize_key( $key ) ] = sanitize_text_field( $value );
99 }
100
101 return $sanitized_query;
102 },
103 ),
104 );
105
106 $rest_route_args = array(
107 array(
108 'methods' => WP_REST_Server::READABLE,
109 'callback' => array( $this, 'get_items' ),
110 'permission_callback' => array( $this, 'permission_callback' ),
111 'args' => $get_items_args,
112 'show_in_index' => $this->permission_callback(),
113 ),
114 );
115
116 register_rest_route( 'wp-parsely/v1', $endpoint, $rest_route_args );
117 }
118
119 /**
120 * Cached "proxy" to the endpoint.
121 *
122 * @param WP_REST_Request $request The request object.
123 * @param bool $require_api_secret Specifies if the API Secret is
124 * required.
125 * @param string $param_item The param element to use to
126 * get the items.
127 *
128 * @return stdClass|WP_Error stdClass containing the data or a WP_Error object on failure.
129 */
130 protected function get_data( WP_REST_Request $request, bool $require_api_secret = true, string $param_item = null ) {
131 if ( false === $this->parsely->site_id_is_set() ) {
132 return new WP_Error(
133 'parsely_site_id_not_set',
134 __( 'A Parse.ly Site ID must be set in site options to use this endpoint', 'wp-parsely' ),
135 array( 'status' => 403 )
136 );
137 }
138
139 if ( true === $require_api_secret && false === $this->parsely->api_secret_is_set() ) {
140 return new WP_Error(
141 'parsely_api_secret_not_set',
142 __( 'A Parse.ly API Secret must be set in site options to use this endpoint', 'wp-parsely' ),
143 array( 'status' => 403 )
144 );
145 }
146
147 if ( null !== $param_item ) {
148 $params = $request->get_param( $param_item );
149 } else {
150 $params = $request->get_params();
151 }
152
153 // A proxy with caching behavior is used here.
154 $response = $this->api->get_items( $params ); // @phpstan-ignore-line.
155
156 if ( is_wp_error( $response ) ) {
157 return $response;
158 }
159
160 return (object) array(
161 'data' => $this->generate_data( $response ), // @phpstan-ignore-line.
162 );
163 }
164 }
165