webp-uploads
Last commit date
deprecated.php
2 years ago
helper.php
1 year ago
hooks.php
1 year ago
image-edit.php
2 years ago
load.php
1 year ago
picture-element.php
1 year ago
readme.txt
1 year ago
rest-api.php
2 years ago
settings.php
1 year ago
uninstall.php
2 years ago
picture-element.php
171 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add `picture` element support |
| 4 | * |
| 5 | * @package webp-uploads |
| 6 | * |
| 7 | * @since 2.0.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Potentially wrap an image tag in a picture element. |
| 12 | * |
| 13 | * @since 2.0.0 |
| 14 | * |
| 15 | * @param string $image The image tag. |
| 16 | * @param string $context The context for the image tag. |
| 17 | * @param int $attachment_id The attachment id. |
| 18 | * |
| 19 | * @return string The new image tag. |
| 20 | */ |
| 21 | function webp_uploads_wrap_image_in_picture( string $image, string $context, int $attachment_id ): string { |
| 22 | if ( 'the_content' !== $context ) { |
| 23 | return $image; |
| 24 | } |
| 25 | |
| 26 | $original_file_mime_type = webp_uploads_get_attachment_file_mime_type( $attachment_id ); |
| 27 | if ( '' === $original_file_mime_type ) { |
| 28 | return $image; |
| 29 | } |
| 30 | |
| 31 | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| 32 | |
| 33 | if ( ! isset( $image_meta['sizes'] ) ) { |
| 34 | return $image; |
| 35 | } |
| 36 | |
| 37 | $image_sizes = $image_meta['sizes']; |
| 38 | |
| 39 | // Append missing full size image in $image_sizes array for srcset. |
| 40 | if ( isset( $image_meta['sources'], $image_meta['width'], $image_meta['height'] ) ) { |
| 41 | array_unshift( $image_sizes, $image_meta ); |
| 42 | } |
| 43 | |
| 44 | // Collect all the sub size image mime types. |
| 45 | $mime_type_data = array(); |
| 46 | foreach ( $image_sizes as $size ) { |
| 47 | if ( isset( $size['sources'] ) && isset( $size['width'] ) && isset( $size['height'] ) ) { |
| 48 | foreach ( $size['sources'] as $mime_type => $data ) { |
| 49 | $mime_type_data[ $mime_type ] = $mime_type_data[ $mime_type ] ?? array(); |
| 50 | $mime_type_data[ $mime_type ]['w'][ $size['width'] ] = $data; |
| 51 | $mime_type_data[ $mime_type ]['h'][ $size['height'] ] = $data; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | $sub_size_mime_types = array_keys( $mime_type_data ); |
| 56 | |
| 57 | // If original image type fallback is not available, don't wrap in picture element. |
| 58 | if ( ! in_array( $original_file_mime_type, $sub_size_mime_types, true ) ) { |
| 59 | return $image; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Filter the image mime types that can be used for the <picture> element. |
| 64 | * |
| 65 | * Default is: ['image/avif', 'image/webp']. Returning an empty array will skip using the `picture` element. |
| 66 | * |
| 67 | * The mime types will output in the picture element in the order they are provided. |
| 68 | * The original image will be used as the fallback image for browsers that don't support the picture element. |
| 69 | * |
| 70 | * @since 2.0.0 |
| 71 | * @since 2.1.0 The default value was updated, removing 'image/jpeg'. |
| 72 | * |
| 73 | * @param string[] $mime_types Mime types than can be used. |
| 74 | * @param int $attachment_id The id of the image being evaluated. |
| 75 | */ |
| 76 | $enabled_mime_types = (array) apply_filters( |
| 77 | 'webp_uploads_picture_element_mime_types', |
| 78 | array( |
| 79 | 'image/avif', |
| 80 | 'image/webp', |
| 81 | ), |
| 82 | $attachment_id |
| 83 | ); |
| 84 | |
| 85 | $mime_types = array_intersect( $enabled_mime_types, $sub_size_mime_types ); |
| 86 | |
| 87 | // No eligible mime types. |
| 88 | if ( count( $mime_types ) === 0 ) { |
| 89 | return $image; |
| 90 | } |
| 91 | |
| 92 | // If the original mime types is the only one available, no picture element is needed. |
| 93 | if ( 1 === count( $mime_types ) && current( $mime_types ) === $original_file_mime_type ) { |
| 94 | return $image; |
| 95 | } |
| 96 | |
| 97 | // Add each mime type to the picture's sources. |
| 98 | $picture_sources = ''; |
| 99 | |
| 100 | // Extract sizes using regex to parse image tag, then use to retrieve tag. |
| 101 | $width = 0; |
| 102 | $height = 0; |
| 103 | $processor = new WP_HTML_Tag_Processor( $image ); |
| 104 | if ( $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) { |
| 105 | $width = (int) $processor->get_attribute( 'width' ); |
| 106 | $height = (int) $processor->get_attribute( 'height' ); |
| 107 | } |
| 108 | $size_to_use = ( $width > 0 && $height > 0 ) ? array( $width, $height ) : 'full'; |
| 109 | |
| 110 | $image_src = wp_get_attachment_image_src( $attachment_id, $size_to_use ); |
| 111 | if ( false === $image_src ) { |
| 112 | return $image; |
| 113 | } |
| 114 | list( $src, $width, $height ) = $image_src; |
| 115 | $size_array = array( absint( $width ), absint( $height ) ); |
| 116 | |
| 117 | // Gets the srcset and sizes from the IMG tag. |
| 118 | $sizes = $processor->get_attribute( 'sizes' ); |
| 119 | $srcset = $processor->get_attribute( 'srcset' ); |
| 120 | |
| 121 | if ( null !== $srcset && null !== $sizes ) { |
| 122 | foreach ( $mime_types as $image_mime_type ) { |
| 123 | // Filter core's wp_get_attachment_image_srcset to return the sources for the current mime type. |
| 124 | $filter = static function ( $sources ) use ( $mime_type_data, $image_mime_type ): array { |
| 125 | $filtered_sources = array(); |
| 126 | foreach ( $sources as $source ) { |
| 127 | // Swap the URL for the current mime type. |
| 128 | if ( isset( $mime_type_data[ $image_mime_type ][ $source['descriptor'] ][ $source['value'] ] ) ) { |
| 129 | $filename = $mime_type_data[ $image_mime_type ][ $source['descriptor'] ][ $source['value'] ]['file']; |
| 130 | $filtered_sources[] = array( |
| 131 | 'url' => dirname( $source['url'] ) . '/' . $filename, |
| 132 | 'descriptor' => $source['descriptor'], |
| 133 | 'value' => $source['value'], |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | return $filtered_sources; |
| 138 | }; |
| 139 | add_filter( 'wp_calculate_image_srcset', $filter ); |
| 140 | $image_srcset = wp_get_attachment_image_srcset( $attachment_id, $size_array, $image_meta ); |
| 141 | remove_filter( 'wp_calculate_image_srcset', $filter ); |
| 142 | if ( is_string( $image_srcset ) ) { |
| 143 | $picture_sources .= sprintf( |
| 144 | '<source type="%s" srcset="%s"%s>', |
| 145 | esc_attr( $image_mime_type ), |
| 146 | esc_attr( $image_srcset ), |
| 147 | is_string( $sizes ) ? sprintf( ' sizes="%s"', esc_attr( $sizes ) ) : '' |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | foreach ( $mime_types as $image_mime_type ) { |
| 153 | $image_srcset = webp_uploads_get_mime_type_image( $attachment_id, $src, $image_mime_type ); |
| 154 | if ( is_string( $image_srcset ) ) { |
| 155 | $picture_sources .= sprintf( |
| 156 | '<source type="%s" srcset="%s">', |
| 157 | esc_attr( $image_mime_type ), |
| 158 | esc_attr( $image_srcset ) |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return sprintf( |
| 165 | '<picture class="%s" style="display: contents;">%s%s</picture>', |
| 166 | esc_attr( 'wp-picture-' . $attachment_id ), |
| 167 | $picture_sources, |
| 168 | $image |
| 169 | ); |
| 170 | } |
| 171 |