1, 'style_location' => 'default', 'style_file_name' => 'merged_style.css', 'force_use_newest_style' => 1, // Scripts 'merge_scripts' => 'merge-header', 'minify_script' => 1, 'script_location' => 'default', 'script_file_name' => 'merged_script.js', 'cache_age' => 1, 'manage_scripts' => 0, 'force_use_newest_script' => 1 ); /** 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 $addOptions = 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->adminNotices = new AphmsAdminNotices; register_activation_hook ( APHMS_PLUGIN_PATH . APHMS_DS . 'plugin.php', array($this, 'activatePlugin') ); register_deactivation_hook ( APHMS_PLUGIN_PATH . APHMS_DS . 'plugin.php', array($this, 'deactivatePlugin') ); add_filter( 'plugin_action_links', array($this, 'actionLink'), 10, 5); add_action( 'admin_enqueue_scripts', array($this, 'registerScripts') ); add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); add_action( 'admin_init', array( $this, 'page_init' ) ); } public function deactivatePlugin() { delete_option(APHMS_OPTION); delete_option(APHMS_OPTION_NOTICE); } public function activatePlugin() { $data = array_merge ($this->data, $this->addOptions); update_option(APHMS_OPTION, $data); update_option(APHMS_OPTION_NOTICE, array()); } public function actionLink($links, $file) { static $plugin; if (!isset($plugin)) $plugin = APHMS_PLUGIN_DIR_NAME . '/plugin.php'; 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() { ?>

APH Merge Scripts Options

deleteCache(); register_setting( 'aph_merge_group', // Option group APHMS_OPTION, array ($this, 'submitValidation')// Option name ); // Merge Style Options add_settings_section('option_page', 'Merge Style Files', '__return_true', 'style_options'); add_settings_field('style_merge', 'Merge & Minify Options', array($this, 'styleOptions'), 'style_options', 'option_page'); add_settings_field('style_location', 'File Name & Location', array($this, 'styleLocation'), 'style_options', 'option_page'); // Merge Script Options add_settings_section('option_page', 'Merge Javascript Files', '__return_true', 'script_options'); add_settings_field('script_merge', 'Merge & Minify Scripts', array($this, 'scriptOptions'), 'script_options', 'option_page'); add_settings_field('script_location', 'File Name & Location', array($this, 'scriptLocation'), 'script_options', 'option_page'); // General Options add_settings_section('option_page', 'General Options', '__return_true', 'general_options'); add_settings_field('cache_age', 'Cache Age', array($this, 'cacheAge'), 'general_options', 'option_page'); add_settings_field('manage_scripts', 'Manage Scripts', array($this, 'manageScripts'), 'general_options', 'option_page'); add_settings_field('clear_cache', 'Clear All Cache', array($this, 'clearCacheOptions'), 'general_options', 'option_page'); } public function styleOptions() { $checked_merge = $this->options['merge_styles'] ? ' checked="checked"' : ''; $checked_minify = $this->options['minify_style'] ? ' checked="checked"' : ''; ?>

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 style that maybe ever cached by the browser.

'Default (plugin)', 'theme' => 'Current Theme');?>

NOTE: The existing file (if any) will be overwritten

options['merge_scripts'] ? ' checked="checked"' : ''; $checked_minify = $this->options['minify_script'] ? ' checked="checked"' : ''; $lists = array('donot-merge' => 'Do not merge', 'merge-header' => 'Merge and place it on the header', 'merge-footer' => 'Merge and place it on the footer' );?>

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.

'Default (plugin)', 'theme' => 'Current Theme');?>

NOTE: The existing file (if any) will be overwritten

restOfOptions); // echo '
'; print_r($inputs);
		foreach ($this->addOptions as $key => $val)
		{
			if (!key_exists($key, $inputs))
			{
				if (key_exists($key, $this->options))
					$inputs[$key] = $this->options[$key];
				else
					$inputs[$key] = $val;
			}
		}
		
		if (key_exists('merge_styles', $inputs)
			&& trim($inputs['style_file_name']) == '')
		{
			add_settings_error(
				'file_script',
				'style-error',
				'Please enter style file name',
				'error'
			);
		}
		
		if (key_exists('merge_scripts', $inputs)
			&& trim($inputs['script_file_name']) == '')
		{
			add_settings_error(
				'file_script',
				'script-error',
				'Please enter script file name',
				'error'
			);
		}
		
		// echo '
'; print_r($inputs); echo '
'; return $inputs; } public function cacheAge() { ?> day(s)

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, then the plugin stop merging the scripts.

Clear Cache

Cache File:
'; if ($file_style_exists) echo 'Style: ' . $file_style['file_url'] . '
'; if ($file_script_exists) echo 'Script: ' . $file_script['file_url'] . '
'; } else { echo 'Cache file not found'; } } public function deleteCache() { $file_style = AphmsHelper::mergeFilePath('style'); $file_script = AphmsHelper::mergeFilePath('script'); $file_style_exists = file_exists($file_style['file_path']); $file_script_exists = file_exists($file_script['file_path']); $msg = ''; $fail = ''; // Style if ($file_style_exists) { unlink($file_style['file_path']); if (!file_exists($file_style['file_path'])) { $msg .= 'Style'; } } else { $fail .= 'Style'; } // Script if ($file_script_exists) { unlink($file_script['file_path']); if (!file_exists($file_script['file_path'])) { if ($msg) $msg .= ' and script'; else $msg .= ' Script'; } } else { if ($fail) $fail .= ' and Script'; else $fail .= 'Script'; } // Save notice if ($msg) { $msg .= ' cache file(s) have been successfully deleted.'; $this->adminNotices->addNotice($msg, 'success', true, true); } if ($fail) { $fail .= ' cache file(s) could not be found.'; $this->adminNotices->addNotice($fail, 'error', true, true); } wp_redirect( remove_query_arg('_wpnonce') ); exit; } } ?>