| 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 stdClass; |
| 15 |
use WP_REST_Request; |
| 16 |
use WP_Error; |
| 17 |
|
| 18 |
/** |
| 19 |
* Configures the `/related` REST API endpoint. |
| 20 |
*/ |
| 21 |
final class Related_API_Proxy extends Base_API_Proxy { |
| 22 |
|
| 23 |
/** |
| 24 |
* Registers the endpoint's WP REST route. |
| 25 |
*/ |
| 26 |
public function run(): void { |
| 27 |
$this->register_endpoint( '/related' ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Cached "proxy" to the Parse.ly `/related` API endpoint. |
| 32 |
* |
| 33 |
* @param WP_REST_Request $request The request object. |
| 34 |
* @return stdClass|WP_Error stdClass containing the data or a WP_Error object on failure. |
| 35 |
*/ |
| 36 |
public function get_items( WP_REST_Request $request ) { |
| 37 |
return $this->get_data( $request, false, 'query' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generates the final data from the passed response. |
| 42 |
* |
| 43 |
* @param array<stdClass> $response The response received by the proxy. |
| 44 |
* @return array<stdClass> The generated data. |
| 45 |
*/ |
| 46 |
protected function generate_data( $response ): array { |
| 47 |
$itm_source = $this->itm_source; |
| 48 |
|
| 49 |
return array_map( |
| 50 |
static function ( stdClass $item ) use ( $itm_source ) { |
| 51 |
return (object) array( |
| 52 |
'image_url' => $item->image_url, |
| 53 |
'thumb_url_medium' => $item->thumb_url_medium, |
| 54 |
'title' => $item->title, |
| 55 |
'url' => Parsely::get_url_with_itm_source( $item->url, $itm_source ), |
| 56 |
); |
| 57 |
}, |
| 58 |
$response |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
|