forked from tinify/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-tiny-source-base.php
More file actions
311 lines (266 loc) · 8.14 KB
/
class-tiny-source-base.php
File metadata and controls
311 lines (266 loc) · 8.14 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?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.
*/
abstract class Tiny_Source_Base {
public $raw_html;
protected $base_dir;
protected $allowed_domains;
protected $valid_mimetypes;
public function __construct( $html, $base_dir, $domains ) {
$this->raw_html = $html;
$this->base_dir = $base_dir;
$this->allowed_domains = $domains;
$this->valid_mimetypes = array( 'image/avif', 'image/webp' );
}
protected static function get_attribute_value( $element, $name ) {
// Match the exact attribute name (not part of data-media, mediaType, etc.)
// and capture a single- or double-quoted value.
$delim = '~';
$attr = preg_quote( $name, $delim );
$regex = $delim . '(?<![\w:-])' . $attr . '\s*=\s*(["\'])(.*?)\1' . $delim . 'is';
if ( preg_match( $regex, $element, $m ) ) {
return $m[2];
}
return null;
}
/**
* Extract elements by tag name from an HTML string (regex-based).
*
* @param string $html The HTML string to search in.
* @param string $tagname The tag name (e.g., 'div', 'source', 'img').
* @return array Array of matched elements as strings.
*/
protected function get_element_by_tag( $html, $tagname ) {
$results = array();
// Self-closing / void tag (e.g. <source />, <img />, <br />)
if ( preg_match_all(
'~<' . preg_quote( $tagname, '~' ) . '\b(?:[^>"\']+|"[^"]*"|\'[^\']*\')*/?>~i',
$html,
$matches
) ) {
$results = array_merge( $results, $matches[0] );
}
// Normal paired tags (e.g. <div>…</div>)
$regex_tag = preg_quote( $tagname, '~' );
if ( preg_match_all(
'~<' . $regex_tag .
'\b(?:[^>"\']+|"[^"]*"|\'[^\']*\')*>.*?</' .
$regex_tag .
'>~is',
$html,
$matches
) ) {
$results = array_merge( $results, $matches[0] );
}
return $results;
}
protected function get_local_path( $url ) {
if ( strpos( $url, 'http' ) === 0 ) {
$matched_domain = null;
foreach ( $this->allowed_domains as $domain ) {
if ( strpos( $url, $domain ) === 0 ) {
$matched_domain = $domain;
break;
}
}
if ( null === $matched_domain ) {
return '';
}
$url = substr( $url, strlen( $matched_domain ) );
}
$url = $this->base_dir . $url;
return $url;
}
protected function get_formatted_source( $image_source_data, $mimetype ) {
$format_url = Tiny_Helpers::replace_file_extension( $mimetype, $image_source_data['path'] );
$local_path = $this->get_local_path( $format_url );
if ( empty( $local_path ) ) {
return null;
}
$exists_local = file_exists( $local_path );
if ( $exists_local ) {
return array(
'src' => $format_url,
'size' => $image_source_data['size'],
'type' => $mimetype,
);
}
return null;
}
/**
* Retrieves the sources from the <img> or <source> element
*
* @return array{path: string, size: string}[] The image sources
*/
protected function get_image_srcsets( $html ) {
$result = array();
$srcset = $this::get_attribute_value( $html, 'srcset' );
if ( $srcset ) {
// Split the srcset to get individual entries
$srcset_entries = explode( ',', $srcset );
foreach ( $srcset_entries as $entry ) {
// Trim whitespace
$entry = trim( $entry );
// Split by whitespace to separate path and size/density descriptor
$parts = preg_split( '/\s+/', $entry, 2 );
if ( count( $parts ) === 2 ) {
// We have both path and size
$result[] = array(
'path' => $parts[0],
'size' => $parts[1],
);
} elseif ( count( $parts ) === 1 ) {
// We only have a path, will be interpreted as pixel
// density 1x (unusual in srcset)
$result[] = array(
'path' => $parts[0],
'size' => '',
);
}
}
}
return $result;
}
/**
* Retrieves the sources from the <img> or <source> element
*
* @return array{path: string, size: string}[] The image sources
*/
private function get_image_src( $html ) {
$source = $this::get_attribute_value( $html, 'src' );
if ( ! empty( $source ) ) {
// No srcset, but we have a src attribute
return array(
'path' => $source,
'size' => '',
);
}
return array();
}
/**
* Creates one or more <source> elements if alternative formats
* are available.
*
* @param string $original_source_html, either <source> or <img>
* @return array{string} array of <source> html
*/
protected function create_alternative_sources( $original_source_html ) {
$srcsets = $this->get_image_srcsets( $original_source_html );
if ( empty( $srcsets ) ) {
// no srcset, try src attribute
$src = $this->get_image_src( $original_source_html );
if ( ! empty( $src ) ) {
$srcsets[] = $src;
}
}
if ( empty( $srcsets ) ) {
return array();
}
$is_source_tag = (bool) preg_match( '#<source\b#i', $original_source_html );
$sources = array();
$width_descriptor = $this->get_largest_width_descriptor( $srcsets );
foreach ( $this->valid_mimetypes as $mimetype ) {
$srcset_parts = array();
foreach ( $srcsets as $srcset ) {
$alt_source = $this->get_formatted_source( $srcset, $mimetype );
if ( $alt_source ) {
$srcset_parts[] = trim( $alt_source['src'] . ' ' . $alt_source['size'] );
}
}
if (
$width_descriptor &&
! self::srcset_contains_width_descriptor(
$srcset_parts,
$width_descriptor
)
) {
continue;
}
if ( empty( $srcset_parts ) ) {
continue;
}
$source_attr_parts = array();
$srcset_attr = implode( ', ', $srcset_parts );
$source_attr_parts['srcset'] = $srcset_attr;
if ( $is_source_tag ) {
foreach ( array( 'sizes', 'media', 'width', 'height' ) as $attr ) {
$attr_value = $this->get_attribute_value( $original_source_html, $attr );
if ( $attr_value ) {
$source_attr_parts[ $attr ] = $attr_value;
}
}
} else {
$sizes_value = $this->get_attribute_value( $original_source_html, 'sizes' );
if ( $sizes_value ) {
$source_attr_parts['sizes'] = $sizes_value;
}
}
$source_attr_parts['type'] = $mimetype;
$source_parts = array( '<source' );
foreach ( $source_attr_parts as $source_attr_name => $source_attr_val ) {
$source_parts[] = $source_attr_name . '="' . esc_attr( $source_attr_val ) . '"';
}
$source_parts[] = '/>';
$sources[] = implode( ' ', $source_parts );
} // End foreach().
return $sources;
}
/**
* Returns the largest numeric width descriptor
* (e.g. 2000 from "2000w") found in the srcset data.
*
* @param array<array{path: string, size: string}> $srcsets
* @return int
*/
public static function get_largest_width_descriptor( $srcsets ) {
$largest = 0;
foreach ( $srcsets as $srcset ) {
if ( empty( $srcset['size'] ) ) {
continue;
}
if ( preg_match( '/(\d+)w/', $srcset['size'], $matches ) ) {
$width = (int) $matches[1];
if ( $width > $largest ) {
$largest = $width;
}
}
}
return $largest;
}
/**
* Determines whether a srcset list contains the provided width descriptor.
*
* @param string[] $srcset_parts
* @param int $width_descriptor
* @return bool true if width is in srcset
*/
public static function srcset_contains_width_descriptor( $srcset_parts, $width_descriptor ) {
if ( empty( $srcset_parts ) || $width_descriptor <= 0 ) {
return false;
}
$suffix = ' ' . $width_descriptor . 'w';
$suffix_length = strlen( $suffix );
foreach ( $srcset_parts as $srcset_part ) {
if ( substr( $srcset_part, -$suffix_length ) === $suffix ) {
return true;
}
}
return false;
}
}