diff --git a/readme.txt b/readme.txt index 0bde5a6..af275be 100644 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,6 @@ === WP Super Minify === Contributors: dipakcg -Tags: minify, compress, combine, html, css, javascript, js, performance, load, speed, time, yslow, pagespeed, external +Tags: minify, compress, combine, html, css, javascript, js, performance, load, speed, time, yslow, pagespeed Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3S8BRPLWLNQ38 Requires at least: 3.5 Tested up to: 4.1.1 @@ -8,12 +8,12 @@ Stable tag: 1.3.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html -A very light weight plugin, minifies, caches and combine JavaScript and CSS files into a single file on demand to speed up page loads. +A very light weight plugin, combines, minifies, and caches inline JavaScript and CSS files on demand to speed up page loads. == Description == -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). +This plugin combines, minifies, and caches inline JavaScript and CSS files on demand to speed up page loads, using [Minify PHP Framework](https://code.google.com/p/minify/). -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). +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). 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: @@ -29,7 +29,7 @@ To check whether this plugin works properly, simply view your site source or pre == Frequently Asked Questions == = What does this plugin do? = -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. +This plugin combines, minifies, and caches inline JavaScript and CSS files on demand to speed up page loads. = Any specific requirements for this plugin to work? = @@ -42,13 +42,9 @@ Pretty much, yeah. == Screenshots == 1. Admin Settings -2. Combined CSS files into a single CSS file (view source) - -3. Sample results (pingdom) - == Changelog == -= 1.3.1, March 15, 2015 = -* Fixed jQuery conflict (by other plugins) += 1.3.1, March 17, 2015 = +* Reverted support for combine external javascript and css files into a single file due to conflict with other plugins = 1.3, March 12, 2015 = * Added support for combine external javascript and css files into a single file diff --git a/wp-super-minify.php b/wp-super-minify.php index a852d7c..304a750 100644 --- a/wp-super-minify.php +++ b/wp-super-minify.php @@ -2,7 +2,7 @@ /* Plugin Name: WP Super Minify Plugin URI: https://github.com/dipakcg/wp-super-minify -Description: Minifies, caches and combine JavaScript and CSS files into a single file to improve page load time. +Description: Minifies, caches and combine inline JavaScript and CSS files to improve page load time. Version: 1.3.1 Author: Dipak C. Gajjar Author URI: http://dipakgajjar.com @@ -32,20 +32,7 @@ function wpsmy_add_admin_menu() { add_menu_page( 'WP Super Minify Settings', 'WP Super Minify', 'manage_options', 'wp-super-minify', 'wpsmy_admin_options', plugins_url('assets/images/wpsmy-icon-24x24.png', __FILE__) ); } -/* Stop contact form 7 from conflicting with our jquery based code */ -add_action( ‘wp_print_scripts’, ‘wpsmy_deregister_wpcf7_javascript’, 100 ); -function wpsmy_deregister_wpcf7_javascript() { - wp_deregister_script( 'contact-form-7' ); -} - -/* Include jQuery - it may required for custom js added in theme's header.php */ -if (!is_admin()) add_action("wp_enqueue_scripts", "wpsmy_prioritize_jquery", 11); -function wpsmy_prioritize_jquery() { - wp_deregister_script('jquery'); - wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null); - wp_enqueue_script('jquery'); -} - +// Admin options/setting page function wpsmy_admin_options() { ?>
@@ -195,426 +182,5 @@ function wpsmy_minify() { } add_action('get_header', 'wpsmy_minify'); -/* minit code ( https://github.com/kasparsd/minit ) */ -$wpsmy_instance = Wpsmy::instance(); -class Wpsmy { - static $instance; - private $wpsmy_done = array(); - private $async_queue = array(); - private function __construct() { - if ( get_option('wpsmy_combine_js', 1) == 'on') { - add_filter( 'print_scripts_array', array( $this, 'init_wpsmy_js' ) ); - } - if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'print_styles_array', array( $this, 'init_wpsmy_css' ) ); - } - // Print external scripts asynchronously in the footer - add_action( 'wp_print_footer_scripts', array( $this, 'async_init' ), 1 ); - add_action( 'wp_print_footer_scripts', array( $this, 'async_print' ), 5 ); - } - public static function instance() { - if ( ! self::$instance ) - self::$instance = new Wpsmy(); - return self::$instance; - } - function init_wpsmy_js( $todo ) { - global $wp_scripts; - return $this->wpsmy_objects( $wp_scripts, $todo, 'js' ); - } - function init_wpsmy_css( $todo ) { - global $wp_styles; - return $this->wpsmy_objects( $wp_styles, $todo, 'css' ); - } - function wpsmy_objects( &$object, $todo, $extension ) { - // Don't run if on admin or already processed - if ( is_admin() || empty( $todo ) ) - return $todo; - // Allow files to be excluded from wpsmy - $wpsmy_exclude = (array) apply_filters( 'wpsmy-exclude-' . $extension, array() ); - // Exluce all wpsmy items by default - $wpsmy_exclude = array_merge( $wpsmy_exclude, $this->get_done() ); - $wpsmy_todo = array_diff( $todo, $wpsmy_exclude ); - if ( empty( $wpsmy_todo ) ) - return $todo; - $done = array(); - $ver = array(); - // Bust cache on wpsmy plugin update - $ver[] = 'wpsmy-ver-' . WPSMY_PLUGIN_VERSION; - // Debug enable - // $ver[] = 'debug-' . time(); - // Use different cache key for SSL and non-SSL - $ver[] = 'is_ssl-' . is_ssl(); - // Use a global cache version key to purge cache - $ver[] = 'wpsmy_cache_ver-' . get_option( 'wpsmy_cache_ver' ); - // Use script version to generate a cache key - foreach ( $wpsmy_todo as $t => $script ) - $ver[] = sprintf( '%s-%s', $script, $object->registered[ $script ]->ver ); - $cache_ver = md5( 'wpsmy-' . implode( '-', $ver ) . $extension ); - // Try to get queue from cache - $cache = get_transient( 'wpsmy-' . $cache_ver ); - if ( isset( $cache['cache_ver'] ) && $cache['cache_ver'] == $cache_ver && file_exists( $cache['file'] ) ) - return $this->wpsmy_enqueue_files( $object, $cache ); - foreach ( $wpsmy_todo as $script ) { - // Get the relative URL of the asset - $src = self::get_asset_relative_path( - $object->base_url, - $object->registered[ $script ]->src - ); - // Add support for pseudo packages such as jquery which return src as empty string - if ( empty( $object->registered[ $script ]->src ) || '' == $object->registered[ $script ]->src ) - $done[ $script ] = null; - // Skip if the file is not hosted locally - if ( ! $src || ! file_exists( ABSPATH . $src ) ) - continue; - $script_content = apply_filters( - 'wpsmy-item-' . $extension, - file_get_contents( ABSPATH . $src ), - $object, - $script - ); - if ( false !== $script_content ) - $done[ $script ] = $script_content; - } - if ( empty( $done ) ) - return $todo; - $wp_upload_dir = wp_upload_dir(); - // Try to create the folder for cache - if ( ! is_dir( $wp_upload_dir['basedir'] . '/wp-super-minify' ) ) - if ( ! mkdir( $wp_upload_dir['basedir'] . '/wp-super-minify' ) ) - return $todo; - $combined_file_path = sprintf( '%s/wp-super-minify/%s.%s', $wp_upload_dir['basedir'], $cache_ver, $extension ); - $combined_file_url = sprintf( '%s/wp-super-minify/%s.%s', $wp_upload_dir['baseurl'], $cache_ver, $extension ); - // Allow other plugins to do something with the resulting URL - $combined_file_url = apply_filters( 'wpsmy-url-' . $extension, $combined_file_url, $done ); - // Allow other plugins to minify and obfuscate - $done_imploded = apply_filters( 'wpsmy-content-' . $extension, implode( "\n\n", $done ), $done ); - // Store the combined file on the filesystem - if ( ! file_exists( $combined_file_path ) ) - if ( ! file_put_contents( $combined_file_path, $done_imploded ) ) - return $todo; - $status = array( - 'cache_ver' => $cache_ver, - 'todo' => $todo, - 'done' => array_keys( $done ), - 'url' => $combined_file_url, - 'file' => $combined_file_path, - 'extension' => $extension - ); - // Cache this set of scripts for 24 hours - set_transient( 'wpsmy-' . $cache_ver, $status, 24 * 60 * 60 ); - $this->set_done( $cache_ver ); - return $this->wpsmy_enqueue_files( $object, $status ); - } - function wpsmy_enqueue_files( &$object, $status ) { - extract( $status ); - switch ( $extension ) { - case 'css': - wp_enqueue_style( - 'wpsmy-' . $cache_ver, - $url, - null, - null - ); - // Add inline styles for all minified styles - foreach ( $done as $script ) { - $inline_style = $object->get_data( $script, 'after' ); - if ( empty( $inline_style ) ) - continue; - if ( is_string( $inline_style ) ) - $object->add_inline_style( 'wpsmy-' . $cache_ver, $inline_style ); - elseif ( is_array( $inline_style ) ) - $object->add_inline_style( 'wpsmy-' . $cache_ver, implode( ' ', $inline_style ) ); - } - break; - case 'js': - wp_enqueue_script( - 'wpsmy-' . $cache_ver, - $url, - null, - null, - apply_filters( 'wpsmy-js-in-footer', true ) - ); - // Add to the correct - $object->set_group( - 'wpsmy-' . $cache_ver, - false, - apply_filters( 'wpsmy-js-in-footer', true ) - ); - $inline_data = array(); - // Add inline scripts for all minified scripts - foreach ( $done as $script ) - $inline_data[] = $object->get_data( $script, 'data' ); - // Filter out empty elements - $inline_data = array_filter( $inline_data ); - if ( ! empty( $inline_data ) ) - $object->add_data( 'wpsmy-' . $cache_ver, 'data', implode( "\n", $inline_data ) ); - break; - default: - return $todo; - } - // Remove scripts that were merged - $todo = array_diff( $todo, $done ); - $todo[] = 'wpsmy-' . $cache_ver; - // Mark these items as done - $object->done = array_merge( $object->done, $done ); - // Remove wpsmy items from the queue - $object->queue = array_diff( $object->queue, $done ); - return $todo; - } - function set_done( $handle ) { - $this->wpsmy_done[] = 'wpsmy-' . $handle; - } - function get_done() { - return $this->wpsmy_done; - } - public static function get_asset_relative_path( $base_url, $item_url ) { - // Remove protocol reference from the local base URL - $base_url = preg_replace( '/^(https?:\/\/|\/\/)/i', '', $base_url ); - // Check if this is a local asset which we can include - $src_parts = explode( $base_url, $item_url ); - // Get the trailing part of the local URL - $maybe_relative = end( $src_parts ); - if ( ! file_exists( ABSPATH . $maybe_relative ) ) - return false; - return $maybe_relative; - } - public function async_init() { - global $wp_scripts; - if ( ! is_object( $wp_scripts ) || empty( $wp_scripts->queue ) ) - return; - $base_url = site_url(); - $wpsmy_exclude = (array) apply_filters( 'wpsmy-exclude-js', array() ); - foreach ( $wp_scripts->queue as $handle ) { - // Skip asyncing explicitly excluded script handles - if ( in_array( $handle, $wpsmy_exclude ) ) { - continue; - } - $script_relative_path = wpsmy::get_asset_relative_path( - $base_url, - $wp_scripts->registered[$handle]->src - ); - if ( ! $script_relative_path ) { - // Add this script to our async queue - $this->async_queue[] = $handle; - // Remove this script from being printed the regular way - wp_dequeue_script( $handle ); - } - } - } - public function async_print() { - global $wp_scripts; - if ( empty( $this->async_queue ) ) - return; - ?> - - - registered[ $script ]->src - ) . $content; -} - -// Add table of contents at the top of the wpsmy file -if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'wpsmy-content-css', 'wpsmy_add_toc', 100, 2 ); -} -if ( get_option('wpsmy_combine_js', 1) == 'on') { - add_filter( 'wpsmy-content-js', 'wpsmy_add_toc', 100, 2 ); -} -function wpsmy_add_toc( $content, $items ) { - if ( ! $content || empty( $items ) ) - return $content; - $toc = array(); - foreach ( $items as $handle => $item_content ) - $toc[] = sprintf( ' - %s', $handle ); - return sprintf( "/* TOC:\n%s\n*/", implode( "\n", $toc ) ) . $content; -} - -// Turn all local asset URLs into absolute URLs -if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'wpsmy-item-css', 'wpsmy_resolve_css_urls', 10, 3 ); -} -function wpsmy_resolve_css_urls( $content, $object, $script ) { - if ( ! $content ) - return $content; - $src = wpsmy::get_asset_relative_path( - $object->base_url, - $object->registered[ $script ]->src - ); - // Make all local asset URLs absolute - $content = preg_replace( - '/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i', - sprintf( "url('%s/$1')", $object->base_url . dirname( $src ) ), - $content - ); - return $content; -} - -// Add support for relative CSS imports -if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'wpsmy-item-css', 'wpsmy_resolve_css_imports', 10, 3 ); -} -function wpsmy_resolve_css_imports( $content, $object, $script ) { - if ( ! $content ) - return $content; - $src = wpsmy::get_asset_relative_path( - $object->base_url, - $object->registered[ $script ]->src - ); - // Make all import asset URLs absolute - $content = preg_replace( - '/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i', - sprintf( "@import url('%s/$2')", $object->base_url . dirname( $src ) ), - $content - ); - return $content; -} - -// Exclude styles with media queries from being included in wpsmy -if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'wpsmy-item-css', 'wpsmy_exclude_css_with_media_query', 10, 3 ); -} -function wpsmy_exclude_css_with_media_query( $content, $object, $script ) { - if ( ! $content ) - return $content; - $whitelist = array( '', 'all', 'screen' ); - // Exclude from wpsmy if media query specified - if ( ! in_array( $object->registered[ $script ]->args, $whitelist ) ) - return false; - return $content; -} - -// Make sure that all wpsmy files are served from the correct protocol -if ( get_option('wpsmy_combine_css', 1) == 'on') { - add_filter( 'wpsmy-url-css', 'wpsmy_maybe_ssl_url' ); -} -if ( get_option('wpsmy_combine_js', 1) == 'on') { - add_filter( 'wpsmy-url-js', 'wpsmy_maybe_ssl_url' ); -} -function wpsmy_maybe_ssl_url( $url ) { - if ( is_ssl() ) - return str_replace( 'http://', 'https://', $url ); - return $url; -} - -// Add a Purge Cache link to the plugin list -add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wpsmy_cache_purge_admin_link' ); -function wpsmy_cache_purge_admin_link( $links ) { - $links[] = sprintf( - '%s', - wp_nonce_url( add_query_arg( 'purge_wpsmy', true ), 'purge_wpsmy' ), - __( 'Purge WP Super Minify cache', 'wpsmy' ) - ); - return $links; -} - -/* Display a notice to Purge Cache */ -add_action('admin_notices', 'wpsmy_admin_notice'); -function wpsmy_admin_notice() { - global $current_user, $pagenow ; - $user_id = $current_user->ID; - $wpsmy_page = $_GET['page']; - // delete_user_meta($user_id, 'wpsmy_ignore_this'); - /* Check that the user hasn't already clicked to ignore the message */ - // if ( ! get_user_meta($user_id, 'wpsmy_ignore_this') ) { - if ( $pagenow == 'plugins.php' || $wpsmy_page == 'wp-super-minify' ) { - echo '

'; - printf('%s', wp_nonce_url( add_query_arg( 'purge_wpsmy', true ), 'purge_wpsmy' ), __( 'Purge WP Super Minify cache', 'wpsmy' ) ); - /* printf('Hide', '?wpsmy_notice_ignore=0' ); */ - echo "

"; - } -} - -// add_action('admin_init', 'wpsmy_notice_ignore'); -function wpsmy_notice_ignore() { - global $current_user; - $user_id = $current_user->ID; - /* If user clicks to ignore the notice, add that to their user meta */ - if ( isset($_GET['wpsmy_notice_ignore']) && '0' == $_GET['wpsmy_notice_ignore'] ) { - // add_user_meta($user_id, 'wpsmy_ignore_this', 'true', true); - setcookie("wpsmy_hide_admin_notice", 'true'); - } -} - -/** - * Maybe purge wpsmy cache - */ -add_action( 'admin_init', 'purge_wpsmy_cache' ); -function purge_wpsmy_cache() { - if ( ! isset( $_GET['purge_wpsmy'] ) ) - return; - if ( ! check_admin_referer( 'purge_wpsmy' ) ) - return; - // Use this as a global cache version number - update_option( 'wpsmy_cache_ver', time() ); - add_action( 'admin_notices', 'wpsmy_cache_purged_success' ); - // Allow other plugins to know that we purged - // do_action( 'wpsmy-cache-purged' ); - - /* Delete files from /uploads/wp-super-minify directory */ - $wp_upload_dir = wp_upload_dir(); - $wpsmy_files = glob( $wp_upload_dir['basedir'] . '/wp-super-minify/*' ); - if ( $wpsmy_files ) { - foreach ( $wpsmy_files as $wpsmy_file ) { - unlink( $wpsmy_file ); - } - } -} - -function wpsmy_cache_purged_success() { - printf( - '

%s

', - __( 'Success: WP Super Minify cache purged.', 'wpsmy' ) - ); -} - -// This can used from cron to delete all wpsmy cache files -// add_action( 'wpsmy-cache-purge-delete', 'wpsmy_cache_delete_files' ); -function wpsmy_cache_delete_files() { - $wp_upload_dir = wp_upload_dir(); - $wpsmy_files = glob( $wp_upload_dir['basedir'] . '/wp-super-minify/*' ); - print_r ($wpsmy_files); - if ( $wpsmy_files ) { - foreach ( $wpsmy_files as $wpsmy_file ) { - unlink( $wpsmy_file ); - } - } -} - /* END OF PLUGIN */ ?>