You are here

function css_emimage_advagg_css_groups_alter in CSS Embedded Images 7

Implements hook_advagg_css_groups_alter().

Process inline CSS.

File

./css_emimage.module, line 198
CSS Embedded Images module.

Code

function css_emimage_advagg_css_groups_alter(&$css_groups, $preprocess_css) {
  if (!$preprocess_css) {
    return;
  }
  if (!variable_get('css_emimage_inline', CSS_EMIMAGE_INLINE)) {
    return;
  }
  $force_inline = variable_get('css_emimage_force_inline', 0);
  $GLOBALS['conf']['css_emimage_force_inline'] = TRUE;
  module_load_include('inc', 'css_emimage', 'css_emimage.advagg');
  $new_css = array();
  foreach ($css_groups as $key => $group) {

    // Only run on inline css.
    if ($group['type'] != 'inline') {
      $new_css[$key] = $group;
      continue;
    }

    // Only run if css is not browser specific.
    if (!empty($fileinfo['browsers']) && ($fileinfo['browsers']['!IE'] !== TRUE || $fileinfo['browsers']['IE'] !== TRUE)) {
      $new_css[$key] = $group;
      continue;
    }
    $group_has_images = FALSE;
    foreach ($group['items'] as $k => $value) {
      $images = css_emimage_string_contains_images($value['data']);

      // If css has images recored it and break out of loop.
      if (!empty($images)) {
        $group_has_images = TRUE;
        break;
      }
    }

    // If this group has no images, skip.
    if (!$group_has_images) {
      $new_css[$key] = $group;
      continue;
    }

    // Add in the css embedded images.
    $new_group = array();
    $new_group = $group;
    $new_group['browsers'] = array(
      'IE' => 'gte IE 8',
    );
    foreach ($new_group['items'] as $k => $fileinfo) {

      // Change the browser.
      $new_group['items'][$k]['browsers'] = array(
        'IE' => 'gte IE 8',
      );

      // Inline the files.
      $contents = $fileinfo['data'];
      $hash = drupal_hash_base64($contents);
      _css_emimage_text_processor($contents, $hash, 'emimage');
      $new_group['items'][$k]['data'] = $contents;
    }
    $key = (string) floatval($key) + 0.1;
    $new_css[(string) $key] = $new_group;

    // Add in the base css.
    $new_group = array();
    $new_group = $group;
    $new_group['browsers'] = array(
      'IE' => 'gte IE 8',
    );
    foreach ($new_group['items'] as $k => $fileinfo) {

      // Change the browser.
      $new_group['items'][$k]['browsers'] = array(
        'IE' => 'gte IE 8',
      );

      // Inline the files.
      $contents = $fileinfo['data'];
      $hash = drupal_hash_base64($contents);
      _css_emimage_text_processor($contents, $hash, 'base');
      if (!empty($contents)) {
        $new_group['items'][$k]['data'] = $contents;
      }
      else {
        unset($new_group['items'][$k]);
      }
    }
    if (!empty($new_group['items'])) {
      $key = (string) floatval($key) + 0.1;
      $new_css[(string) $key] = $new_group;
    }

    // Add in the IE fallback file (orginal file).
    $new_group = array();
    $new_group = $group;
    $new_group['browsers'] = array(
      '!IE' => FALSE,
      'IE' => 'lte IE 7',
    );
    foreach ($new_group['items'] as $k => $fileinfo) {
      $new_group['items'][$k]['browsers'] = array(
        '!IE' => FALSE,
        'IE' => 'lte IE 7',
      );
    }
    $key = (string) floatval($key) + 0.1;
    $new_css[(string) $key] = $new_group;
  }
  $new_css = array_values($new_css);
  $GLOBALS['conf']['css_emimage_force_inline'] = $force_inline;
  $css_groups = $new_css;
}