| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — My WordPress: REST bridge for non-REST post types. |
| 4 |
* |
| 5 |
* Post types registered with `show_in_rest => false` have no `wp/v2` |
| 6 |
* collection, so the site window cannot browse them the way it browses |
| 7 |
* Posts or Products. This module re-exposes them under |
| 8 |
* `desktop-mode/v1/post-type/<slug>` by subclassing Core's own |
| 9 |
* `WP_REST_Posts_Controller`, which means `_fields`, `_embed`, |
| 10 |
* `search`, `status`, `X-WP-Total` and `X-WP-TotalPages` all behave |
| 11 |
* exactly as they do on `wp/v2` — the bundle needs no special-casing, |
| 12 |
* only a different `restPath`. |
| 13 |
* |
| 14 |
* The controller itself lives in |
| 15 |
* `class-openstation-my-wordpress-post-type-controller.php`. |
| 16 |
* |
| 17 |
* ## Security |
| 18 |
* |
| 19 |
* These types opted out of REST deliberately, so the bridge is |
| 20 |
* deliberately narrower than Core's controller: |
| 21 |
* |
| 22 |
* - Core's `get_items_permissions_check()` returns true for any |
| 23 |
* non-`edit` context, i.e. public read. We override it (and the |
| 24 |
* single-item check) to require the type's `edit_posts` capability |
| 25 |
* in **every** context. Never anonymous, never subscriber-readable. |
| 26 |
* - Only `GET` collection, `GET` item, and `DELETE` item (trash, for |
| 27 |
* recycle-bin parity) are registered. No create, no update — a |
| 28 |
* write schema the type's author never vetted is a footgun. |
| 29 |
* - `openstation_my_wordpress_post_type_rest_enabled` lets a site or |
| 30 |
* the owning plugin veto the bridge per type. |
| 31 |
* |
| 32 |
* @package OpenStation |
| 33 |
*/ |
| 34 |
|
| 35 |
defined( 'ABSPATH' ) || exit; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register a bridge controller for every eligible non-REST post type. |
| 39 |
* |
| 40 |
* Runs on `rest_api_init`, which fires well after `init`, so post type |
| 41 |
* discovery is complete by the time this executes. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
function openstation_my_wordpress_register_post_type_routes() { |
| 46 |
if ( ! openstation_my_wordpress_user_can_use() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
foreach ( openstation_my_wordpress_eligible_post_types() as $name => $post_type ) { |
| 51 |
if ( ! empty( $post_type->show_in_rest ) ) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
if ( ! openstation_my_wordpress_post_type_is_bridged( $name ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
$controller = new OpenStation_My_WordPress_Post_Type_Controller( $name ); |
| 58 |
$controller->register_routes(); |
| 59 |
} |
| 60 |
} |
| 61 |
add_action( 'rest_api_init', 'openstation_my_wordpress_register_post_type_routes' ); |
| 62 |
|