Repository navigation
Expand file tree
/
Copy pathclass-tiny-picture.php
More file actions
260 lines (228 loc) · 7.4 KB
/
Copy pathclass-tiny-picture.php
File metadata and controls
260 lines (228 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
require_once __DIR__ . '/class-tiny-source-base.php';
require_once __DIR__ . '/class-tiny-source-image.php';
require_once __DIR__ . '/class-tiny-source-picture.php';
/**
* Class responsible for parsing and modifying html to insert picture elements.
*
* 1) searches for <picture> elements or <img> elements
* 2) checks wether existing source has a modern alternative
* 3) augments or creates a picture element
* 4) replaces the original source with the source which includes the modern format
*/
class Tiny_Picture extends Tiny_WP_Base {
/** @var string */
private $base_dir;
/** @var array */
private $allowed_domains = array();
/** @var Tiny_Settings */
private $settings;
/**
* Initialize the plugin.
*
* @param Tiny_Settings $settings Tinify Settings
* @param string $base_dir Absolute path (e.g. ABSPATH)
* @param array $domains List of allowed domain URLs
*/
public function __construct( $settings, $base_dir = ABSPATH, $domains = array() ) {
$this->settings = $settings;
$this->base_dir = $base_dir;
$this->allowed_domains = $domains;
if ( is_admin() || is_customize_preview() ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
if ( static::is_pagebuilder_request() ) {
return;
}
add_action( 'template_redirect', array( $this, 'on_template_redirect' ) );
}
/**
* Checks whether the current request originates from a page builder.
*
* Detects known page builder query parameters to prevent picture-element
* injection from interfering with builder previews.
*
* @since 3.6.5
*
* @return bool True if a page builder query parameter is present.
*/
protected static function is_pagebuilder_request() {
$pagebuilder_keys = array(
'fl_builder', // Beaver Builder
'et_fb', // Divi Builder
'bricks', // Bricks Builder
'breakdance', // Breakdance Builder
'breakdance_browser', // Breakdance Builder
'ct_builder', // Oxygen Builder
'fb-edit', // Avada Live Builder
'builder', // Avada Live Builder
'spio_no_cdn', // Site Origin
'tatsu', // Tatsu Builder
'tve', // Thrive Architect
'tcbf', // Thrive Architect
);
foreach ( $pagebuilder_keys as $key ) {
if ( static::has_get_var( $key ) ) {
return true;
}
}
return false;
}
/**
* Checks whether a GET variable exists in the original request.
*
* Wraps filter_has_var() to allow overriding in tests via late static binding.
*
* @since 3.6.5
*
* @param string $key The query string key to check.
* @return bool True if the key exists in the original GET request.
*/
protected static function has_get_var( $key ) {
return filter_has_var( INPUT_GET, $key );
}
public function on_template_redirect() {
$conversion_enabled = $this->settings->get_conversion_enabled();
if ( apply_filters( 'tiny_replace_with_picture', $conversion_enabled ) ) {
/**
* Controls wether the page should replace <img> with <picture> elements
* converted sources.
*
* @since 3.6.9
*/
ob_start( array( $this, 'replace_sources' ), 1000 );
}
}
public function replace_sources( $content ) {
$content = $this->replace_picture_sources( $content );
$content = $this->replace_img_sources( $content );
return $content;
}
/**
* Will extend existing picture elements with additional sourcesets
*
* @param string $content
* @return string the new source html
*/
private function replace_picture_sources( $content ) {
$picture_sources = $this->filter_pictures( $content );
foreach ( $picture_sources as $picture_source ) {
$content = $this->replace_picture( $content, $picture_source );
}
return $content;
}
private function replace_img_sources( $content ) {
$image_sources = $this->filter_images( $content );
foreach ( $image_sources as $image_source ) {
$content = self::replace_image( $content, $image_source );
}
return $content;
}
/**
* Will search for all picture elements within the given source html
*
* @param string $content
* @return array<Tiny_Source_Picture> an array of picture element sources
*/
private function filter_pictures( $content ) {
$matches = array();
if ( ! preg_match_all(
'#<picture\b[^>]*>.*?<\/picture>#is',
$content,
$matches
) ) {
return array();
}
$pictures = array();
foreach ( $matches[0] as $raw_picture ) {
$pictures[] = new Tiny_Source_Picture(
$raw_picture,
$this->base_dir,
$this->allowed_domains
);
}
return $pictures;
}
/**
* Will add additional sourcesets to picture elements.
*
* @param string $content the full page content
* @param Tiny_Source_Picture $source the picture element
*
* @return string the updated content including augmented picture elements
*/
private function replace_picture( $content, $source ) {
$content = str_replace( $source->raw_html, $source->augment_picture_element(), $content );
return $content;
}
/**
* Will replace img elements with picture elements that (possibly) have additional formats.
*
* @param string $content the full page content
* @param Tiny_Source_Image $source the picture element
*
* @return string the updated content including augmented picture elements
*/
private static function replace_image( $content, $source ) {
$content = str_replace( $source->raw_html, $source->create_picture_elements(), $content );
return $content;
}
/**
* Filters out all images from the content and returns them as an array.
*
* @return Tiny_Image[]
*/
private function filter_images( $content ) {
// Extract only the <body>...</body> section.
if ( preg_match( '/(?=<body).*<\/body>/is', $content, $body ) ) {
$content = $body[0];
}
// strip HTML comments.
$content = preg_replace( '/<!--(.*)-->/Uis', '', $content );
// strip existing <picture> blocks to avoid double-processing.
$content = preg_replace( '/<picture\b.*?>.*?<\/picture>/is', '', $content );
// Strip <noscript> blocks to avoid altering their contents.
$content = preg_replace( '/<noscript\b.*?>.*?<\/noscript>/is', '', $content );
// Find all <img> tags with any attributes.
if ( ! preg_match_all( '/<img\b[^>]*>/is', $content, $matches ) ) {
return array();
}
$images = array();
foreach ( $matches[0] as $img ) {
// Skip images without src or srcset
if ( ! preg_match( '/\b(?:src|srcset)\s*=/i', $img ) ) {
continue;
}
$images[] = new Tiny_Source_Image(
$img,
$this->base_dir,
$this->allowed_domains
);
}
return $images;
}
}