-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1699 from WordPress/update/separate-file-structure
Auto sizes: Reorganize file structure by feature
- Loading branch information
Showing
5 changed files
with
292 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Functionality to implement auto-sizes for lazy loaded images. | ||
* | ||
* @package auto-sizes | ||
* @since n.e.x.t | ||
*/ | ||
|
||
/** | ||
* Adds auto to the sizes attribute to the image, if applicable. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param array<string, string>|mixed $attr Attributes for the image markup. | ||
* @return array<string, string> The filtered attributes for the image markup. | ||
*/ | ||
function auto_sizes_update_image_attributes( $attr ): array { | ||
if ( ! is_array( $attr ) ) { | ||
$attr = array(); | ||
} | ||
|
||
// Bail early if the image is not lazy-loaded. | ||
if ( ! isset( $attr['loading'] ) || 'lazy' !== $attr['loading'] ) { | ||
return $attr; | ||
} | ||
|
||
// Bail early if the image is not responsive. | ||
if ( ! isset( $attr['sizes'] ) ) { | ||
return $attr; | ||
} | ||
|
||
// Don't add 'auto' to the sizes attribute if it already exists. | ||
if ( auto_sizes_attribute_includes_valid_auto( $attr['sizes'] ) ) { | ||
return $attr; | ||
} | ||
|
||
$attr['sizes'] = 'auto, ' . $attr['sizes']; | ||
|
||
return $attr; | ||
} | ||
|
||
/** | ||
* Adds auto to the sizes attribute to the image, if applicable. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param string|mixed $html The HTML image tag markup being filtered. | ||
* @return string The filtered HTML image tag markup. | ||
*/ | ||
function auto_sizes_update_content_img_tag( $html ): string { | ||
if ( ! is_string( $html ) ) { | ||
$html = ''; | ||
} | ||
|
||
$processor = new WP_HTML_Tag_Processor( $html ); | ||
|
||
// Bail if there is no IMG tag. | ||
if ( ! $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) { | ||
return $html; | ||
} | ||
|
||
// Bail early if the image is not lazy-loaded. | ||
$value = $processor->get_attribute( 'loading' ); | ||
if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) { | ||
return $html; | ||
} | ||
|
||
$sizes = $processor->get_attribute( 'sizes' ); | ||
|
||
// Bail early if the image is not responsive. | ||
if ( ! is_string( $sizes ) ) { | ||
return $html; | ||
} | ||
|
||
// Don't add 'auto' to the sizes attribute if it already exists. | ||
if ( auto_sizes_attribute_includes_valid_auto( $sizes ) ) { | ||
return $html; | ||
} | ||
|
||
$processor->set_attribute( 'sizes', "auto, $sizes" ); | ||
return $processor->get_updated_html(); | ||
} | ||
|
||
/** | ||
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list. | ||
* | ||
* Per the HTML spec, if present it must be the first entry. | ||
* | ||
* @since 1.2.0 | ||
* | ||
* @param string $sizes_attr The 'sizes' attribute value. | ||
* @return bool True if the 'auto' keyword is present, false otherwise. | ||
*/ | ||
function auto_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool { | ||
list( $first_size ) = explode( ',', $sizes_attr, 2 ); | ||
return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) ); | ||
} |
Oops, something went wrong.