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
← All changes | src/Endpoints/class-related-api-proxy.php +24 -95 3.2.03.8.4 View file →
@@ -1,10 +1,10 @@
1 1 <?php
2 2 /**
3 - * Parsely Related REST API Endpoint
3 + * Endpoints: Parse.ly `/related` API proxy endpoint class
4 4 *
5 5 * @package Parsely
6 - * @since 3.2.0
6 + * @since 3.2.0
7 7 */
8 8
9 9 declare(strict_types=1);
10 10
@@ -9,122 +9,51 @@
9 9 declare(strict_types=1);
10 10
11 11 namespace Parsely\Endpoints;
12 12
13 -use Parsely\Parsely;
14 -use Parsely\RemoteAPI\Proxy;
15 13 use stdClass;
14 +use WP_REST_Request;
16 15 use WP_Error;
17 -use WP_REST_Server;
18 -use WP_REST_Request;
19 16
20 17 /**
21 - * Configure a REST API endpoint for use e.g. by the Recommendations Block.
18 + * Configures the `/related` REST API endpoint.
22 19 */
23 -final class Related_API_Proxy {
24 - /**
25 - * Parsely instance.
26 - *
27 - * @var Parsely
28 - */
29 - private $parsely;
20 +final class Related_API_Proxy extends Base_API_Proxy {
30 21
31 22 /**
32 - * Proxy object which does the actual calls to the Parse.ly API.
33 - *
34 - * @var Proxy
23 + * Registers the endpoint's WP REST route.
35 24 */
36 - private $proxy;
37 -
38 - /**
39 - * Used to inject dependencies.
40 - *
41 - * @param Parsely $parsely Instance of Parsely class.
42 - * @param Proxy $proxy Proxy object which does the actual calls to the Parse.ly API.
43 - */
44 - public function __construct( Parsely $parsely, Proxy $proxy ) {
45 - $this->parsely = $parsely;
46 - $this->proxy = $proxy;
25 + public function run(): void {
26 + $this->register_endpoint( '/related' );
47 27 }
48 28
49 29 /**
50 - * Entrypoint to register the endpoint and otherwise initialize this class.
30 + * Cached "proxy" to the Parse.ly `/related` API endpoint.
51 31 *
52 - * @return void
53 - */
54 - public function run(): void {
55 - if ( ! apply_filters( 'wp_parsely_recommendations_block_enabled', false ) ) {
56 - return;
57 - }
58 -
59 - $get_items_args = array(
60 - 'query' => array(
61 - 'default' => array(),
62 - 'sanitize_callback' => function ( $query ) {
63 - // question: how should we sanitize these?
64 - return (array) $query;
65 - },
66 - ),
67 - );
68 -
69 - $rest_route_args = array(
70 - array(
71 - 'methods' => WP_REST_Server::READABLE,
72 - 'callback' => array( $this, 'get_items' ),
73 - 'permission_callback' => array( $this, 'permission_callback' ),
74 - 'args' => $get_items_args,
75 - ),
76 - );
77 -
78 - register_rest_route( 'wp-parsely/v1', '/related', $rest_route_args );
79 - }
80 -
81 - /**
82 - * Determine if the endpoint can be called.
32 + * @param WP_REST_Request $request The request object.
83 33 *
84 - * @return bool
34 + * @return stdClass|WP_Error stdClass containing the data or a WP_Error object on failure.
85 35 */
86 - public function permission_callback(): bool {
87 - // Unauthenticated.
88 - return true;
36 + public function get_items( WP_REST_Request $request ) {
37 + return $this->get_data( $request, false, 'query' );
89 38 }
90 39
91 40 /**
92 - * Cached "proxy" to the Parsely `/related` endpoint
41 + * Generates the final data from the passed response.
93 42 *
94 - * @param WP_REST_Request $request The request object.
95 - * @return stdClass
43 + * @param array<stdClass> $response The response received by the proxy.
44 + * @return array<stdClass> The generated data.
96 45 */
97 - public function get_items( WP_REST_Request $request ) {
98 - $params = $request->get_params();
99 -
100 - if ( $this->parsely->api_key_is_missing() ) {
101 - return (object) array(
102 - 'data' => array(),
103 - 'error' => new WP_Error( 400, __( 'A Parse.ly API Key must be set in site options to use this endpoint', 'wp-parsely' ) ),
104 - );
105 - }
106 -
107 - // A proxy with caching behaviour is used here.
108 - $links = $this->proxy->get_items( $params['query'] );
109 -
110 - if ( is_wp_error( $links ) ) {
111 - return (object) array(
112 - 'data' => array(),
113 - 'error' => $links,
114 - );
115 - }
116 -
117 - $data = array_map(
118 - static function( stdClass $link ) {
46 + protected function generate_data( $response ): array {
47 + return array_map(
48 + static function( stdClass $item ) {
119 49 return (object) array(
120 - 'image_url' => $link->image_url,
121 - 'title' => $link->title,
122 - 'url' => $link->url,
50 + 'image_url' => $item->image_url,
51 + 'thumb_url_medium' => $item->thumb_url_medium,
52 + 'title' => $item->title,
53 + 'url' => $item->url,
123 54 );
124 55 },
125 - $links
56 + $response
126 57 );
127 -
128 - return (object) array( 'data' => $data );
129 58 }
130 59 }