| @@ -1,21 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Recycle Bin: REST routes. | |
| 3 | + * OpenStation — Recycle Bin: REST routes. | |
| 4 | 4 | * |
| 5 | - * Three routes, all under `/desktop-mode/v1/recycle-bin`: | |
| 5 | + * Five routes, all under `/desktop-mode/v1/recycle-bin`: | |
| 6 | 6 | * |
| 7 | 7 | * GET / — list trashed items |
| 8 | 8 | * POST /restore — body { ids: int[] } |
| 9 | 9 | * POST /purge — body { ids: int[] } |
| 10 | 10 | * POST /empty — empties the visible bin |
| 11 | + * GET /count — total items in the bin (badge polling) | |
| 11 | 12 | * |
| 12 | - * Permission gating delegates to `desktop_mode_recycle_bin_user_can_*` per item; | |
| 13 | + * Permission gating delegates to `openstation_recycle_bin_user_can_*` per item; | |
| 13 | 14 | * the route-level callback only checks "are you logged in and in |
| 14 | - * desktop mode at all?" via `desktop_mode_is_enabled()`. | |
| 15 | + * OpenStation at all?" via `openstation_is_enabled()`. | |
| 15 | 16 | * |
| 16 | - * @package WPDesktopMode | |
| 17 | - * @since 0.19.0 | |
| 17 | + * @package OpenStation | |
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | defined( 'ABSPATH' ) || exit; |
| 21 | 21 | |
| @@ -20,12 +20,10 @@ | ||
| 20 | 20 | defined( 'ABSPATH' ) || exit; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Register REST routes. |
| 24 | - * | |
| 25 | - * @since 0.19.0 | |
| 26 | 24 | */ |
| 27 | -function desktop_mode_recycle_bin_register_rest_routes() { | |
| 25 | +function openstation_recycle_bin_register_rest_routes() { | |
| 28 | 26 | $namespace = 'desktop-mode/v1'; |
| 29 | 27 | |
| 30 | 28 | register_rest_route( |
| 31 | 29 | $namespace, |
| @@ -31,10 +29,10 @@ | ||
| 31 | 29 | $namespace, |
| 32 | 30 | '/recycle-bin', |
| 33 | 31 | array( |
| 34 | 32 | 'methods' => WP_REST_Server::READABLE, |
| 35 | - 'permission_callback' => 'desktop_mode_recycle_bin_rest_permission', | |
| 36 | - 'callback' => 'desktop_mode_recycle_bin_rest_list', | |
| 33 | + 'permission_callback' => 'openstation_recycle_bin_rest_permission', | |
| 34 | + 'callback' => 'openstation_recycle_bin_rest_list', | |
| 37 | 35 | 'args' => array( |
| 38 | 36 | 'page' => array( |
| 39 | 37 | 'type' => 'integer', |
| 40 | 38 | 'default' => 1, |
| @@ -63,10 +61,10 @@ | ||
| 63 | 61 | $namespace, |
| 64 | 62 | '/recycle-bin/restore', |
| 65 | 63 | array( |
| 66 | 64 | 'methods' => WP_REST_Server::CREATABLE, |
| 67 | - 'permission_callback' => 'desktop_mode_recycle_bin_rest_permission', | |
| 68 | - 'callback' => 'desktop_mode_recycle_bin_rest_restore', | |
| 65 | + 'permission_callback' => 'openstation_recycle_bin_rest_permission', | |
| 66 | + 'callback' => 'openstation_recycle_bin_rest_restore', | |
| 69 | 67 | // Accepts either `items: [{id, type}]` (preferred) or |
| 70 | 68 | // `ids: int[]` (legacy — assumes post-ish entities). |
| 71 | 69 | // Both registered as optional; the handler validates. |
| 72 | 70 | 'args' => array( |
| @@ -87,10 +85,10 @@ | ||
| 87 | 85 | $namespace, |
| 88 | 86 | '/recycle-bin/purge', |
| 89 | 87 | array( |
| 90 | 88 | 'methods' => WP_REST_Server::CREATABLE, |
| 91 | - 'permission_callback' => 'desktop_mode_recycle_bin_rest_permission', | |
| 92 | - 'callback' => 'desktop_mode_recycle_bin_rest_purge', | |
| 89 | + 'permission_callback' => 'openstation_recycle_bin_rest_permission', | |
| 90 | + 'callback' => 'openstation_recycle_bin_rest_purge', | |
| 93 | 91 | 'args' => array( |
| 94 | 92 | 'items' => array( |
| 95 | 93 | 'type' => 'array', |
| 96 | 94 | 'required' => false, |
| @@ -108,10 +106,10 @@ | ||
| 108 | 106 | $namespace, |
| 109 | 107 | '/recycle-bin/empty', |
| 110 | 108 | array( |
| 111 | 109 | 'methods' => WP_REST_Server::CREATABLE, |
| 112 | - 'permission_callback' => 'desktop_mode_recycle_bin_rest_permission', | |
| 113 | - 'callback' => 'desktop_mode_recycle_bin_rest_empty', | |
| 110 | + 'permission_callback' => 'openstation_recycle_bin_rest_permission', | |
| 111 | + 'callback' => 'openstation_recycle_bin_rest_empty', | |
| 114 | 112 | ) |
| 115 | 113 | ); |
| 116 | 114 | |
| 117 | 115 | register_rest_route( |
| @@ -118,31 +116,29 @@ | ||
| 118 | 116 | $namespace, |
| 119 | 117 | '/recycle-bin/count', |
| 120 | 118 | array( |
| 121 | 119 | 'methods' => WP_REST_Server::READABLE, |
| 122 | - 'permission_callback' => 'desktop_mode_recycle_bin_rest_permission', | |
| 120 | + 'permission_callback' => 'openstation_recycle_bin_rest_permission', | |
| 123 | 121 | 'callback' => static function () { |
| 124 | 122 | return rest_ensure_response( |
| 125 | - array( 'count' => desktop_mode_recycle_bin_count() ) | |
| 123 | + array( 'count' => openstation_recycle_bin_count() ) | |
| 126 | 124 | ); |
| 127 | 125 | }, |
| 128 | 126 | ) |
| 129 | 127 | ); |
| 130 | 128 | } |
| 131 | -add_action( 'rest_api_init', 'desktop_mode_recycle_bin_register_rest_routes' ); | |
| 129 | +add_action( 'rest_api_init', 'openstation_recycle_bin_register_rest_routes' ); | |
| 132 | 130 | |
| 133 | 131 | /** |
| 134 | 132 | * Route-level permission gate. |
| 135 | 133 | * |
| 136 | 134 | * Per-item capability checks happen inside the store layer; here we |
| 137 | - * only enforce "logged-in user with desktop mode opted in" — same | |
| 135 | + * only enforce "logged-in user with OpenStation opted in" — same | |
| 138 | 136 | * baseline as every other desktop REST surface. |
| 139 | 137 | * |
| 140 | - * @since 0.19.0 | |
| 141 | - * | |
| 142 | 138 | * @return bool|WP_Error |
| 143 | 139 | */ |
| 144 | -function desktop_mode_recycle_bin_rest_permission() { | |
| 140 | +function openstation_recycle_bin_rest_permission() { | |
| 145 | 141 | if ( ! is_user_logged_in() ) { |
| 146 | 142 | return new WP_Error( |
| 147 | 143 | 'rest_forbidden', |
| 148 | 144 | __( 'Sorry, you must be logged in.', 'desktop-mode' ), |
| @@ -148,12 +144,12 @@ | ||
| 148 | 144 | __( 'Sorry, you must be logged in.', 'desktop-mode' ), |
| 149 | 145 | array( 'status' => 401 ) |
| 150 | 146 | ); |
| 151 | 147 | } |
| 152 | - if ( function_exists( 'desktop_mode_is_enabled' ) && ! desktop_mode_is_enabled() ) { | |
| 148 | + if ( function_exists( 'openstation_is_enabled' ) && ! openstation_is_enabled() ) { | |
| 153 | 149 | return new WP_Error( |
| 154 | 150 | 'rest_forbidden', |
| 155 | - __( 'Desktop mode is not enabled for this user.', 'desktop-mode' ), | |
| 151 | + __( 'OpenStation is not enabled for this user.', 'desktop-mode' ), | |
| 156 | 152 | array( 'status' => 403 ) |
| 157 | 153 | ); |
| 158 | 154 | } |
| 159 | 155 | return true; |
| @@ -161,15 +157,13 @@ | ||
| 161 | 157 | |
| 162 | 158 | /** |
| 163 | 159 | * GET /recycle-bin |
| 164 | 160 | * |
| 165 | - * @since 0.19.0 | |
| 166 | - * | |
| 167 | 161 | * @param WP_REST_Request $request REST request. |
| 168 | 162 | * @return WP_REST_Response |
| 169 | 163 | */ |
| 170 | -function desktop_mode_recycle_bin_rest_list( $request ) { | |
| 171 | - $payload = desktop_mode_recycle_bin_get_items( | |
| 164 | +function openstation_recycle_bin_rest_list( $request ) { | |
| 165 | + $payload = openstation_recycle_bin_get_items( | |
| 172 | 166 | array( |
| 173 | 167 | 'page' => (int) $request->get_param( 'page' ), |
| 174 | 168 | 'per_page' => (int) $request->get_param( 'per_page' ), |
| 175 | 169 | 'type' => (string) $request->get_param( 'type' ), |
| @@ -182,29 +176,25 @@ | ||
| 182 | 176 | |
| 183 | 177 | /** |
| 184 | 178 | * POST /recycle-bin/restore |
| 185 | 179 | * |
| 186 | - * @since 0.19.0 | |
| 187 | - * | |
| 188 | 180 | * @param WP_REST_Request $request REST request. |
| 189 | 181 | * @return WP_REST_Response |
| 190 | 182 | */ |
| 191 | -function desktop_mode_recycle_bin_rest_restore( $request ) { | |
| 192 | - $items = desktop_mode_recycle_bin_normalize_items( $request ); | |
| 193 | - return rest_ensure_response( desktop_mode_recycle_bin_apply_bulk( $items, 'desktop_mode_recycle_bin_restore' ) ); | |
| 183 | +function openstation_recycle_bin_rest_restore( $request ) { | |
| 184 | + $items = openstation_recycle_bin_normalize_items( $request ); | |
| 185 | + return rest_ensure_response( openstation_recycle_bin_apply_bulk( $items, 'openstation_recycle_bin_restore' ) ); | |
| 194 | 186 | } |
| 195 | 187 | |
| 196 | 188 | /** |
| 197 | 189 | * POST /recycle-bin/purge |
| 198 | 190 | * |
| 199 | - * @since 0.19.0 | |
| 200 | - * | |
| 201 | 191 | * @param WP_REST_Request $request REST request. |
| 202 | 192 | * @return WP_REST_Response |
| 203 | 193 | */ |
| 204 | -function desktop_mode_recycle_bin_rest_purge( $request ) { | |
| 205 | - $items = desktop_mode_recycle_bin_normalize_items( $request ); | |
| 206 | - return rest_ensure_response( desktop_mode_recycle_bin_apply_bulk( $items, 'desktop_mode_recycle_bin_purge' ) ); | |
| 194 | +function openstation_recycle_bin_rest_purge( $request ) { | |
| 195 | + $items = openstation_recycle_bin_normalize_items( $request ); | |
| 196 | + return rest_ensure_response( openstation_recycle_bin_apply_bulk( $items, 'openstation_recycle_bin_purge' ) ); | |
| 207 | 197 | } |
| 208 | 198 | |
| 209 | 199 | /** |
| 210 | 200 | * Read either `items: [{id, type}]` (preferred) or `ids: int[]` |
| @@ -210,14 +200,12 @@ | ||
| 210 | 200 | * Read either `items: [{id, type}]` (preferred) or `ids: int[]` |
| 211 | 201 | * (legacy) and normalise into a list of `[id, type]` pairs the |
| 212 | 202 | * bulk runner can hand straight to a typed callback. |
| 213 | 203 | * |
| 214 | - * @since 0.21.0 | |
| 215 | - * | |
| 216 | 204 | * @param WP_REST_Request $request REST request. |
| 217 | 205 | * @return array<int, array{id:int, type:string}> |
| 218 | 206 | */ |
| 219 | -function desktop_mode_recycle_bin_normalize_items( $request ) { | |
| 207 | +function openstation_recycle_bin_normalize_items( $request ) { | |
| 220 | 208 | $out = array(); |
| 221 | 209 | |
| 222 | 210 | $items = $request->get_param( 'items' ); |
| 223 | 211 | if ( is_array( $items ) ) { |
| @@ -255,30 +243,22 @@ | ||
| 255 | 243 | |
| 256 | 244 | /** |
| 257 | 245 | * POST /recycle-bin/empty |
| 258 | 246 | * |
| 259 | - * @since 0.19.0 | |
| 260 | - * | |
| 261 | 247 | * @return WP_REST_Response |
| 262 | 248 | */ |
| 263 | -function desktop_mode_recycle_bin_rest_empty() { | |
| 264 | - return rest_ensure_response( desktop_mode_recycle_bin_empty() ); | |
| 249 | +function openstation_recycle_bin_rest_empty() { | |
| 250 | + return rest_ensure_response( openstation_recycle_bin_empty() ); | |
| 265 | 251 | } |
| 266 | 252 | |
| 267 | 253 | /** |
| 268 | 254 | * Run a per-item callback over a list, capturing per-item results. |
| 269 | 255 | * |
| 270 | - * @since 0.19.0 | |
| 271 | - * @since 0.21.0 `$items` now carries `{id, type}` pairs; the | |
| 272 | - * callback receives both arguments. Legacy id-only | |
| 273 | - * callers go through `desktop_mode_recycle_bin_normalize_items`, | |
| 274 | - * which fills `type` with `''` (post default). | |
| 275 | - * | |
| 276 | 256 | * @param array<int, array{id:int, type:string}> $items Items to operate on. |
| 277 | 257 | * @param callable $callback `($id, $type) → true|WP_Error`. |
| 278 | 258 | * @return array{ok:int[], errors:array<int, array{id:int, code:string, message:string}>} |
| 279 | 259 | */ |
| 280 | -function desktop_mode_recycle_bin_apply_bulk( $items, $callback ) { | |
| 260 | +function openstation_recycle_bin_apply_bulk( $items, $callback ) { | |
| 281 | 261 | $ok = array(); |
| 282 | 262 | $errors = array(); |
| 283 | 263 | |
| 284 | 264 | foreach ( $items as $item ) { |