You are here

function advagg_js_compress_advagg_save_aggregate_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_advagg_save_aggregate_alter().

Used to add in a .gz file if none exits and use packer on non gzip file.

Related topics

File

advagg_js_compress/advagg_js_compress.advagg.inc, line 164

Code

function advagg_js_compress_advagg_save_aggregate_alter(&$files_to_save, $aggregate_settings, $other_parameters) {
  list($files, $type) = $other_parameters;

  // Return if gzip and brotli are disabled.
  // Return if packer is disabled.
  // Return if type is not js.
  if (empty($aggregate_settings['variables']['advagg_gzip']) && empty($aggregate_settings['variables']['advagg_brotli']) || empty($aggregate_settings['variables']['advagg_js_compress_packer']) || $type !== 'js') {
    return;
  }

  // Use the first file in the array.
  $data = reset($files_to_save);
  $uri = key($files_to_save);

  // Use packer on non gzip/brotli js files.
  $compressor = 2;
  module_load_include('inc', 'advagg', 'advagg');

  // Make sure all files in this aggregate are compatible with packer.
  foreach ($files as $file => $settings) {
    $info = advagg_get_info_on_file($file);
    if (!isset($info['advagg_js_compress'][$compressor]['code'])) {

      // Add in selected compressor.
      $compressors = advagg_js_compress_get_enabled_compressors(array(), $compressor);

      // Test file here on the spot.
      $info['advagg_js_compress'] = advagg_js_compress_run_test($file, $info, $compressors);
    }

    // If this file causes php to bomb or the ratio is way too good then do not
    // use packer on this aggregate.
    if (!isset($info['advagg_js_compress'][$compressor]['code']) || $info['advagg_js_compress'][$compressor]['code'] == -1 || $info['advagg_js_compress'][$compressor]['code'] == -3) {
      return;
    }
  }

  // Use packer on non gzip/brotli JS data.
  $aggregate_settings['variables']['advagg_js_compressor'] = $compressor;
  advagg_js_compress_prep($data, $uri, $aggregate_settings, FALSE);
  $files_to_save[$uri] = $data;
}