You are here

function advagg_relocate_js_post_alter in Advanced CSS/JS Aggregation 7.2

Alter the js array.

Parameters

array $js: JS array.

bool $force_check: TRUE if you want to force check the external source.

Related topics

2 calls to advagg_relocate_js_post_alter()
advagg_relocate_advagg_relocate_process_http_request_alter in advagg_relocate/advagg_relocate.advagg.inc
Implements hook_advagg_relocate_process_http_request_alter().
advagg_relocate_cron in advagg_relocate/advagg_relocate.module
Implements hook_cron().

File

advagg_relocate/advagg_relocate.module, line 963
Advanced aggregation relocate module.

Code

function advagg_relocate_js_post_alter(array &$js, $force_check = FALSE) {
  if (!module_exists('advagg') || !advagg_enabled()) {
    return;
  }

  // Check external js setting.
  $aggregate_settings = advagg_current_hooks_hash_array();
  if (empty($aggregate_settings['variables']['advagg_relocate_js'])) {
    return;
  }

  // Look for inline scrtips that add external js via inline code.
  $scripts_found = array();
  module_load_include('inc', 'advagg', 'advagg');
  $temp_inserts = array();
  foreach ($js as $key => $value) {
    if ($value['type'] === 'inline') {
      $scripts_found += advagg_relocate_js_script_rewrite_list($key, $value, $aggregate_settings);
    }
    if ($value['type'] === 'file') {
      $info = advagg_get_info_on_file($key);
      if (!empty($info['advagg_relocate'])) {

        // Get the file contents.
        $file_contents = (string) @advagg_file_get_contents($info['data']);
        if (empty($file_contents)) {
          continue;
        }
        $value['data'] = $file_contents;
        $temp_inserts[$key] = array(
          "advagg_relocate:{$key}" => $value,
        );
        $scripts_found += advagg_relocate_js_script_rewrite_list("advagg_relocate:{$key}", $value, $aggregate_settings);
      }
    }
  }

  // Add in file as inline so replacement works.
  if (!empty($temp_inserts)) {
    foreach ($temp_inserts as $key => $value) {
      $js = advagg_insert_into_array_at_key($js, $value, $key);
    }
  }

  // Rewrite inline scrtips and add external references to js array.
  advagg_relocate_js_script_rewrite($js, $scripts_found);

  // Remove temp inline code.
  if (!empty($temp_inserts)) {
    foreach ($temp_inserts as $value) {
      reset($value);
      $key = key($value);
      unset($js[$key]);
    }
  }

  // Get all external js files.
  $urls = _advagg_relocate_get_urls($js, 'js', $aggregate_settings);
  if (empty($urls)) {
    return;
  }

  // Make advagg_save_data() available.
  module_load_include('inc', 'advagg', 'advagg.missing');
  module_load_include('advagg.inc', 'advagg_relocate');
  $responses = advagg_relocate_get_remote_data($urls, 'js', array(), $force_check);

  // Get s3fs no_rewrite_cssjs setting.
  $s3fs_no_rewrite_cssjs = advagg_get_s3fs_config('no_rewrite_cssjs');
  $filenames = array();
  $advagg_relocate_js_ttl = variable_get('advagg_relocate_js_ttl', ADVAGG_RELOCATE_JS_TTL);
  foreach ($responses as $value) {

    // If the external file has a longer TTL than 1 week, do not cache.
    if (!empty($advagg_relocate_js_ttl) && $value->ttl >= $advagg_relocate_js_ttl) {
      continue;
    }
    list($full_filename, $errors, $saved) = _advagg_relocate_save_remote_asset($value->options['filename'], $value->data, $value->local_cache, $value->hash);
    if (!empty($errors)) {
      watchdog('advagg-relocate', 'Write failed. Errors: <pre><tt>@errors</tt></pre>', array(
        '@errors' => $errors,
      ));
      continue;
    }

    // Replace remote data with local data.
    $relative_path = advagg_get_relative_path($full_filename);
    $key = $urls[$value->options['filename']];
    $js = advagg_relocate_key_rename($js, array(
      $relative_path => $key,
    ));
    $js[$relative_path]['pre_relocate_data'] = $js[$relative_path]['data'];
    $js[$relative_path]['data'] = $relative_path;
    $js[$relative_path]['type'] = 'file';
    if (defined('ADVAGG_MOD_JS_PREPROCESS') && variable_get('advagg_mod_js_preprocess', ADVAGG_MOD_JS_PREPROCESS) && empty($js[$relative_path]['preprocess_lock'])) {
      $js[$relative_path]['preprocess'] = TRUE;
    }

    // Check for any .write( statements in the JS code.
    if (strpos($value->data, '.write(') !== FALSE) {
      if (!isset($js[$relative_path]['noasync']) || $js[$relative_path]['noasync'] !== FALSE || (!isset($js[$relative_path]['nodefer']) || $js[$relative_path]['nodefer'] !== FALSE)) {
        $js[$relative_path]['async'] = FALSE;
        $js[$relative_path]['defer'] = FALSE;
        $js[$relative_path]['noasync'] = TRUE;
        $js[$relative_path]['nodefer'] = TRUE;
      }
    }

    // Handle domain prefectch.
    $parse = @parse_url($key);
    $key_host = '';
    if (!empty($parse['host'])) {
      $key_host = $parse['host'];
    }
    if (!empty($key_host) && !empty($js[$relative_path]['dns_prefetch'])) {
      foreach ($js[$relative_path]['dns_prefetch'] as $key => $domain_name) {
        if (strpos($domain_name, $key_host) !== FALSE && strpos($value->data, $key_host)) {
          unset($js[$relative_path]['dns_prefetch'][$key]);
        }
      }
    }

    // Make sure the external reference has been removed.
    $schemes = array(
      '//',
      'http',
      'https',
    );
    foreach ($schemes as $scheme) {
      $parse['scheme'] = $scheme;
      $key = advagg_glue_url($parse);
      if (isset($js[$key])) {
        unset($js[$key]);
      }
    }

    // List of files that need the cache cleared.
    if ($saved) {
      $filenames[] = !is_null($s3fs_no_rewrite_cssjs) && empty($s3fs_no_rewrite_cssjs) ? $full_filename : $relative_path;
    }
  }
  if (!empty($filenames)) {
    module_load_include('inc', 'advagg');
    module_load_include('cache.inc', 'advagg');
    $files = advagg_get_info_on_files($filenames, TRUE);
    advagg_push_new_changes($files);
  }
}