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