function advagg_mod_remove_js_if_not_used in Advanced CSS/JS Aggregation 7.2
Remove JS if not in use on current page.
Parameters
array $js: JS array.
1 call to advagg_mod_remove_js_if_not_used()
- advagg_mod_js_post_alter in advagg_mod/
advagg_mod.module - Alter the js array.
File
- advagg_mod/
advagg_mod.module, line 3570 - Advanced aggregation modifier module.
Code
function advagg_mod_remove_js_if_not_used(array &$js) {
$files_skiplist = array(
'drupal.js',
'jquery.js',
'jquery.min.js',
'jquery.once.js',
);
$inline_skiplist = array();
if (module_exists('jquery_update')) {
$inline_skiplist[] = 'document.write("<script src=\'' . $GLOBALS['base_path'] . drupal_get_path('module', 'jquery_update') . '/replace/jquery/' . variable_get('jquery_update_jquery_version', '1.10') . '/jquery' . (variable_get('jquery_update_compression_type', 'min') === 'none' ? '' : '.min') . ".js'>";
}
if (module_exists('labjs')) {
$inline_skiplist[] = 'var $L = $LAB.setGlobalDefaults';
}
$include_jquery = FALSE;
$include_drupal = FALSE;
module_load_include('inc', 'advagg', 'advagg');
// Look at each JavaScript entry and get the info on it.
$files_info_filenames = array();
foreach ($js as &$values) {
if ($values['type'] !== 'file' || !is_string($values['data'])) {
continue;
}
foreach ($files_skiplist as $skip_name) {
if (strlen($skip_name) < strlen($values['data']) && substr_compare($values['data'], $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
continue 2;
}
}
$files_info_filenames[] = $values['data'];
}
unset($values);
$files_info = advagg_get_info_on_files($files_info_filenames);
// Look at each JavaScript entry and see if it uses jquery or drupal.
foreach ($js as $name => &$values) {
if ($values['type'] === 'file' || $values['type'] === 'external') {
foreach ($files_skiplist as $skip_name) {
if (substr_compare($values['data'], $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
continue 2;
}
}
}
if ($values['type'] === 'inline' && !empty($inline_skiplist)) {
foreach ($inline_skiplist as $skip_string) {
if (stripos($values['data'], $skip_string) !== FALSE) {
continue 2;
}
}
}
// Get advagg_mod info if not set; skip external files.
if (!isset($files_info[$name]['advagg_mod']) && $values['type'] !== 'external') {
$files_info[$name]['advagg_mod'] = advagg_mod_js_contains_jquery_drupal($values['data'], $values['type']);
}
// See what needs to be included.
if (!empty($files_info[$name]['advagg_mod']['contents']['drupal'])) {
$include_jquery = TRUE;
$include_drupal = TRUE;
break;
}
elseif (!empty($files_info[$name]['advagg_mod']['contents']['jquery'])) {
$include_jquery = TRUE;
}
elseif (isset($values['requires_jquery']) && !empty($values['requires_jquery'])) {
$include_jquery = TRUE;
}
}
unset($values);
// Kill only drupal JavaScript.
if (!$include_drupal) {
unset($js['settings']);
foreach ($js as $name => &$values) {
$drupal = 'drupal.js';
if (substr_compare($name, $drupal, -strlen($drupal), strlen($drupal)) === 0) {
unset($js[$name]);
}
}
unset($values);
// Kill all default JavaScript.
if (!$include_jquery) {
foreach ($js as $name => &$values) {
if ($values['type'] === 'file' || $values['type'] === 'external') {
foreach ($files_skiplist as $skip_name) {
if (substr_compare($name, $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
unset($js[$name]);
}
}
}
elseif ($values['type'] === 'inline') {
foreach ($inline_skiplist as $skip_string) {
if (stripos($values['data'], $skip_string) !== FALSE) {
unset($js[$name]);
}
}
}
}
unset($values);
}
}
}