PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
← All changes | includes/content-graph/rest.php +56 -69 0.9.61.1.10 View file →
@@ -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,10 +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
21 - * @since 0.8.2
22 + * @package OpenStation
22 23 */
23 24
24 25 defined( 'ABSPATH' ) || exit;
25 26
@@ -25,29 +26,25 @@
25 26
26 27 /**
27 28 * Capability check shared across every endpoint.
28 29 *
29 - * @since 0.8.2
30 - *
31 30 * @return bool
32 31 */
33 -function desktop_mode_content_graph_rest_permission() {
34 - return desktop_mode_content_graph_user_can_use();
32 +function openstation_content_graph_rest_permission() {
33 + return openstation_content_graph_user_can_use();
35 34 }
36 35
37 36 /**
38 37 * Register the routes.
39 - *
40 - * @since 0.8.2
41 38 */
42 -function desktop_mode_content_graph_register_routes() {
39 +function openstation_content_graph_register_routes() {
43 40 register_rest_route(
44 41 'desktop-mode/v1',
45 42 '/content-graph/post-types',
46 43 array(
47 44 'methods' => WP_REST_Server::READABLE,
48 - 'callback' => 'desktop_mode_content_graph_rest_post_types',
49 - 'permission_callback' => 'desktop_mode_content_graph_rest_permission',
45 + 'callback' => 'openstation_content_graph_rest_post_types',
46 + 'permission_callback' => 'openstation_content_graph_rest_permission',
50 47 )
51 48 );
52 49 register_rest_route(
53 50 'desktop-mode/v1',
@@ -53,15 +50,20 @@
53 50 'desktop-mode/v1',
54 51 '/content-graph/nodes',
55 52 array(
56 53 'methods' => WP_REST_Server::READABLE,
57 - 'callback' => 'desktop_mode_content_graph_rest_nodes',
58 - 'permission_callback' => 'desktop_mode_content_graph_rest_permission',
54 + 'callback' => 'openstation_content_graph_rest_nodes',
55 + 'permission_callback' => 'openstation_content_graph_rest_permission',
59 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.
60 63 'types' => array(
61 - '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.',
62 65 'type' => 'string',
63 - 'default' => '',
64 66 ),
65 67 ),
66 68 )
67 69 );
@@ -69,10 +71,10 @@
69 71 'desktop-mode/v1',
70 72 '/content-graph/post/(?P<id>\d+)',
71 73 array(
72 74 'methods' => WP_REST_Server::READABLE,
73 - 'callback' => 'desktop_mode_content_graph_rest_post_detail',
74 - 'permission_callback' => 'desktop_mode_content_graph_rest_permission',
75 + 'callback' => 'openstation_content_graph_rest_post_detail',
76 + 'permission_callback' => 'openstation_content_graph_rest_permission',
75 77 'args' => array(
76 78 'id' => array(
77 79 'type' => 'integer',
78 80 'required' => true,
@@ -80,19 +82,17 @@
80 82 ),
81 83 )
82 84 );
83 85 }
84 -add_action( 'rest_api_init', 'desktop_mode_content_graph_register_routes' );
86 +add_action( 'rest_api_init', 'openstation_content_graph_register_routes' );
85 87
86 88 /**
87 89 * GET /post-types
88 90 *
89 - * @since 0.8.2
90 - *
91 91 * @return WP_REST_Response
92 92 */
93 -function desktop_mode_content_graph_rest_post_types() {
94 - $types = desktop_mode_content_graph_post_types();
93 +function openstation_content_graph_rest_post_types() {
94 + $types = openstation_content_graph_post_types();
95 95 $out = array();
96 96 foreach ( $types as $entry ) {
97 97 $slug = isset( $entry['slug'] ) ? (string) $entry['slug'] : '';
98 98 // 'readable' scopes the private bucket to posts the current
@@ -120,20 +120,23 @@
120 120
121 121 /**
122 122 * GET /nodes
123 123 *
124 - * @since 0.8.2
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).
125 128 *
126 129 * @param WP_REST_Request $request
127 130 * @return WP_REST_Response
128 131 */
129 -function desktop_mode_content_graph_rest_nodes( WP_REST_Request $request ) {
130 - $raw = (string) $request->get_param( 'types' );
131 - $types = '' === $raw
132 - ? wp_list_pluck( desktop_mode_content_graph_post_types(), 'slug' )
133 - : array_map( 'trim', explode( ',', $raw ) );
134 - $payload = desktop_mode_content_graph_build( (array) $types );
135 - 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 ) );
136 139 }
137 140
138 141 /**
139 142 * Strip revision-derived data the current user may not see from a
@@ -146,14 +149,12 @@
146 149 * were referenced only via stripped contributor ids are removed too.
147 150 * This runs at response time, not build time, because the cached
148 151 * payload is shared across users of the same privilege tier.
149 152 *
150 - * @since 0.9.2
151 - *
152 - * @param array $payload Payload from `desktop_mode_content_graph_build()`.
153 + * @param array $payload Payload from `openstation_content_graph_build()`.
153 154 * @return array
154 155 */
155 -function desktop_mode_content_graph_filter_payload_for_user( array $payload ) {
156 +function openstation_content_graph_filter_payload_for_user( array $payload ) {
156 157 if ( empty( $payload['nodes'] ) || ! is_array( $payload['nodes'] ) ) {
157 158 return $payload;
158 159 }
159 160
@@ -199,19 +200,17 @@
199 200
200 201 /**
201 202 * GET /post/<id>
202 203 *
203 - * @since 0.8.2
204 - *
205 204 * @param WP_REST_Request $request
206 205 * @return WP_REST_Response|WP_Error
207 206 */
208 -function desktop_mode_content_graph_rest_post_detail( WP_REST_Request $request ) {
207 +function openstation_content_graph_rest_post_detail( WP_REST_Request $request ) {
209 208 $id = (int) $request['id'];
210 209 $post = $id > 0 ? get_post( $id ) : null;
211 210 if ( ! $post ) {
212 211 return new WP_Error(
213 - 'desktop_mode_content_graph_post_not_found',
212 + 'openstation_content_graph_post_not_found',
214 213 __( 'Post not found.', 'desktop-mode' ),
215 214 array( 'status' => 404 )
216 215 );
217 216 }
@@ -216,9 +215,9 @@
216 215 );
217 216 }
218 217 if ( ! current_user_can( 'read_post', $id ) ) {
219 218 return new WP_Error(
220 - 'desktop_mode_content_graph_forbidden',
219 + 'openstation_content_graph_forbidden',
221 220 __( 'Insufficient permissions.', 'desktop-mode' ),
222 221 array( 'status' => 403 )
223 222 );
224 223 }
@@ -227,14 +226,14 @@
227 226 // edit-level data in core — wp/v2 only exposes revisions behind
228 227 // edit_post. Mirror that: readers get comment-author contributors
229 228 // only, no revision list.
230 229 $can_edit = current_user_can( 'edit_post', $post->ID );
231 - $author = desktop_mode_content_graph_format_user( (int) $post->post_author );
232 - $contributors = desktop_mode_content_graph_collect_contributors( $post, $can_edit );
233 - $comments = desktop_mode_content_graph_collect_comments( $post );
234 - $categories = desktop_mode_content_graph_collect_terms( $post );
235 - $attached = desktop_mode_content_graph_collect_attached_media( $post );
236 - $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();
237 236
238 237 return rest_ensure_response(
239 238 array(
240 239 'post' => array(
@@ -260,14 +259,12 @@
260 259
261 260 /**
262 261 * Format a user record for the side panel.
263 262 *
264 - * @since 0.8.2
265 - *
266 263 * @param int $user_id
267 264 * @return array|null
268 265 */
269 -function desktop_mode_content_graph_format_user( $user_id ) {
266 +function openstation_content_graph_format_user( $user_id ) {
270 267 $user_id = (int) $user_id;
271 268 if ( $user_id <= 0 ) {
272 269 return null;
273 270 }
@@ -288,10 +285,8 @@
288 285 * Collect contributors: distinct revision authors (excluding the
289 286 * current author) plus distinct comment authors who have a
290 287 * registered user account.
291 288 *
292 - * @since 0.8.2
293 - *
294 289 * @param WP_Post $post
295 290 * @param bool $include_revision_authors Whether to include revision
296 291 * authors. Pass false for users who cannot `edit_post`
297 292 * the post — revision authorship is edit-level data;
@@ -297,9 +292,9 @@
297 292 * the post — revision authorship is edit-level data;
298 293 * approved comment authors are public either way.
299 294 * @return array[]
300 295 */
301 -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 ) {
302 297 $author_id = (int) $post->post_author;
303 298 $ids = array();
304 299 if ( $include_revision_authors ) {
305 300 $revs = wp_get_post_revisions(
@@ -330,9 +325,9 @@
330 325 }
331 326 }
332 327 $out = array();
333 328 foreach ( array_keys( $ids ) as $uid ) {
334 - $entry = desktop_mode_content_graph_format_user( $uid );
329 + $entry = openstation_content_graph_format_user( $uid );
335 330 if ( $entry ) {
336 331 $out[] = $entry;
337 332 }
338 333 }
@@ -341,14 +336,12 @@
341 336
342 337 /**
343 338 * Collect approved comments (most recent first, capped at 50).
344 339 *
345 - * @since 0.8.2
346 - *
347 340 * @param WP_Post $post
348 341 * @return array[]
349 342 */
350 -function desktop_mode_content_graph_collect_comments( WP_Post $post ) {
343 +function openstation_content_graph_collect_comments( WP_Post $post ) {
351 344 $comments = get_comments(
352 345 array(
353 346 'post_id' => $post->ID,
354 347 'status' => 'approve',
@@ -356,9 +349,9 @@
356 349 'orderby' => 'comment_date_gmt',
357 350 'order' => 'DESC',
358 351 )
359 352 );
360 - $out = array();
353 + $out = array();
361 354 foreach ( $comments as $comment ) {
362 355 $out[] = array(
363 356 'id' => (int) $comment->comment_ID,
364 357 'author' => (string) $comment->comment_author,
@@ -374,14 +367,12 @@
374 367 /**
375 368 * Collect every taxonomy term attached to the post (categories, tags,
376 369 * and any custom taxonomy registered for the post type).
377 370 *
378 - * @since 0.8.2
379 - *
380 371 * @param WP_Post $post
381 372 * @return array[]
382 373 */
383 -function desktop_mode_content_graph_collect_terms( WP_Post $post ) {
374 +function openstation_content_graph_collect_terms( WP_Post $post ) {
384 375 $taxes = get_object_taxonomies( $post->post_type, 'objects' );
385 376 $out = array();
386 377 foreach ( $taxes as $tax ) {
387 378 if ( ! $tax->public && ! $tax->show_ui ) {
@@ -409,14 +400,12 @@
409 400 /**
410 401 * Collect attached media (anything with this post as its `post_parent`)
411 402 * plus any media referenced from a `wp:image` block. Returns up to 50.
412 403 *
413 - * @since 0.8.2
414 - *
415 404 * @param WP_Post $post
416 405 * @return array[]
417 406 */
418 -function desktop_mode_content_graph_collect_attached_media( WP_Post $post ) {
407 +function openstation_content_graph_collect_attached_media( WP_Post $post ) {
419 408 $attachments = get_attached_media( '', $post );
420 409 $out = array();
421 410 foreach ( $attachments as $att ) {
422 411 $out[] = array(
@@ -435,14 +424,12 @@
435 424
436 425 /**
437 426 * Collect post revisions (most recent first, capped at 30).
438 427 *
439 - * @since 0.8.2
440 - *
441 428 * @param WP_Post $post
442 429 * @return array[]
443 430 */
444 -function desktop_mode_content_graph_collect_revisions( WP_Post $post ) {
431 +function openstation_content_graph_collect_revisions( WP_Post $post ) {
445 432 $revs = wp_get_post_revisions(
446 433 $post->ID,
447 434 array(
448 435 'posts_per_page' => 30,
@@ -452,9 +439,9 @@
452 439 foreach ( $revs as $rev ) {
453 440 $out[] = array(
454 441 'id' => (int) $rev->ID,
455 442 'date' => mysql2date( 'c', $rev->post_date_gmt, false ),
456 - 'author' => desktop_mode_content_graph_format_user( (int) $rev->post_author ),
443 + 'author' => openstation_content_graph_format_user( (int) $rev->post_author ),
457 444 'edit_url' => (string) admin_url( 'revision.php?revision=' . (int) $rev->ID ),
458 445 );
459 446 }
460 447 return $out;