You are here

function advagg_relocate_css_post_alter in Advanced CSS/JS Aggregation 7.2

Alter the css array.

Parameters

array $css: CSS array.

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

Related topics

2 calls to advagg_relocate_css_post_alter()
advagg_relocate_cron in advagg_relocate/advagg_relocate.module
Implements hook_cron().
advagg_relocate_load_stylesheet_external in advagg_relocate/advagg_relocate.module
Convert external @import statements to be local.

File

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

Code

function advagg_relocate_css_post_alter(array &$css, $force_check = FALSE) {
  if (!module_exists('advagg') || !advagg_enabled()) {
    return;
  }
  $aggregate_settings = advagg_current_hooks_hash_array();

  // Check external css setting.
  if (empty($aggregate_settings['variables']['advagg_relocate_css'])) {
    return;
  }

  // Get all external css files.
  $urls = _advagg_relocate_get_urls($css, 'css', $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, 'css', array(), $force_check);

  // Get s3fs no_rewrite_cssjs setting.
  $s3fs_no_rewrite_cssjs = advagg_get_s3fs_config('no_rewrite_cssjs');
  $filenames = array();
  $advagg_relocate_css_ttl = variable_get('advagg_relocate_css_ttl', ADVAGG_RELOCATE_CSS_TTL);
  foreach ($responses as $value) {
    if (!empty($advagg_relocate_css_ttl) && $value->ttl >= $advagg_relocate_css_ttl) {
      continue;
    }
    $rehash = FALSE;

    // Handle @import statements.
    if (strpos($value->data, '@import') !== FALSE) {

      // Handle "local" import statements.
      advagg_relocate_load_stylesheet_local(array(), dirname($value->url) . '/');
      $value->data = preg_replace_callback('%@import\\s*+(?:url\\(\\s*+)?+[\'"]?+(?![a-z]++:|/)([^\'"()\\s]++)[\'"]?+\\s*+\\)?+\\s*+;%i', 'advagg_relocate_load_stylesheet_local', $value->data);

      // Replace external import statements with the contents of them.
      $value->data = preg_replace_callback('%@import\\s*+(?:url\\(\\s*+)?+[\'"]?+((?:http:\\/\\/|https:\\/\\/|\\/\\/)(?:[^\'"()\\s]++))[\'"]?+\\s*+\\)?+\\s*+;%i', 'advagg_relocate_load_stylesheet_external', $value->data);
      $rehash = TRUE;
    }

    // Fix external url references.
    if (strpos($value->data, 'url(') !== FALSE) {
      module_load_include('inc', 'advagg', 'advagg');

      // Set anchor point for local url() statements in the css.
      _advagg_build_css_path(array(), dirname($value->url) . '/');

      // Anchor all paths in the CSS with its base URL, ignoring external,
      // absolute paths, and urls that start with # or %23 (SVG).
      $value->data = preg_replace_callback('%url\\(\\s*+[\'"]?+(?![a-z]++:|/|\\#|\\%23+)([^\'"\\)]++)[\'"]?+\\s*+\\)%i', '_advagg_build_css_path', $value->data);
      $rehash = TRUE;
    }
    if ($rehash) {
      $value->hash = drupal_hash_base64($value->data);
    }

    // Save remote data.
    list($full_filename, $errors, $saved) = _advagg_relocate_save_remote_asset($value->options['filename'], $value->data, $value->local_cache, $value->hash);
    if (!empty($errors)) {
      continue;
    }

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

    // Handle domain prefectch.
    $key_host = parse_url($key, PHP_URL_HOST);
    if (!empty($css[$relative_path]['dns_prefetch'])) {
      foreach ($css[$relative_path]['dns_prefetch'] as $key => $domain_name) {
        if (strpos($domain_name, $key_host) !== FALSE && strpos($value->data, $key_host)) {
          unset($css[$relative_path]['dns_prefetch'][$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);
  }
}