You are here

function advagg_mod_js_move_to_footer in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 8.2 advagg_mod/advagg_mod.module \advagg_mod_js_move_to_footer()

Move JS to the footer.

Parameters

array $js: JS array.

2 calls to advagg_mod_js_move_to_footer()
advagg_get_full_js in ./advagg.module
Get full JS array.
advagg_mod_js_post_alter in advagg_mod/advagg_mod.module
Alter the js array.
1 string reference to 'advagg_mod_js_move_to_footer'
advagg_get_full_js in ./advagg.module
Get full JS array.

File

advagg_mod/advagg_mod.module, line 2241
Advanced aggregation modifier module.

Code

function advagg_mod_js_move_to_footer(array &$js) {

  // Move all JS to the footer.
  list($header_file_list, $header_inline_list, , , , , $all_in_footer_list, $move_js_to_footer) = advagg_mod_get_lists($js);
  if (empty($move_js_to_footer)) {
    return;
  }

  // Process all in footer list.
  if ($move_js_to_footer == 3 && !empty($all_in_footer_list)) {
    foreach ($all_in_footer_list as $key => $search_strings) {
      if (isset($js[$key])) {
        foreach ($js as $name => &$values) {
          foreach ($search_strings as $string) {
            if (!empty($string) && strpos($name, (string) $string) !== FALSE) {
              $values['scope_lock'] = TRUE;
              break;
            }
            if (is_string($values['data']) && !empty($string) && strpos($values['data'], (string) $string) !== FALSE) {
              $values['scope_lock'] = TRUE;
              break;
            }
            if (!empty($values['pre_relocate_data']) && is_string($values['pre_relocate_data']) && !empty($string) && strpos($values['pre_relocate_data'], (string) $string) !== FALSE) {
              $values['scope_lock'] = TRUE;
              break;
            }
          }
        }
      }
    }
  }
  foreach ($js as $key => &$values) {

    // If scope is not set, this js is not getting used. remove it.
    if (!isset($values['scope'])) {
      unset($js[$key]);
      continue;
    }
    if (strpos($values['scope'], ':') !== FALSE) {
      continue;
    }

    // Skip if a library and configured to do so.
    if ($move_js_to_footer == 1 && isset($values['group']) && $values['group'] <= JS_LIBRARY) {
      continue;
    }

    // Skip if the scope has been locked.
    if (!empty($values['scope_lock'])) {
      continue;
    }

    // Allow certain scripts to be kept in the header.
    if ($values['type'] !== 'inline' && $values['type'] !== 'setting') {
      foreach ($header_file_list as $search_string) {
        if (stripos($values['data'], $search_string) !== FALSE) {
          continue 2;
        }
        if (!empty($values['pre_relocate_data']) && stripos($values['pre_relocate_data'], $search_string) !== FALSE) {
          continue 2;
        }
      }
    }

    // Allow certain inline scripts to be kept in the header.
    if ($values['type'] === 'inline') {
      foreach ($header_inline_list as $search_string) {
        if (strpos($values['data'], $search_string) !== FALSE) {
          continue 2;
        }
      }
    }

    // If group is not set, make it JS_DEFAULT (0).
    if (!isset($values['group'])) {
      $values['group'] = JS_DEFAULT;
    }

    // If weight is not set, make it 0.
    if (!isset($values['weight'])) {
      $values['weight'] = 0;
    }

    // If every_page is not set, make it FALSE.
    if (!isset($values['every_page'])) {
      $values['every_page'] = FALSE;
    }

    // If JS is not in the header increase group by 10000.
    if ($values['scope'] !== 'header' && is_numeric($values['group'])) {
      $values['group'] += 10000;
    }

    // If JS is already in the footer increase group by 10000.
    if ($values['scope'] === 'footer' && is_numeric($values['group'])) {
      $values['group'] += 10000;
    }
    $values['scope'] = 'footer';
  }
  unset($values);
}