You are here

function advagg_processor in Advanced CSS/JS Aggregation 6

Process variables for page.tpl.php

Parameters

$variables: The existing theme data structure.

2 string references to 'advagg_processor'
advagg_requirements in ./advagg.install
Implementation of hook_requirements().
advagg_theme_registry_alter in ./advagg.module
Implementation of hook_theme_registry_alter().

File

./advagg.module, line 698
Advanced CSS/JS aggregation module

Code

function advagg_processor(&$variables) {
  global $_advagg, $conf;

  // Invoke hook_advagg_disable_processor
  $disabled = module_invoke_all('advagg_disable_processor');

  // If disabled, skip
  if (!variable_get('advagg_enabled', ADVAGG_ENABLED) || in_array(TRUE, $disabled, TRUE)) {
    if (module_exists('jquery_update')) {
      return jquery_update_preprocess_page($variables);
    }
    else {
      return;
    }
  }

  // Do not use the cache if advagg is set in the URL.
  if (isset($_GET['advagg']) && user_access('bypass advanced aggregation')) {
    $conf['advagg_use_full_cache'] = FALSE;
  }

  // Do not use the cache if the disable cookie is set.
  $cookie_name = 'AdvAggDisabled';
  $key = md5(drupal_get_private_key());
  if (!empty($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == $key) {
    $conf['advagg_use_full_cache'] = FALSE;
  }

  // http or https.
  $schema = advagg_get_server_schema();

  // CSS
  $css_var = $variables['css'];
  $css_orig = $css_var;
  if (!variable_get('advagg_only_css_from_variables', ADVAGG_ONLY_CSS_FROM_VARIABLES)) {
    $css_func = drupal_add_css();
    advagg_css_array_fixer($css_func);
  }
  else {
    $css_func = array();
  }
  $css = advagg_merge_css($css_var, $css_func);
  $css_func_inline = advagg_add_css_inline();
  if (!empty($css_func_inline)) {
    $css = advagg_merge_inline_css($css, $css_func_inline);
  }
  $css_conditional_styles = !empty($variables['conditional_styles']) ? $variables['conditional_styles'] : '';
  $css_styles = $variables['styles'];
  advagg_color_css($css);

  // Try cache.
  if (variable_get('advagg_use_full_cache', ADVAGG_USE_FULL_CACHE)) {

    // Build the cache ID
    // md5 of the CSS input
    // http or https
    // hostname
    // the js rendering function
    // css/js query string
    $cid = 'advagg_processor:css:' . md5(serialize(array(
      $css,
      $css_conditional_styles,
    ))) . ':' . $schema . ':' . $_SERVER['HTTP_HOST'] . ':' . variable_get('advagg_css_render_function', ADVAGG_CSS_RENDER_FUNCTION) . ':' . substr(variable_get('css_js_query_string', '0'), 0, 1);
    $cache = cache_get($cid, 'cache_advagg_bundle_reuse');
  }
  elseif (isset($cid)) {
    unset($cid);
  }
  if (!empty($cache->data)) {
    $variables['styles'] = $cache->data;
  }
  else {

    // Build HTML code.
    $processed_css = advagg_process_css($css);
    if (!empty($processed_css)) {
      $variables['styles'] = $processed_css;
    }
    $variables['styles'] .= $css_conditional_styles;

    // Cache output.
    if (isset($cid) && variable_get('advagg_use_full_cache', ADVAGG_USE_FULL_CACHE) && lock_acquire($cid)) {
      cache_set($cid, $variables['styles'], 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
      lock_release($cid);
    }
  }

  // JS
  $js_code = array();
  $js_code['header'] = drupal_add_js(NULL, NULL, 'header');
  if (variable_get('advagg_closure', ADVAGG_CLOSURE) && !empty($_advagg['closure'])) {
    $js_code['footer'] = drupal_add_js(NULL, NULL, 'footer');
  }
  $skip_keys = variable_get('advagg_region_skip_keys', array(
    'styles',
    'scripts',
    'zebra',
    'id',
    'directory',
    'layout',
    'head_title',
    'base_path',
    'front_page',
    'head',
    'body_classes',
    'header',
    'footer',
    'closure',
  ));
  foreach ($variables as $key => $value) {
    if (!in_array($key, $skip_keys) && is_string($value) && !empty($value) && !isset($js_code[$key])) {
      $js_code[$key] = drupal_add_js(NULL, NULL, $key);
    }
  }
  $js_code_orig = $js_code;

  // Try cache.
  if (variable_get('advagg_use_full_cache', ADVAGG_USE_FULL_CACHE)) {

    // Build the cache ID
    // md5 of the JS input
    // http or https
    // hostname
    // the js rendering function
    // css/js query string
    $cid = 'advagg_processor:js:' . md5(serialize($js_code)) . ':' . $schema . ':' . $_SERVER['HTTP_HOST'] . ':' . variable_get('advagg_js_render_function', ADVAGG_JS_RENDER_FUNCTION) . ':' . substr(variable_get('css_js_query_string', '0'), 0, 1);
    $cache = cache_get($cid, 'cache_advagg_bundle_reuse');
  }
  elseif (isset($cid)) {
    unset($cid);
  }
  if (!empty($cache->data)) {
    $js_code = $cache->data;
  }
  else {

    // Build HTML code.
    advagg_jquery_updater($js_code['header']);
    $js_code = advagg_process_js($js_code);

    // Cache array.
    if (isset($cid) && variable_get('advagg_use_full_cache', ADVAGG_USE_FULL_CACHE) && lock_acquire($cid)) {
      cache_set($cid, $js_code, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
      lock_release($cid);
    }
  }

  // Place JS in the correct places.
  foreach ($js_code as $key => $value) {
    if ($key == 'header') {
      $variables['scripts'] = $value;
    }
    elseif ($key == 'footer' && variable_get('advagg_closure', ADVAGG_CLOSURE) && !empty($_advagg['closure'])) {
      $variables['closure'] .= $value;
    }
    else {
      $variables[$key] .= $value;
    }
  }

  // Send requests to server if async enabled.
  advagg_async_send_http_request();

  // Write debug info to watchdog if debugging enabled.
  if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
    $data = array(
      'css_before_vars' => $css_orig,
      'css_before_function' => $css_func,
      'css_before_styles' => $css_styles,
      'css_before_inline' => $css_func_inline,
      'css_before_conditional_styles' => $css_conditional_styles,
      'css_merged' => $css,
      'css_after' => $processed_css,
      'js_before' => $js_code_orig,
      'js_after' => $js_code,
    );
    $data['runtime'] = isset($_advagg['debug']) ? $_advagg['debug'] : FALSE;
    $data = str_replace('    ', '    ', nl2br(htmlentities(iconv('utf-8', 'utf-8//IGNORE', print_r($data, TRUE)), ENT_QUOTES, 'UTF-8')));
    watchdog('advagg', 'Debug info: !data', array(
      '!data' => $data,
    ), WATCHDOG_DEBUG);
  }
}