You are here

function advagg_relocate_advagg_get_css_aggregate_contents_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_advagg_get_css_aggregate_contents_alter().

Related topics

File

advagg_relocate/advagg_relocate.advagg.inc, line 70
Advanced aggregation relocate module.

Code

function advagg_relocate_advagg_get_css_aggregate_contents_alter(&$data, $files, $aggregate_settings) {

  // Set variables if needed.
  if (!isset($aggregate_settings['variables']['advagg_relocate_css_inline_import'])) {
    $aggregate_settings['variables']['advagg_relocate_css_inline_import'] = variable_get('advagg_relocate_css_inline_import', ADVAGG_RELOCATE_CSS_INLINE_IMPORT);
  }

  // Do nothing if this is disabled.
  if (empty($aggregate_settings['variables']['advagg_relocate_css_inline_import'])) {
    return;
  }
  if (strpos($data, '@import') !== FALSE) {

    // Set values that will be used when preg_replace_callback is ran.
    _advagg_relocate_callback(array(), $files, $aggregate_settings);

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

    // Per the W3C specification at
    // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import rules
    // must proceed any other style, so we move those to the top.
    $matches = array();
    $regexp = '/@import[^;]+;/i';
    preg_match_all($regexp, $data, $matches);
    $data = preg_replace($regexp, '', $data);

    // Add import statements to the top of the stylesheet.
    $data = implode("\n", $matches[0]) . $data;
  }
}