PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.2
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.2
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
← All changes | public/class-display-webp.php +43 -53 3.2.34.1.2 View file →
@@ -46,23 +46,20 @@
46 46 /**
47 47 * Start buffering the page content
48 48 */
49 49 public function start_content_process() {
50 -
51 50 $display_webp = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default('quickwebp_settings_conversion_display_webp_mode') );
52 -
53 51 if ( $display_webp != 'picture' ){
54 52 return;
55 53 }
56 54
57 - ob_start( array( $this, 'maybe_process_buffer' ) );
58 -
55 + ob_start( array( $this, 'maybe_process_buffer' ) );
59 56 }
60 57
61 58 /**
62 59 * Maybe process the page content
63 60 */
64 - public function maybe_process_buffer( $buffer ) {
61 + private function maybe_process_buffer( $buffer ) {
65 62
66 63 if ( ! $this->is_html( $buffer ) ) {
67 64 return $buffer;
68 65 }
@@ -76,12 +73,19 @@
76 73
77 74 return $buffer;
78 75 }
79 76
77 + /**
78 + * Tell if a content is HTML
79 + */
80 + private function is_html( $content ) {
81 + return preg_match( '/<\/html>/i', $content );
82 + }
83 +
80 84 /**
81 85 * Process the content
82 86 */
83 - public function process_content( $content ) {
87 + private function process_content( $content ) {
84 88
85 89 $html_no_picture_tags = $this->remove_picture_tags( $content );
86 90 $images = $this->get_images( $html_no_picture_tags );
87 91
@@ -131,33 +135,17 @@
131 135 }
132 136
133 137 foreach ( $images as $i => $image ) {
134 138
135 - if ( empty( $image['src']['webp_exists'] ) || empty( $image['src']['webp_url'] ) ) {
139 + if ( empty( $image['src'] ) ) {
136 140 unset( $images[ $i ] );
137 141 continue;
138 142 }
139 143
140 - unset( $images[ $i ]['src']['webp_path'], $images[ $i ]['src']['webp_exists'] );
141 -
142 144 if ( empty( $image['srcset'] ) || ! is_array( $image['srcset'] ) ) {
143 145 unset( $images[ $i ]['srcset'] );
144 146 continue;
145 147 }
146 -
147 - foreach ( $image['srcset'] as $j => $srcset ) {
148 -
149 - if ( ! is_array( $srcset ) ) {
150 - continue;
151 - }
152 -
153 - if ( empty( $srcset['webp_exists'] ) || empty( $srcset['webp_url'] ) ) {
154 - unset( $images[ $i ]['srcset'][ $j ]['webp_url'] );
155 - }
156 -
157 - unset( $images[ $i ]['srcset'][ $j ]['webp_path'], $images[ $i ]['srcset'][ $j ]['webp_exists'] );
158 - }
159 -
160 148 }
161 149
162 150 return $images;
163 151 }
@@ -162,15 +150,8 @@
162 150 return $images;
163 151 }
164 152
165 153 /**
166 - * Tell if a content is HTML
167 - */
168 - protected function is_html( $content ) {
169 - return preg_match( '/<\/html>/i', $content );
170 - }
171 -
172 - /**
173 154 * Process an image tag and get an array containing some data.
174 155 */
175 156 protected function process_image( $image ) {
176 157
@@ -213,11 +194,18 @@
213 194 // Not a supported image format.
214 195 return false;
215 196 }
216 197
217 - $webp_url = $src['src'] . '.webp';
218 - $webp_path = $this->url_to_path( $webp_url );
219 - $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
198 + $avif_url = $src['src'] . '.avif';
199 + $webp_url = $src['src'] . '.webp';
200 + $avif_path = $this->url_to_path( $avif_url );
201 + $webp_path = $this->url_to_path( $webp_url );
202 + $avif_url .= ! empty( $src['query'] ) ? $src['query'] : '';
203 + $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
204 + $avif_exist = $avif_path && @file_exists( $avif_path );
205 + $webp_exist = $webp_path && @file_exists( $webp_path );
206 + $type = $avif_exist ? 'image/avif' : ( $webp_exist ? 'image/webp' : '' );
207 + $new_url = $avif_exist ? $avif_url : ( $webp_exist ? $webp_url : '' );
220 208
221 209 $data = [
222 210 'tag' => $image,
223 211 'attributes' => $attributes,
@@ -223,14 +211,13 @@
223 211 'attributes' => $attributes,
224 212 'src_attribute' => $src_source,
225 213 'src' => [
226 214 'url' => $attributes[ $src_source ],
227 - 'webp_url' => $webp_url,
228 - 'webp_path' => $webp_path,
229 - 'webp_exists' => $webp_path && @file_exists( $webp_path )
215 + 'new_url' => $new_url,
230 216 ],
231 217 'srcset_attribute' => false,
232 - 'srcset' => []
218 + 'srcset' => [],
219 + 'type' => $type,
233 220 ];
234 221
235 222 // Deal with the srcset attribute.
236 223 $srcset_source = false;
@@ -268,18 +255,23 @@
268 255 ];
269 256 continue;
270 257 }
271 258
272 - $webp_url = $src['src'] . '.webp';
273 - $webp_path = $this->url_to_path( $webp_url );
274 - $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
259 + $avif_url = $src['src'] . '.avif';
260 + $webp_url = $src['src'] . '.webp';
261 + $avif_path = $this->url_to_path( $avif_url );
262 + $webp_path = $this->url_to_path( $webp_url );
263 + $avif_url .= ! empty( $src['query'] ) ? $src['query'] : '';
264 + $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
265 + $avif_exist = $avif_path && @file_exists( $avif_path );
266 + $webp_exist = $webp_path && @file_exists( $webp_path );
267 + $type = $avif_exist ? 'image/avif' : ( $webp_exist ? 'image/webp' : '' );
268 + $new_url = $avif_exist ? $avif_url : ( $webp_exist ? $webp_url : '' );
275 269
276 270 $data['srcset'][] = [
277 - 'url' => $srcs[0],
278 - 'descriptor' => $srcs[1],
279 - 'webp_url' => $webp_url,
280 - 'webp_path' => $webp_path,
281 - 'webp_exists' => $webp_path && @file_exists( $webp_path )
271 + 'url' => $srcs[0],
272 + 'descriptor' => $srcs[1],
273 + 'new_url' => $new_url,
282 274 ];
283 275 }
284 276 }
285 277
@@ -346,9 +338,9 @@
346 338 $root_dir = trailingslashit( wp_normalize_path( ABSPATH ) );
347 339
348 340 } else {
349 341
350 - $document_root = realpath( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) );
342 + $document_root = realpath( sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ?? '' ) ) );
351 343 $document_root = trailingslashit( str_replace( '\\', '/', $document_root ) );
352 344 $path_current_site = trim( str_replace( '\\', '/', PATH_CURRENT_SITE ), '/' );
353 345 $root_dir = trailingslashit( wp_normalize_path( $document_root . $path_current_site ) );
354 346 }
@@ -359,9 +351,8 @@
359 351 $domain_url = $domain_url['scheme'] . '://' . $domain_url['host'] . '/';
360 352 } else {
361 353 $domain_url = false;
362 354 }
363 -
364 355 }
365 356
366 357 // Get the right URL format.
367 358 if ( $domain_url && strpos( $url, '/' ) === 0 ) {
@@ -448,24 +439,24 @@
448 439 protected function build_source_tag( $image ) {
449 440
450 441 $srcset_source = ! empty( $image['srcset_attribute'] ) ? $image['srcset_attribute'] : $image['src_attribute'] . 'set';
451 442 $attributes = [
452 - 'type' => 'image/webp',
443 + 'type' => $image['type'],
453 444 $srcset_source => [],
454 445 ];
455 446
456 447 if ( ! empty( $image['srcset'] ) ) {
457 448 foreach ( $image['srcset'] as $srcset ) {
458 - if ( empty( $srcset['webp_url'] ) ) {
449 + if ( empty( $srcset['new_url'] ) ) {
459 450 continue;
460 451 }
461 452
462 - $attributes[ $srcset_source ][] = $srcset['webp_url'] . ' ' . $srcset['descriptor'];
453 + $attributes[ $srcset_source ][] = $srcset['new_url'] . ' ' . $srcset['descriptor'];
463 454 }
464 455 }
465 456
466 457 if ( empty( $attributes[ $srcset_source ] ) ) {
467 - $attributes[ $srcset_source ][] = $image['src']['webp_url'];
458 + $attributes[ $srcset_source ][] = $image['src']['new_url'];
468 459 }
469 460
470 461 $attributes[ $srcset_source ] = implode( ', ', $attributes[ $srcset_source ] );
471 462
@@ -516,6 +507,5 @@
516 507 }
517 508
518 509 return '<img' . $this->build_attributes( $attributes ) . "/>\n";
519 510 }
520 -
521 -}
511 +}