EWWW Image Optimizer - Version 4.3.2

Version Description

  • changed: prevent dynamic JS/CSS urls within wp-admin/ from being rewritten by ExactDN
  • fixed: auto-convert PNG to JPG was running on images with transparency
  • fixed: Alt WebP broken on sites that have jquery-migrate disabled
Download this release

Release Info

Developer nosilver4u
Plugin Icon 128x128 EWWW Image Optimizer
Version 4.3.2
Comparing to
See all releases

Code changes from version 4.3.1 to 4.3.2

changelog.txt CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  = 4.3.1 =
2
  * fixed: fatal error on older WP versions due to missing privacy policy function
3
 
1
+ = 4.3.2 =
2
+ * changed: prevent dynamic JS/CSS urls within wp-admin/ from being rewritten by ExactDN
3
+ * fixed: auto-convert PNG to JPG was running on images with transparency
4
+ * fixed: Alt WebP broken on sites that have jquery-migrate disabled
5
+
6
  = 4.3.1 =
7
  * fixed: fatal error on older WP versions due to missing privacy policy function
8
 
classes/class-ewwwio-alt-webp.php CHANGED
@@ -681,9 +681,11 @@ class EWWWIO_Alt_Webp extends EWWWIO_Page_Parser {
681
  * Enqueue script dependency for alt webp rewriting when running inline.
682
  */
683
  function load_jquery() {
684
- wp_enqueue_script( 'jquery' );
 
 
685
  ewwwio_debug_message( 'loading webp script with wp_add_inline_script' );
686
- wp_add_inline_script( 'jquery-migrate', $this->inline_script );
687
  }
688
 
689
  /**
681
  * Enqueue script dependency for alt webp rewriting when running inline.
682
  */
683
  function load_jquery() {
684
+ if ( ! wp_script_is( 'jquery', 'done' ) ) {
685
+ wp_enqueue_script( 'jquery' );
686
+ }
687
  ewwwio_debug_message( 'loading webp script with wp_add_inline_script' );
688
+ wp_add_inline_script( 'jquery-core', $this->inline_script );
689
  }
690
 
691
  /**
classes/class-exactdn.php CHANGED
@@ -13,7 +13,7 @@ if ( ! defined( 'ABSPATH' ) ) {
13
  /**
14
  * Enables EWWW IO to filter the page content and replace image urls with ExactDN urls.
15
  */
16
- class ExactDN {
17
 
18
  /**
19
  * Allowed image extensions.
@@ -21,7 +21,7 @@ class ExactDN {
21
  * @access private
22
  * @var array $extensions
23
  */
24
- private $extensions = array(
25
  'gif',
26
  'jpg',
27
  'jpeg',
@@ -148,11 +148,6 @@ class ExactDN {
148
  // DNS prefetching.
149
  add_action( 'wp_head', array( $this, 'dns_prefetch' ) );
150
 
151
- // Helpers for manipulated images.
152
- if ( defined( 'EXACTDN_RECALC' ) && EXACTDN_RECALC ) {
153
- add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 );
154
- }
155
-
156
  // Get all the script/css urls and rewrite them (if enabled).
157
  if ( ewww_image_optimizer_get_option( 'exactdn_all_the_things' ) ) {
158
  add_filter( 'style_loader_src', array( $this, 'parse_enqueue' ), 20 );
@@ -521,56 +516,6 @@ class ExactDN {
521
  }
522
  }
523
 
524
- /**
525
- * Match all images and any relevant <a> tags in a block of HTML.
526
- *
527
- * @param string $content Some HTML.
528
- * @return array An array of $images matches, where $images[0] is
529
- * an array of full matches, and the link_url, img_tag,
530
- * and img_url keys are arrays of those matches.
531
- */
532
- function parse_images_from_html( $content ) {
533
- ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
534
- $images = array();
535
-
536
- if ( preg_match_all( '#(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><img[^>]*?\s+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*</a>)?#is', $content, $images ) ) {
537
- foreach ( $images as $key => $unused ) {
538
- // Simplify the output as much as possible, mostly for confirming test results.
539
- if ( is_numeric( $key ) && $key > 0 ) {
540
- unset( $images[ $key ] );
541
- }
542
- }
543
- return $images;
544
- }
545
- return array();
546
- }
547
-
548
- /**
549
- * Try to determine height and width from strings WP appends to resized image filenames.
550
- *
551
- * @param string $src The image URL.
552
- * @return array An array consisting of width and height.
553
- */
554
- function parse_dimensions_from_filename( $src ) {
555
- ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
556
- $width_height_string = array();
557
- ewwwio_debug_message( "looking for dimensions in $src" );
558
- if ( preg_match( '#-(\d+)x(\d+)(@2x)?\.(?:' . implode( '|', $this->extensions ) . '){1}(?:\?.+)?$#i', $src, $width_height_string ) ) {
559
- $width = (int) $width_height_string[1];
560
- $height = (int) $width_height_string[2];
561
-
562
- if ( strpos( $src, '@2x' ) ) {
563
- $width = 2 * $width;
564
- $height = 2 * $height;
565
- }
566
- if ( $width && $height ) {
567
- ewwwio_debug_message( "found w$width h$height" );
568
- return array( $width, $height );
569
- }
570
- }
571
- return array( false, false );
572
- }
573
-
574
  /**
575
  * Get $content_width, with a filter.
576
  *
@@ -618,7 +563,7 @@ class ExactDN {
618
  function filter_the_content( $content ) {
619
  $started = microtime( true );
620
  ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
621
- $images = $this->parse_images_from_html( $content );
622
 
623
  if ( ! empty( $images ) ) {
624
  ewwwio_debug_message( 'we have images to parse' );
@@ -642,6 +587,9 @@ class ExactDN {
642
  // Flag if we need to munge a fullsize URL.
643
  $fullsize_url = false;
644
 
 
 
 
645
  // Identify image source.
646
  $src = $images['img_url'][ $index ];
647
  $src_orig = $images['img_url'][ $index ];
@@ -681,22 +629,17 @@ class ExactDN {
681
  // Check if image URL should be used with ExactDN.
682
  if ( $this->validate_image_url( $src ) ) {
683
  ewwwio_debug_message( 'url validated' );
684
- // Find the width and height attributes.
685
- $width = false;
686
- $height = false;
687
 
 
688
  // First, check the image tag.
689
- if ( preg_match( '#width=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $width_string ) ) {
690
- $width = $width_string[1];
691
- }
692
  if ( preg_match( '#max-width:\s?(\d+)px#', $images['img_tag'][ $index ], $max_width_string ) ) {
693
  if ( $max_width_string[1] && ( ! $width || $max_width_string[1] < $width ) ) {
694
  $width = $max_width_string[1];
695
  }
696
  }
697
- if ( preg_match( '#height=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $height_string ) ) {
698
- $height = $height_string[1];
699
- }
700
 
701
  // Can't pass both a relative width and height, so unset the dimensions in favor of not breaking the horizontal layout.
702
  if ( false !== strpos( $width, '%' ) && false !== strpos( $height, '%' ) ) {
@@ -718,7 +661,7 @@ class ExactDN {
718
  unset( $size );
719
  }
720
 
721
- list( $filename_width, $filename_height ) = $this->parse_dimensions_from_filename( $src );
722
  // WP Attachment ID, if uploaded to this site.
723
  preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id );
724
  if ( ! ewww_image_optimizer_get_option( 'exactdn_prevent_db_queries' ) && empty( $attachment_id ) ) {
@@ -755,6 +698,7 @@ class ExactDN {
755
  }
756
 
757
  if ( $src_per_wp && $this->validate_image_url( $src_per_wp[0] ) ) {
 
758
  ewwwio_debug_message( "detected $width filenamew $filename_width" );
759
  if ( $resize_existing || ( $width && $filename_width != $width ) ) {
760
  ewwwio_debug_message( 'resizing existing or width does not match' );
@@ -896,23 +840,15 @@ class ExactDN {
896
  unset( $placeholder_src );
897
  }
898
 
899
- // Enable image dimension recalculation via wp-config.php.
900
- if ( defined( 'EXACTDN_RECALC' ) && EXACTDN_RECALC ) {
901
- // Remove the width and height arguments from the tag to prevent distortion.
902
- $new_tag = preg_replace( '#(?<=\s)(width|height)=["|\']?[\d%]+["|\']?\s?#i', '', $new_tag );
903
-
904
- // Tag an image for dimension checking (via JS).
905
- $new_tag = preg_replace( '#(\s?/)?>(\s*</a>)?$#i', ' data-recalc-dims="1"\1>\2', $new_tag );
906
- }
907
  // Replace original tag with modified version.
908
  $content = str_replace( $tag, $new_tag, $content );
909
  }
910
  } elseif ( ! preg_match( '#data-lazy-(original|src)=#i', $images['img_tag'][ $index ] ) && $this->validate_image_url( $src, true ) ) {
911
  ewwwio_debug_message( 'found a potential exactdn src url to insert into srcset' );
912
  // Find the width attribute.
913
- $width = false;
914
  // First, check the image tag.
915
- if ( preg_match( '#width=["|\']?([\d%]+)["|\']?#i', $tag, $width_string ) ) {
916
  $width = $width_string[1];
917
  ewwwio_debug_message( 'found the width' );
918
  // Insert new image src into the srcset as well, if we have a width.
@@ -943,6 +879,19 @@ class ExactDN {
943
  }
944
  }
945
  } // End if().
 
 
 
 
 
 
 
 
 
 
 
 
 
946
  } // End foreach().
947
  } // End if();
948
  if ( $this->filtering_the_page && ewww_image_optimizer_get_option( 'exactdn_all_the_things' ) ) {
@@ -1165,7 +1114,7 @@ class ExactDN {
1165
  $image_url_basename = wp_basename( $image_url );
1166
  $intermediate_url = str_replace( $image_url_basename, $image_meta['sizes'][ $size ]['file'], $image_url );
1167
 
1168
- list( $filename_width, $filename_height ) = $this->parse_dimensions_from_filename( $intermediate_url );
1169
  if ( $filename_width && $filename_height && $image_args['width'] === $filename_width && $image_args['height'] === $filename_height ) {
1170
  $image_url = $intermediate_url;
1171
  } else {
@@ -1331,7 +1280,7 @@ class ExactDN {
1331
 
1332
  $url = $source['url'];
1333
 
1334
- list( $width, $height ) = $this->parse_dimensions_from_filename( $url );
1335
  if ( ! $resize_existing && 'w' === $source['descriptor'] && $source['value'] == $width ) {
1336
  ewwwio_debug_message( "preventing further processing for $url" );
1337
  $sources[ $i ]['url'] = $this->generate_url( $source['url'] );
@@ -1875,6 +1824,12 @@ class ExactDN {
1875
  }
1876
  }
1877
  }
 
 
 
 
 
 
1878
  // Unable to parse.
1879
  if ( ! $parsed_url || ! is_array( $parsed_url ) || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
1880
  ewwwio_debug_message( 'src url no good' );
13
  /**
14
  * Enables EWWW IO to filter the page content and replace image urls with ExactDN urls.
15
  */
16
+ class ExactDN extends EWWWIO_Page_Parser {
17
 
18
  /**
19
  * Allowed image extensions.
21
  * @access private
22
  * @var array $extensions
23
  */
24
+ protected $extensions = array(
25
  'gif',
26
  'jpg',
27
  'jpeg',
148
  // DNS prefetching.
149
  add_action( 'wp_head', array( $this, 'dns_prefetch' ) );
150
 
 
 
 
 
 
151
  // Get all the script/css urls and rewrite them (if enabled).
152
  if ( ewww_image_optimizer_get_option( 'exactdn_all_the_things' ) ) {
153
  add_filter( 'style_loader_src', array( $this, 'parse_enqueue' ), 20 );
516
  }
517
  }
518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
  /**
520
  * Get $content_width, with a filter.
521
  *
563
  function filter_the_content( $content ) {
564
  $started = microtime( true );
565
  ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
566
+ $images = $this->get_images_from_html( $content, true );
567
 
568
  if ( ! empty( $images ) ) {
569
  ewwwio_debug_message( 'we have images to parse' );
587
  // Flag if we need to munge a fullsize URL.
588
  $fullsize_url = false;
589
 
590
+ // To be stored and used for later srcset generation.
591
+ $original_width = 0;
592
+
593
  // Identify image source.
594
  $src = $images['img_url'][ $index ];
595
  $src_orig = $images['img_url'][ $index ];
629
  // Check if image URL should be used with ExactDN.
630
  if ( $this->validate_image_url( $src ) ) {
631
  ewwwio_debug_message( 'url validated' );
 
 
 
632
 
633
+ // Find the width and height attributes.
634
  // First, check the image tag.
635
+ $width = $this->get_attribute( $images['img_tag'][ $index ], 'width' );
636
+ // Then check for an inline max-width directive.
 
637
  if ( preg_match( '#max-width:\s?(\d+)px#', $images['img_tag'][ $index ], $max_width_string ) ) {
638
  if ( $max_width_string[1] && ( ! $width || $max_width_string[1] < $width ) ) {
639
  $width = $max_width_string[1];
640
  }
641
  }
642
+ $height = $this->get_attribute( $images['img_tag'][ $index ], 'height' );
 
 
643
 
644
  // Can't pass both a relative width and height, so unset the dimensions in favor of not breaking the horizontal layout.
645
  if ( false !== strpos( $width, '%' ) && false !== strpos( $height, '%' ) ) {
661
  unset( $size );
662
  }
663
 
664
+ list( $filename_width, $filename_height ) = $this->get_dimensions_from_filename( $src );
665
  // WP Attachment ID, if uploaded to this site.
666
  preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id );
667
  if ( ! ewww_image_optimizer_get_option( 'exactdn_prevent_db_queries' ) && empty( $attachment_id ) ) {
698
  }
699
 
700
  if ( $src_per_wp && $this->validate_image_url( $src_per_wp[0] ) ) {
701
+ $original_width = $src_per_wp[1];
702
  ewwwio_debug_message( "detected $width filenamew $filename_width" );
703
  if ( $resize_existing || ( $width && $filename_width != $width ) ) {
704
  ewwwio_debug_message( 'resizing existing or width does not match' );
840
  unset( $placeholder_src );
841
  }
842
 
 
 
 
 
 
 
 
 
843
  // Replace original tag with modified version.
844
  $content = str_replace( $tag, $new_tag, $content );
845
  }
846
  } elseif ( ! preg_match( '#data-lazy-(original|src)=#i', $images['img_tag'][ $index ] ) && $this->validate_image_url( $src, true ) ) {
847
  ewwwio_debug_message( 'found a potential exactdn src url to insert into srcset' );
848
  // Find the width attribute.
849
+ $width = $this->get_attribute( $tag, 'width' );
850
  // First, check the image tag.
851
+ if ( $width ) {
852
  $width = $width_string[1];
853
  ewwwio_debug_message( 'found the width' );
854
  // Insert new image src into the srcset as well, if we have a width.
879
  }
880
  }
881
  } // End if().
882
+ if ( ! preg_match( '#data-lazy-(original|src)=#i', $images['img_tag'][ $index ] ) && $this->validate_image_url( $src, true ) ) {
883
+ if ( ! $this->get_attribute( $images['img_tag'][ $index ], 'srcset' ) && ! $this->get_attribute( $images['img_tag'][ $index ], 'sizes' ) ) {
884
+ list( $filename_width, $filename_height ) = $this->get_dimensions_from_filename( $src );
885
+ // Alright, we look for $filename_width or $original_width, otherwise try to grok the dimensions if it's a local image.
886
+ // Also need to somehow check to be sure we don't end up with anything larger than the width as configured above...
887
+ // --perhaps that can be accomplished just by ensuring all srcset images are smaller than $width.
888
+ // --probably only way that happens is if the $fullsize_url is used/found.
889
+ // Then add a srcset and sizes.
890
+ if ( $width ) { // We somehow have to get the actual width of the image, this is mandatory for srcset calculation.
891
+ } else {
892
+ }
893
+ }
894
+ }
895
  } // End foreach().
896
  } // End if();
897
  if ( $this->filtering_the_page && ewww_image_optimizer_get_option( 'exactdn_all_the_things' ) ) {
1114
  $image_url_basename = wp_basename( $image_url );
1115
  $intermediate_url = str_replace( $image_url_basename, $image_meta['sizes'][ $size ]['file'], $image_url );
1116
 
1117
+ list( $filename_width, $filename_height ) = $this->get_dimensions_from_filename( $intermediate_url );
1118
  if ( $filename_width && $filename_height && $image_args['width'] === $filename_width && $image_args['height'] === $filename_height ) {
1119
  $image_url = $intermediate_url;
1120
  } else {
1280
 
1281
  $url = $source['url'];
1282
 
1283
+ list( $width, $height ) = $this->get_dimensions_from_filename( $url );
1284
  if ( ! $resize_existing && 'w' === $source['descriptor'] && $source['value'] == $width ) {
1285
  ewwwio_debug_message( "preventing further processing for $url" );
1286
  $sources[ $i ]['url'] = $this->generate_url( $source['url'] );
1824
  }
1825
  }
1826
  }
1827
+ if ( false !== strpos( $url, 'wp-admin/' ) ) {
1828
+ return $url;
1829
+ }
1830
+ if ( false !== strpos( $url, 'xmlrpc.php' ) ) {
1831
+ return $url;
1832
+ }
1833
  // Unable to parse.
1834
  if ( ! $parsed_url || ! is_array( $parsed_url ) || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
1835
  ewwwio_debug_message( 'src url no good' );
common.php CHANGED
@@ -29,7 +29,7 @@ if ( ! defined( 'ABSPATH' ) ) {
29
  exit;
30
  }
31
 
32
- define( 'EWWW_IMAGE_OPTIMIZER_VERSION', '431.0' );
33
 
34
  // Initialize a couple globals.
35
  $ewww_debug = '';
@@ -194,6 +194,10 @@ register_uninstall_hook( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE, 'ewww_image_optimizer
194
  add_action( 'shutdown', 'ewww_image_optimizer_debug_log' );
195
  // If ExactDN is enabled.
196
  if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) && empty( $_GET['exactdn_disable'] ) ) {
 
 
 
 
197
  /**
198
  * ExactDN class for parsing image urls and rewriting them.
199
  */
@@ -4284,7 +4288,12 @@ function ewww_image_optimizer_autoconvert( $file ) {
4284
  return;
4285
  }
4286
  $orig_size = ewww_image_optimizer_filesize( $file );
4287
- if ( $orig_size < 350000 ) {
 
 
 
 
 
4288
  return;
4289
  }
4290
  $ewww_image = new EWWW_Image( 0, '', $file );
29
  exit;
30
  }
31
 
32
+ define( 'EWWW_IMAGE_OPTIMIZER_VERSION', '432.0' );
33
 
34
  // Initialize a couple globals.
35
  $ewww_debug = '';
194
  add_action( 'shutdown', 'ewww_image_optimizer_debug_log' );
195
  // If ExactDN is enabled.
196
  if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) && empty( $_GET['exactdn_disable'] ) ) {
197
+ /**
198
+ * Page Parsing class for working with HTML content.
199
+ */
200
+ require_once( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-ewwwio-page-parser.php' );
201
  /**
202
  * ExactDN class for parsing image urls and rewriting them.
203
  */
4288
  return;
4289
  }
4290
  $orig_size = ewww_image_optimizer_filesize( $file );
4291
+ if ( $orig_size < 300000 ) {
4292
+ ewwwio_debug_message( 'not a large PNG, skipping' );
4293
+ return;
4294
+ }
4295
+ if ( ewww_image_optimizer_png_alpha( $file ) && ! ewww_image_optimizer_jpg_background() ) {
4296
+ ewwwio_debug_message( 'alpha detected, skipping' );
4297
  return;
4298
  }
4299
  $ewww_image = new EWWW_Image( 0, '', $file );
ewww-image-optimizer.php CHANGED
@@ -14,7 +14,7 @@ Plugin URI: https://wordpress.org/plugins/ewww-image-optimizer/
14
  Description: Reduce file sizes for images within WordPress including NextGEN Gallery and GRAND FlAGallery. Uses jpegtran, optipng/pngout, and gifsicle.
15
  Author: Shane Bishop
16
  Text Domain: ewww-image-optimizer
17
- Version: 4.3.1
18
  Author URI: https://ewww.io/
19
  License: GPLv3
20
  */
14
  Description: Reduce file sizes for images within WordPress including NextGEN Gallery and GRAND FlAGallery. Uses jpegtran, optipng/pngout, and gifsicle.
15
  Author: Shane Bishop
16
  Text Domain: ewww-image-optimizer
17
+ Version: 4.3.2
18
  Author URI: https://ewww.io/
19
  License: GPLv3
20
  */
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: image, compress, resize, optimize, optimization, lossless, lossy, seo, web
5
  Requires at least: 4.6
6
  Tested up to: 4.9
7
  Requires PHP: 5.4
8
- Stable tag: 4.3.1
9
  License: GPLv3
10
 
11
  Speed up your website and improve your visitors' experience by automatically compressing and resizing images and PDFs. Boost SEO and improve sales.
@@ -174,6 +174,11 @@ http://developer.yahoo.com/performance/rules.html#opt_images
174
  * Feature requests can be submitted via https://ewww.io/contact-us/ and commented on here: https://trello.com/b/Fp81dWof/ewww-image-optimizer
175
  * If you would like to help translate this plugin in your language, get started here: https://translate.wordpress.org/projects/wp-plugins/ewww-image-optimizer/
176
 
 
 
 
 
 
177
  = 4.3.1 =
178
  * fixed: fatal error on older WP versions due to missing privacy policy function
179
 
5
  Requires at least: 4.6
6
  Tested up to: 4.9
7
  Requires PHP: 5.4
8
+ Stable tag: 4.3.2
9
  License: GPLv3
10
 
11
  Speed up your website and improve your visitors' experience by automatically compressing and resizing images and PDFs. Boost SEO and improve sales.
174
  * Feature requests can be submitted via https://ewww.io/contact-us/ and commented on here: https://trello.com/b/Fp81dWof/ewww-image-optimizer
175
  * If you would like to help translate this plugin in your language, get started here: https://translate.wordpress.org/projects/wp-plugins/ewww-image-optimizer/
176
 
177
+ = 4.3.2 =
178
+ * changed: prevent dynamic JS/CSS urls within wp-admin/ from being rewritten by ExactDN
179
+ * fixed: auto-convert PNG to JPG was running on images with transparency
180
+ * fixed: Alt WebP broken on sites that have jquery-migrate disabled
181
+
182
  = 4.3.1 =
183
  * fixed: fatal error on older WP versions due to missing privacy policy function
184