1, 'minify_style' => 1, 'style_location' => 'default', 'style_file_name' => 'merged_style.css', 'style_query_string' => 0, // Scripts 'merge_scripts' => 1, 'minify_script' => 1, 'script_location' => 'default', 'script_file_name' => 'merged_script.js', 'cache_age' => 1, 'manage_scripts' => 0, 'script_query_string' => 0 ); /** Additional options that do not include in the admin form. We use it on the admin bar section. We use wordpress settings API so when we save the options, only input elements that have name attribute will be pass the value, and overwrite the existing options. */ private $add_options = array( 'excluded_handles' => array('merged-script' => 'merged-script'), 'rest_of_handles' => array('script' => array( 'header' => array(), 'footer' => array() ), 'style' => array( 'header' => array(), 'footer' => array() ) ), // Current handles used to build the merged file (.js or .css), useful if we use the cache option 'curr_handles' => array('style' => array('time' => '', 'handles' => '') , 'script' => array('time' => '', 'handles' => '') ) ); /** * Start up */ public function __construct() { $this->options = get_option( APHMS_OPTION, array() ); $this->admin_notices = new Aphms_Admin_Notices; register_activation_hook ( APHMS_PLUGIN_PATH . APHMS_DS . APHMS_PLUGIN_FILE_NAME, array($this, 'activate_plugin') ); register_deactivation_hook ( APHMS_PLUGIN_PATH . APHMS_DS . APHMS_PLUGIN_FILE_NAME, array($this, 'deactivate_plugin') ); add_action( 'wp_ajax_aphms-check-exec', array($this, 'ajax_check_exec') ); add_filter( 'plugin_action_links', array($this, 'action_link'), 10, 5); add_action( 'admin_enqueue_scripts', array($this, 'register_scripts') ); add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); add_action( 'admin_init', array( $this, 'page_init' ) ); } public function deactivate_plugin() { // Clear cache array_map('unlink', glob(APHMS_PLUGIN_PATH . APHMS_DS . 'merged/*.*')); } public function activate_plugin() { $data = array_merge ($this->data, $this->add_options); $opt_version = get_option( APHMS_OPTION_VERSION, array() ); if ($this->options) { if (!$opt_version) { $new_opt = array(); if (key_exists('merge_styles', $this->options)) { $this->options['minify_style'] = 1; } else { $new_opt['merge_styles'] = 0; $new_opt['minify_style'] = 1; } if ($this->options['merge_scripts'] == 'merge-header') $this->options['merge_scripts'] = 1; else if ($this->options['merge_scripts'] == 'merge-footer') $this->options['merge_scripts'] = 2; else if ($this->options['merge_scripts'] == 'donot-merge') $this->options['merge_scripts'] = 0; if (!key_exists('minify_script', $this->options)) { $new_opt['minify_script'] = 0; } if (key_exists('force_use_newest_style', $this->options)) { unset($this->options['force_use_newest_style']); } $new_opt['style_query_string'] = 0; if (key_exists('force_use_newest_script', $this->options)) { unset($this->options['force_use_newest_script']); } $new_opt['script_query_string'] = 0; $new_options = array_merge($new_opt, $this->options); update_option(APHMS_OPTION, $new_options); update_option(APHMS_OPTION_VERSION, APHMS_PLUGIN_VERSION); } } else { update_option(APHMS_OPTION, $data); update_option(APHMS_OPTION_VERSION, APHMS_PLUGIN_VERSION); } } /** * Add Settings ling to plugin list Settings | Deactivate | Edit */ public function action_link($links, $file) { static $plugin; if (!isset($plugin)) $plugin = APHMS_PLUGIN_DIR_NAME . '/' . APHMS_PLUGIN_FILE_NAME; // echo $plugin; die; if ($plugin == $file) { $setting_link = 'Settings'; array_unshift($links, $setting_link); } return $links; } /** * Add options page */ public function add_plugin_page() { // This page will be under "Settings" $page_title = 'APH Merge Scripts Options'; $menu_title = 'APH Merge Scripts'; add_options_page( $page_title, $menu_title, 'manage_options', 'aph-merge-scripts', array( $this, 'admin_page' ) ); } /** * Options page callback */ public function admin_page() { ?>
'Do not merge', 1 => 'Merge all CSS files');?>
'Do not minify', 1 => 'Minify using default minifier');?>
This option will add a random value of query string (eq. merged_style.css?rand=43562). This is useful when we need the fresh content of the css file that maybe ever cached by the browser.
'Do not minify', 1 => 'Minify using default minifier', 2 => 'Minify using default minifier + YUI Compressor' ); $disabled = $warn = ''; if (!function_exists('exec')) { $warn = "PHP exec() function is disabled, so, we don't able to use YUI Compressor"; $disabled = ' disabled="disabled"'; } else { if(!shell_exec('java -version 2>&1')) { $warn = "Your server doesn't have java installed, so, we don't able to use YUI Compressor"; } } ?> ' . $warn . '
'; } ?>We don't use Closure Compiler as it's not 100% safe, tested on jQuery, read here.
options['script_query_string'] ? ' checked="checked"' : ''; ?>
This option will add a random value of query string (eq. merged_script.css?rand=43562). This is useful when we need the fresh content of the script that maybe ever cached by the browser.
Fill 0 to not cache the merged style & script. Plugin first search for cache (the merged file), if the last modified time less than the given value, then the cache will be used
While using the cache, the plugin will always check what style and script files are merged into the cache file, if those files are meet all of the files needed by the current page, then cached version is used, otherwise, the plugin will generate new files.
options['manage_scripts'] ? ' checked="checked"' : ''; ?>While opening a page, the plugin will automatically detect and merge javascript files that are loaded, but on certain conditions, not all of those scripts can be detected (typically scripts loaded very late in the wp_footer such as: akismet-form).
You can find these files through the "APH Merge Scripts" menu located on the admin bar on each page, this menu appears when you open a page, from that menu, you can add those kind of files to manually combine with others.
When this option is activated and the admin bar is displayed, the plugin will stop merging script
options['excluded_handles']) == 1 && in_array('merged-script', $this->options['excluded_handles'])) { echo '-'; } else { echo 'The following script handle(s) are excluded from merging:
Style Cache:
File: ' . $file_style_url . ' ('.round(filesize($file_style)/1024) . ' KB )
Handles: ' . $show_handle . '
'; print_r($exp); echo ''; } echo ''; } if ($file_script_exists) { $file_script_url = APHMS_PLUGIN_URL . '/merged/merged-script-' . $this->options['curr_handles']['script']['time'] . '.js'; $file_script_handles = APHMS_PLUGIN_PATH . APHMS_DS . 'merged' . APHMS_DS . 'script-handles.txt'; $show_handle = file_exists($file_script_handles) ? 'Show Handles' : '-'; echo 'Script Cache:
File: ' . $file_script_url . ' ('.round(filesize($file_script)/1024) . ' KB )
'; if (file_exists($file_script_handles)) { $exp = explode (PHP_EOL, file_get_contents($file_script_handles)); echo ''; } echo ''; } echo '
Handles: ' . $show_handle . '