Version Description
Download this release
Release Info
Developer | dipakcg |
Plugin | WP Super Minify |
Version | 1.3 |
Comparing to | |
See all releases |
Code changes from version 1.2 to 1.3
- readme.txt +13 -6
- wp-super-minify.php +424 -4
readme.txt
CHANGED
@@ -1,19 +1,19 @@
|
|
1 |
=== WP Super Minify ===
|
2 |
Contributors: dipakcg
|
3 |
-
Tags: minify, compress, combine, html, css, javascript, js, performance, load, speed, time, yslow, pagespeed
|
4 |
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3S8BRPLWLNQ38
|
5 |
Requires at least: 3.5
|
6 |
Tested up to: 4.1.1
|
7 |
-
Stable tag: 1.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
11 |
-
A very light weight plugin,
|
12 |
|
13 |
== Description ==
|
14 |
-
This plugin
|
15 |
|
16 |
-
By activating this plugin, you will see the source of your HTML, inline JavaScript and CSS are now compressed. The size will be smaller and quite helpful to improve your page load speed as well as google page speed and yslow grade (if you care).
|
17 |
|
18 |
To check whether this plugin works properly, simply view your site source or press Ctrl + U from your keyboard. In the end of the source, you should see message something like:
|
19 |
|
@@ -29,7 +29,7 @@ To check whether this plugin works properly, simply view your site source or pre
|
|
29 |
== Frequently Asked Questions ==
|
30 |
= What does this plugin do? =
|
31 |
|
32 |
-
This plugin
|
33 |
|
34 |
= Any specific requirements for this plugin to work? =
|
35 |
|
@@ -42,7 +42,14 @@ Pretty much, yeah.
|
|
42 |
== Screenshots ==
|
43 |
1. Admin Settings
|
44 |
|
|
|
|
|
|
|
|
|
45 |
== Changelog ==
|
|
|
|
|
|
|
46 |
= 1.2, Feb 28, 2015 =
|
47 |
* Added News and Updates section in admin options
|
48 |
|
1 |
=== WP Super Minify ===
|
2 |
Contributors: dipakcg
|
3 |
+
Tags: minify, compress, combine, html, css, javascript, js, performance, load, speed, time, yslow, pagespeed, external
|
4 |
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3S8BRPLWLNQ38
|
5 |
Requires at least: 3.5
|
6 |
Tested up to: 4.1.1
|
7 |
+
Stable tag: 1.3
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
11 |
+
A very light weight plugin, minifies, caches and combine JavaScript and CSS files into a single file on demand to speed up page loads.
|
12 |
|
13 |
== Description ==
|
14 |
+
This plugin minifies, caches and combine JavaScript and CSS files into a single file on demand to speed up page loads, using [Minify PHP Framework](https://code.google.com/p/minify/) and [minit](https://github.com/kasparsd/minit).
|
15 |
|
16 |
+
By activating this plugin, you will see the source of your HTML, inline JavaScript and CSS are now compressed and your external Javascript and CSS files are combined into a single file. The size will be smaller and quite helpful to improve your page load speed as well as google page speed and yslow grade (if you care).
|
17 |
|
18 |
To check whether this plugin works properly, simply view your site source or press Ctrl + U from your keyboard. In the end of the source, you should see message something like:
|
19 |
|
29 |
== Frequently Asked Questions ==
|
30 |
= What does this plugin do? =
|
31 |
|
32 |
+
This plugin minifies, caches and combine JavaScript and CSS files into a single file on demand to speed up page loads. It uses the latest modified time in filename generation to ensure freshness, and loads all external Javascript files asynchronously.
|
33 |
|
34 |
= Any specific requirements for this plugin to work? =
|
35 |
|
42 |
== Screenshots ==
|
43 |
1. Admin Settings
|
44 |
|
45 |
+
2. Combined CSS files into a single CSS file (view source)
|
46 |
+
|
47 |
+
3. Sample results (pingdom)
|
48 |
+
|
49 |
== Changelog ==
|
50 |
+
= 1.3, March 12, 2015 =
|
51 |
+
* Added support for combine external javascript and css files into a single file
|
52 |
+
|
53 |
= 1.2, Feb 28, 2015 =
|
54 |
* Added News and Updates section in admin options
|
55 |
|
wp-super-minify.php
CHANGED
@@ -2,8 +2,8 @@
|
|
2 |
/*
|
3 |
Plugin Name: WP Super Minify
|
4 |
Plugin URI: https://github.com/dipakcg/wp-super-minify
|
5 |
-
Description:
|
6 |
-
Version: 1.
|
7 |
Author: Dipak C. Gajjar
|
8 |
Author URI: http://dipakgajjar.com
|
9 |
*/
|
@@ -13,7 +13,7 @@ if (!defined('WPSMY_PLUGIN_VERSION')) {
|
|
13 |
define('WPSMY_PLUGIN_VERSION', 'wpsmy_plugin_version');
|
14 |
}
|
15 |
if (!defined('WPSMY_PLUGIN_VERSION_NUM')) {
|
16 |
-
define('WPSMY_PLUGIN_VERSION_NUM', '1.
|
17 |
}
|
18 |
update_option(WPSMY_PLUGIN_VERSION, WPSMY_PLUGIN_VERSION_NUM);
|
19 |
|
@@ -79,7 +79,8 @@ function wpsmy_admin_options() {
|
|
79 |
<p>
|
80 |
<input type="checkbox" name="<?php echo $combine_css; ?>" <?php checked( $combine_css_val == 'on',true); ?> /> <span class="wpsmy_settings"> Compress CSS </span>
|
81 |
</p>
|
82 |
-
<p><input type="submit" value="<?php esc_attr_e('Save Changes') ?>" class="button button-primary" name="submit"
|
|
|
83 |
</form>
|
84 |
</td>
|
85 |
<td style="text-align: left;">
|
@@ -179,4 +180,423 @@ function wpsmy_minify() {
|
|
179 |
ob_start('wpsmy_minify_html');
|
180 |
}
|
181 |
add_action('get_header', 'wpsmy_minify');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
?>
|
2 |
/*
|
3 |
Plugin Name: WP Super Minify
|
4 |
Plugin URI: https://github.com/dipakcg/wp-super-minify
|
5 |
+
Description: Minifies, caches and combine JavaScript and CSS files into a single file to improve page load time.
|
6 |
+
Version: 1.3
|
7 |
Author: Dipak C. Gajjar
|
8 |
Author URI: http://dipakgajjar.com
|
9 |
*/
|
13 |
define('WPSMY_PLUGIN_VERSION', 'wpsmy_plugin_version');
|
14 |
}
|
15 |
if (!defined('WPSMY_PLUGIN_VERSION_NUM')) {
|
16 |
+
define('WPSMY_PLUGIN_VERSION_NUM', '1.3');
|
17 |
}
|
18 |
update_option(WPSMY_PLUGIN_VERSION, WPSMY_PLUGIN_VERSION_NUM);
|
19 |
|
79 |
<p>
|
80 |
<input type="checkbox" name="<?php echo $combine_css; ?>" <?php checked( $combine_css_val == 'on',true); ?> /> <span class="wpsmy_settings"> Compress CSS </span>
|
81 |
</p>
|
82 |
+
<p><input type="submit" value="<?php esc_attr_e('Save Changes') ?>" class="button button-primary" name="submit" />
|
83 |
+
</p>
|
84 |
</form>
|
85 |
</td>
|
86 |
<td style="text-align: left;">
|
180 |
ob_start('wpsmy_minify_html');
|
181 |
}
|
182 |
add_action('get_header', 'wpsmy_minify');
|
183 |
+
|
184 |
+
/* minit code ( https://github.com/kasparsd/minit ) */
|
185 |
+
$wpsmy_instance = Wpsmy::instance();
|
186 |
+
class Wpsmy {
|
187 |
+
static $instance;
|
188 |
+
private $wpsmy_done = array();
|
189 |
+
private $async_queue = array();
|
190 |
+
private function __construct() {
|
191 |
+
add_filter( 'print_scripts_array', array( $this, 'init_wpsmy_js' ) );
|
192 |
+
add_filter( 'print_styles_array', array( $this, 'init_wpsmy_css' ) );
|
193 |
+
// Print external scripts asynchronously in the footer
|
194 |
+
add_action( 'wp_print_footer_scripts', array( $this, 'async_init' ), 5 );
|
195 |
+
add_action( 'wp_print_footer_scripts', array( $this, 'async_print' ), 20 );
|
196 |
+
}
|
197 |
+
public static function instance() {
|
198 |
+
if ( ! self::$instance )
|
199 |
+
self::$instance = new Wpsmy();
|
200 |
+
return self::$instance;
|
201 |
+
}
|
202 |
+
function init_wpsmy_js( $todo ) {
|
203 |
+
global $wp_scripts;
|
204 |
+
return $this->wpsmy_objects( $wp_scripts, $todo, 'js' );
|
205 |
+
}
|
206 |
+
function init_wpsmy_css( $todo ) {
|
207 |
+
global $wp_styles;
|
208 |
+
return $this->wpsmy_objects( $wp_styles, $todo, 'css' );
|
209 |
+
}
|
210 |
+
function wpsmy_objects( &$object, $todo, $extension ) {
|
211 |
+
// Don't run if on admin or already processed
|
212 |
+
if ( is_admin() || empty( $todo ) )
|
213 |
+
return $todo;
|
214 |
+
// Allow files to be excluded from wpsmy
|
215 |
+
$wpsmy_exclude = (array) apply_filters( 'wpsmy-exclude-' . $extension, array() );
|
216 |
+
// Exluce all wpsmy items by default
|
217 |
+
$wpsmy_exclude = array_merge( $wpsmy_exclude, $this->get_done() );
|
218 |
+
$wpsmy_todo = array_diff( $todo, $wpsmy_exclude );
|
219 |
+
if ( empty( $wpsmy_todo ) )
|
220 |
+
return $todo;
|
221 |
+
$done = array();
|
222 |
+
$ver = array();
|
223 |
+
// Bust cache on wpsmy plugin update
|
224 |
+
$ver[] = 'wpsmy-ver-' . WPSMY_PLUGIN_VERSION;
|
225 |
+
// Debug enable
|
226 |
+
// $ver[] = 'debug-' . time();
|
227 |
+
// Use different cache key for SSL and non-SSL
|
228 |
+
$ver[] = 'is_ssl-' . is_ssl();
|
229 |
+
// Use a global cache version key to purge cache
|
230 |
+
$ver[] = 'wpsmy_cache_ver-' . get_option( 'wpsmy_cache_ver' );
|
231 |
+
// Use script version to generate a cache key
|
232 |
+
foreach ( $wpsmy_todo as $t => $script )
|
233 |
+
$ver[] = sprintf( '%s-%s', $script, $object->registered[ $script ]->ver );
|
234 |
+
$cache_ver = md5( 'wpsmy-' . implode( '-', $ver ) . $extension );
|
235 |
+
// Try to get queue from cache
|
236 |
+
$cache = get_transient( 'wpsmy-' . $cache_ver );
|
237 |
+
if ( isset( $cache['cache_ver'] ) && $cache['cache_ver'] == $cache_ver && file_exists( $cache['file'] ) )
|
238 |
+
return $this->wpsmy_enqueue_files( $object, $cache );
|
239 |
+
foreach ( $wpsmy_todo as $script ) {
|
240 |
+
// Get the relative URL of the asset
|
241 |
+
$src = self::get_asset_relative_path(
|
242 |
+
$object->base_url,
|
243 |
+
$object->registered[ $script ]->src
|
244 |
+
);
|
245 |
+
// Add support for pseudo packages such as jquery which return src as empty string
|
246 |
+
if ( empty( $object->registered[ $script ]->src ) || '' == $object->registered[ $script ]->src )
|
247 |
+
$done[ $script ] = null;
|
248 |
+
// Skip if the file is not hosted locally
|
249 |
+
if ( ! $src || ! file_exists( ABSPATH . $src ) )
|
250 |
+
continue;
|
251 |
+
$script_content = apply_filters(
|
252 |
+
'wpsmy-item-' . $extension,
|
253 |
+
file_get_contents( ABSPATH . $src ),
|
254 |
+
$object,
|
255 |
+
$script
|
256 |
+
);
|
257 |
+
if ( false !== $script_content )
|
258 |
+
$done[ $script ] = $script_content;
|
259 |
+
}
|
260 |
+
if ( empty( $done ) )
|
261 |
+
return $todo;
|
262 |
+
$wp_upload_dir = wp_upload_dir();
|
263 |
+
// Try to create the folder for cache
|
264 |
+
if ( ! is_dir( $wp_upload_dir['basedir'] . '/wp-super-minify' ) )
|
265 |
+
if ( ! mkdir( $wp_upload_dir['basedir'] . '/wp-super-minify' ) )
|
266 |
+
return $todo;
|
267 |
+
$combined_file_path = sprintf( '%s/wp-super-minify/%s.%s', $wp_upload_dir['basedir'], $cache_ver, $extension );
|
268 |
+
$combined_file_url = sprintf( '%s/wp-super-minify/%s.%s', $wp_upload_dir['baseurl'], $cache_ver, $extension );
|
269 |
+
// Allow other plugins to do something with the resulting URL
|
270 |
+
$combined_file_url = apply_filters( 'wpsmy-url-' . $extension, $combined_file_url, $done );
|
271 |
+
// Allow other plugins to minify and obfuscate
|
272 |
+
$done_imploded = apply_filters( 'wpsmy-content-' . $extension, implode( "\n\n", $done ), $done );
|
273 |
+
// Store the combined file on the filesystem
|
274 |
+
if ( ! file_exists( $combined_file_path ) )
|
275 |
+
if ( ! file_put_contents( $combined_file_path, $done_imploded ) )
|
276 |
+
return $todo;
|
277 |
+
$status = array(
|
278 |
+
'cache_ver' => $cache_ver,
|
279 |
+
'todo' => $todo,
|
280 |
+
'done' => array_keys( $done ),
|
281 |
+
'url' => $combined_file_url,
|
282 |
+
'file' => $combined_file_path,
|
283 |
+
'extension' => $extension
|
284 |
+
);
|
285 |
+
// Cache this set of scripts for 24 hours
|
286 |
+
set_transient( 'wpsmy-' . $cache_ver, $status, 24 * 60 * 60 );
|
287 |
+
$this->set_done( $cache_ver );
|
288 |
+
return $this->wpsmy_enqueue_files( $object, $status );
|
289 |
+
}
|
290 |
+
function wpsmy_enqueue_files( &$object, $status ) {
|
291 |
+
extract( $status );
|
292 |
+
switch ( $extension ) {
|
293 |
+
case 'css':
|
294 |
+
wp_enqueue_style(
|
295 |
+
'wpsmy-' . $cache_ver,
|
296 |
+
$url,
|
297 |
+
null,
|
298 |
+
null
|
299 |
+
);
|
300 |
+
// Add inline styles for all minified styles
|
301 |
+
foreach ( $done as $script ) {
|
302 |
+
$inline_style = $object->get_data( $script, 'after' );
|
303 |
+
if ( empty( $inline_style ) )
|
304 |
+
continue;
|
305 |
+
if ( is_string( $inline_style ) )
|
306 |
+
$object->add_inline_style( 'wpsmy-' . $cache_ver, $inline_style );
|
307 |
+
elseif ( is_array( $inline_style ) )
|
308 |
+
$object->add_inline_style( 'wpsmy-' . $cache_ver, implode( ' ', $inline_style ) );
|
309 |
+
}
|
310 |
+
break;
|
311 |
+
case 'js':
|
312 |
+
wp_enqueue_script(
|
313 |
+
'wpsmy-' . $cache_ver,
|
314 |
+
$url,
|
315 |
+
null,
|
316 |
+
null,
|
317 |
+
apply_filters( 'wpsmy-js-in-footer', true )
|
318 |
+
);
|
319 |
+
// Add to the correct
|
320 |
+
$object->set_group(
|
321 |
+
'wpsmy-' . $cache_ver,
|
322 |
+
false,
|
323 |
+
apply_filters( 'wpsmy-js-in-footer', true )
|
324 |
+
);
|
325 |
+
$inline_data = array();
|
326 |
+
// Add inline scripts for all minified scripts
|
327 |
+
foreach ( $done as $script )
|
328 |
+
$inline_data[] = $object->get_data( $script, 'data' );
|
329 |
+
// Filter out empty elements
|
330 |
+
$inline_data = array_filter( $inline_data );
|
331 |
+
if ( ! empty( $inline_data ) )
|
332 |
+
$object->add_data( 'wpsmy-' . $cache_ver, 'data', implode( "\n", $inline_data ) );
|
333 |
+
break;
|
334 |
+
default:
|
335 |
+
return $todo;
|
336 |
+
}
|
337 |
+
// Remove scripts that were merged
|
338 |
+
$todo = array_diff( $todo, $done );
|
339 |
+
$todo[] = 'wpsmy-' . $cache_ver;
|
340 |
+
// Mark these items as done
|
341 |
+
$object->done = array_merge( $object->done, $done );
|
342 |
+
// Remove wpsmy items from the queue
|
343 |
+
$object->queue = array_diff( $object->queue, $done );
|
344 |
+
return $todo;
|
345 |
+
}
|
346 |
+
function set_done( $handle ) {
|
347 |
+
$this->wpsmy_done[] = 'wpsmy-' . $handle;
|
348 |
+
}
|
349 |
+
function get_done() {
|
350 |
+
return $this->wpsmy_done;
|
351 |
+
}
|
352 |
+
public static function get_asset_relative_path( $base_url, $item_url ) {
|
353 |
+
// Remove protocol reference from the local base URL
|
354 |
+
$base_url = preg_replace( '/^(https?:\/\/|\/\/)/i', '', $base_url );
|
355 |
+
// Check if this is a local asset which we can include
|
356 |
+
$src_parts = explode( $base_url, $item_url );
|
357 |
+
// Get the trailing part of the local URL
|
358 |
+
$maybe_relative = end( $src_parts );
|
359 |
+
if ( ! file_exists( ABSPATH . $maybe_relative ) )
|
360 |
+
return false;
|
361 |
+
return $maybe_relative;
|
362 |
+
}
|
363 |
+
public function async_init() {
|
364 |
+
global $wp_scripts;
|
365 |
+
if ( ! is_object( $wp_scripts ) || empty( $wp_scripts->queue ) )
|
366 |
+
return;
|
367 |
+
$base_url = site_url();
|
368 |
+
$wpsmy_exclude = (array) apply_filters( 'wpsmy-exclude-js', array() );
|
369 |
+
foreach ( $wp_scripts->queue as $handle ) {
|
370 |
+
// Skip asyncing explicitly excluded script handles
|
371 |
+
if ( in_array( $handle, $wpsmy_exclude ) ) {
|
372 |
+
continue;
|
373 |
+
}
|
374 |
+
$script_relative_path = wpsmy::get_asset_relative_path(
|
375 |
+
$base_url,
|
376 |
+
$wp_scripts->registered[$handle]->src
|
377 |
+
);
|
378 |
+
if ( ! $script_relative_path ) {
|
379 |
+
// Add this script to our async queue
|
380 |
+
$this->async_queue[] = $handle;
|
381 |
+
// Remove this script from being printed the regular way
|
382 |
+
wp_dequeue_script( $handle );
|
383 |
+
}
|
384 |
+
}
|
385 |
+
}
|
386 |
+
public function async_print() {
|
387 |
+
global $wp_scripts;
|
388 |
+
if ( empty( $this->async_queue ) )
|
389 |
+
return;
|
390 |
+
?>
|
391 |
+
<!-- Asynchronous scripts by wpsmy -->
|
392 |
+
<script id="wpsmy-async-scripts" type="text/javascript">
|
393 |
+
(function() {
|
394 |
+
var js, fjs = document.getElementById('wpsmy-async-scripts'),
|
395 |
+
add = function( url, id ) {
|
396 |
+
js = document.createElement('script');
|
397 |
+
js.type = 'text/javascript';
|
398 |
+
js.src = url;
|
399 |
+
js.async = true;
|
400 |
+
js.id = id;
|
401 |
+
fjs.parentNode.insertBefore(js, fjs);
|
402 |
+
};
|
403 |
+
<?php
|
404 |
+
foreach ( $this->async_queue as $handle ) {
|
405 |
+
printf(
|
406 |
+
'add("%s", "%s"); ',
|
407 |
+
$wp_scripts->registered[$handle]->src,
|
408 |
+
'async-script-' . esc_attr( $handle )
|
409 |
+
);
|
410 |
+
}
|
411 |
+
?>
|
412 |
+
})();
|
413 |
+
</script>
|
414 |
+
<?php
|
415 |
+
}
|
416 |
+
}
|
417 |
+
|
418 |
+
// Prepend the filename of the file being included
|
419 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
420 |
+
add_filter( 'wpsmy-item-css', 'wpsmy_comment_combined', 15, 3 );
|
421 |
+
}
|
422 |
+
if ( get_option('wpsmy_combine_js', 1) == 'on') {
|
423 |
+
add_filter( 'wpsmy-item-js', 'wpsmy_comment_combined', 15, 3 );
|
424 |
+
}
|
425 |
+
function wpsmy_comment_combined( $content, $object, $script ) {
|
426 |
+
if ( ! $content )
|
427 |
+
return $content;
|
428 |
+
return sprintf(
|
429 |
+
"\n\n/* wpsmy: %s */\n",
|
430 |
+
$object->registered[ $script ]->src
|
431 |
+
) . $content;
|
432 |
+
}
|
433 |
+
|
434 |
+
// Add table of contents at the top of the wpsmy file
|
435 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
436 |
+
add_filter( 'wpsmy-content-css', 'wpsmy_add_toc', 100, 2 );
|
437 |
+
}
|
438 |
+
if ( get_option('wpsmy_combine_js', 1) == 'on') {
|
439 |
+
add_filter( 'wpsmy-content-js', 'wpsmy_add_toc', 100, 2 );
|
440 |
+
}
|
441 |
+
function wpsmy_add_toc( $content, $items ) {
|
442 |
+
if ( ! $content || empty( $items ) )
|
443 |
+
return $content;
|
444 |
+
$toc = array();
|
445 |
+
foreach ( $items as $handle => $item_content )
|
446 |
+
$toc[] = sprintf( ' - %s', $handle );
|
447 |
+
return sprintf( "/* TOC:\n%s\n*/", implode( "\n", $toc ) ) . $content;
|
448 |
+
}
|
449 |
+
|
450 |
+
// Turn all local asset URLs into absolute URLs
|
451 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
452 |
+
add_filter( 'wpsmy-item-css', 'wpsmy_resolve_css_urls', 10, 3 );
|
453 |
+
}
|
454 |
+
function wpsmy_resolve_css_urls( $content, $object, $script ) {
|
455 |
+
if ( ! $content )
|
456 |
+
return $content;
|
457 |
+
$src = wpsmy::get_asset_relative_path(
|
458 |
+
$object->base_url,
|
459 |
+
$object->registered[ $script ]->src
|
460 |
+
);
|
461 |
+
// Make all local asset URLs absolute
|
462 |
+
$content = preg_replace(
|
463 |
+
'/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i',
|
464 |
+
sprintf( "url('%s/$1')", $object->base_url . dirname( $src ) ),
|
465 |
+
$content
|
466 |
+
);
|
467 |
+
return $content;
|
468 |
+
}
|
469 |
+
|
470 |
+
// Add support for relative CSS imports
|
471 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
472 |
+
add_filter( 'wpsmy-item-css', 'wpsmy_resolve_css_imports', 10, 3 );
|
473 |
+
}
|
474 |
+
function wpsmy_resolve_css_imports( $content, $object, $script ) {
|
475 |
+
if ( ! $content )
|
476 |
+
return $content;
|
477 |
+
$src = wpsmy::get_asset_relative_path(
|
478 |
+
$object->base_url,
|
479 |
+
$object->registered[ $script ]->src
|
480 |
+
);
|
481 |
+
// Make all import asset URLs absolute
|
482 |
+
$content = preg_replace(
|
483 |
+
'/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i',
|
484 |
+
sprintf( "@import url('%s/$2')", $object->base_url . dirname( $src ) ),
|
485 |
+
$content
|
486 |
+
);
|
487 |
+
return $content;
|
488 |
+
}
|
489 |
+
|
490 |
+
// Exclude styles with media queries from being included in wpsmy
|
491 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
492 |
+
add_filter( 'wpsmy-item-css', 'wpsmy_exclude_css_with_media_query', 10, 3 );
|
493 |
+
}
|
494 |
+
function wpsmy_exclude_css_with_media_query( $content, $object, $script ) {
|
495 |
+
if ( ! $content )
|
496 |
+
return $content;
|
497 |
+
$whitelist = array( '', 'all', 'screen' );
|
498 |
+
// Exclude from wpsmy if media query specified
|
499 |
+
if ( ! in_array( $object->registered[ $script ]->args, $whitelist ) )
|
500 |
+
return false;
|
501 |
+
return $content;
|
502 |
+
}
|
503 |
+
|
504 |
+
// Make sure that all wpsmy files are served from the correct protocol
|
505 |
+
if ( get_option('wpsmy_combine_css', 1) == 'on') {
|
506 |
+
add_filter( 'wpsmy-url-css', 'wpsmy_maybe_ssl_url' );
|
507 |
+
}
|
508 |
+
if ( get_option('wpsmy_combine_js', 1) == 'on') {
|
509 |
+
add_filter( 'wpsmy-url-js', 'wpsmy_maybe_ssl_url' );
|
510 |
+
}
|
511 |
+
function wpsmy_maybe_ssl_url( $url ) {
|
512 |
+
if ( is_ssl() )
|
513 |
+
return str_replace( 'http://', 'https://', $url );
|
514 |
+
return $url;
|
515 |
+
}
|
516 |
+
|
517 |
+
// Add a Purge Cache link to the plugin list
|
518 |
+
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wpsmy_cache_purge_admin_link' );
|
519 |
+
function wpsmy_cache_purge_admin_link( $links ) {
|
520 |
+
$links[] = sprintf(
|
521 |
+
'<a href="%s">%s</a>',
|
522 |
+
wp_nonce_url( add_query_arg( 'purge_wpsmy', true ), 'purge_wpsmy' ),
|
523 |
+
__( 'Purge WP Super Minify cache', 'wpsmy' )
|
524 |
+
);
|
525 |
+
return $links;
|
526 |
+
}
|
527 |
+
|
528 |
+
/* Display a notice to Purge Cache */
|
529 |
+
add_action('admin_notices', 'wpsmy_admin_notice');
|
530 |
+
function wpsmy_admin_notice() {
|
531 |
+
global $current_user, $pagenow ;
|
532 |
+
$user_id = $current_user->ID;
|
533 |
+
$wpsmy_page = $_GET['page'];
|
534 |
+
// delete_user_meta($user_id, 'wpsmy_ignore_this');
|
535 |
+
/* Check that the user hasn't already clicked to ignore the message */
|
536 |
+
// if ( ! get_user_meta($user_id, 'wpsmy_ignore_this') ) {
|
537 |
+
if ( $pagenow == 'plugins.php' || $wpsmy_page == 'wp-super-minify' ) {
|
538 |
+
echo '<div class="updated"><p>';
|
539 |
+
printf('<a style="font-weight: bold; color: #60AA1F;" href="%s">%s</a>', wp_nonce_url( add_query_arg( 'purge_wpsmy', true ), 'purge_wpsmy' ), __( 'Purge WP Super Minify cache', 'wpsmy' ) );
|
540 |
+
/* printf('<span style="float: right;"><a href="%1$s">Hide</a></span>', '?wpsmy_notice_ignore=0' ); */
|
541 |
+
echo "</p></div>";
|
542 |
+
}
|
543 |
+
}
|
544 |
+
|
545 |
+
// add_action('admin_init', 'wpsmy_notice_ignore');
|
546 |
+
function wpsmy_notice_ignore() {
|
547 |
+
global $current_user;
|
548 |
+
$user_id = $current_user->ID;
|
549 |
+
/* If user clicks to ignore the notice, add that to their user meta */
|
550 |
+
if ( isset($_GET['wpsmy_notice_ignore']) && '0' == $_GET['wpsmy_notice_ignore'] ) {
|
551 |
+
// add_user_meta($user_id, 'wpsmy_ignore_this', 'true', true);
|
552 |
+
setcookie("wpsmy_hide_admin_notice", 'true');
|
553 |
+
}
|
554 |
+
}
|
555 |
+
|
556 |
+
/**
|
557 |
+
* Maybe purge wpsmy cache
|
558 |
+
*/
|
559 |
+
add_action( 'admin_init', 'purge_wpsmy_cache' );
|
560 |
+
function purge_wpsmy_cache() {
|
561 |
+
if ( ! isset( $_GET['purge_wpsmy'] ) )
|
562 |
+
return;
|
563 |
+
if ( ! check_admin_referer( 'purge_wpsmy' ) )
|
564 |
+
return;
|
565 |
+
// Use this as a global cache version number
|
566 |
+
update_option( 'wpsmy_cache_ver', time() );
|
567 |
+
add_action( 'admin_notices', 'wpsmy_cache_purged_success' );
|
568 |
+
// Allow other plugins to know that we purged
|
569 |
+
// do_action( 'wpsmy-cache-purged' );
|
570 |
+
|
571 |
+
/* Delete files from /uploads/wp-super-minify directory */
|
572 |
+
$wp_upload_dir = wp_upload_dir();
|
573 |
+
$wpsmy_files = glob( $wp_upload_dir['basedir'] . '/wp-super-minify/*' );
|
574 |
+
if ( $wpsmy_files ) {
|
575 |
+
foreach ( $wpsmy_files as $wpsmy_file ) {
|
576 |
+
unlink( $wpsmy_file );
|
577 |
+
}
|
578 |
+
}
|
579 |
+
}
|
580 |
+
|
581 |
+
function wpsmy_cache_purged_success() {
|
582 |
+
printf(
|
583 |
+
'<div class="updated"><p>%s</p></div>',
|
584 |
+
__( 'Success: WP Super Minify cache purged.', 'wpsmy' )
|
585 |
+
);
|
586 |
+
}
|
587 |
+
|
588 |
+
// This can used from cron to delete all wpsmy cache files
|
589 |
+
// add_action( 'wpsmy-cache-purge-delete', 'wpsmy_cache_delete_files' );
|
590 |
+
function wpsmy_cache_delete_files() {
|
591 |
+
$wp_upload_dir = wp_upload_dir();
|
592 |
+
$wpsmy_files = glob( $wp_upload_dir['basedir'] . '/wp-super-minify/*' );
|
593 |
+
print_r ($wpsmy_files);
|
594 |
+
if ( $wpsmy_files ) {
|
595 |
+
foreach ( $wpsmy_files as $wpsmy_file ) {
|
596 |
+
unlink( $wpsmy_file );
|
597 |
+
}
|
598 |
+
}
|
599 |
+
}
|
600 |
+
|
601 |
+
/* END OF PLUGIN */
|
602 |
?>
|