Same name in other branches
  1. 5.0.x advagg.module \advagg_js_alter()
  2. 6.0.x advagg.module \advagg_js_alter()
  3. 7.x-2.x advagg.module \advagg_js_alter()
  4. 8.x-3.x advagg.module \advagg_js_alter()
  5. 8.x-4.x advagg.module \advagg_js_alter()

Implements hook_js_alter().

File

./advagg.module, line 118

Code

function advagg_js_alter(&$js) {
    // Skip if advagg is disabled.
    if (!advagg_enabled()) {
        return;
    }
    // Add DNS information for some of the more popular modules.
    foreach ($js as &$value) {
        if (!is_string($value['data'])) {
            continue;
        }
        // Google Ad Manager.
        if (strpos($value['data'], '/google_service.') !== FALSE) {
            if (!empty($value['dns_prefetch']) && is_string($value['dns_prefetch'])) {
                $temp = $value['dns_prefetch'];
                unset($value['dns_prefetch']);
                $value['dns_prefetch'] = [
                    $temp,
                ];
            }
            // Domains in the google_service.js file.
            $value['dns_prefetch'][] = 'https://csi.gstatic.com';
            $value['dns_prefetch'][] = 'https://pubads.g.doubleclick.net';
            $value['dns_prefetch'][] = 'https://partner.googleadservices.com';
            $value['dns_prefetch'][] = 'https://securepubads.g.doubleclick.net';
            // Domains in the google_ads.js file.
            $value['dns_prefetch'][] = 'https://pagead2.googlesyndication.com';
            // Other domains that usually get hit.
            $value['dns_prefetch'][] = 'https://cm.g.doubleclick.net';
            $value['dns_prefetch'][] = 'https://tpc.googlesyndication.com';
        }
        // Google Analytics.
        if (strpos($value['data'], 'GoogleAnalyticsObject') !== FALSE || strpos($value['data'], '.google-analytics.com/ga.js') !== FALSE) {
            if (!empty($value['dns_prefetch']) && is_string($value['dns_prefetch'])) {
                $temp = $value['dns_prefetch'];
                unset($value['dns_prefetch']);
                $value['dns_prefetch'] = [
                    $temp,
                ];
            }
            if (strpos($value['data'], '.google-analytics.com/ga.js') !== FALSE) {
                $value['dns_prefetch'][] = 'https://ssl.google-analytics.com';
            }
            $value['dns_prefetch'][] = 'https://stats.g.doubleclick.net';
        }
    }
    unset($value);
    // Fix type if it was incorrectly set.
    if (\Drupal::config('advagg.settings')->get('js_fix_type')) {
        // Get hostname and base path.
        $mod_base_url = substr($GLOBALS['base_root'] . $GLOBALS['base_path'], strpos($GLOBALS['base_root'] . $GLOBALS['base_path'], '//') + 2);
        $mod_base_url_len = strlen($mod_base_url);
        foreach ($js as &$value) {
            // Skip if the data is empty or not a string.
            if (empty($value['data']) || !is_string($value['data'])) {
                continue;
            }
            // Default to file if not file or external.
            if ($value['type'] !== 'file' && $value['type'] !== 'external') {
                if ($value['type'] === 'settings') {
                    $value['type'] = 'setting';
                }
                else {
                    $value['type'] = 'file';
                }
            }
            // If type is external but doesn't start with http, https, or // change it
            // to file.
            if ($value['type'] === 'external' && stripos($value['data'], 'http://') !== 0 && stripos($value['data'], 'https://') !== 0 && stripos($value['data'], '//') !== 0) {
                $value['type'] = 'file';
            }
            // If type is file but it starts with http, https, or // change it to
            // external.
            if ($value['type'] === 'file' && (stripos($value['data'], 'http://') === 0 || stripos($value['data'], 'https://') === 0 || stripos($value['data'], '//') === 0 && stripos($value['data'], '///') === FALSE)) {
                $value['type'] = 'external';
            }
            // If type is external and starts with http, https, or // but points to
            // this host change to file, but move it to the top of the aggregation
            // stack as long as js_preserve_external is not set.
            if (\Drupal::config('advagg.settings')->get('js_preserve_external') === FALSE) {
                if ($value['type'] === 'external' && stripos($value['data'], $mod_base_url) !== FALSE && (stripos($value['data'], 'http://') === 0 || stripos($value['data'], 'https://') === 0 || stripos($value['data'], '//') === 0)) {
                    $value['type'] = 'file';
                    $value['group'] = JS_LIBRARY;
                    $value['every_page'] = TRUE;
                    $value['weight'] = -40000;
                    $value['data'] = substr($value['data'], stripos($value['data'], $mod_base_url) + $mod_base_url_len);
                }
            }
        }
        unset($value);
    }
}