|
1 | 1 | <?php |
2 | 2 | namespace BEA\Theme\Framework\Helpers\Formatting\Image; |
3 | 3 |
|
| 4 | +use BEA\Theme\Framework\Framework; |
| 5 | + |
4 | 6 | /** |
5 | 7 | * @usage BEA\Theme\Framework\Helpers\Formatting\Image\get_the_image( 1, [ 'data-location' => 'image-size' ] ); |
6 | 8 | * |
|
32 | 34 | * @type string $after Optional. Markup to append to the image. Default empty. |
33 | 35 | * @type boolean $default Optional. If WP image does not exists, display a default image. |
34 | 36 | * @type boolean $is_lcp Optional. When true, applies LCP-friendly attributes |
35 | | - * (loading omitted, fetchpriority=high, decoding=sync) |
36 | | - * unless decoding is already set in $attributes. |
| 37 | + * (loading omitted, fetchpriority=high). |
| 38 | + * decoding may be set explicitly in $attributes. |
37 | 39 | * |
38 | 40 | * } |
39 | 41 | * |
@@ -71,7 +73,6 @@ function get_the_image( int $image_id, array $attributes, array $settings = [] ) |
71 | 73 | // Always win over caller/WP defaults: the LCP image must not be lazy-loaded. |
72 | 74 | $attributes['loading'] = false; |
73 | 75 | $attributes['fetchpriority'] = 'high'; |
74 | | - $attributes['decoding'] = $attributes['decoding'] ?? 'sync'; |
75 | 76 | } |
76 | 77 |
|
77 | 78 | // Never combine fetchpriority=high with loading=lazy (undefined browser behavior). |
@@ -143,18 +144,78 @@ function the_image( int $image_id, array $attributes, array $settings = [] ): vo |
143 | 144 | echo get_the_image( $image_id, $attributes, $settings ); |
144 | 145 | } |
145 | 146 |
|
| 147 | +/** |
| 148 | + * Resolve the public URI for a static theme asset without redundant filesystem checks. |
| 149 | + * |
| 150 | + * @param string $relative_theme_path Path relative to the theme root. |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | +function get_theme_static_image_uri( string $relative_theme_path ): string { |
| 155 | + static $uri_cache = []; |
| 156 | + |
| 157 | + $relative_theme_path = ltrim( $relative_theme_path, '/' ); |
| 158 | + |
| 159 | + if ( isset( $uri_cache[ $relative_theme_path ] ) ) { |
| 160 | + return $uri_cache[ $relative_theme_path ]; |
| 161 | + } |
| 162 | + |
| 163 | + if ( get_stylesheet() === get_template() ) { |
| 164 | + $uri = get_stylesheet_directory_uri() . '/' . $relative_theme_path; |
| 165 | + $uri = apply_filters( 'theme_file_uri', $uri, $relative_theme_path ); |
| 166 | + } else { |
| 167 | + $uri = get_theme_file_uri( $relative_theme_path ); |
| 168 | + } |
| 169 | + |
| 170 | + $uri_cache[ $relative_theme_path ] = $uri; |
| 171 | + |
| 172 | + return $uri; |
| 173 | +} |
| 174 | + |
146 | 175 | /** |
147 | 176 | * Read intrinsic dimensions from a static file under the active theme directory. |
148 | 177 | * |
| 178 | + * Uses build-time dimensions from `dist/assets.json` when available; otherwise reads the file once per request. |
| 179 | + * |
149 | 180 | * @usage BEA\Theme\Framework\Helpers\Formatting\Image\get_theme_static_image_dimensions( 'dist/images/logo.svg' ); |
150 | 181 | * |
151 | 182 | * @param string $relative_theme_path Path relative to the theme root (e.g. dist/images/logo.svg). |
152 | 183 | * |
153 | 184 | * @return array{width:int,height:int}|null Null when the file is missing or dimensions cannot be read. |
154 | 185 | */ |
155 | 186 | function get_theme_static_image_dimensions( string $relative_theme_path ): ?array { |
| 187 | + static $cache = []; |
| 188 | + |
156 | 189 | $relative_theme_path = ltrim( $relative_theme_path, '/' ); |
157 | | - $file_path = get_theme_file_path( $relative_theme_path ); |
| 190 | + |
| 191 | + if ( array_key_exists( $relative_theme_path, $cache ) ) { |
| 192 | + return $cache[ $relative_theme_path ]; |
| 193 | + } |
| 194 | + |
| 195 | + $assets = Framework::get_container()->get_service( 'assets' ); |
| 196 | + $from_manifest = $assets->get_static_image_dimensions( $relative_theme_path ); |
| 197 | + |
| 198 | + if ( null !== $from_manifest ) { |
| 199 | + $cache[ $relative_theme_path ] = $from_manifest; |
| 200 | + |
| 201 | + return $from_manifest; |
| 202 | + } |
| 203 | + |
| 204 | + $from_filesystem = read_theme_static_image_dimensions_from_filesystem( $relative_theme_path ); |
| 205 | + $cache[ $relative_theme_path ] = $from_filesystem; |
| 206 | + |
| 207 | + return $from_filesystem; |
| 208 | +} |
| 209 | + |
| 210 | +/** |
| 211 | + * Fallback: read intrinsic dimensions from the theme filesystem. |
| 212 | + * |
| 213 | + * @param string $relative_theme_path Path relative to the theme root. |
| 214 | + * |
| 215 | + * @return array{width:int,height:int}|null |
| 216 | + */ |
| 217 | +function read_theme_static_image_dimensions_from_filesystem( string $relative_theme_path ): ?array { |
| 218 | + $file_path = get_theme_file_path( $relative_theme_path ); |
158 | 219 |
|
159 | 220 | if ( ! is_string( $file_path ) || '' === $file_path || ! is_readable( $file_path ) ) { |
160 | 221 | return null; |
@@ -214,7 +275,7 @@ function get_theme_static_image_markup( string $relative_theme_path, array $attr |
214 | 275 | $attributes = wp_parse_args( |
215 | 276 | $attributes, |
216 | 277 | [ |
217 | | - 'src' => get_theme_file_uri( $relative_theme_path ), |
| 278 | + 'src' => get_theme_static_image_uri( $relative_theme_path ), |
218 | 279 | 'alt' => '', |
219 | 280 | ] |
220 | 281 | ); |
|
0 commit comments