compatibility
5 months ago
config
5 months ago
css
5 months ago
data
3 years ago
images
3 years ago
js
5 months ago
vendor
4 months ago
views
2 months ago
class-tiny-apache-rewrite.php
2 months ago
class-tiny-bulk-optimization.php
5 months ago
class-tiny-cli.php
5 months ago
class-tiny-compress-client.php
5 months ago
class-tiny-compress-fopen.php
5 months ago
class-tiny-compress.php
5 months ago
class-tiny-conversion.php
2 months ago
class-tiny-diagnostics.php
5 months ago
class-tiny-exception.php
5 months ago
class-tiny-helpers.php
5 months ago
class-tiny-image-size.php
5 months ago
class-tiny-image.php
5 months ago
class-tiny-logger.php
2 months ago
class-tiny-notices.php
2 months ago
class-tiny-php.php
5 months ago
class-tiny-picture.php
4 months ago
class-tiny-plugin.php
2 months ago
class-tiny-settings.php
2 months ago
class-tiny-source-base.php
2 months ago
class-tiny-source-image.php
5 months ago
class-tiny-source-picture.php
5 months ago
class-tiny-wp-base.php
5 months ago
class-tiny-source-picture.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Tiny Compress Images - WordPress plugin. |
| 5 | * Copyright (C) 2015-2018 Tinify B.V. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 19 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | class Tiny_Source_Picture extends Tiny_Source_Base { |
| 23 | /** |
| 24 | * Adds alternative format sources (e.g., image/webp, image/avif) to an existing |
| 25 | * <picture> element based on locally available converted files. |
| 26 | * |
| 27 | * @return string The augmented <picture> HTML or the original if no additions. |
| 28 | */ |
| 29 | public function augment_picture_element() { |
| 30 | $modified_sources = array(); |
| 31 | |
| 32 | foreach ( $this->get_element_by_tag( $this->raw_html, 'source' ) as $source_tag_html ) { |
| 33 | $type_attr = self::get_attribute_value( $source_tag_html, 'type' ); |
| 34 | $type_attr = null !== $type_attr ? strtolower( trim( $type_attr ) ) : ''; |
| 35 | |
| 36 | // Skip if already optimized. |
| 37 | if ( '' !== $type_attr && in_array( $type_attr, $this->valid_mimetypes, true ) ) { |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | $alternative_sources = $this->create_alternative_sources( $source_tag_html ); |
| 42 | if ( is_array( $alternative_sources ) && $alternative_sources ) { |
| 43 | $modified_sources = array_merge( $modified_sources, $alternative_sources ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // handle inner image |
| 48 | foreach ( $this->get_element_by_tag( $this->raw_html, 'img' ) as $img_tag_html ) { |
| 49 | $alt_image_source = $this->create_alternative_sources( $img_tag_html ); |
| 50 | $modified_sources = array_merge( $modified_sources, $alt_image_source ); |
| 51 | } |
| 52 | |
| 53 | $modified_source = implode( '', $modified_sources ); |
| 54 | |
| 55 | // Insert newly built <source> elements immediately before the first <img> |
| 56 | return preg_replace( '#(<img\b)#i', $modified_source . '$1', $this->raw_html, 1 ); |
| 57 | } |
| 58 | } |
| 59 |