You are here

function advagg_css_alter in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 8.4 advagg.module \advagg_css_alter()
  2. 8.2 advagg.module \advagg_css_alter()
  3. 8.3 advagg.module \advagg_css_alter()

Implements hook_css_alter().

File

advagg_font/advagg_font.module, line 136
Advanced aggregation font module.

Code

function advagg_css_alter(&$css) {

  // Skip if advagg is disabled.
  if (!advagg_enabled()) {
    return;
  }

  // Skip if fontface is disabled.
  if (empty(variable_get('advagg_font_fontfaceobserver', ADVAGG_FONT_FONTFACEOBSERVER))) {
    return;
  }

  // Skip if fonts added to critical css is disabled.
  if (empty(variable_get('advagg_font_add_to_critical_css', ADVAGG_FONT_ADD_TO_CRITICAL_CSS))) {
    return;
  }
  $critical_css_key = NULL;
  foreach ($css as $key => $values) {
    if (!empty($values['critical-css']) && $values['type'] === 'inline') {
      $critical_css_key = $key;
    }
  }

  // Skip if no critical css.
  if (is_null($critical_css_key)) {
    return;
  }
  module_load_include('inc', 'advagg', 'advagg');
  $css_to_add = '';
  foreach ($css as $key => $values) {
    if ($values['type'] === 'file') {
      $info = advagg_get_info_on_file($key);
      if (!empty($info['advagg_font'])) {

        // Get the file contents.
        $file_contents = (string) @advagg_file_get_contents($info['data']);
        if (empty($file_contents)) {
          continue;
        }
        list($replacements) = advagg_font_get_replacements_array($file_contents);
        foreach ($replacements as $replace) {
          $css_to_add .= $replace[2];
        }
      }
    }
  }
  if (!empty($css_to_add)) {
    $css[$critical_css_key]['data'] .= "\n{$css_to_add}";
  }
}