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, // CDN 'remote_files' => 3, 'remote_files_age' => 30, // day 'request_timeout' => 10, // sec // Optiond 'run_on_404' => 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' => '') ), 'remote_handles' => array('style' => array(), 'script' => array() ) ); /** * 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); $plugin_version = get_option( APHMS_OPTION_VERSION, array() ); if ($this->options) { if (!$plugin_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 { if (version_compare('1.3', $plugin_version) > 0) { $this->options['remote_handles'] = array('style' => array(), 'script' => array() ); $this->options['remote_files'] = 3; $this->options['remote_files_age'] = 30; $this->options['request_timeout'] = 10; $this->options['run_on_404'] = 0; update_option(APHMS_OPTION, $this->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.
Getting remote files will take a moment at the first run
'; print_r($inputs); die;
return $inputs;
}
public function cache_age()
{ ?>
day(s)
Fill 0 to force the plugin to generate new merged file
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 merged file.
';
foreach ($lists as $key => $list) {
$selected = $key == $this->options['run_on_404'] ? ' selected="selected"' : '';
echo '';
}
?>
If chosing yes, then MAKE SURE that there is NO broken link of assets (css, js, fonts). In broken links, WordPress will redirect to 404 page in the background, so this plugin will also run in the background and break the merged CSS and JS.
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 scripts
options['excluded_handles']) == 1 && in_array('merged-script', $this->options['excluded_handles']))
{
echo 'Excluded handle not found';
} else {
echo 'The following script handle(s) are excluded from merging:
';
foreach ($this->options['excluded_handles'] as $handle)
{
if ($handle == 'merged-script')
continue;
echo '- ' . $handle . '
';
}
echo '
You can include those handle(s) to be merged with others through Admin Bar on the page where the script is loaded. Don\'t forget to activate the option: "Show Manage Scripts Menu on the Admin Bar"';
}
}
public function clear_cache_options()
{
$file_style = APHMS_PLUGIN_PATH . APHMS_DS . 'merged' . APHMS_DS . 'merged-style-' . $this->options['curr_handles']['style']['time'] . '.css';
$file_script = APHMS_PLUGIN_PATH . APHMS_DS . 'merged' . APHMS_DS . 'merged-script-' . $this->options['curr_handles']['script']['time'] . '.js';
$file_style_exists = file_exists($file_style);
$file_script_exists = file_exists($file_script);
if ($file_style_exists || $file_script_exists)
{
$site_url = site_url();
$parse_url = parse_url($_SERVER['REQUEST_URI']);
echo 'Clear All Cache
';
$lists = array('style', 'script');
foreach ($lists as $type)
{
if (${'file_' . $type . '_exists'})
{
$file_merged_url = APHMS_PLUGIN_URL . '/merged/merged-' . $type . '-' . $this->options['curr_handles'][$type]['time'] . '.js';
$file_merged_handles = APHMS_PLUGIN_PATH . APHMS_DS . 'merged' . APHMS_DS . $type . '-handles.txt';
$show_handle = file_exists($file_merged_handles) ? 'Show Handles' : '-';
echo '
' . ucfirst($type) . ' Cache:
File: ' . $file_merged_url . ' ('.round(filesize(${'file_' . $type})/1024) . ' KB )
Handles: ' . $show_handle . '
';
if (file_exists($file_merged_handles))
{
$exp = explode (PHP_EOL, file_get_contents($file_merged_handles));
echo '';
}
echo '';
}
}
echo '';
} else {
echo 'Cache file not found';
}
}
public function delete_cache()
{
$deleted = array_map('unlink', glob(APHMS_PLUGIN_PATH . APHMS_DS . 'merged/*.*'));
// Save notice
if ($deleted[0])
$msg = 'Cache file(s) have successfully been deleted.';
else
$msg = 'Cache file(s) could not be deleted.';
$this->admin_notices->add_notice($msg, 'success', true, true);
wp_redirect( remove_query_arg('_wpnonce') );
exit;
}
}
?>