| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parsely Related REST API Endpoint |
| 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 |
* Configure 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 |
* 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; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Entrypoint to register the endpoint and otherwise initialize this class. |
| 51 |
* |
| 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. |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public function permission_callback(): bool { |
| 87 |
// Unauthenticated. |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Cached "proxy" to the Parsely `/related` endpoint |
| 93 |
* |
| 94 |
* @param WP_REST_Request $request The request object. |
| 95 |
* @return stdClass |
| 96 |
*/ |
| 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 ) { |
| 119 |
return (object) array( |
| 120 |
'image_url' => $link->image_url, |
| 121 |
'title' => $link->title, |
| 122 |
'url' => $link->url, |
| 123 |
); |
| 124 |
}, |
| 125 |
$links |
| 126 |
); |
| 127 |
|
| 128 |
return (object) array( 'data' => $data ); |
| 129 |
} |
| 130 |
} |
| 131 |
|