You are here

function advagg_build_aggregate_plans in Advanced CSS/JS Aggregation 7.2

Replacement for drupal_build_css_cache() and drupal_build_js_cache().

Parameters

array $files_to_aggregate: An array of CSS/JS groups.

string $type: String; css or js.

Return value

array array of aggregate files.

2 calls to advagg_build_aggregate_plans()
_advagg_aggregate_css in ./advagg.module
Default callback to aggregate CSS files and inline content.
_advagg_aggregate_js in ./advagg.module
Default callback to aggregate JavaScript files.

File

./advagg.inc, line 1420
Advanced CSS/JS aggregation module.

Code

function advagg_build_aggregate_plans(array $files_to_aggregate, $type) {
  if ($type !== 'css' && $type !== 'js') {
    return array();
  }

  // Place into biggest grouping possible.
  $groups = advagg_generate_groups($files_to_aggregate, $type);

  // Get filenames.
  $files = advagg_generate_filenames($groups, $type);

  // Insert/Update Database.
  advagg_insert_update_db($files, $type, 1);

  // Update atimes for root.
  advagg_multi_update_atime($files);

  // Run hooks to modify the aggregate.
  // Call hook_advagg_build_aggregate_plans_alter().
  $modified = FALSE;
  drupal_alter('advagg_build_aggregate_plans', $files, $modified, $type);

  // If the hook above modified anything, re-insert into database.
  if ($modified) {

    // Insert/Update Database.
    advagg_insert_update_db($files, $type, 0);

    // Update atimes for non root.
    advagg_multi_update_atime($files);
  }

  // Get file paths.
  list($css_path, $js_path) = advagg_get_root_files_dir();

  // Build the plan.
  $plans = array();
  foreach ($files as $agg_filename => $values) {
    if ($type === 'css') {
      $mixed_media = FALSE;
      $media = NULL;
      foreach ($values['files'] as $value) {
        if (!isset($value['media'])) {
          continue;
        }
        if (is_null($media)) {
          $media = $value['media'];
        }
        if ($media != $value['media']) {
          $mixed_media = TRUE;
        }
      }
    }
    $onload = array();
    $onerror = array();
    $attributes = array();
    $onloadcss = array();
    foreach ($values['files'] as &$items) {

      // Get onload.
      if (!empty($items['onload'])) {
        $onload[] = $items['onload'];
      }

      // Get attributes onload.
      if (!empty($items['attributes']['onload'])) {
        $onload[] = $items['attributes']['onload'];
        unset($items['attributes']['onload']);
      }

      // Get onerror.
      if (!empty($items['onerror'])) {
        $onload[] = $items['onerror'];
      }

      // Get attributes onerror.
      if (!empty($items['attributes']['onerror'])) {
        $onload[] = $items['attributes']['onerror'];
        unset($items['attributes']['onerror']);
      }

      // Get attributes onloadCSS.
      if (!empty($items['onloadCSS'])) {
        $onloadcss[] = $items['onloadCSS'];
      }

      // Get attributes onloadCSS.
      if (!empty($items['attributes']['onloadCSS'])) {
        $onloadcss[] = $items['attributes']['onloadCSS'];
        unset($items['attributes']['onloadCSS']);
      }

      // Get attributes.
      if (!empty($items['attributes'])) {
        $attributes += $items['attributes'];
      }
    }
    $onload = implode(';', array_unique(array_filter($onload)));
    $onerror = implode(';', array_unique(array_filter($onerror)));
    $onloadcss = implode(';', array_unique(array_filter($onloadcss)));
    $first = reset($values['files']);
    if (!empty($mixed_media)) {
      $first['media'] = 'all';
    }
    $url = $type === 'css' ? $css_path[0] : $js_path[0];
    $path = $type === 'css' ? $css_path[1] : $js_path[1];
    $plans[$agg_filename] = array(
      'data' => $url . '/' . $agg_filename,
      'media' => isset($first['media']) ? $first['media'] : '',
      'defer' => isset($first['defer']) ? $first['defer'] : '',
      'async' => isset($first['async']) ? $first['async'] : '',
      'onload' => $onload,
      'onerror' => $onerror,
      'browsers' => isset($first['browsers']) ? $first['browsers'] : array(),
      'cache' => isset($first['cache']) ? $first['cache'] : TRUE,
      'type' => $first['type'],
      'items' => $values,
      'filepath' => $path . '/' . $agg_filename,
      'filename' => $agg_filename,
      'attributes' => $attributes,
    );
    if (!empty($onloadcss)) {
      $plans[$agg_filename]['attributes']['onloadCSS'] = $onloadcss;
    }
  }
  $plans = array_values($plans);

  // Create the aggregate files.
  if (variable_get('advagg_pregenerate_aggregate_files', ADVAGG_PREGENERATE_AGGREGATE_FILES)) {
    advagg_create_aggregate_files($plans, $type);
  }

  // Run hooks to modify the plans.
  // Call hook_advagg_build_aggregate_plans_post_alter().
  drupal_alter('advagg_build_aggregate_plans_post', $plans, $type);
  return $plans;
}