SG Optimizer - Version 5.1.1

Version Description

Download this release

Release Info

Developer sstoqnov
Plugin Icon 128x128 SG Optimizer
Version 5.1.1
Comparing to
See all releases

Code changes from version 5.1.0 to 5.1.1

core/Combinator/Combinator.php CHANGED
@@ -138,11 +138,12 @@ class Combinator {
138
 
139
  // Get groups of handles.
140
  foreach ( $styles->to_do as $handle ) {
 
141
  if (
142
  ( true === $in_header && $styles->groups[ $handle ] > 0 ) || // Bail if the style is not in the header/footer.
143
  in_array( $handle, $excluded_styles ) || // If the style is excluded from combination.
144
  false === $wp_styles->registered[ $handle ]->src || // If the source is empty.
145
- strpos( Helper::get_home_url(), parse_url( $wp_styles->registered[ $handle ]->src, PHP_URL_HOST ) ) === false || // Skip all external sources.
146
  pathinfo( $wp_styles->registered[ $handle ]->src, PATHINFO_EXTENSION ) === 'php' // If it's dynamically generated css.
147
  ) {
148
  continue;
@@ -174,8 +175,11 @@ class Combinator {
174
  return;
175
  }
176
 
 
 
177
  // Finally change the source to combined style.
178
- $wp_styles->registered[ $combined_styles_handle ]->src = $this->create_temp_style_and_get_url( $content, $combined_styles_handle );
 
179
  }
180
 
181
  /**
@@ -257,8 +261,13 @@ class Combinator {
257
  $new_file = $this->assets_dir . 'siteground-optimizer-combined-styles-' . $style_hash . '.css';
258
  $url = str_replace( ABSPATH, Helper::get_home_url(), $new_file );
259
 
 
 
 
 
 
260
  if ( is_file( $new_file ) ) {
261
- return $url;
262
  }
263
 
264
  // Create the new file.
@@ -270,7 +279,7 @@ class Combinator {
270
  $this->get_style_content_with_replacements( $content )
271
  );
272
 
273
- return $url;
274
 
275
  }
276
  }
138
 
139
  // Get groups of handles.
140
  foreach ( $styles->to_do as $handle ) {
141
+
142
  if (
143
  ( true === $in_header && $styles->groups[ $handle ] > 0 ) || // Bail if the style is not in the header/footer.
144
  in_array( $handle, $excluded_styles ) || // If the style is excluded from combination.
145
  false === $wp_styles->registered[ $handle ]->src || // If the source is empty.
146
+ strpos( Helper::get_home_url(), chr( parse_url( $wp_styles->registered[ $handle ]->src, PHP_URL_HOST ) ) ) === false || // Skip all external sources.
147
  pathinfo( $wp_styles->registered[ $handle ]->src, PATHINFO_EXTENSION ) === 'php' // If it's dynamically generated css.
148
  ) {
149
  continue;
175
  return;
176
  }
177
 
178
+ $new_file_data = $this->create_temp_style_and_get_url( $content, $combined_styles_handle );
179
+
180
  // Finally change the source to combined style.
181
+ $wp_styles->registered[ $combined_styles_handle ]->src = $new_file_data['url'];
182
+ $wp_styles->registered[ $combined_styles_handle ]->handle = $new_file_data['handle'];
183
  }
184
 
185
  /**
261
  $new_file = $this->assets_dir . 'siteground-optimizer-combined-styles-' . $style_hash . '.css';
262
  $url = str_replace( ABSPATH, Helper::get_home_url(), $new_file );
263
 
264
+ $data = array(
265
+ 'handle' => 'siteground-optimizer-combined-styles-' . $style_hash,
266
+ 'url' => $url,
267
+ );
268
+
269
  if ( is_file( $new_file ) ) {
270
+ return $data;
271
  }
272
 
273
  // Create the new file.
279
  $this->get_style_content_with_replacements( $content )
280
  );
281
 
282
+ return $data;
283
 
284
  }
285
  }
core/Front_End_Optimization/Front_End_Optimization.php CHANGED
@@ -64,11 +64,17 @@ class Front_End_Optimization {
64
 
65
  self::$instance = $this;
66
 
 
 
 
 
 
 
 
 
 
67
  // Remove query strings only if the option is emabled.
68
- if (
69
- Options::is_enabled( 'siteground_optimizer_remove_query_strings' ) &&
70
- ! is_admin()
71
- ) {
72
  // Filters for static style and script loaders.
73
  add_filter( 'style_loader_src', array( $this, 'remove_query_strings' ) );
74
  add_filter( 'script_loader_src', array( $this, 'remove_query_strings' ) );
@@ -84,20 +90,12 @@ class Front_End_Optimization {
84
  new Lazy_Load_Images();
85
  }
86
 
87
- // Enabled lazy load images.
88
- if ( Options::is_enabled( 'siteground_optimizer_optimize_images' ) ) {
89
- new Images_Optimizer();
90
- }
91
-
92
  if ( Options::is_enabled( 'siteground_optimizer_combine_css' ) ) {
93
  new Combinator();
94
  }
95
 
96
  // Enabled async load js files.
97
- if (
98
- Options::is_enabled( 'siteground_optimizer_optimize_javascript_async' ) &&
99
- ! is_admin()
100
- ) {
101
  add_action( 'wp_print_scripts', array( $this, 'prepare_scripts_for_async_load' ), PHP_INT_MAX );
102
 
103
  // Add async attr to all scripts.
64
 
65
  self::$instance = $this;
66
 
67
+ // Enabled lazy load images.
68
+ if ( Options::is_enabled( 'siteground_optimizer_optimize_images' ) ) {
69
+ new Images_Optimizer();
70
+ }
71
+
72
+ if ( is_admin() ) {
73
+ return;
74
+ }
75
+
76
  // Remove query strings only if the option is emabled.
77
+ if ( Options::is_enabled( 'siteground_optimizer_remove_query_strings' ) ) {
 
 
 
78
  // Filters for static style and script loaders.
79
  add_filter( 'style_loader_src', array( $this, 'remove_query_strings' ) );
80
  add_filter( 'script_loader_src', array( $this, 'remove_query_strings' ) );
90
  new Lazy_Load_Images();
91
  }
92
 
 
 
 
 
 
93
  if ( Options::is_enabled( 'siteground_optimizer_combine_css' ) ) {
94
  new Combinator();
95
  }
96
 
97
  // Enabled async load js files.
98
+ if ( Options::is_enabled( 'siteground_optimizer_optimize_javascript_async' ) ) {
 
 
 
99
  add_action( 'wp_print_scripts', array( $this, 'prepare_scripts_for_async_load' ), PHP_INT_MAX );
100
 
101
  // Add async attr to all scripts.
core/Lazy_Load_Images/Lazy_Load_Images.php CHANGED
@@ -84,7 +84,6 @@ class Lazy_Load_Images {
84
  true
85
  );
86
  }
87
-
88
  }
89
 
90
  /**
@@ -112,39 +111,57 @@ class Lazy_Load_Images {
112
 
113
  preg_match_all( '/<img[\s\r\n]+.*?>/is', $content, $matches );
114
 
115
- $search = array();
116
  $replace = array();
117
 
118
- foreach ( $matches[0] as $img_html ) {
119
  // Skip already replaced images.
120
- if ( preg_match( "/src=['\"]data:image/is", $img_html ) || preg_match( "/src=.*lazy_placeholder.gif['\"]/s", $img_html ) ) {
121
  continue;
122
  }
123
 
124
- // Replace the src and add the data-src attribute.
125
- $replace_html = '';
126
- $replace_html = preg_replace( '/<img(.*?)src=/is', '<img$1src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src=', $img_html );
127
- $replace_html = preg_replace( '/<img(.*?)srcset=/is', '<img$1srcset="" data-srcset=', $replace_html );
 
 
128
 
129
- // Add the lazy class to the img element.
130
- if ( preg_match( '/class=["\']/i', $replace_html ) ) {
131
- $replace_html = preg_replace( '/class=(["\'])(.*?)["\']/is', 'class=$1lazy lazy-hidden $2$1', $replace_html );
132
- } else {
133
- $replace_html = preg_replace( '/<img/is', '<img class="lazy lazy-hidden"', $replace_html );
 
 
134
  }
135
 
136
- $replace_html .= '<noscript>' . $img_html . '</noscript>';
 
 
 
 
 
137
 
138
- array_push( $search, $img_html );
139
- array_push( $replace, $replace_html );
140
- }
 
 
 
141
 
142
- $search = array_unique( $search );
143
- $replace = array_unique( $replace );
 
 
 
 
144
 
145
- $content = str_replace( $search, $replace, $content );
 
 
146
 
147
- return $content;
148
  }
149
 
150
  /**
84
  true
85
  );
86
  }
 
87
  }
88
 
89
  /**
111
 
112
  preg_match_all( '/<img[\s\r\n]+.*?>/is', $content, $matches );
113
 
114
+ $search = array();
115
  $replace = array();
116
 
117
+ foreach ( $matches[0] as $image ) {
118
  // Skip already replaced images.
119
+ if ( preg_match( "/src=['\"]data:image/is", $image ) ) {
120
  continue;
121
  }
122
 
123
+ // Get image classes.
124
+ preg_match( '/class=["\'](.*?)["\']/is', $image, $class_matches );
125
+
126
+ if ( ! empty( $class_matches[1] ) ) {
127
+ // Load the ignored image classes.
128
+ $ignored_classes = apply_filters( 'sgo_lazy_load_exclude_classes', array() );
129
 
130
+ // Convert all classes to array.
131
+ $image_classes = explode( ' ', $class_matches[1] );
132
+
133
+ // Check if the image has ignored class and bail if has.
134
+ if ( array_intersect( $image_classes, $ignored_classes ) ) {
135
+ continue;
136
+ }
137
  }
138
 
139
+ // Search patterns.
140
+ $patterns = array(
141
+ '/(?<!noscript\>)((<img.*?src=["|\'].*?["|\']).*?(\/?>))/i',
142
+ '/(?<!noscript\>)(<img.*?)(src)=["|\']((?!data).*?)["|\']/i',
143
+ '/(?<!noscript\>)(<img.*?)((srcset)=["|\'](.*?)["|\'])/i',
144
+ );
145
 
146
+ // Replacements.
147
+ $replacements = array(
148
+ '$1<noscript>$2$3</noscript>',
149
+ '$1src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-$2="$3"',
150
+ '$1data-$3="$4"',
151
+ );
152
 
153
+ // Finally do the search/replace and return modified content.
154
+ $new_image = preg_replace(
155
+ $patterns,
156
+ $replacements,
157
+ $image
158
+ );
159
 
160
+ array_push( $search, $image );
161
+ array_push( $replace, $new_image );
162
+ }
163
 
164
+ return str_replace( $search, $replace, $content );
165
  }
166
 
167
  /**
core/Minifier/Minifier.php CHANGED
@@ -78,7 +78,6 @@ class Minifier {
78
 
79
  if (
80
  Options::is_enabled( 'siteground_optimizer_optimize_html' ) &&
81
- ! $this->is_blacklisted_url() &&
82
  ! defined( 'WP_CLI' )
83
  ) {
84
  // Add the hooks that we will use t ominify the html.
@@ -244,7 +243,7 @@ class Minifier {
244
  $original_filepath = Front_End_Optimization::get_original_filepath( $wp_styles->registered[ $handle ]->src );
245
 
246
  // Build the minified version filename.
247
- $filename = dirname( $original_filepath ) . '/' . $handle . '.min.css';
248
 
249
  // Check for original file modifications and create the minified copy.
250
  $is_minified_file_ok = $this->check_and_create_file( $filename, $original_filepath );
@@ -278,6 +277,11 @@ class Minifier {
278
  * @since 5.0.0
279
  */
280
  public function start_html_minifier_buffer() {
 
 
 
 
 
281
  ob_start( array( $this, 'minify_html' ) );
282
  }
283
 
@@ -293,23 +297,53 @@ class Minifier {
293
  }
294
 
295
  /**
296
- * Check if the current url has params that are blacklisted.
297
  *
298
  * @since 5.1.0
299
  *
300
- * @return boolean True if the url is blacklisted, false otherwise.
301
  */
302
- public function is_blacklisted_url() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  if ( ! isset( $_REQUEST ) ) {
304
  return false;
305
  }
306
 
307
- $blacklisted_keys = array(
308
- 'pdf-catalog',
309
- );
310
 
311
- foreach ( $blacklisted_keys as $key ) {
312
- if ( array_key_exists( $key, $_REQUEST ) ) {
 
313
  return true;
314
  }
315
  }
78
 
79
  if (
80
  Options::is_enabled( 'siteground_optimizer_optimize_html' ) &&
 
81
  ! defined( 'WP_CLI' )
82
  ) {
83
  // Add the hooks that we will use t ominify the html.
243
  $original_filepath = Front_End_Optimization::get_original_filepath( $wp_styles->registered[ $handle ]->src );
244
 
245
  // Build the minified version filename.
246
+ $filename = dirname( $original_filepath ) . '/' . $wp_styles->registered[ $handle ]->handle . '.min.css';
247
 
248
  // Check for original file modifications and create the minified copy.
249
  $is_minified_file_ok = $this->check_and_create_file( $filename, $original_filepath );
277
  * @since 5.0.0
278
  */
279
  public function start_html_minifier_buffer() {
280
+ // Do not minify the html if the current url is excluded.
281
+ if ( $this->is_url_excluded() ) {
282
+ return;
283
+ }
284
+
285
  ob_start( array( $this, 'minify_html' ) );
286
  }
287
 
297
  }
298
 
299
  /**
300
+ * Check if the current url has params that are excluded.
301
  *
302
  * @since 5.1.0
303
  *
304
+ * @return boolean True if the url is excluded, false otherwise.
305
  */
306
+ public function is_url_excluded() {
307
+ $protocol = isset( $_SERVER['HTTPS'] ) ? 'https' : 'http';
308
+
309
+ // Build the current url.
310
+ $url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
311
+
312
+ // Remove the query params from the url.
313
+ $plain_url = trailingslashit( $protocol . '://' . $_SERVER['HTTP_HOST'] . parse_url( $_SERVER["REQUEST_URI"], PHP_URL_PATH ) );
314
+
315
+ // Get excluded urls.
316
+ $excluded_urls = apply_filters( 'sgo_html_minify_exclude_urls', array() );
317
+
318
+ foreach ( $excluded_urls as $excluded_url ) {
319
+
320
+ // Add trailingslash if the excluded url doesn't have query params.
321
+ if ( ! parse_url( $excluded_url, PHP_URL_QUERY ) ) {
322
+ $excluded_url = trailingslashit( $excluded_url );
323
+
324
+ // Check if the current url matches the excluded url.
325
+ if ( $plain_url === $excluded_url ) {
326
+ return true;
327
+ }
328
+ }
329
+
330
+ // Compare the original url with excluded url provided from the client.
331
+ if ( $url === $excluded_url ) {
332
+ return true;
333
+ }
334
+ }
335
+
336
+ // If there are no params we don't need to check the query params.
337
  if ( ! isset( $_REQUEST ) ) {
338
  return false;
339
  }
340
 
341
+ // Get excluded params.
342
+ $excluded_params = apply_filters( 'sgo_html_minify_exclude_params', array( 'pdf-catalog' ) );
 
343
 
344
+ // Check if any of the excluded params exists in the request.
345
+ foreach ( $excluded_params as $param ) {
346
+ if ( array_key_exists( $param, $_REQUEST ) ) {
347
  return true;
348
  }
349
  }
core/Supercacher/Supercacher.php CHANGED
@@ -66,6 +66,7 @@ class Supercacher {
66
  add_action( 'update_option_siteground_optimizer_enable_cache', array( $this, 'purge_everything' ) );
67
  add_action( 'update_option_siteground_optimizer_autoflush_cache', array( $this, 'purge_everything' ) );
68
  add_action( 'update_option_siteground_optimizer_enable_memcached', array( $this, 'purge_everything' ) );
 
69
 
70
  // Delete assets (minified js and css files) every 30 days.
71
  add_action( 'siteground_delete_assets', array( $this, 'delete_assets' ) );
66
  add_action( 'update_option_siteground_optimizer_enable_cache', array( $this, 'purge_everything' ) );
67
  add_action( 'update_option_siteground_optimizer_autoflush_cache', array( $this, 'purge_everything' ) );
68
  add_action( 'update_option_siteground_optimizer_enable_memcached', array( $this, 'purge_everything' ) );
69
+ add_action( 'update_option_siteground_optimizer_combine_css', array( $this, 'delete_assets' ) );
70
 
71
  // Delete assets (minified js and css files) every 30 days.
72
  add_action( 'siteground_delete_assets', array( $this, 'delete_assets' ) );
readme.txt CHANGED
@@ -41,15 +41,15 @@ Here, you can enable or disable optimization for your newly uploaded images, bul
41
 
42
  If your plugin does not trigger standard WordPress hooks or you need us to purge the cache, you can use this public function in your code:
43
 
44
- if (function_exists('sg_cachepress_purge_cache')) {
45
- sg_cachepress_purge_cache();
46
- }
47
 
48
  Preferrably, you can pass an URL to the function to clear the cache just for it instead of purging the entire cache. For example:
49
 
50
- if (function_exists('sg_cachepress_purge_cache')) {
51
- sg_cachepress_purge_cache('https://yoursite.com/pluginpage');
52
- }
53
 
54
  You can exclude styles from being combined and minified using the filters we’ve designed for that purpose. Here’s an example of the code, you can add to your functions.php file:
55
 
@@ -91,6 +91,34 @@ You can exclude script from being loaded asynchronous using the filter we’ve d
91
  return $exclude_list;
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  = WP-CLI Support =
95
 
96
  In version 5.0 we've added full WP-CLI support for all plugin options and functionalities.
@@ -141,6 +169,14 @@ Our plugin uses a cookie in order to function properly. It does not store person
141
 
142
  == Changelog ==
143
 
 
 
 
 
 
 
 
 
144
  = Version 5.1.0 =
145
  * Added CSS Combination Functionality
146
  * Added Async Load of Render-Blocking JS
41
 
42
  If your plugin does not trigger standard WordPress hooks or you need us to purge the cache, you can use this public function in your code:
43
 
44
+ if (function_exists('sg_cachepress_purge_cache')) {
45
+ sg_cachepress_purge_cache();
46
+ }
47
 
48
  Preferrably, you can pass an URL to the function to clear the cache just for it instead of purging the entire cache. For example:
49
 
50
+ if (function_exists('sg_cachepress_purge_cache')) {
51
+ sg_cachepress_purge_cache('https://yoursite.com/pluginpage');
52
+ }
53
 
54
  You can exclude styles from being combined and minified using the filters we’ve designed for that purpose. Here’s an example of the code, you can add to your functions.php file:
55
 
91
  return $exclude_list;
92
  }
93
 
94
+ You can exclude url or url that contain specific query param using the following filters:
95
+
96
+ add_filter( 'sgo_html_minify_exclude_params', 'html_minify_exclude_params' );
97
+ function html_minify_exclude_params( $exclude_params ) {
98
+ // Add the query params that you want to exclude.
99
+ $exclude_params[] = 'test';
100
+
101
+ return $exclude_params;
102
+ }
103
+
104
+ add_filter( 'sgo_html_minify_exclude_urls', 'html_minify_exclude' );
105
+ function html_minify_exclude( $exclude_urls ) {
106
+ // Add the url that you want to exclude.
107
+ $exclude_urls[] = 'http://mydomain.com/page-slug';
108
+
109
+ return $exclude_urls;
110
+ }
111
+
112
+ You can exclude images from Lazy Load using the following filter:
113
+
114
+ add_filter( 'sgo_lazy_load_exclude_classes', 'exclude_images_with_specific_class' );
115
+ function exclude_images_with_specific_class( $classes ) {
116
+ // Add the class name that you want to exclude from lazy load.
117
+ $classes[] = 'test-class';
118
+
119
+ return $classes;
120
+ }
121
+
122
  = WP-CLI Support =
123
 
124
  In version 5.0 we've added full WP-CLI support for all plugin options and functionalities.
169
 
170
  == Changelog ==
171
 
172
+ = Version 5.1.1 =
173
+ * Improved cache invalidation for combined styles
174
+ * Cache purge from the admin bar now handles combined files too
175
+ * Added filter to exclude images from Lazy Loading
176
+ * Added filter to exclude pages from HTML Minification
177
+ * Added Filter to query params from HTML Minification
178
+ * Added PHP 7.3 support
179
+
180
  = Version 5.1.0 =
181
  * Added CSS Combination Functionality
182
  * Added Async Load of Render-Blocking JS
sg-cachepress.php CHANGED
@@ -9,7 +9,7 @@
9
  * @wordpress-plugin
10
  * Plugin Name: SG Optimizer
11
  * Description: This plugin will link your WordPress application with all the performance optimizations provided by SiteGround
12
- * Version: 5.1.0
13
  * Author: SiteGround
14
  * Text Domain: sg-cachepress
15
  * Domain Path: /languages
@@ -29,7 +29,7 @@ if ( ! defined( 'WPINC' ) ) {
29
 
30
  // Define version constant.
31
  if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
32
- define( __NAMESPACE__ . '\VERSION', '5.1.0' );
33
  }
34
 
35
  // Define slug constant.
9
  * @wordpress-plugin
10
  * Plugin Name: SG Optimizer
11
  * Description: This plugin will link your WordPress application with all the performance optimizations provided by SiteGround
12
+ * Version: 5.1.1
13
  * Author: SiteGround
14
  * Text Domain: sg-cachepress
15
  * Domain Path: /languages
29
 
30
  // Define version constant.
31
  if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
32
+ define( __NAMESPACE__ . '\VERSION', '5.1.1' );
33
  }
34
 
35
  // Define slug constant.