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