| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Media dimension filtering for REST. | |
| 3 | + * OpenStation — Media dimension filtering for REST. | |
| 4 | 4 | * |
| 5 | 5 | * Adds two opt-in query parameters to `/wp/v2/media`: |
| 6 | 6 | * |
| 7 | - * - `desktop_mode_min_width` — only return images at least this many pixels wide. | |
| 8 | - * - `desktop_mode_min_height` — only return images at least this many pixels tall. | |
| 7 | + * - `openstation_min_width` — only return images at least this many pixels wide. | |
| 8 | + * - `openstation_min_height` — only return images at least this many pixels tall. | |
| 9 | 9 | * |
| 10 | 10 | * The OS Settings wallpaper picker uses these to keep a site with thousands |
| 11 | 11 | * of small product images from burying the handful of desktop-worthy HD |
| 12 | 12 | * shots under an infinite-scroll slog. The client still applies the same |
| @@ -18,22 +18,45 @@ | ||
| 18 | 18 | * reliably. We stamp two flat numeric meta keys on every new attachment |
| 19 | 19 | * (and opportunistically backfill existing ones) so `WP_Meta_Query` can |
| 20 | 20 | * use a normal NUMERIC `>=` comparison. |
| 21 | 21 | * |
| 22 | - * @package WPDesktopMode | |
| 22 | + * @package OpenStation | |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | 25 | defined( 'ABSPATH' ) || exit; |
| 26 | 26 | |
| 27 | -/** Numeric post-meta keys stamped on every image attachment. */ | |
| 28 | -const DESKTOP_MODE_META_WIDTH = '_desktop_mode_width'; | |
| 29 | -const DESKTOP_MODE_META_HEIGHT = '_desktop_mode_height'; | |
| 27 | +/** | |
| 28 | + * Numeric post-meta keys stamped on every image attachment. | |
| 29 | + * | |
| 30 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 31 | + * persisted or externally-visible identifier, so renaming it would | |
| 32 | + * orphan data already written by live installs (or break a live | |
| 33 | + * URL). The mismatch between this constant's name and its value is | |
| 34 | + * deliberate — it is NOT a half-finished rename. | |
| 35 | + */ | |
| 36 | +const OPENSTATION_META_WIDTH = '_desktop_mode_width'; | |
| 37 | +/** | |
| 38 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 39 | + * persisted or externally-visible identifier, so renaming it would | |
| 40 | + * orphan data already written by live installs (or break a live | |
| 41 | + * URL). The mismatch between this constant's name and its value is | |
| 42 | + * deliberate — it is NOT a half-finished rename. | |
| 43 | + */ | |
| 44 | +const OPENSTATION_META_HEIGHT = '_desktop_mode_height'; | |
| 30 | 45 | |
| 31 | -/** Option key flipped to `1` once every image has been backfilled. */ | |
| 32 | -const DESKTOP_MODE_BACKFILL_DONE_OPTION = 'desktop_mode_media_dims_backfilled'; | |
| 46 | +/** | |
| 47 | + * Option key flipped to `1` once every image has been backfilled. | |
| 48 | + * | |
| 49 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 50 | + * persisted or externally-visible identifier, so renaming it would | |
| 51 | + * orphan data already written by live installs (or break a live | |
| 52 | + * URL). The mismatch between this constant's name and its value is | |
| 53 | + * deliberate — it is NOT a half-finished rename. | |
| 54 | + */ | |
| 55 | +const OPENSTATION_BACKFILL_DONE_OPTION = 'desktop_mode_media_dims_backfilled'; | |
| 33 | 56 | |
| 34 | 57 | /** How many legacy attachments to backfill per filtered REST request. */ |
| 35 | -const DESKTOP_MODE_MEDIA_BACKFILL_BATCH = 50; | |
| 58 | +const OPENSTATION_MEDIA_BACKFILL_BATCH = 50; | |
| 36 | 59 | |
| 37 | 60 | /** |
| 38 | 61 | * Stamp numeric dimension meta whenever an attachment's metadata is |
| 39 | 62 | * generated or updated. Hooked on both `wp_generate_attachment_metadata` |
| @@ -44,9 +67,9 @@ | ||
| 44 | 67 | * @param array $metadata Attachment metadata. |
| 45 | 68 | * @param int $attachment_id Attachment post ID. |
| 46 | 69 | * @return array The metadata, unchanged — we only read from it. |
| 47 | 70 | */ |
| 48 | -function desktop_mode_stamp_media_dimensions( $metadata, $attachment_id ) { | |
| 71 | +function openstation_stamp_media_dimensions( $metadata, $attachment_id ) { | |
| 49 | 72 | if ( ! is_array( $metadata ) ) { |
| 50 | 73 | return $metadata; |
| 51 | 74 | } |
| 52 | 75 | |
| @@ -55,18 +78,18 @@ | ||
| 55 | 78 | |
| 56 | 79 | // Stamp zero for images we can't measure (SVGs, broken files) so the |
| 57 | 80 | // backfill sweep knows we've already inspected this row and doesn't |
| 58 | 81 | // re-check it on every subsequent filtered request. |
| 59 | - update_post_meta( $attachment_id, DESKTOP_MODE_META_WIDTH, max( 0, $width ) ); | |
| 60 | - update_post_meta( $attachment_id, DESKTOP_MODE_META_HEIGHT, max( 0, $height ) ); | |
| 82 | + update_post_meta( $attachment_id, OPENSTATION_META_WIDTH, max( 0, $width ) ); | |
| 83 | + update_post_meta( $attachment_id, OPENSTATION_META_HEIGHT, max( 0, $height ) ); | |
| 61 | 84 | |
| 62 | 85 | return $metadata; |
| 63 | 86 | } |
| 64 | -add_filter( 'wp_generate_attachment_metadata', 'desktop_mode_stamp_media_dimensions', 10, 2 ); | |
| 65 | -add_filter( 'wp_update_attachment_metadata', 'desktop_mode_stamp_media_dimensions', 10, 2 ); | |
| 87 | +add_filter( 'wp_generate_attachment_metadata', 'openstation_stamp_media_dimensions', 10, 2 ); | |
| 88 | +add_filter( 'wp_update_attachment_metadata', 'openstation_stamp_media_dimensions', 10, 2 ); | |
| 66 | 89 | |
| 67 | 90 | /** |
| 68 | - * Register the `desktop_mode_min_width` / `desktop_mode_min_height` query parameters on | |
| 91 | + * Register the `openstation_min_width` / `openstation_min_height` query parameters on | |
| 69 | 92 | * the media collection so Core sanitizes them before they reach our |
| 70 | 93 | * query filter. Without this, the params would still work but would |
| 71 | 94 | * show up as unknown to any REST consumer introspecting the schema. |
| 72 | 95 | * |
| @@ -72,15 +95,15 @@ | ||
| 72 | 95 | * |
| 73 | 96 | * @param array $params Existing collection params. |
| 74 | 97 | * @return array |
| 75 | 98 | */ |
| 76 | -function desktop_mode_register_media_query_params( $params ) { | |
| 77 | - $params['desktop_mode_min_width'] = array( | |
| 99 | +function openstation_register_media_query_params( $params ) { | |
| 100 | + $params['openstation_min_width'] = array( | |
| 78 | 101 | 'description' => __( 'Only return images at least this many pixels wide.', 'desktop-mode' ), |
| 79 | 102 | 'type' => 'integer', |
| 80 | 103 | 'minimum' => 1, |
| 81 | 104 | ); |
| 82 | - $params['desktop_mode_min_height'] = array( | |
| 105 | + $params['openstation_min_height'] = array( | |
| 83 | 106 | 'description' => __( 'Only return images at least this many pixels tall.', 'desktop-mode' ), |
| 84 | 107 | 'type' => 'integer', |
| 85 | 108 | 'minimum' => 1, |
| 86 | 109 | ); |
| @@ -85,9 +108,9 @@ | ||
| 85 | 108 | 'minimum' => 1, |
| 86 | 109 | ); |
| 87 | 110 | return $params; |
| 88 | 111 | } |
| 89 | -add_filter( 'rest_attachment_collection_params', 'desktop_mode_register_media_query_params' ); | |
| 112 | +add_filter( 'rest_attachment_collection_params', 'openstation_register_media_query_params' ); | |
| 90 | 113 | |
| 91 | 114 | /** |
| 92 | 115 | * Inject meta_query clauses on the attachment REST query when either |
| 93 | 116 | * dimension filter is present. Triggers a bounded backfill first so |
| @@ -97,11 +120,11 @@ | ||
| 97 | 120 | * @param array $args WP_Query args built by the REST controller. |
| 98 | 121 | * @param WP_REST_Request $request The REST request. |
| 99 | 122 | * @return array The query args, possibly with extra meta_query entries. |
| 100 | 123 | */ |
| 101 | -function desktop_mode_filter_media_by_dimensions( $args, $request ) { | |
| 102 | - $min_width = absint( $request->get_param( 'desktop_mode_min_width' ) ); | |
| 103 | - $min_height = absint( $request->get_param( 'desktop_mode_min_height' ) ); | |
| 124 | +function openstation_filter_media_by_dimensions( $args, $request ) { | |
| 125 | + $min_width = absint( $request->get_param( 'openstation_min_width' ) ); | |
| 126 | + $min_height = absint( $request->get_param( 'openstation_min_height' ) ); | |
| 104 | 127 | |
| 105 | 128 | if ( ! $min_width && ! $min_height ) { |
| 106 | 129 | return $args; |
| 107 | 130 | } |
| @@ -112,9 +135,9 @@ | ||
| 112 | 135 | // Logged-in users only — anonymous REST readers can still use the |
| 113 | 136 | // dimension filters, but must never trigger database writes (or the |
| 114 | 137 | // NOT EXISTS sweep query) on a public, unauthenticated endpoint. |
| 115 | 138 | if ( is_user_logged_in() ) { |
| 116 | - desktop_mode_backfill_media_dimensions( DESKTOP_MODE_MEDIA_BACKFILL_BATCH ); | |
| 139 | + openstation_backfill_media_dimensions( OPENSTATION_MEDIA_BACKFILL_BATCH ); | |
| 117 | 140 | } |
| 118 | 141 | |
| 119 | 142 | $meta_query = isset( $args['meta_query'] ) && is_array( $args['meta_query'] ) |
| 120 | 143 | ? $args['meta_query'] |
| @@ -122,9 +145,9 @@ | ||
| 122 | 145 | |
| 123 | 146 | $clauses = array(); |
| 124 | 147 | if ( $min_width ) { |
| 125 | 148 | $clauses[] = array( |
| 126 | - 'key' => DESKTOP_MODE_META_WIDTH, | |
| 149 | + 'key' => OPENSTATION_META_WIDTH, | |
| 127 | 150 | 'value' => $min_width, |
| 128 | 151 | 'compare' => '>=', |
| 129 | 152 | 'type' => 'NUMERIC', |
| 130 | 153 | ); |
| @@ -130,9 +153,9 @@ | ||
| 130 | 153 | ); |
| 131 | 154 | } |
| 132 | 155 | if ( $min_height ) { |
| 133 | 156 | $clauses[] = array( |
| 134 | - 'key' => DESKTOP_MODE_META_HEIGHT, | |
| 157 | + 'key' => OPENSTATION_META_HEIGHT, | |
| 135 | 158 | 'value' => $min_height, |
| 136 | 159 | 'compare' => '>=', |
| 137 | 160 | 'type' => 'NUMERIC', |
| 138 | 161 | ); |
| @@ -155,9 +178,9 @@ | ||
| 155 | 178 | } |
| 156 | 179 | |
| 157 | 180 | return $args; |
| 158 | 181 | } |
| 159 | -add_filter( 'rest_attachment_query', 'desktop_mode_filter_media_by_dimensions', 10, 2 ); | |
| 182 | +add_filter( 'rest_attachment_query', 'openstation_filter_media_by_dimensions', 10, 2 ); | |
| 160 | 183 | |
| 161 | 184 | /** |
| 162 | 185 | * Stamp dimension meta on up to $batch image attachments that don't |
| 163 | 186 | * have it yet. Returns early once a site has been fully backfilled so |
| @@ -169,10 +192,10 @@ | ||
| 169 | 192 | * |
| 170 | 193 | * @param int $batch Maximum attachments to stamp this pass. |
| 171 | 194 | * @return int Number of attachments stamped (0 when nothing remained). |
| 172 | 195 | */ |
| 173 | -function desktop_mode_backfill_media_dimensions( $batch ) { | |
| 174 | - if ( get_option( DESKTOP_MODE_BACKFILL_DONE_OPTION ) ) { | |
| 196 | +function openstation_backfill_media_dimensions( $batch ) { | |
| 197 | + if ( get_option( OPENSTATION_BACKFILL_DONE_OPTION ) ) { | |
| 175 | 198 | return 0; |
| 176 | 199 | } |
| 177 | 200 | |
| 178 | 201 | $ids = get_posts( |
| @@ -188,9 +211,9 @@ | ||
| 188 | 211 | 'update_post_term_cache' => false, |
| 189 | 212 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- one-shot backfill targeting the small subset of attachments that lack our dimension stamp; runs at most $batch rows per filtered request and stops once the site option flips. |
| 190 | 213 | 'meta_query' => array( |
| 191 | 214 | array( |
| 192 | - 'key' => DESKTOP_MODE_META_WIDTH, | |
| 215 | + 'key' => OPENSTATION_META_WIDTH, | |
| 193 | 216 | 'compare' => 'NOT EXISTS', |
| 194 | 217 | ), |
| 195 | 218 | ), |
| 196 | 219 | ) |
| @@ -200,9 +223,9 @@ | ||
| 200 | 223 | // Flip the flag so future filtered requests skip this query. |
| 201 | 224 | // If a plugin ever adds new images via a back door that bypasses |
| 202 | 225 | // `wp_generate_attachment_metadata`, those can be picked up by |
| 203 | 226 | // manually deleting this option. |
| 204 | - update_option( DESKTOP_MODE_BACKFILL_DONE_OPTION, 1, false ); | |
| 227 | + update_option( OPENSTATION_BACKFILL_DONE_OPTION, 1, false ); | |
| 205 | 228 | return 0; |
| 206 | 229 | } |
| 207 | 230 | |
| 208 | 231 | foreach ( $ids as $id ) { |
| @@ -216,10 +239,10 @@ | ||
| 216 | 239 | $height = is_array( $metadata ) && isset( $metadata['height'] ) ? (int) $metadata['height'] : 0; |
| 217 | 240 | |
| 218 | 241 | // Always stamp, even when zero — that's how the sweep query |
| 219 | 242 | // knows to skip this row on the next pass. |
| 220 | - update_post_meta( $id, DESKTOP_MODE_META_WIDTH, max( 0, $width ) ); | |
| 221 | - update_post_meta( $id, DESKTOP_MODE_META_HEIGHT, max( 0, $height ) ); | |
| 243 | + update_post_meta( $id, OPENSTATION_META_WIDTH, max( 0, $width ) ); | |
| 244 | + update_post_meta( $id, OPENSTATION_META_HEIGHT, max( 0, $height ) ); | |
| 222 | 245 | } |
| 223 | 246 | |
| 224 | 247 | return count( $ids ); |
| 225 | 248 | } |