You are here

function advagg_js_compress_advagg_js_inline_alter in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_advagg_js_inline_alter()

Implement hook_advagg_js_inline_alter.

File

advagg_js_compress/advagg_js_compress.module, line 144
Advanced CSS/JS aggregation js compression module.

Code

function advagg_js_compress_advagg_js_inline_alter(&$contents) {
  if (!variable_get('advagg_js_compress_inline', ADVAGG_JS_COMPRESS_INLINE)) {
    return;
  }
  $compressor = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);

  // If using a cache, try to get the contents of it.
  if (variable_get('advagg_js_compress_inline_cache', ADVAGG_JS_COMPRESS_INLINE_CACHE)) {
    $key = md5($contents) . $compressor;
    $table = 'cache_advagg_js_compress_inline';
    $data = cache_get($key, $table);
    if (!empty($data->data)) {
      $contents = $data->data;
      return;
    }
  }
  if ($compressor == 0) {
    $original_contents = $contents;
    list($before, $after) = advagg_js_compress_jsminplus($contents);
    $ratio = 0;
    if ($before != 0) {
      $ratio = ($before - $after) / $before;
    }

    // Make sure the returned string is not empty or has a VERY high
    // compression ratio.
    if (empty($contents) || empty($ratio) || $ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
      $contents = $original_contents;
    }
  }
  if ($compressor == 1) {
    $contents = jsmin($contents);

    // Ensure that $contents ends with ; or }.
    if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {

      // ; or } not found, add in ; to the end of $contents.
      $contents = trim($contents) . ';';
    }
  }

  // If using a cache set it.
  if (isset($key)) {
    cache_set($key, $contents, $table, CACHE_TEMPORARY);
  }
}