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/my-wordpress/media-usage.php +46 -46 0.9.81.1.10 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * Desktop Mode — My WordPress: per-attachment "used in" endpoint.
3 + * OpenStation — My WordPress: per-attachment "used in" endpoint.
4 4 *
5 5 * `GET /desktop-mode/v1/media-usage/<id>` returns the list of public
6 6 * post-type entries that reference a given attachment, either as
7 7 * featured image (`_thumbnail_id` meta) or as an embed inside
@@ -27,9 +27,9 @@
27 27 * `read_post` gate runs on every request, so a cached scan can never
28 28 * leak rows across viewers with different capabilities. Cache is
29 29 * busted whenever any post is saved or deleted.
30 30 *
31 - * @package WPDesktopMode
31 + * @package OpenStation
32 32 */
33 33
34 34 defined( 'ABSPATH' ) || exit;
35 35
@@ -35,15 +35,15 @@
35 35
36 36 /**
37 37 * Register the route.
38 38 */
39 -function desktop_mode_my_wordpress_register_media_usage_route() {
39 +function openstation_my_wordpress_register_media_usage_route() {
40 40 register_rest_route(
41 41 'desktop-mode/v1',
42 42 '/media-usage/(?P<id>\d+)',
43 43 array(
44 44 'methods' => WP_REST_Server::READABLE,
45 - 'callback' => 'desktop_mode_my_wordpress_media_usage_callback',
45 + 'callback' => 'openstation_my_wordpress_media_usage_callback',
46 46 'permission_callback' => static function ( $request ) {
47 47 $id = (int) $request->get_param( 'id' );
48 48 if ( $id <= 0 ) {
49 49 return false;
@@ -63,9 +63,9 @@
63 63 ),
64 64 )
65 65 );
66 66 }
67 -add_action( 'rest_api_init', 'desktop_mode_my_wordpress_register_media_usage_route' );
67 +add_action( 'rest_api_init', 'openstation_my_wordpress_register_media_usage_route' );
68 68
69 69 /**
70 70 * Cache TTL (seconds). Filterable so sites that bulk-import media
71 71 * can shorten the window, or sites with stable libraries can
@@ -73,9 +73,9 @@
73 73 *
74 74 * @param int $attachment_id Attachment id.
75 75 * @return int
76 76 */
77 -function desktop_mode_my_wordpress_media_usage_ttl( $attachment_id ) {
77 +function openstation_my_wordpress_media_usage_ttl( $attachment_id ) {
78 78 /**
79 79 * Filter the media-usage transient TTL.
80 80 *
81 81 * @param int $seconds Default 300 (5 minutes).
@@ -80,9 +80,9 @@
80 80 *
81 81 * @param int $seconds Default 300 (5 minutes).
82 82 * @param int $attachment_id Attachment id the cache key is for.
83 83 */
84 - return (int) apply_filters( 'desktop_mode_my_wordpress_media_usage_cache_ttl', 300, $attachment_id );
84 + return (int) apply_filters( 'openstation_my_wordpress_media_usage_cache_ttl', 300, $attachment_id );
85 85 }
86 86
87 87 /**
88 88 * Capability buckets the cache namespaces over. A second-tier
@@ -91,9 +91,9 @@
91 91 * never go out of sync.
92 92 *
93 93 * @return string[]
94 94 */
95 -function desktop_mode_my_wordpress_media_usage_cache_buckets() {
95 +function openstation_my_wordpress_media_usage_cache_buckets() {
96 96 return array( 'edit', 'read' );
97 97 }
98 98
99 99 /**
@@ -101,9 +101,9 @@
101 101 * cache slot to read/write.
102 102 *
103 103 * @return string
104 104 */
105 -function desktop_mode_my_wordpress_media_usage_current_bucket() {
105 +function openstation_my_wordpress_media_usage_current_bucket() {
106 106 return current_user_can( 'edit_others_posts' ) ? 'edit' : 'read';
107 107 }
108 108
109 109 /**
@@ -117,11 +117,11 @@
117 117 * @param string $bucket Optional bucket override. Defaults to
118 118 * the current user's bucket.
119 119 * @return string
120 120 */
121 -function desktop_mode_my_wordpress_media_usage_cache_key( $attachment_id, $bucket = null ) {
121 +function openstation_my_wordpress_media_usage_cache_key( $attachment_id, $bucket = null ) {
122 122 if ( null === $bucket ) {
123 - $bucket = desktop_mode_my_wordpress_media_usage_current_bucket();
123 + $bucket = openstation_my_wordpress_media_usage_current_bucket();
124 124 }
125 125 return 'dm_media_usage_' . (int) $attachment_id . '_' . $bucket . '_v1';
126 126 }
127 127
@@ -130,27 +130,27 @@
130 130 *
131 131 * @param WP_REST_Request $request REST request.
132 132 * @return array|WP_Error
133 133 */
134 -function desktop_mode_my_wordpress_media_usage_callback( $request ) {
134 +function openstation_my_wordpress_media_usage_callback( $request ) {
135 135 $attachment_id = (int) $request->get_param( 'id' );
136 136 $attachment = get_post( $attachment_id );
137 137 if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
138 138 return new WP_Error(
139 - 'desktop_mode_media_not_found',
139 + 'openstation_media_not_found',
140 140 __( 'Attachment not found.', 'desktop-mode' ),
141 141 array( 'status' => 404 )
142 142 );
143 143 }
144 144
145 - $cache_key = desktop_mode_my_wordpress_media_usage_cache_key( $attachment_id );
145 + $cache_key = openstation_my_wordpress_media_usage_cache_key( $attachment_id );
146 146 $rows_by_post = get_transient( $cache_key );
147 147 if ( ! is_array( $rows_by_post ) ) {
148 - $rows_by_post = desktop_mode_my_wordpress_media_usage_collect( $attachment );
148 + $rows_by_post = openstation_my_wordpress_media_usage_collect( $attachment );
149 149 set_transient(
150 150 $cache_key,
151 151 $rows_by_post,
152 - desktop_mode_my_wordpress_media_usage_ttl( $attachment_id )
152 + openstation_my_wordpress_media_usage_ttl( $attachment_id )
153 153 );
154 154 }
155 155
156 156 /*
@@ -163,9 +163,9 @@
163 163 * heavy LIKE-scan portion stays cached. The base map only
164 164 * refreshes on the cache-bust events (save_post,
165 165 * before_delete_post, delete_attachment).
166 166 */
167 - $payload = desktop_mode_my_wordpress_media_usage_build( $attachment, $rows_by_post );
167 + $payload = openstation_my_wordpress_media_usage_build( $attachment, $rows_by_post );
168 168
169 169 /**
170 170 * Filter the media-usage payload before returning to the bundle.
171 171 * Plugins (ACF, page builders, Yoast image meta) can append rows
@@ -173,9 +173,9 @@
173 173 *
174 174 * @param array $payload Default payload.
175 175 * @param int $attachment_id Subject attachment id.
176 176 */
177 - return apply_filters( 'desktop_mode_my_wordpress_media_usage', $payload, $attachment_id );
177 + return apply_filters( 'openstation_my_wordpress_media_usage', $payload, $attachment_id );
178 178 }
179 179
180 180 /**
181 181 * Collect the viewer-independent reference map for an attachment:
@@ -181,9 +181,9 @@
181 181 * Collect the viewer-independent reference map for an attachment:
182 182 * post id => 'featured'|'content'. This is the heavy SQL portion of
183 183 * the payload and the ONLY part that gets transient-cached — the
184 184 * per-row `read_post` gate lives in
185 - * `desktop_mode_my_wordpress_media_usage_build()` and runs on every
185 + * `openstation_my_wordpress_media_usage_build()` and runs on every
186 186 * request, so a cached map can never leak rows across viewers with
187 187 * different capabilities.
188 188 *
189 189 * @param WP_Post $attachment Attachment post.
@@ -188,9 +188,9 @@
188 188 *
189 189 * @param WP_Post $attachment Attachment post.
190 190 * @return array<int,string> Map of post id => usedAs kind.
191 191 */
192 -function desktop_mode_my_wordpress_media_usage_collect( $attachment ) {
192 +function openstation_my_wordpress_media_usage_collect( $attachment ) {
193 193 global $wpdb;
194 194
195 195 $attachment_id = (int) $attachment->ID;
196 196 $file_url = (string) wp_get_attachment_url( $attachment_id );
@@ -236,10 +236,10 @@
236 236 $basename_variants[] = $m[1] . $m[2];
237 237 }
238 238 $basename_variants = array_values( array_unique( $basename_variants ) );
239 239
240 - $class_pattern = '%wp-image-' . $attachment_id . '%';
241 - $url_patterns = array();
240 + $class_pattern = '%wp-image-' . $attachment_id . '%';
241 + $url_patterns = array();
242 242 foreach ( $basename_variants as $variant ) {
243 243 $url_patterns[] = '%' . $wpdb->esc_like( $variant ) . '%';
244 244 }
245 245
@@ -318,9 +318,9 @@
318 318 * @param array<int,string>|null $rows_by_post Optional precollected map of
319 319 * post id => usedAs kind.
320 320 * @return array
321 321 */
322 -function desktop_mode_my_wordpress_media_usage_build( $attachment, $rows_by_post = null ) {
322 +function openstation_my_wordpress_media_usage_build( $attachment, $rows_by_post = null ) {
323 323 $attachment_id = (int) $attachment->ID;
324 324 $file_url = (string) wp_get_attachment_url( $attachment_id );
325 325 $file_basename = '' !== $file_url ? wp_basename( $file_url ) : '';
326 326
@@ -338,9 +338,9 @@
338 338 ),
339 339 );
340 340
341 341 if ( ! is_array( $rows_by_post ) ) {
342 - $rows_by_post = desktop_mode_my_wordpress_media_usage_collect( $attachment );
342 + $rows_by_post = openstation_my_wordpress_media_usage_collect( $attachment );
343 343 }
344 344
345 345 $public_types = array_values( get_post_types( array( 'public' => true ), 'names' ) );
346 346 // Filter out `attachment` from the search — attachments don't
@@ -407,9 +407,9 @@
407 407 * attachment N stale until the TTL expires.
408 408 *
409 409 * @var array<int,array<int,true>>
410 410 */
411 -$GLOBALS['desktop_mode_media_usage_pre_save_refs'] = array();
411 +$GLOBALS['openstation_media_usage_pre_save_refs'] = array();
412 412
413 413 /**
414 414 * Extract attachment ids referenced by a post — featured image plus
415 415 * everything resolvable from `post_content`. Defers to the canonical
@@ -416,9 +416,9 @@
416 416 * resolver in `attached-media.php` so this buster catches the same
417 417 * cases the REST field does (block-class scan, classic `[caption]`
418 418 * shortcodes, `data-id` / `data-attachment-id`, and raw `<img src>`
419 419 * URL resolution including `-scaled.jpg` ↔ original swaps), plus
420 - * any ids appended via the `desktop_mode_my_wordpress_attached_media`
420 + * any ids appended via the `openstation_my_wordpress_attached_media`
421 421 * filter (ACF, page builders, post-meta galleries). Without this
422 422 * delegation, editing a post to add or remove a raw URL embed
423 423 * wouldn't bust the affected attachment's media-usage cache until
424 424 * the TTL expired.
@@ -425,15 +425,15 @@
425 425 *
426 426 * @param int|WP_Post $post Post id or object.
427 427 * @return array<int,true> Set of attachment ids keyed for dedup.
428 428 */
429 -function desktop_mode_my_wordpress_media_usage_extract_refs( $post ) {
429 +function openstation_my_wordpress_media_usage_extract_refs( $post ) {
430 430 $ids = array();
431 431 $obj = is_object( $post ) ? $post : get_post( (int) $post );
432 432 if ( ! $obj || ! isset( $obj->ID ) ) {
433 433 return $ids;
434 434 }
435 - if ( ! function_exists( 'desktop_mode_my_wordpress_post_attached_media' ) ) {
435 + if ( ! function_exists( 'openstation_my_wordpress_post_attached_media' ) ) {
436 436 // Defensive: bootstrap order should always load
437 437 // attached-media.php before this can be called from a save
438 438 // hook, but fall back to the legacy minimal scan just in
439 439 // case so the buster never silently no-ops.
@@ -448,9 +448,9 @@
448 448 }
449 449 }
450 450 return $ids;
451 451 }
452 - foreach ( desktop_mode_my_wordpress_post_attached_media( (int) $obj->ID ) as $id ) {
452 + foreach ( openstation_my_wordpress_post_attached_media( (int) $obj->ID ) as $id ) {
453 453 $id = (int) $id;
454 454 if ( $id > 0 ) {
455 455 $ids[ $id ] = true;
456 456 }
@@ -465,17 +465,17 @@
465 465 * in a per-request global and read it back in the `save_post` hook.
466 466 *
467 467 * @param int $post_id Post id about to be updated.
468 468 */
469 -function desktop_mode_my_wordpress_media_usage_snapshot_pre_save( $post_id ) {
469 +function openstation_my_wordpress_media_usage_snapshot_pre_save( $post_id ) {
470 470 $post_id = (int) $post_id;
471 471 if ( $post_id <= 0 ) {
472 472 return;
473 473 }
474 - $GLOBALS['desktop_mode_media_usage_pre_save_refs'][ $post_id ] =
475 - desktop_mode_my_wordpress_media_usage_extract_refs( $post_id );
474 + $GLOBALS['openstation_media_usage_pre_save_refs'][ $post_id ] =
475 + openstation_my_wordpress_media_usage_extract_refs( $post_id );
476 476 }
477 -add_action( 'pre_post_update', 'desktop_mode_my_wordpress_media_usage_snapshot_pre_save' );
477 +add_action( 'pre_post_update', 'openstation_my_wordpress_media_usage_snapshot_pre_save' );
478 478
479 479 /**
480 480 * Bust the transient when a post changes. The cache key is
481 481 * per-attachment, so we don't know which entries reference what —
@@ -491,30 +491,30 @@
491 491 * version of the content + the post's `_thumbnail_id`.
492 492 *
493 493 * @param int $post_id Post id that was just modified.
494 494 */
495 -function desktop_mode_my_wordpress_media_usage_bust_for_post( $post_id ) {
495 +function openstation_my_wordpress_media_usage_bust_for_post( $post_id ) {
496 496 $post_id = (int) $post_id;
497 497 if ( $post_id <= 0 ) {
498 498 return;
499 499 }
500 500
501 - $ids = desktop_mode_my_wordpress_media_usage_extract_refs( $post_id );
501 + $ids = openstation_my_wordpress_media_usage_extract_refs( $post_id );
502 502
503 - if ( isset( $GLOBALS['desktop_mode_media_usage_pre_save_refs'][ $post_id ] ) ) {
504 - $ids += $GLOBALS['desktop_mode_media_usage_pre_save_refs'][ $post_id ];
505 - unset( $GLOBALS['desktop_mode_media_usage_pre_save_refs'][ $post_id ] );
503 + if ( isset( $GLOBALS['openstation_media_usage_pre_save_refs'][ $post_id ] ) ) {
504 + $ids += $GLOBALS['openstation_media_usage_pre_save_refs'][ $post_id ];
505 + unset( $GLOBALS['openstation_media_usage_pre_save_refs'][ $post_id ] );
506 506 }
507 507
508 508 foreach ( array_keys( $ids ) as $attachment_id ) {
509 - foreach ( desktop_mode_my_wordpress_media_usage_cache_buckets() as $bucket ) {
509 + foreach ( openstation_my_wordpress_media_usage_cache_buckets() as $bucket ) {
510 510 delete_transient(
511 - desktop_mode_my_wordpress_media_usage_cache_key( (int) $attachment_id, $bucket )
511 + openstation_my_wordpress_media_usage_cache_key( (int) $attachment_id, $bucket )
512 512 );
513 513 }
514 514 }
515 515 }
516 -add_action( 'save_post', 'desktop_mode_my_wordpress_media_usage_bust_for_post' );
516 +add_action( 'save_post', 'openstation_my_wordpress_media_usage_bust_for_post' );
517 517 // `before_delete_post`, NOT `deleted_post`. By the time `deleted_post`
518 518 // fires, `delete_all_meta_for_post` has already wiped `_thumbnail_id`
519 519 // and the row itself is gone — `extract_refs()` would return an empty
520 520 // set, so the cache for any referenced attachment would survive until
@@ -520,9 +520,9 @@
520 520 // set, so the cache for any referenced attachment would survive until
521 521 // its 5-minute TTL. `before_delete_post` fires while the post + meta
522 522 // are still readable. Signature matches (we only consume the first
523 523 // arg, the post id).
524 -add_action( 'before_delete_post', 'desktop_mode_my_wordpress_media_usage_bust_for_post' );
524 +add_action( 'before_delete_post', 'openstation_my_wordpress_media_usage_bust_for_post' );
525 525 // New posts skip `pre_post_update` but still go through `save_post`,
526 526 // so the buster works as-is — the pre-snapshot is just empty.
527 527
528 528 /**
@@ -529,16 +529,16 @@
529 529 * Bust the transient when the attachment itself is deleted.
530 530 *
531 531 * @param int $post_id Attachment id.
532 532 */
533 -function desktop_mode_my_wordpress_media_usage_bust_for_attachment( $post_id ) {
533 +function openstation_my_wordpress_media_usage_bust_for_attachment( $post_id ) {
534 534 $post_id = (int) $post_id;
535 535 if ( $post_id <= 0 ) {
536 536 return;
537 537 }
538 - foreach ( desktop_mode_my_wordpress_media_usage_cache_buckets() as $bucket ) {
538 + foreach ( openstation_my_wordpress_media_usage_cache_buckets() as $bucket ) {
539 539 delete_transient(
540 - desktop_mode_my_wordpress_media_usage_cache_key( $post_id, $bucket )
540 + openstation_my_wordpress_media_usage_cache_key( $post_id, $bucket )
541 541 );
542 542 }
543 543 }
544 -add_action( 'delete_attachment', 'desktop_mode_my_wordpress_media_usage_bust_for_attachment' );
544 +add_action( 'delete_attachment', 'openstation_my_wordpress_media_usage_bust_for_attachment' );