You are here

function magic_css_js_alter in Magic 7.2

Same name and namespace in other branches
  1. 7 magic.module \magic_css_js_alter()

Helper function to remove unwanted css or js.

Parameters

array $data: The css or js array.

string $type: (Optional) Either 'css' or 'js' depending on the file array. Defaults to 'css'.

2 calls to magic_css_js_alter()
magic_css_alter in ./magic.module
Implements hook_css_alter().
magic_js_alter in ./magic.module
Implements hook_js_alter().

File

./magic.module, line 263
Keep Frontend DRY; sprinkle it with MAGIC!

Code

function magic_css_js_alter(&$data, $type = 'css') {

  // First check to see if we are even going to exclude anything.
  $setting = "magic_{$type}_excludes";
  if (!($excludes = theme_get_setting($setting))) {
    return;
  }

  // We get the hash of the css array to check if it has been passed already.
  // Note: we use md4 as it is nominally faster than other hashing methods, and
  // as we only need matches, not security, it works just fine.
  $hash = hash('md4', serialize($data));
  $cid = "{$GLOBALS['theme_key']}:{$type}:{$hash}";
  if ($cache = cache_get($cid, 'cache_magic')) {

    // We have this array cached, use it.
    $data = $cache->data;
    return;
  }
  module_load_include('inc', 'magic', 'includes/magic.assets');
  $regex_cid = "{$GLOBALS['theme_key']}:{$type}:runtime_excludes";
  if (!($cache = cache_get($regex_cid, 'cache_magic'))) {

    // Explode and trim the values for the exclusion rules.
    $items = array_filter(array_map('trim', explode("\n", $excludes)));
    $steps = magic_assets_regex_steps($items);
    cache_set($regex_cid, $steps, 'cache_magic', CACHE_TEMPORARY);
  }
  else {
    $steps = $cache->data;
  }
  magic_assets_exclude($data, $steps);
  cache_set($cid, $data, 'cache_magic', CACHE_TEMPORARY);
}