| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utils API Endpoint: Post |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.20.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\REST_API\Utils; |
| 12 |
|
| 13 |
use Parsely\REST_API\Base_Endpoint; |
| 14 |
use Parsely\REST_API\Use_Post_ID_Parameter_Trait; |
| 15 |
use WP_REST_Request; |
| 16 |
use WP_REST_Response; |
| 17 |
|
| 18 |
/** |
| 19 |
* The Utils API Post endpoint. |
| 20 |
* |
| 21 |
* Provides an endpoint for utility functions related to Posts. |
| 22 |
* |
| 23 |
* @since 3.20.5 |
| 24 |
*/ |
| 25 |
class Endpoint_Post extends Base_Endpoint { |
| 26 |
use Use_Post_ID_Parameter_Trait; |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the endpoint's name. |
| 30 |
* |
| 31 |
* @since 3.20.5 |
| 32 |
* |
| 33 |
* @return string The endpoint's name. |
| 34 |
*/ |
| 35 |
public static function get_endpoint_name(): string { |
| 36 |
return 'post'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Registers the routes for the endpoint. |
| 41 |
* |
| 42 |
* @since 3.20.5 |
| 43 |
*/ |
| 44 |
public function register_routes(): void { |
| 45 |
/** |
| 46 |
* GET /utils/post/{post_id}/rest-route |
| 47 |
* Returns the REST route of a Post. |
| 48 |
*/ |
| 49 |
$this->register_rest_route_with_post_id( |
| 50 |
'/rest-route', |
| 51 |
array( 'GET' ), |
| 52 |
array( $this, 'get_rest_route' ) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* API Endpoint: GET /utils/post/{post_id}/rest-route |
| 58 |
* |
| 59 |
* Returns the REST route of a post. |
| 60 |
* |
| 61 |
* @since 3.20.5 |
| 62 |
* |
| 63 |
* @param WP_REST_Request $request The request object. |
| 64 |
* @return WP_REST_Response The response object. |
| 65 |
*/ |
| 66 |
public function get_rest_route( WP_REST_Request $request ) { |
| 67 |
$post_id = $request->get_param( 'post_id' ); |
| 68 |
$post_rest_route = rest_get_route_for_post( $post_id ); |
| 69 |
|
| 70 |
return new WP_REST_Response( array( 'data' => $post_rest_route ), 200 ); |
| 71 |
} |
| 72 |
} |
| 73 |
|