array(), 'footer' => array() ); /** Wordpress to_do scripts, used both by mergescripts and admin_bar */ private $to_do; /** Check whether the method is already executed. This is to prevent the method from executed by other plugins */ private $isExecuted = array('print_scripts' => false, 'footer_scripts' => false, 'header_scripts' => false ); public function __construct () { $this->options = get_option(APHMS_OPTION); // unset($this->options['style_handles_inc']); // unset($this->options['style_handles_ex']); // $this->options['rest_of_handles']['header'] = array(); // $this->options['rest_of_handles']['footer'] = array(); // $this->options['excluded_handles'][] = 'merged-scripts'; // update_option(APHMS_OPTION, $this->options); // echo '
'; print_r($this->options);die;
// die;
}
public function init()
{
if (!$this->options['manage_scripts'])
return;
add_action( 'wp_ajax_nopriv_aphms-save-assets' , array( $this , 'ajaxNoPriv' ) );
add_action( 'wp_ajax_aphms-save-assets', array($this, 'ajaxSaveAssets') );
if (is_admin())
return;
add_action( 'admin_bar_init', array($this, 'adminBarInit'));
}
public function adminBarInit()
{
add_action( 'wp_print_scripts', array( $this, 'loadCurrentScripts' ),10 );
add_action( 'wp_head', array( $this, 'loadHeadScripts' ),9999 );
add_action( 'wp_footer', array( $this, 'loadFooterScripts' ),9999 );
add_action( 'wp_footer', array( $this, 'printAllScripts' ),9999 );
// add_action( 'wp_head', array( $this, 'deregisterScript2' ) );
add_action( 'wp_enqueue_scripts', array($this, 'registerScripts') );
add_action( 'admin_bar_menu', array($this, 'AphAdminBar'));
}
public function ajaxNoPriv()
{
// echo 'xxx'; die;
}
public function ajaxSaveAssets()
{
parse_str($_POST['input'], $inputs);
$all_handles = explode(',', $inputs['all_handles']);
$to_do = explode(',', $inputs['to_do_handles']);
$scripts = unserialize(file_get_contents(APHMS_PLUGIN_PATH . APHMS_DS . 'temp' . APHMS_DS . 'temp.dt'));
$rest = array('header' => array(), 'footer' => array());
foreach ($scripts as $pos => $handles)
{
foreach ($handles as $handle => $handle_value)
{
// Key send by ajax will replace . to _, so we use in_array, instead of key_exists
if (in_array($handle, $inputs))
{
if (in_array($handle, $this->options['excluded_handles']))
{
unset($this->options['excluded_handles'][$handle]);
}
} else {
// unset($this->options['rest_of_handles'][$handle]);
if (!in_array($handle, $this->options['excluded_handles']))
$this->options['excluded_handles'][$handle] = $handle;
}
if (!in_array($handle, $to_do))
$this->options['rest_of_handles']['script'][$pos][$handle] = $handle_value;
}
}
// $this->options['rest_of_handles']['script'] = $rest;
update_option( APHMS_OPTION, $this->options );
wp_send_json_success(
array(
'msg' => 'Saved',
'script' => $this->options
)
);
}
public function AphAdminBar()
{
global $wp_admin_bar;
$menu_id = 'aphms-admin-bar';
$wp_admin_bar->add_menu(
array('id' => $menu_id,
'title' => _('APH Show Scripts'),
'parent' => 'top-secondary'
)
);
}
public function registerScripts()
{
wp_register_style( 'aph-merge-script', plugins_url() . '/aph-merge-scripts/assets/css/aphms.css?rand='.time(), '', APHMS_PLUGIN_VERSION);
wp_enqueue_style('aph-merge-script');
wp_enqueue_script('aphms-admin-bar', untrailingslashit( plugin_dir_url(__FILE__) ) . '/assets/js/aphms.js?r='.time(), array('jquery'), APHMS_PLUGIN_VERSION, true);
wp_localize_script (
'aphms-admin-bar',
'aphms',
array(
'nonce' => wp_create_nonce('aph-merge-scripts'),
'ajaxurl' => admin_url('admin-ajax.php')
)
);
}
public function loadHeadScripts()
{
if ($this->isExecuted['header_scripts'])
return;
global $wp_scripts;
$this->scripts['header'] = array();
foreach ($wp_scripts->done as $handle)
{
if (key_exists($handle, $wp_scripts->registered))
{
$this->scripts['header'][$handle]['group'] = $wp_scripts->groups[$handle];
foreach ($wp_scripts->registered[$handle] as $key => $val)
{
$this->scripts['header'][$handle][$key] = $val;
}
}
}
$this->isExecuted['header_scripts'] = true;
}
public function loadFooterScripts()
{
if ($this->isExecuted['footer_scripts'])
return;
global $wp_scripts;
$this->scripts['footer'] = array();
// echo 'ttt'; print_r($wp_scripts->done);
foreach ($wp_scripts->done as $handle)
{
if (key_exists($handle, $this->scripts['header']))
continue;
if (key_exists($handle, $wp_scripts->registered))
{
// $this->scripts['footer'][$handle]['group'] = $wp_scripts->groups[$handle];
foreach ($wp_scripts->registered[$handle] as $key => $val)
{
$this->scripts['footer'][$handle][$key] = $val;
}
}
}
$this->isExecuted['footer_scripts'] = true;
}
public function loadCurrentScripts()
{
if ($this->isExecuted['print_scripts'])
return;
global $wp_scripts;
$aphms_scripts = $wp_scripts;
$queues = $wp_scripts->queue;
$aphms_scripts->all_deps($queues);
$this->to_do = $aphms_scripts->to_do;
$this->isExecuted['print_scripts'] = true;
}
public function printAllScripts()
{
// echo ''; print_r($this->scripts);
$notMerged = $not_detected = $not_in_current = 0;
$merged['header'] = array_merge($this->scripts['header'], $this->options['rest_of_handles']['script']['header']);
$merged['footer'] = array_merge($this->scripts['footer'], $this->options['rest_of_handles']['script']['footer']);
// echo 'yys'; print_r( $merged);echo '
';
$all_handles = array();
$rest = array('header' => $this->scripts['header'], 'footer' => $this->scripts['footer']);
file_put_contents(APHMS_PLUGIN_PATH . APHMS_DS . 'temp' . APHMS_DS .'temp.dt', serialize($rest));
echo '
Choose scripts you want to merge. Show Info
Note that the merged scripts will be deregistered, this will make wordpress deregister the dependent script too. For example: admin-bar script need jquery, so if we merge the jquery script, the admin-bar script will be deregistered too
If the admin-bar script included in the merged script, then it will be ok, otherwise, it will not be loaded by the wordpress that probably cause your page has not functionality as it should
';
if ($notMerged)
{
echo '';;
}
echo '
';
}
private function skipHandle($handle)
{
$excluded_script = $this->options['excluded_handles'];
if (in_array($handle, $excluded_script))
return true;
else
return false;
}
public function displayNotice($msg, $type) {
add_settings_error(
'aph_notice',
'option-notice',
$msg,
$type
);
}
}