. */ class WPMinify { var $author_homepage = 'http://omninoggin.com/'; var $homepage = 'http://omninoggin.com/wordpress-plugins/wp-minify-wordpress-plugin/'; var $name = 'wp_minify'; var $name_dashed = 'wp-minify'; var $name_proper = 'WP Minify'; var $required_wp_version = '2.7'; var $version = '1.2.0'; var $c = null; var $debug = false; var $cache_location = 'wp-content/plugins/wp-minify/cache/'; var $url_len_limit = 2000; var $minify_limit = 50; var $buffer_started = false; var $default_exclude = array(); function WPMinify() { // initialize common functions $this->c = new WPMinifyCommon($this); // load translation $this->c->load_text_domain(); // register admin scripts add_action('admin_init', array($this->c, 'a_register_scripts')); add_action('admin_init', array($this->c, 'a_register_styles')); // check wp version add_action('admin_head', array($this->c, 'a_check_version')); // load admin menu add_action('admin_menu', array($this, 'a_menu')); // register ajax handler add_action('wp_ajax_wpm', array($this, 'a_ajax_handler')); // No need to minify admin stuff if (!is_admin() && !is_feed() && !defined('XMLRPC_REQUEST')) { // Don't minify if if user passes wp-minify-off=1 in GET parameter if (!(isset($_GET['wp-minify-off']) && $_GET['wp-minify-off'])) { add_action('init', array($this, 'pre_content'), 99999); add_action('wp_footer', array($this, 'post_content')); } // advertise hook add_action('wp_footer', array($this, 'advertise')); } } // admin functions function a_default_options() { return array( 'cache_external' => false, 'cache_interval' => 900, 'css_exclude' => array(), 'css_include' => array(), 'debug_nominify' => false, 'debug_firephp' => false, 'enable_css' => true, 'enable_js' => true, 'enable_html' => true, 'auto_base' => true, 'extra_minify_options' => '', 'js_exclude' => array(), 'js_include' => array(), 'js_in_footer' => false, 'force_https' => false, 'pretty_url' => false, 'show_link' => false, 'show_advanced' => false, 'uri_exclude' => array(), 'version' => $this->version, 'deprecated' => array( 'wp_path', 'debug', 'debug_noprettyurl' ) ); } function a_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['enable_js'])) $wpm_new_options['enable_js'] = true; else $wpm_new_options['enable_js'] = false; if (isset($wpm_new_options['enable_css'])) $wpm_new_options['enable_css'] = true; else $wpm_new_options['enable_css'] = false; if (isset($wpm_new_options['enable_html'])) $wpm_new_options['enable_html'] = true; else $wpm_new_options['enable_html'] = false; if (isset($wpm_new_options['auto_base'])) $wpm_new_options['auto_base'] = true; else $wpm_new_options['auto_base'] = false; if (isset($wpm_new_options['cache_external'])) $wpm_new_options['cache_external'] = true; else $wpm_new_options['cache_external'] = false; if (isset($wpm_new_options['js_in_footer'])) $wpm_new_options['js_in_footer'] = true; else $wpm_new_options['js_in_footer'] = false; if (isset($wpm_new_options['pretty_url'])) $wpm_new_options['pretty_url'] = true; else $wpm_new_options['pretty_url'] = false; if (isset($wpm_new_options['debug_nominify'])) $wpm_new_options['debug_nominify'] = true; else $wpm_new_options['debug_nominify'] = false; if (isset($wpm_new_options['debug_firephp'])) $wpm_new_options['debug_firephp'] = true; else $wpm_new_options['debug_firephp'] = false; $this->a_set_minify_config($wpm_new_options['debug_nominify'], $wpm_new_options['debug_firephp']); if ( isset($wpm_new_options['force_https']) ) $wpm_new_options['force_https'] = true; else $wpm_new_options['force_https'] = 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(); if ( strlen(trim($wpm_new_options['uri_exclude'])) > 0 ) $wpm_new_options['uri_exclude'] = $this->array_trim(split(chr(10), str_replace(chr(13), '', $wpm_new_options['uri_exclude']))); else $wpm_new_options['uri_exclude'] = array(); // Update options foreach ($wpm_new_options as $key => $value) { $wpm_current_options[$key] = $value; } update_option($this->name, $wpm_current_options); } function a_set_advanced_options($val) { $wpm_options = get_option($this->name); $wpm_options['show_advanced'] = $val; update_option($this->name, $wpm_options); } function a_ajax_handler() { check_ajax_referer($this->name); if (isset($_POST['wpm_action'])) { if (strtolower($_POST['wpm_action']) == 'show_advanced') { $this->a_set_advanced_options(true); } elseif (strtolower($_POST['wpm_action']) == 'hide_advanced') { $this->a_set_advanced_options(false); } else { echo 'Invalid wpm_action.'; } } exit(); } function a_request_handler() { if (isset($_POST['wpm_options_update_submit'])) { check_admin_referer($this->name); $this->a_update_options(); add_action('admin_notices', array($this->c, 'a_notify_updated')); } elseif (isset($_POST['wpm_options_clear_cache_submit'])) { // if user wants to regenerate nonce check_admin_referer($this->name); $this->c->a_clear_cache(); add_action('admin_notices', array($this->c, 'a_notify_cache_cleared')); } 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->c->a_upgrade_options(); add_action('admin_notices', array($this->c, 'a_notify_upgraded')); } elseif (isset($_POST['wpm_options_reset_submit'])) { // if user wants to reset all options check_admin_referer($this->name); $this->c->a_reset_options(); add_action('admin_notices', array($this->c, 'a_notify_reset')); } // only check these on plugin settings page $this->c->a_check_dir_writable($this->c->get_plugin_dir().'cache/', array($this, 'a_notify_cache_not_writable')); $this->c->a_check_orphan_options(array($this, 'a_notify_orphan_options')); if ($this->c->a_check_dir_writable($this->c->get_plugin_dir().'min/config.php', array($this, 'a_notify_config_not_writable'))) { $this->a_check_minify_config(); } } function a_check_minify_config() { $fname = $this->c->get_plugin_dir().'min/config.php'; $fhandle = fopen($fname,'r'); $content = fread($fhandle,filesize($fname)); $config_modified = false; preg_match('/\/\/###WPM-CACHE-PATH-BEFORE###(.*)\/\/###WPM-CACHE-PATH-AFTER###/s', $content, $matches); $cache_path_code = $matches[1]; if (!preg_match('/\$min_cachePath.*?/', $cache_path_code)) { $content = preg_replace( '/\/\/###WPM-CACHE-PATH-BEFORE###(.*)\/\/###WPM-CACHE-PATH-AFTER###/s', "//###WPM-CACHE-PATH-BEFORE###\n".'$min_cachePath = \''.$this->c->get_plugin_dir()."cache/';\n//###WPM-CACHE-PATH-AFTER###", $content); $config_modified = true; } preg_match('/\/\/###WPM-CACHE-AGE-BEFORE###(.*)\/\/###WPM-CACHE-AGE-AFTER###/s', $content, $matches); $cache_age_code = $matches[1]; if (!preg_match('/\$min_serveOptions\[\'maxAge\'].*?/', $cache_age_code)) { $content = preg_replace( '/\/\/###WPM-CACHE-AGE-BEFORE###(.*)\/\/###WPM-CACHE-AGE-AFTER###/s', "//###WPM-CACHE-AGE-BEFORE###\n".'$min_serveOptions[\'maxAge\'] = 2592000;'."\n//###WPM-CACHE-AGE-AFTER###", $content); $config_modified = true; } if ($config_modified) { $this->a_notify_modified_minify_config(); } $fhandle = fopen($fname,"w"); fwrite($fhandle,$content); fclose($fhandle); } function a_set_minify_config($nominify = false, $firephp = false) { if ($nominify) { $nominify = 'true'; } else { $nominify = 'false'; } if ($firephp) { $firephp = 'true'; } else { $firephp = 'false'; } $fname = $this->c->get_plugin_dir().'min/config.php'; $fhandle = fopen($fname,'r'); $content = fread($fhandle,filesize($fname)); $content = preg_replace( '/\/\/###WPM-DEBUG-FLAG-BEFORE###(.*)\/\/###WPM-DEBUG-FLAG-AFTER###/s', "//###WPM-DEBUG-FLAG-BEFORE###\n".'$min_allowDebugFlag = '.$nominify.";\n//###WPM-DEBUG-FLAG-AFTER###", $content); $content = preg_replace( '/\/\/###WPM-ERROR-LOGGER-BEFORE###(.*)\/\/###WPM-ERROR-LOGGER-AFTER###/s', "//###WPM-ERROR-LOGGER-BEFORE###\n".'$min_errorLogger = '.$firephp.";\n//###WPM-ERROR-LOGGER-AFTER###", $content); $fhandle = fopen($fname,"w"); fwrite($fhandle,$content); fclose($fhandle); } function a_notify_cache_not_writable() { $this->c->a_notify( sprintf('%s: %s', __('Cache directory is not writable. Please grant your server write permissions to the directory', $this->name), $this->c->get_plugin_dir().'cache/'), true); } function a_notify_config_not_writable() { $this->c->a_notify( sprintf('%s: %s', __('Minify Engine config.php is not writable. Please grant your server write permissions to file', $this->name), $this->c->get_plugin_dir().'min/config.php')); } function a_notify_orphan_options() { $this->c->a_notify( sprintf('%s', __('Some option settings are missing (possibly from plugin upgrade).', $this->name))); } function a_notify_modified_minify_config() { $this->c->a_notify(__('Minify Engine config.php was configured automatically.', $this->name)); } function a_menu() { $options_page = add_options_page($this->name_proper, $this->name_proper, 'manage_options', 'wp-minify', array($this, 'a_page')); add_action('admin_head-'.$options_page, array($this, 'a_request_handler')); add_action('admin_print_scripts-'.$options_page, array($this->c, 'a_enqueue_scripts')); add_action('admin_print_styles-'.$options_page, array($this->c, 'a_enqueue_styles')); } function a_page() { $wpm_options = get_option($this->name); printf('
Page optimized by $this->name_proper WordPress Plugin
"); } } } // class WPMinify require_once('common.php'); require_once('http_build_url.php'); $wp_minify = new WPMinify(); ?>