You are here

function advagg_mod_find_inline_domains in Advanced CSS/JS Aggregation 7.2

Add dns_prefetch for inline js domains.

Parameters

array $js: JS array.

1 call to advagg_mod_find_inline_domains()
advagg_mod_js_pre_alter in advagg_mod/advagg_mod.module
Alter the js array.

File

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

Code

function advagg_mod_find_inline_domains(array &$js) {
  $parsed = @parse_url($GLOBALS['base_root']);
  $host = $parsed['host'];
  foreach ($js as &$values) {
    if ($values['type'] !== 'inline') {
      continue;
    }

    // Find quoted strings in JS.
    $matches = array();
    $pattern = "/[\"'](.*?)[\"']/";
    $matched = preg_match_all($pattern, $values['data'], $matches);
    if (!$matched) {
      continue;
    }

    // Find domains in the quoted strings and dns_prefetch it.
    foreach ($matches[1] as $value) {
      if (strpos($value, '//') !== FALSE) {
        $parsed = @parse_url($value);
        if (!empty($parsed['host']) && $host !== $parsed['host']) {
          $values['dns_prefetch'][] = $value;
        }
      }
    }
  }
}