| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Content Graph: REST routes. | |
| 3 | + * OpenStation — Content Graph: REST routes. | |
| 4 | 4 | * |
| 5 | 5 | * Three endpoints under `desktop-mode/v1/content-graph`: |
| 6 | 6 | * |
| 7 | 7 | * GET /post-types |
| @@ -8,10 +8,12 @@ | ||
| 8 | 8 | * Lists the types eligible for the graph (`slug`, `label`, `icon`, |
| 9 | 9 | * `count`, `taxonomies`). |
| 10 | 10 | * |
| 11 | 11 | * GET /nodes?types=post,page,... |
| 12 | - * Returns the full `{ nodes, edges, stats }` tuple. Cached server- | |
| 13 | - * side, see graph-builder.php. | |
| 12 | + * Returns the full `{ nodes, edges, groups, stats }` tuple. Cached | |
| 13 | + * server-side, see graph-builder.php. `types` omitted means every | |
| 14 | + * registered type; `types=` (present but empty) means none — the | |
| 15 | + * shell sends the latter when every toolbar chip is off. | |
| 14 | 16 | * |
| 15 | 17 | * GET /post/<id> |
| 16 | 18 | * Returns the side-panel detail bundle for one post: |
| 17 | 19 | * { post: {...}, author, contributors, comments, categories, |
| @@ -16,9 +18,9 @@ | ||
| 16 | 18 | * Returns the side-panel detail bundle for one post: |
| 17 | 19 | * { post: {...}, author, contributors, comments, categories, |
| 18 | 20 | * attached_media, revisions }. |
| 19 | 21 | * |
| 20 | - * @package WPDesktopMode | |
| 22 | + * @package OpenStation | |
| 21 | 23 | */ |
| 22 | 24 | |
| 23 | 25 | defined( 'ABSPATH' ) || exit; |
| 24 | 26 | |
| @@ -26,23 +28,23 @@ | ||
| 26 | 28 | * Capability check shared across every endpoint. |
| 27 | 29 | * |
| 28 | 30 | * @return bool |
| 29 | 31 | */ |
| 30 | -function desktop_mode_content_graph_rest_permission() { | |
| 31 | - return desktop_mode_content_graph_user_can_use(); | |
| 32 | +function openstation_content_graph_rest_permission() { | |
| 33 | + return openstation_content_graph_user_can_use(); | |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | /** |
| 35 | 37 | * Register the routes. |
| 36 | 38 | */ |
| 37 | -function desktop_mode_content_graph_register_routes() { | |
| 39 | +function openstation_content_graph_register_routes() { | |
| 38 | 40 | register_rest_route( |
| 39 | 41 | 'desktop-mode/v1', |
| 40 | 42 | '/content-graph/post-types', |
| 41 | 43 | array( |
| 42 | 44 | 'methods' => WP_REST_Server::READABLE, |
| 43 | - 'callback' => 'desktop_mode_content_graph_rest_post_types', | |
| 44 | - 'permission_callback' => 'desktop_mode_content_graph_rest_permission', | |
| 45 | + 'callback' => 'openstation_content_graph_rest_post_types', | |
| 46 | + 'permission_callback' => 'openstation_content_graph_rest_permission', | |
| 45 | 47 | ) |
| 46 | 48 | ); |
| 47 | 49 | register_rest_route( |
| 48 | 50 | 'desktop-mode/v1', |
| @@ -48,15 +50,20 @@ | ||
| 48 | 50 | 'desktop-mode/v1', |
| 49 | 51 | '/content-graph/nodes', |
| 50 | 52 | array( |
| 51 | 53 | 'methods' => WP_REST_Server::READABLE, |
| 52 | - 'callback' => 'desktop_mode_content_graph_rest_nodes', | |
| 53 | - 'permission_callback' => 'desktop_mode_content_graph_rest_permission', | |
| 54 | + 'callback' => 'openstation_content_graph_rest_nodes', | |
| 55 | + 'permission_callback' => 'openstation_content_graph_rest_permission', | |
| 54 | 56 | 'args' => array( |
| 57 | + // Deliberately no `default`: the dispatcher copies a | |
| 58 | + // registered default into the request before the | |
| 59 | + // callback runs, and `WP_REST_Request::has_param()` | |
| 60 | + // sees it, so a default here would make an omitted | |
| 61 | + // parameter indistinguishable from an explicitly empty | |
| 62 | + // one — and the callback tells those two apart. | |
| 55 | 63 | 'types' => array( |
| 56 | - 'description' => 'Comma-separated list of post type slugs to include.', | |
| 64 | + 'description' => 'Comma-separated list of post type slugs to include. Omit for every registered type; pass an empty value for none.', | |
| 57 | 65 | 'type' => 'string', |
| 58 | - 'default' => '', | |
| 59 | 66 | ), |
| 60 | 67 | ), |
| 61 | 68 | ) |
| 62 | 69 | ); |
| @@ -64,10 +71,10 @@ | ||
| 64 | 71 | 'desktop-mode/v1', |
| 65 | 72 | '/content-graph/post/(?P<id>\d+)', |
| 66 | 73 | array( |
| 67 | 74 | 'methods' => WP_REST_Server::READABLE, |
| 68 | - 'callback' => 'desktop_mode_content_graph_rest_post_detail', | |
| 69 | - 'permission_callback' => 'desktop_mode_content_graph_rest_permission', | |
| 75 | + 'callback' => 'openstation_content_graph_rest_post_detail', | |
| 76 | + 'permission_callback' => 'openstation_content_graph_rest_permission', | |
| 70 | 77 | 'args' => array( |
| 71 | 78 | 'id' => array( |
| 72 | 79 | 'type' => 'integer', |
| 73 | 80 | 'required' => true, |
| @@ -75,9 +82,9 @@ | ||
| 75 | 82 | ), |
| 76 | 83 | ) |
| 77 | 84 | ); |
| 78 | 85 | } |
| 79 | -add_action( 'rest_api_init', 'desktop_mode_content_graph_register_routes' ); | |
| 86 | +add_action( 'rest_api_init', 'openstation_content_graph_register_routes' ); | |
| 80 | 87 | |
| 81 | 88 | /** |
| 82 | 89 | * GET /post-types |
| 83 | 90 | * |
| @@ -82,10 +89,10 @@ | ||
| 82 | 89 | * GET /post-types |
| 83 | 90 | * |
| 84 | 91 | * @return WP_REST_Response |
| 85 | 92 | */ |
| 86 | -function desktop_mode_content_graph_rest_post_types() { | |
| 87 | - $types = desktop_mode_content_graph_post_types(); | |
| 93 | +function openstation_content_graph_rest_post_types() { | |
| 94 | + $types = openstation_content_graph_post_types(); | |
| 88 | 95 | $out = array(); |
| 89 | 96 | foreach ( $types as $entry ) { |
| 90 | 97 | $slug = isset( $entry['slug'] ) ? (string) $entry['slug'] : ''; |
| 91 | 98 | // 'readable' scopes the private bucket to posts the current |
| @@ -113,18 +120,23 @@ | ||
| 113 | 120 | |
| 114 | 121 | /** |
| 115 | 122 | * GET /nodes |
| 116 | 123 | * |
| 124 | + * An omitted `types` parameter selects every registered type; a | |
| 125 | + * present-but-empty one selects none. The route registers no default | |
| 126 | + * for the parameter so the two stay distinguishable through a real | |
| 127 | + * dispatch (see the route registration above). | |
| 128 | + * | |
| 117 | 129 | * @param WP_REST_Request $request |
| 118 | 130 | * @return WP_REST_Response |
| 119 | 131 | */ |
| 120 | -function desktop_mode_content_graph_rest_nodes( WP_REST_Request $request ) { | |
| 121 | - $raw = (string) $request->get_param( 'types' ); | |
| 122 | - $types = '' === $raw | |
| 123 | - ? wp_list_pluck( desktop_mode_content_graph_post_types(), 'slug' ) | |
| 124 | - : array_map( 'trim', explode( ',', $raw ) ); | |
| 125 | - $payload = desktop_mode_content_graph_build( (array) $types ); | |
| 126 | - return rest_ensure_response( desktop_mode_content_graph_filter_payload_for_user( $payload ) ); | |
| 132 | +function openstation_content_graph_rest_nodes( WP_REST_Request $request ) { | |
| 133 | + $raw = $request->get_param( 'types' ); | |
| 134 | + $types = $request->has_param( 'types' ) | |
| 135 | + ? array_map( 'trim', explode( ',', (string) $raw ) ) | |
| 136 | + : wp_list_pluck( openstation_content_graph_post_types(), 'slug' ); | |
| 137 | + $payload = openstation_content_graph_build( (array) $types ); | |
| 138 | + return rest_ensure_response( openstation_content_graph_filter_payload_for_user( $payload ) ); | |
| 127 | 139 | } |
| 128 | 140 | |
| 129 | 141 | /** |
| 130 | 142 | * Strip revision-derived data the current user may not see from a |
| @@ -137,12 +149,12 @@ | ||
| 137 | 149 | * were referenced only via stripped contributor ids are removed too. |
| 138 | 150 | * This runs at response time, not build time, because the cached |
| 139 | 151 | * payload is shared across users of the same privilege tier. |
| 140 | 152 | * |
| 141 | - * @param array $payload Payload from `desktop_mode_content_graph_build()`. | |
| 153 | + * @param array $payload Payload from `openstation_content_graph_build()`. | |
| 142 | 154 | * @return array |
| 143 | 155 | */ |
| 144 | -function desktop_mode_content_graph_filter_payload_for_user( array $payload ) { | |
| 156 | +function openstation_content_graph_filter_payload_for_user( array $payload ) { | |
| 145 | 157 | if ( empty( $payload['nodes'] ) || ! is_array( $payload['nodes'] ) ) { |
| 146 | 158 | return $payload; |
| 147 | 159 | } |
| 148 | 160 | |
| @@ -191,14 +203,14 @@ | ||
| 191 | 203 | * |
| 192 | 204 | * @param WP_REST_Request $request |
| 193 | 205 | * @return WP_REST_Response|WP_Error |
| 194 | 206 | */ |
| 195 | -function desktop_mode_content_graph_rest_post_detail( WP_REST_Request $request ) { | |
| 207 | +function openstation_content_graph_rest_post_detail( WP_REST_Request $request ) { | |
| 196 | 208 | $id = (int) $request['id']; |
| 197 | 209 | $post = $id > 0 ? get_post( $id ) : null; |
| 198 | 210 | if ( ! $post ) { |
| 199 | 211 | return new WP_Error( |
| 200 | - 'desktop_mode_content_graph_post_not_found', | |
| 212 | + 'openstation_content_graph_post_not_found', | |
| 201 | 213 | __( 'Post not found.', 'desktop-mode' ), |
| 202 | 214 | array( 'status' => 404 ) |
| 203 | 215 | ); |
| 204 | 216 | } |
| @@ -203,9 +215,9 @@ | ||
| 203 | 215 | ); |
| 204 | 216 | } |
| 205 | 217 | if ( ! current_user_can( 'read_post', $id ) ) { |
| 206 | 218 | return new WP_Error( |
| 207 | - 'desktop_mode_content_graph_forbidden', | |
| 219 | + 'openstation_content_graph_forbidden', | |
| 208 | 220 | __( 'Insufficient permissions.', 'desktop-mode' ), |
| 209 | 221 | array( 'status' => 403 ) |
| 210 | 222 | ); |
| 211 | 223 | } |
| @@ -214,14 +226,14 @@ | ||
| 214 | 226 | // edit-level data in core — wp/v2 only exposes revisions behind |
| 215 | 227 | // edit_post. Mirror that: readers get comment-author contributors |
| 216 | 228 | // only, no revision list. |
| 217 | 229 | $can_edit = current_user_can( 'edit_post', $post->ID ); |
| 218 | - $author = desktop_mode_content_graph_format_user( (int) $post->post_author ); | |
| 219 | - $contributors = desktop_mode_content_graph_collect_contributors( $post, $can_edit ); | |
| 220 | - $comments = desktop_mode_content_graph_collect_comments( $post ); | |
| 221 | - $categories = desktop_mode_content_graph_collect_terms( $post ); | |
| 222 | - $attached = desktop_mode_content_graph_collect_attached_media( $post ); | |
| 223 | - $revisions = $can_edit ? desktop_mode_content_graph_collect_revisions( $post ) : array(); | |
| 230 | + $author = openstation_content_graph_format_user( (int) $post->post_author ); | |
| 231 | + $contributors = openstation_content_graph_collect_contributors( $post, $can_edit ); | |
| 232 | + $comments = openstation_content_graph_collect_comments( $post ); | |
| 233 | + $categories = openstation_content_graph_collect_terms( $post ); | |
| 234 | + $attached = openstation_content_graph_collect_attached_media( $post ); | |
| 235 | + $revisions = $can_edit ? openstation_content_graph_collect_revisions( $post ) : array(); | |
| 224 | 236 | |
| 225 | 237 | return rest_ensure_response( |
| 226 | 238 | array( |
| 227 | 239 | 'post' => array( |
| @@ -250,9 +262,9 @@ | ||
| 250 | 262 | * |
| 251 | 263 | * @param int $user_id |
| 252 | 264 | * @return array|null |
| 253 | 265 | */ |
| 254 | -function desktop_mode_content_graph_format_user( $user_id ) { | |
| 266 | +function openstation_content_graph_format_user( $user_id ) { | |
| 255 | 267 | $user_id = (int) $user_id; |
| 256 | 268 | if ( $user_id <= 0 ) { |
| 257 | 269 | return null; |
| 258 | 270 | } |
| @@ -280,9 +292,9 @@ | ||
| 280 | 292 | * the post — revision authorship is edit-level data; |
| 281 | 293 | * approved comment authors are public either way. |
| 282 | 294 | * @return array[] |
| 283 | 295 | */ |
| 284 | -function desktop_mode_content_graph_collect_contributors( WP_Post $post, $include_revision_authors = true ) { | |
| 296 | +function openstation_content_graph_collect_contributors( WP_Post $post, $include_revision_authors = true ) { | |
| 285 | 297 | $author_id = (int) $post->post_author; |
| 286 | 298 | $ids = array(); |
| 287 | 299 | if ( $include_revision_authors ) { |
| 288 | 300 | $revs = wp_get_post_revisions( |
| @@ -313,9 +325,9 @@ | ||
| 313 | 325 | } |
| 314 | 326 | } |
| 315 | 327 | $out = array(); |
| 316 | 328 | foreach ( array_keys( $ids ) as $uid ) { |
| 317 | - $entry = desktop_mode_content_graph_format_user( $uid ); | |
| 329 | + $entry = openstation_content_graph_format_user( $uid ); | |
| 318 | 330 | if ( $entry ) { |
| 319 | 331 | $out[] = $entry; |
| 320 | 332 | } |
| 321 | 333 | } |
| @@ -327,9 +339,9 @@ | ||
| 327 | 339 | * |
| 328 | 340 | * @param WP_Post $post |
| 329 | 341 | * @return array[] |
| 330 | 342 | */ |
| 331 | -function desktop_mode_content_graph_collect_comments( WP_Post $post ) { | |
| 343 | +function openstation_content_graph_collect_comments( WP_Post $post ) { | |
| 332 | 344 | $comments = get_comments( |
| 333 | 345 | array( |
| 334 | 346 | 'post_id' => $post->ID, |
| 335 | 347 | 'status' => 'approve', |
| @@ -337,9 +349,9 @@ | ||
| 337 | 349 | 'orderby' => 'comment_date_gmt', |
| 338 | 350 | 'order' => 'DESC', |
| 339 | 351 | ) |
| 340 | 352 | ); |
| 341 | - $out = array(); | |
| 353 | + $out = array(); | |
| 342 | 354 | foreach ( $comments as $comment ) { |
| 343 | 355 | $out[] = array( |
| 344 | 356 | 'id' => (int) $comment->comment_ID, |
| 345 | 357 | 'author' => (string) $comment->comment_author, |
| @@ -358,9 +370,9 @@ | ||
| 358 | 370 | * |
| 359 | 371 | * @param WP_Post $post |
| 360 | 372 | * @return array[] |
| 361 | 373 | */ |
| 362 | -function desktop_mode_content_graph_collect_terms( WP_Post $post ) { | |
| 374 | +function openstation_content_graph_collect_terms( WP_Post $post ) { | |
| 363 | 375 | $taxes = get_object_taxonomies( $post->post_type, 'objects' ); |
| 364 | 376 | $out = array(); |
| 365 | 377 | foreach ( $taxes as $tax ) { |
| 366 | 378 | if ( ! $tax->public && ! $tax->show_ui ) { |
| @@ -391,9 +403,9 @@ | ||
| 391 | 403 | * |
| 392 | 404 | * @param WP_Post $post |
| 393 | 405 | * @return array[] |
| 394 | 406 | */ |
| 395 | -function desktop_mode_content_graph_collect_attached_media( WP_Post $post ) { | |
| 407 | +function openstation_content_graph_collect_attached_media( WP_Post $post ) { | |
| 396 | 408 | $attachments = get_attached_media( '', $post ); |
| 397 | 409 | $out = array(); |
| 398 | 410 | foreach ( $attachments as $att ) { |
| 399 | 411 | $out[] = array( |
| @@ -415,9 +427,9 @@ | ||
| 415 | 427 | * |
| 416 | 428 | * @param WP_Post $post |
| 417 | 429 | * @return array[] |
| 418 | 430 | */ |
| 419 | -function desktop_mode_content_graph_collect_revisions( WP_Post $post ) { | |
| 431 | +function openstation_content_graph_collect_revisions( WP_Post $post ) { | |
| 420 | 432 | $revs = wp_get_post_revisions( |
| 421 | 433 | $post->ID, |
| 422 | 434 | array( |
| 423 | 435 | 'posts_per_page' => 30, |
| @@ -427,9 +439,9 @@ | ||
| 427 | 439 | foreach ( $revs as $rev ) { |
| 428 | 440 | $out[] = array( |
| 429 | 441 | 'id' => (int) $rev->ID, |
| 430 | 442 | 'date' => mysql2date( 'c', $rev->post_date_gmt, false ), |
| 431 | - 'author' => desktop_mode_content_graph_format_user( (int) $rev->post_author ), | |
| 443 | + 'author' => openstation_content_graph_format_user( (int) $rev->post_author ), | |
| 432 | 444 | 'edit_url' => (string) admin_url( 'revision.php?revision=' . (int) $rev->ID ), |
| 433 | 445 | ); |
| 434 | 446 | } |
| 435 | 447 | return $out; |