| @@ -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,31 +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 | |
| 29 | - * @since 0.21.0 | |
| 28 | + * @package OpenStation | |
| 30 | 29 | */ |
| 31 | 30 | |
| 32 | 31 | defined( 'ABSPATH' ) || exit; |
| 33 | 32 | |
| 34 | 33 | /** |
| 35 | - * Register `desktop_mode_attached_media` on every public post type. | |
| 36 | - * | |
| 37 | - * @since 0.21.0 | |
| 34 | + * Register `openstation_attached_media` on every post type the site | |
| 35 | + * window can browse. | |
| 38 | 36 | */ |
| 39 | -function desktop_mode_my_wordpress_register_attached_media_field() { | |
| 40 | - $types = get_post_types( | |
| 41 | - array( | |
| 42 | - 'show_in_rest' => true, | |
| 43 | - 'public' => true, | |
| 44 | - ), | |
| 45 | - 'names' | |
| 46 | - ); | |
| 37 | +function openstation_my_wordpress_register_attached_media_field() { | |
| 38 | + $types = openstation_my_wordpress_rest_field_post_types(); | |
| 47 | 39 | foreach ( $types as $type ) { |
| 48 | 40 | if ( 'attachment' === $type ) { |
| 49 | 41 | continue; |
| 50 | 42 | } |
| @@ -49,13 +41,13 @@ | ||
| 49 | 41 | continue; |
| 50 | 42 | } |
| 51 | 43 | register_rest_field( |
| 52 | 44 | $type, |
| 53 | - 'desktop_mode_attached_media', | |
| 45 | + 'openstation_attached_media', | |
| 54 | 46 | array( |
| 55 | 47 | 'get_callback' => static function ( $post ) { |
| 56 | 48 | $id = isset( $post['id'] ) ? (int) $post['id'] : 0; |
| 57 | - return desktop_mode_my_wordpress_post_attached_media( $id ); | |
| 49 | + return openstation_my_wordpress_post_attached_media( $id ); | |
| 58 | 50 | }, |
| 59 | 51 | 'schema' => array( |
| 60 | 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' ), |
| 61 | 53 | 'type' => 'array', |
| @@ -66,9 +58,9 @@ | ||
| 66 | 58 | ) |
| 67 | 59 | ); |
| 68 | 60 | } |
| 69 | 61 | } |
| 70 | -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' ); | |
| 71 | 63 | |
| 72 | 64 | /** |
| 73 | 65 | * Resolve every attachment referenced by `$post_id`. Featured |
| 74 | 66 | * image + a multi-pass scan of `post_content`: |
| @@ -80,14 +72,12 @@ | ||
| 80 | 72 | * 4. Raw `<img src="…">` URLs resolved via `attachment_url_to_postid()`. |
| 81 | 73 | * Resolver results are cached in-process so a post embedding |
| 82 | 74 | * the same image twice only takes one DB hit per URL. |
| 83 | 75 | * |
| 84 | - * @since 0.21.0 | |
| 85 | - * | |
| 86 | 76 | * @param int $post_id Post id. |
| 87 | 77 | * @return int[] Attachment ids, deduped, no order guarantee. |
| 88 | 78 | */ |
| 89 | -function desktop_mode_my_wordpress_post_attached_media( $post_id ) { | |
| 79 | +function openstation_my_wordpress_post_attached_media( $post_id ) { | |
| 90 | 80 | $post_id = (int) $post_id; |
| 91 | 81 | if ( $post_id <= 0 ) { |
| 92 | 82 | return array(); |
| 93 | 83 | } |
| @@ -134,9 +124,9 @@ | ||
| 134 | 124 | if ( '' === $url || isset( $seen_urls[ $url ] ) ) { |
| 135 | 125 | continue; |
| 136 | 126 | } |
| 137 | 127 | $seen_urls[ $url ] = true; |
| 138 | - $resolved = desktop_mode_my_wordpress_resolve_attachment_url( $url ); | |
| 128 | + $resolved = openstation_my_wordpress_resolve_attachment_url( $url ); | |
| 139 | 129 | if ( $resolved > 0 ) { |
| 140 | 130 | $ids[ $resolved ] = true; |
| 141 | 131 | } |
| 142 | 132 | } |
| @@ -151,14 +141,12 @@ | ||
| 151 | 141 | * Plugins that store attachment references outside `post_content` |
| 152 | 142 | * (ACF image fields, page-builder block storage, post-meta |
| 153 | 143 | * galleries) can append their ids here. |
| 154 | 144 | * |
| 155 | - * @since 0.21.0 | |
| 156 | - * | |
| 157 | 145 | * @param int[] $out Attachment ids resolved by the core scan. |
| 158 | 146 | * @param int $post_id Subject post id. |
| 159 | 147 | */ |
| 160 | - $filtered = apply_filters( 'desktop_mode_my_wordpress_attached_media', $out, $post_id ); | |
| 148 | + $filtered = apply_filters( 'openstation_my_wordpress_attached_media', $out, $post_id ); | |
| 161 | 149 | |
| 162 | 150 | if ( ! is_array( $filtered ) ) { |
| 163 | 151 | return $out; |
| 164 | 152 | } |
| @@ -178,16 +166,14 @@ | ||
| 178 | 166 | * runs a SQL query per call; a post that embeds the same image |
| 179 | 167 | * multiple times (gallery + cover + content) would otherwise pay |
| 180 | 168 | * for each occurrence. |
| 181 | 169 | * |
| 182 | - * @since 0.21.0 | |
| 183 | - * | |
| 184 | 170 | * @param string $url Image URL pulled from `<img src>`. |
| 185 | 171 | * @return int Attachment id, or 0 when no match. |
| 186 | 172 | */ |
| 187 | -function desktop_mode_my_wordpress_resolve_attachment_url( $url ) { | |
| 173 | +function openstation_my_wordpress_resolve_attachment_url( $url ) { | |
| 188 | 174 | static $cache = array(); |
| 189 | - $url = (string) $url; | |
| 175 | + $url = (string) $url; | |
| 190 | 176 | if ( '' === $url ) { |
| 191 | 177 | return 0; |
| 192 | 178 | } |
| 193 | 179 | if ( isset( $cache[ $url ] ) ) { |
| @@ -195,16 +181,16 @@ | ||
| 195 | 181 | } |
| 196 | 182 | // `attachment_url_to_postid()` does a literal `_wp_attached_file` |
| 197 | 183 | // meta lookup — it doesn't account for WP-generated variants: |
| 198 | 184 | // |
| 199 | - // - Sized intermediates `image-300x200.jpg` — strip `-WxH`. | |
| 200 | - // - `-scaled.jpg` WP autogenerates this for uploads | |
| 201 | - // past the big-image threshold and | |
| 202 | - // stores it as `_wp_attached_file`, | |
| 203 | - // while editors emit the original | |
| 204 | - // URL in `<img src>`. Try BOTH the | |
| 205 | - // scaled→original strip AND the | |
| 206 | - // 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. | |
| 207 | 193 | // |
| 208 | 194 | // Strip query strings from every candidate so cache-buster URLs |
| 209 | 195 | // (`?ver=…`) still resolve. |
| 210 | 196 | $strip_query = static function ( $u ) { |
| @@ -211,9 +197,9 @@ | ||
| 211 | 197 | $pos = strpos( $u, '?' ); |
| 212 | 198 | return false === $pos ? $u : substr( $u, 0, $pos ); |
| 213 | 199 | }; |
| 214 | 200 | |
| 215 | - $clean = $strip_query( $url ); | |
| 201 | + $clean = $strip_query( $url ); | |
| 216 | 202 | $candidates = array( $clean ); |
| 217 | 203 | |
| 218 | 204 | // `image-300x200.jpg` → `image.jpg` |
| 219 | 205 | if ( preg_match( '/^(.*)-\d+x\d+(\.[a-zA-Z0-9]+)$/', $clean, $m ) ) { |