| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — My WordPress: per-post attached-media REST field. | |
| 3 | + * OpenStation — My WordPress: per-post attached-media REST field. | |
| 4 | 4 | * |
| 5 | - * Registers `desktop_mode_attached_media` on every public post | |
| 5 | + * Registers `openstation_attached_media` on every public post | |
| 6 | 6 | * type. Returns the canonical set of attachment ids referenced by |
| 7 | 7 | * the post — featured image + everything in `post_content` — |
| 8 | 8 | * computed server-side where `attachment_url_to_postid()` is |
| 9 | 9 | * available. |
| @@ -20,28 +20,23 @@ | ||
| 20 | 20 | * |
| 21 | 21 | * Server has the full post object + WP core's URL→id resolver, so |
| 22 | 22 | * one round-trip per detail-view fetches an authoritative list. |
| 23 | 23 | * |
| 24 | - * Filterable via `desktop_mode_my_wordpress_attached_media` for | |
| 24 | + * Filterable via `openstation_my_wordpress_attached_media` for | |
| 25 | 25 | * plugins (page builders, ACF, custom field handlers) to append |
| 26 | 26 | * their own references. |
| 27 | 27 | * |
| 28 | - * @package WPDesktopMode | |
| 28 | + * @package OpenStation | |
| 29 | 29 | */ |
| 30 | 30 | |
| 31 | 31 | defined( 'ABSPATH' ) || exit; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | - * Register `desktop_mode_attached_media` on every public post type. | |
| 34 | + * Register `openstation_attached_media` on every post type the site | |
| 35 | + * window can browse. | |
| 35 | 36 | */ |
| 36 | -function desktop_mode_my_wordpress_register_attached_media_field() { | |
| 37 | - $types = get_post_types( | |
| 38 | - array( | |
| 39 | - 'show_in_rest' => true, | |
| 40 | - 'public' => true, | |
| 41 | - ), | |
| 42 | - 'names' | |
| 43 | - ); | |
| 37 | +function openstation_my_wordpress_register_attached_media_field() { | |
| 38 | + $types = openstation_my_wordpress_rest_field_post_types(); | |
| 44 | 39 | foreach ( $types as $type ) { |
| 45 | 40 | if ( 'attachment' === $type ) { |
| 46 | 41 | continue; |
| 47 | 42 | } |
| @@ -46,13 +41,13 @@ | ||
| 46 | 41 | continue; |
| 47 | 42 | } |
| 48 | 43 | register_rest_field( |
| 49 | 44 | $type, |
| 50 | - 'desktop_mode_attached_media', | |
| 45 | + 'openstation_attached_media', | |
| 51 | 46 | array( |
| 52 | 47 | 'get_callback' => static function ( $post ) { |
| 53 | 48 | $id = isset( $post['id'] ) ? (int) $post['id'] : 0; |
| 54 | - return desktop_mode_my_wordpress_post_attached_media( $id ); | |
| 49 | + return openstation_my_wordpress_post_attached_media( $id ); | |
| 55 | 50 | }, |
| 56 | 51 | 'schema' => array( |
| 57 | 52 | 'description' => __( 'Attachment ids referenced by this post — featured image plus every attachment found in post_content (block-class scan + raw `<img src>` URL resolution).', 'desktop-mode' ), |
| 58 | 53 | 'type' => 'array', |
| @@ -63,9 +58,9 @@ | ||
| 63 | 58 | ) |
| 64 | 59 | ); |
| 65 | 60 | } |
| 66 | 61 | } |
| 67 | -add_action( 'rest_api_init', 'desktop_mode_my_wordpress_register_attached_media_field' ); | |
| 62 | +add_action( 'rest_api_init', 'openstation_my_wordpress_register_attached_media_field' ); | |
| 68 | 63 | |
| 69 | 64 | /** |
| 70 | 65 | * Resolve every attachment referenced by `$post_id`. Featured |
| 71 | 66 | * image + a multi-pass scan of `post_content`: |
| @@ -80,9 +75,9 @@ | ||
| 80 | 75 | * |
| 81 | 76 | * @param int $post_id Post id. |
| 82 | 77 | * @return int[] Attachment ids, deduped, no order guarantee. |
| 83 | 78 | */ |
| 84 | -function desktop_mode_my_wordpress_post_attached_media( $post_id ) { | |
| 79 | +function openstation_my_wordpress_post_attached_media( $post_id ) { | |
| 85 | 80 | $post_id = (int) $post_id; |
| 86 | 81 | if ( $post_id <= 0 ) { |
| 87 | 82 | return array(); |
| 88 | 83 | } |
| @@ -129,9 +124,9 @@ | ||
| 129 | 124 | if ( '' === $url || isset( $seen_urls[ $url ] ) ) { |
| 130 | 125 | continue; |
| 131 | 126 | } |
| 132 | 127 | $seen_urls[ $url ] = true; |
| 133 | - $resolved = desktop_mode_my_wordpress_resolve_attachment_url( $url ); | |
| 128 | + $resolved = openstation_my_wordpress_resolve_attachment_url( $url ); | |
| 134 | 129 | if ( $resolved > 0 ) { |
| 135 | 130 | $ids[ $resolved ] = true; |
| 136 | 131 | } |
| 137 | 132 | } |
| @@ -149,9 +144,9 @@ | ||
| 149 | 144 | * |
| 150 | 145 | * @param int[] $out Attachment ids resolved by the core scan. |
| 151 | 146 | * @param int $post_id Subject post id. |
| 152 | 147 | */ |
| 153 | - $filtered = apply_filters( 'desktop_mode_my_wordpress_attached_media', $out, $post_id ); | |
| 148 | + $filtered = apply_filters( 'openstation_my_wordpress_attached_media', $out, $post_id ); | |
| 154 | 149 | |
| 155 | 150 | if ( ! is_array( $filtered ) ) { |
| 156 | 151 | return $out; |
| 157 | 152 | } |
| @@ -174,11 +169,11 @@ | ||
| 174 | 169 | * |
| 175 | 170 | * @param string $url Image URL pulled from `<img src>`. |
| 176 | 171 | * @return int Attachment id, or 0 when no match. |
| 177 | 172 | */ |
| 178 | -function desktop_mode_my_wordpress_resolve_attachment_url( $url ) { | |
| 173 | +function openstation_my_wordpress_resolve_attachment_url( $url ) { | |
| 179 | 174 | static $cache = array(); |
| 180 | - $url = (string) $url; | |
| 175 | + $url = (string) $url; | |
| 181 | 176 | if ( '' === $url ) { |
| 182 | 177 | return 0; |
| 183 | 178 | } |
| 184 | 179 | if ( isset( $cache[ $url ] ) ) { |
| @@ -186,16 +181,16 @@ | ||
| 186 | 181 | } |
| 187 | 182 | // `attachment_url_to_postid()` does a literal `_wp_attached_file` |
| 188 | 183 | // meta lookup — it doesn't account for WP-generated variants: |
| 189 | 184 | // |
| 190 | - // - Sized intermediates `image-300x200.jpg` — strip `-WxH`. | |
| 191 | - // - `-scaled.jpg` WP autogenerates this for uploads | |
| 192 | - // past the big-image threshold and | |
| 193 | - // stores it as `_wp_attached_file`, | |
| 194 | - // while editors emit the original | |
| 195 | - // URL in `<img src>`. Try BOTH the | |
| 196 | - // scaled→original strip AND the | |
| 197 | - // original→scaled append. | |
| 185 | + // - Sized intermediates `image-300x200.jpg` — strip `-WxH`. | |
| 186 | + // - `-scaled.jpg` WP autogenerates this for uploads | |
| 187 | + // past the big-image threshold and | |
| 188 | + // stores it as `_wp_attached_file`, | |
| 189 | + // while editors emit the original | |
| 190 | + // URL in `<img src>`. Try BOTH the | |
| 191 | + // scaled→original strip AND the | |
| 192 | + // original→scaled append. | |
| 198 | 193 | // |
| 199 | 194 | // Strip query strings from every candidate so cache-buster URLs |
| 200 | 195 | // (`?ver=…`) still resolve. |
| 201 | 196 | $strip_query = static function ( $u ) { |
| @@ -202,9 +197,9 @@ | ||
| 202 | 197 | $pos = strpos( $u, '?' ); |
| 203 | 198 | return false === $pos ? $u : substr( $u, 0, $pos ); |
| 204 | 199 | }; |
| 205 | 200 | |
| 206 | - $clean = $strip_query( $url ); | |
| 201 | + $clean = $strip_query( $url ); | |
| 207 | 202 | $candidates = array( $clean ); |
| 208 | 203 | |
| 209 | 204 | // `image-300x200.jpg` → `image.jpg` |
| 210 | 205 | if ( preg_match( '/^(.*)-\d+x\d+(\.[a-zA-Z0-9]+)$/', $clean, $m ) ) { |