You are here

function advagg_advagg_get_info_on_files_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_advagg_get_info_on_files_alter().

Used to make sure the info is up to date in the cache.

Related topics

File

./advagg.advagg.inc, line 271
Advanced CSS/JS aggregation module.

Code

function advagg_advagg_get_info_on_files_alter(&$return, $cached_data, $bypass_cache) {

  // Check for the ie_css_selector_limiter.
  if (variable_get('advagg_ie_css_selector_limiter', ADVAGG_IE_CSS_SELECTOR_LIMITER)) {
    $limit_value = variable_get('advagg_ie_css_selector_limiter_value', ADVAGG_IE_CSS_SELECTOR_LIMITER_VALUE);

    // Get the css path.
    list($css_path) = advagg_get_root_files_dir();
    $css_parts_path = advagg_s3fs_evaluate_no_rewrite_cssjs(FALSE) ? $css_path[0] : $css_path[1];
    $parts_path = $css_parts_path . '/parts/';
    foreach ($return as &$info) {

      // Skip if not a css file.
      if (empty($info['fileext']) || $info['fileext'] !== 'css') {
        continue;
      }

      // Check if this is a split css file.
      if (strpos($info['data'], $parts_path) !== FALSE) {
        $info['split'] = TRUE;
      }
      elseif ($info['linecount'] > $limit_value) {
        advagg_split_css_file($info);
      }
    }
    unset($info);
  }

  // Capture resource_hints.
  if (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH) || variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT) || variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
    $aggregate_settings = advagg_current_hooks_hash_array();
    foreach ($return as &$info) {

      // Skip if not a css file.
      if (empty($info['fileext']) || $info['fileext'] !== 'css') {
        continue;
      }

      // Get the file contents.
      $file_contents = (string) @advagg_file_get_contents($info['data']);
      $file_contents = advagg_load_css_stylesheet($info['data'], FALSE, $aggregate_settings, $file_contents);

      // Get domain names and external assets in this css file.
      $hosts = array();
      $urls = array();
      $matches = array();
      $pattern = '%url\\(\\s*+[\'"]?+(http:\\/\\/|https:\\/\\/|\\/\\/)([^\'"()\\s]++)[\'"]?+\\s*+\\)%i';
      preg_match_all($pattern, $file_contents, $matches);
      if (!empty($matches[1])) {
        foreach ($matches[1] as $key => $match) {
          $url = $match . $matches[2][$key];
          $parse = @parse_url($url);
          if (!empty($parse['host'])) {
            $extra = '';
            $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
            $supported = array(
              'eot',
              'woff2',
              'woff',
              'ttf',
              'otf',
            );
            if (in_array($ext, $supported)) {
              $extra .= '#crossorigin';
            }
            $hosts[$parse['host'] . $extra] = $match . $parse['host'] . '/' . $extra;
            $urls[$url] = $url;
          }
        }
      }
      if (!empty($hosts) && (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH) || variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT))) {
        $info['dns_prefetch'] = array_values($hosts);
      }

      // Get local files to preload in this css file.
      if (variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
        $matches = array();
        $pattern = '/url\\(\\s*+[\'"]?(.*?)[\'"\\s]?+\\)/i';
        preg_match_all($pattern, $file_contents, $matches);
        if (!empty($matches[1])) {
          foreach ($matches[1] as $key => $match) {
            $url = advagg_convert_abs_to_rel($match);
            $parse = @parse_url($url);
            if (empty($parse['host'])) {
              $urls[$url] = $url;
            }
          }
        }
        if (!empty($urls)) {
          $info['preload'] = array_values($urls);
        }
      }
    }
    unset($info);
  }
}