. */ if (!class_exists('WPMinify')) { class WPMinify{ var $homepage = 'http://omninoggin.com/projects/wordpress-plugins/wp-minify-wordpress-plugin/'; var $name = 'wp_minify'; var $version = '0.2'; var $debug = false; var $cache_location = 'wp-content/plugins/wp-minify/cache/'; var $url_len_limit = 2000; var $minify_limit = 50; function get_default_options() { return array( 'cache_interval' => 900, 'css_exclude' => array(), 'css_include' => array(), 'debug' => false, 'js_exclude' => array(), 'js_include' => array(), 'show_link' => true, 'version' => $this->version, 'wp_path' => '' ); } function activate() { $this->upgrade_options(); } function deactivate() { // nothing to clean up } function need_upgrade() { $wpm_options = get_option($this->name); if ( array_key_exists('version', $wpm_options ) ) { $old_version_arr = split('\.', $wpm_options['version']); $new_version_arr = split('\.', $this->version); for($i = 0; $i < 3; $i++) { if ( (int)$new_version_arr[$i] > (int)$old_version_arr[$i] ) { return true; } elseif ( (int)$new_version_arr[$i] < (int)$old_version_arr[$i] ) { return false; } } return false; } return true; } function advertise() { $wpm_options = get_option($this->name); if ( !is_admin() && $wpm_options['show_link'] ) { printf("

Page optimized by WP Minify

"); } } function fetch_and_cache($url, $cache_file) { $content = implode(file($url)); // save cache file $fh = fopen($cache_file, 'w'); if ( $fh ) { fwrite($fh, $content); fclose($fh); } else { // cannot open for write. no error b/c something else is probably writing to the file. } return $content; } function cache_dir_writable() { $cache_dir = $this->get_plugin_dir().'/cache/'; if ( is_writable($cache_dir) ) { return true; } else { return false; } } function refetch_cache_if_expired($url, $cache_file) { $wpm_options = get_option($this->name); $cache_file_mtime = filemtime($cache_file); if ( (time() - $cache_file_mtime) > $wpm_options['cache_interval'] ) { $this->fetch_and_cache($url, $cache_file); } } function clear_cache() { $cache_location = $this->get_plugin_dir().'/cache/'; if(!$dh = @opendir($cache_location)) { return; } while (false !== ($obj = readdir($dh))) { if($obj == '.' || $obj == '..') { continue; } @unlink(trailingslashit($cache_location) . $obj); } closedir($dh); if ( function_exists('prune_super_cache') ) { prune_super_cache( $this->get_content_dir().'/cache/', true ); } } function tiny_filename($str) { $f = __FILE__; // no fancy shortening for Windows return ('/' === $f[0]) ? strtr(base64_encode(md5($str, true)), '+/=', '-_(') : md5($str); } function array_trim($arr, $charlist=null){ foreach($arr as $key => $value){ if (is_array($value)) $result[$key] = array_trim($value, $charlist); else $result[$key] = trim($value, $charlist); } return $result; } function check_and_split_url($url) { $wpm_options = get_option($this->name); // append &debug if we need to if ( $wpm_options['debug'] ) { $debug_url = '&debug=true'; } else { $debug_url = ''; } $url_chunk = explode('?f=', $url); $base_url = array_shift($url_chunk); $files = explode(',', array_shift($url_chunk)); $num_files = sizeof($files); if ( $url > $this->url_len_limit or $num_files > $this->minify_limit ) { $first_half = $this->check_and_split_url($base_url . '?f=' . implode(',', array_slice($files, 0, $num_files/2))); $second_half = $this->check_and_split_url($base_url . '?f=' . implode(',', array_slice($files, $num_files/2))); return $first_half + $second_half; } else { return array($base_url . '?f=' . implode(',', $files) . $debug_url); } } function fetch_content($url, $type) { $wpm_options = get_option($this->name); $cache_file = $this->get_plugin_dir().'/cache/'.md5($url).$type; $content = ''; if ( file_exists($cache_file) ) { // check cache expiration $this->refetch_cache_if_expired($url, $cache_file); $fh = fopen($cache_file, 'r'); if ( $fh && filesize($cache_file) > 0 ) { $content = fread($fh, filesize($cache_file)); fclose($fh); } else { // cannot open cache file so fetch it $content = $this->fetch_and_cache($url, $cache_file); } } else { // no cache file. fetch from internet and save to local cache $content = $this->fetch_and_cache($url, $cache_file); } return $content; } function get_script_src_from_handle($handle) { global $wp_scripts; $ver = $wp_scripts->registered[$handle]->ver ? $wp_scripts->registered[$handle]->ver : $wp_scripts->default_version; if ( isset($wp_scripts->args[$handle]) ) $ver .= '&' . $wp_scripts->args[$handle]; $src = $wp_scripts->registered[$handle]->src; if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) { $src = $wp_scripts->base_url . $src; } $src = add_query_arg('ver', $ver, $src); $src = clean_url(apply_filters( 'script_loader_src', $src, $handle )); $wp_scripts->print_scripts_l10n( $handle ); return $src; } function get_js_location($src) { if ( $this->debug ) echo 'Script URL:'.$src."
\n"; $wpm_options = get_option($this->name); $site_url = get_option('siteurl'); $script_path = str_replace(trailingslashit($site_url), '', $src); $script_path = preg_replace('/\?.*/i', '', $script_path); if ( substr($script_path, 0, 4) != 'http' && substr($script_path, -3, 3) == '.js' ) { // if script is local to server if ( $this->debug ) echo 'Local script detected:'.$script_path."
\n"; $location = $script_path; } else { // fetch scripts if necessary $this->fetch_content($src, '.js'); $location = $this->cache_location . md5($src) . '.js'; } return trailingslashit($wpm_options['wp_path']) . $location; } function get_css_location($src) { if ( $this->debug ) echo 'Style URL:'.$src."
\n"; $wpm_options = get_option($this->name); $site_url = get_option('siteurl'); $css_path = str_replace(trailingslashit($site_url), '', $src); $css_path = preg_replace('/^\//', '', $css_path); $css_path = preg_replace('/\?.*/i', '', $css_path); if ( substr($css_path, 0, 4) != 'http' && substr($css_path, -4, 4) == '.css' ) { // if css is local to server if ( $this->debug ) echo 'Local css detected:'.$css_path."
\n"; $location = $css_path; } else { // fetch css if necessary $this->fetch_content($src, '.css'); $location = $this->cache_location . md5($src) . '.css'; } return trailingslashit($wpm_options['wp_path']) . $location; } function build_minify_urls($locations) { $minify_url = $this->get_plugin_url() . '/min/?f='; $minify_url .= implode(',', $locations); return $this->check_and_split_url($minify_url); } function extract_css($content) { $wpm_options = get_option($this->name); $css_locations = array(); preg_match_all('/]*?)>/i', $content, $link_tags_match); foreach ($link_tags_match[0] as $link_tag) { if(strpos(strtolower($link_tag), 'stylesheet') !== false) { preg_match('/href=[\'"]([^\'"]+)/', $link_tag, $href_match); if ( $href_match[1] ) { // do not include anything in excluded list $skip = false; foreach ($wpm_options['css_exclude'] as $exclude_pat) { if ( strpos($src_match[1], $exclude_pat) !== false ) { $skip = true; break; } } if ( $skip ) continue; $content = str_replace($link_tag, '', $content); $css_locations[] = $this->get_css_location($href_match[1]); } } } foreach ($wpm_options['css_include'] as $src) { $css_locations[] = $this->get_css_location($src); } return array($content, $css_locations); } function inject_css($content, $css_locations) { if ( count($css_locations) > 0 ) { // build minify URLS $css_tags = ''; $minify_urls = $this->build_minify_urls($css_locations); foreach ($minify_urls as $minify_url) { if ( $this->debug ) echo 'Minify URL:'.$minify_url; $css_tags .= ""; } $content = preg_replace('/]*?>/', "\\0\n$css_tags", $content); } return $content; } function extract_js($content) { $wpm_options = get_option($this->name); $js_locations = array(); preg_match_all('/]*?)><\/script>/i', $content, $script_tags_match); foreach ($script_tags_match[0] as $script_tag) { if(strpos(strtolower($script_tag), 'text/javascript') !== false) { preg_match('/src=[\'"]([^\'"]+)/', $script_tag, $src_match); if ( $src_match[1] ) { // do not include anything in excluded list $skip = false; foreach ($wpm_options['js_exclude'] as $exclude_pat) { if ( strpos($src_match[1], $exclude_pat) !== false ) { $skip = true; break; } } if ( $skip ) continue; $content = str_replace($script_tag, '', $content); $js_locations[] = $this->get_js_location($src_match[1]); } } } foreach ($wpm_options['js_include'] as $src) { $js_locations[] = $this->get_js_location($src); } return array($content, $js_locations); } function inject_js($content, $js_locations) { if ( count($js_locations) > 0 ) { // build minify URLS $js_tags = ''; $minify_urls = $this->build_minify_urls($js_locations); foreach ($minify_urls as $minify_url) { if ( $this->debug ) echo 'Minify URL:'.$minify_url; $js_tags .= ""; } $content = preg_replace('/]*?>/', "\\0\n$js_tags", $content); } return $content; } function pre_content() { ob_start(); } function post_content() { $content = ob_get_contents(); ob_end_clean(); // minify JS list($content, $js_locations) = $this->extract_js($content); $content = $this->inject_js($content, $js_locations); // minify CSS list($content, $css_locations) = $this->extract_css($content); $content = $this->inject_css($content, $css_locations); echo $content; } function check_version() { // check WP version global $wp_version; if (!empty($wp_version) && is_admin() && version_compare($wp_version,'2.7','<')) { // load text domain for translations load_plugin_textdomain($this->name); printf('

%s '.$wp_version.'%s %s

', __('You are using WordPress version', $this->name), __('. WP Minify recommends that you use WordPress 2.7 or newer.', $this->name), __('Please update', $this->name)); } // check WP Minify version if ( $this->need_upgrade() ) { // load text domain for translations load_plugin_textdomain($this->name); $this->upgrade_options(); printf('

%s

', __('WP Minify options has been upgraded.', $this->name)); } } function update_options() { // new options $wpm_new_options = stripslashes_deep($_POST['wpm_options_update']); // current options $wpm_current_options = get_option($this->name); // convert "on" to true and "off" to false for checkbox fields // and set defaults for fields that are left blank if ( isset($wpm_new_options['show_link']) && $wpm_new_options['show_link'] == "on") $wpm_new_options['show_link'] = true; else $wpm_new_options['show_link'] = false; if ( isset($wpm_new_options['debug']) ) $wpm_new_options['debug'] = true; else $wpm_new_options['debug'] = false; if ( strlen(trim($wpm_new_options['js_include'])) > 0 ) $wpm_new_options['js_include'] = $this->array_trim(split(chr(10), str_replace(chr(13), '', $wpm_new_options['js_include']))); else $wpm_new_options['js_include'] = array(); if ( strlen(trim($wpm_new_options['js_exclude'])) > 0 ) $wpm_new_options['js_exclude'] = $this->array_trim(split(chr(10), str_replace(chr(13), '', $wpm_new_options['js_exclude']))); else $wpm_new_options['js_exclude'] = array(); if ( strlen(trim($wpm_new_options['css_include'])) > 0 ) $wpm_new_options['css_include'] = $this->array_trim(split(chr(10), str_replace(chr(13), '', $wpm_new_options['css_include']))); else $wpm_new_options['css_include'] = array(); if ( strlen(trim($wpm_new_options['css_exclude'])) > 0 ) $wpm_new_options['css_exclude'] = $this->array_trim(split(chr(10), str_replace(chr(13), '', $wpm_new_options['css_exclude']))); else $wpm_new_options['css_exclude'] = array(); // Update options foreach($wpm_new_options as $key => $value) { $wpm_current_options[$key] = $value; } update_option($this->name, $wpm_current_options); } function upgrade_options() { $wpm_options = get_option($this->name); if ( !$wpm_options ) { add_option($this->name, $this->get_default_options()); } else { $default_options = $this->get_default_options(); foreach($default_options as $option_name => $option_value) { if(!isset($wpm_options[$option_name])) { $wpm_options[$option_name] = $option_value; } } $wpm_options['version'] = $this->version; update_option($this->name, $wpm_options); } } function reset_options() { $wpm_options = get_option($this->name); if ( !$wpm_options ) { add_option($this->name, $this->get_default_options()); } else { // persists some options $default_options = $this->get_default_options(); $default_options['widget_ids'] = $wpm_options['widget_ids']; update_option($this->name, $default_options); } } function get_content_dir() { // Pre-2.6 compatibility if ( !defined('WP_CONTENT_DIR') ) define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); return WP_CONTENT_DIR; } function get_plugin_dir() { // Pre-2.6 compatibility return $this->get_content_dir().'/plugins/'.plugin_basename(dirname(__FILE__)); } function get_plugin_url() { // Pre-2.6 compatibility if ( !defined('WP_CONTENT_URL') ) define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); return WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)); } function get_current_page_url() { $isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"); $port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443"))); $port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : ''; $url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"]; return $url; } function admin_menu() { add_options_page('WP Minify', 'WP Minify', 'manage_options', 'wp-minify', array($this, 'admin_page')); } function admin_page() { // load text domain for translations load_plugin_textdomain($this->name); if ( isset($_POST['wpm_options_update_submit']) ) { // if user wants to update options check_admin_referer($this->name); $this->update_options(); printf('

%s

', __('WP Minify options has been updated.', $this->name)); } elseif ( isset($_POST['wpm_options_clear_cache_submit']) ) { // if user wants to regenerate nonce check_admin_referer($this->name); $this->clear_cache(); printf('

%s

', __('WP Minify cache has been cleared.', $this->name)); } elseif ( isset($_POST['wpm_options_upgrade_submit']) ) { // if user wants to upgrade options ( for new options on version upgrades ) check_admin_referer($this->name); $this->upgrade_options(); printf('

%s

', __('WP Minify options has been upgraded.', $this->name)); } elseif ( isset($_POST['wpm_options_reset_submit']) ) { // if user wants to reset all options check_admin_referer($this->name); $this->reset_options(); printf('

%s

', __('WP Minify options has been reset.', $this->name)); } if ( !$this->cache_dir_writable() ) { printf('

%s%s

', __('Cache directory is not writable. Please grant your server write permissions to the directory:', $this->name), $this->get_plugin_dir().'/cache/'); } printf('

WP Minify Options

%s |  %s
', preg_replace('/&wpm-page=[^&]*/', '', $_SERVER['REQUEST_URI']), __('General Configurations', $this->name), $this->homepage, __('Documentation', $this->name) ); if ( isset($_GET['wpm-page']) ) { if ( $_GET['wpm-page'] || !$_GET['wpm-page'] ) { require_once('options-generic.php'); } } else { require_once('options-generic.php'); } printf('
' ); } // function admin_page() } // class wpm } // if !class_exists('WPMinify' $wp_minify = new WPMinify(); add_action('admin_notices', array($wp_minify, 'check_version')); add_action('admin_menu', array($wp_minify, 'admin_menu')); add_action('wp_footer', array($wp_minify, 'advertise')); // No need to minify admin stuff if ( !is_admin() ) { add_action('get_header', array($wp_minify, 'pre_content')); add_action('get_footer', array($wp_minify, 'post_content')); } register_activation_hook(__FILE__, array($wp_minify, 'activate')); register_deactivation_hook(__FILE__, array($wp_minify, 'deactivate')); ?>