PluginProbe
Parse.ly / 3.3.2
Parse.ly v3.3.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 3.20.3 All 105 releases
wp-parsely / src / Endpoints / class-related-api-proxy.php

class-related-api-proxy.php in Parse.ly 3.3.2, at src/Endpoints/class-related-api-proxy.php

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