You are here

function _advagg_build_css_path in Advanced CSS/JS Aggregation 7.2

Prefixes all paths within a CSS file for drupal_build_css_cache().

Parameters

array $matches: Array of matched items from preg_replace_callback().

string $base: Base path.

array $aggregate_settings: Array of settings.

Return value

string New version of the url() string from the css.

See also

_drupal_build_css_path()

https://drupal.org/node/1961340#comment-7735815

https://drupal.org/node/1514182#comment-7875489

2 calls to _advagg_build_css_path()
advagg_load_css_stylesheet in ./advagg.inc
Loads the stylesheet and resolves all @import commands.
advagg_relocate_css_post_alter in advagg_relocate/advagg_relocate.module
Alter the css array.
2 string references to '_advagg_build_css_path'
advagg_load_css_stylesheet in ./advagg.inc
Loads the stylesheet and resolves all @import commands.
advagg_relocate_css_post_alter in advagg_relocate/advagg_relocate.module
Alter the css array.

File

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

Code

function _advagg_build_css_path(array $matches, $base = '', array $aggregate_settings = array()) {
  $_base =& drupal_static(__FUNCTION__, '');
  $_aggregate_settings =& drupal_static(__FUNCTION__ . '_aggregate_settings', array());

  // Store base path for preg_replace_callback.
  if (!empty($base)) {
    $_base = $base;
  }
  if (!empty($aggregate_settings)) {
    $_aggregate_settings = $aggregate_settings;
  }

  // Short circuit if no matches were passed in.
  if (empty($matches)) {
    return '';
  }

  // Prefix with base.
  $url = $_base . $matches[1];

  // If advagg_file_create_url() is not being used and the $url is local, redo
  // the $url taking the base_path into account.
  if (!advagg_is_external($url) && variable_get('advagg_skip_file_create_url_inside_css', ADVAGG_SKIP_FILE_CREATE_URL_INSIDE_CSS)) {
    $new_base_path = $GLOBALS['base_path'];
    if (isset($_aggregate_settings['variables']['base_path'])) {
      $new_base_path = $_aggregate_settings['variables']['base_path'];
    }

    // Remove first /.
    $new_base_path = ltrim($new_base_path, '/');
    $pos = FALSE;

    // See if base_path is in the passed in $_base.
    if (!empty($new_base_path)) {
      $pos = strpos($_base, $new_base_path);
    }
    if ($pos !== FALSE) {
      $url = substr($_base, $pos) . $matches[1];
    }
    else {
      $url = $new_base_path . $_base . $matches[1];
    }
  }

  // Remove '../' segments where possible.
  $last = '';
  while ($url != $last) {
    $last = $url;
    $url = preg_replace('`(^|/)(?!\\.\\./)([^/]+)/\\.\\./`', '$1', $url);
  }

  // Parse and build back the url without the query and fragment parts.
  $parsed_url = parse_url($url);
  $base_url = advagg_glue_url($parsed_url, TRUE);
  $query = isset($parsed_url['query']) ? $parsed_url['query'] : '';

  // In the case of certain URLs, we may have simply a '?' character without
  // further parameters. parse_url() misses this and leaves 'query' blank, so
  // need to this back in.
  // See http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  // for more information.
  if ($query != '' || strpos($url, $base_url . '?') === 0) {
    $query = '?' . $query;
  }
  $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  $run_file_create_url = FALSE;
  if (!variable_get('advagg_skip_file_create_url_inside_css', ADVAGG_SKIP_FILE_CREATE_URL_INSIDE_CSS)) {
    $run_file_create_url = TRUE;
  }
  if (empty($parsed_url['host'])) {
    $base_url = ltrim($base_url, '/');
  }
  $base_url = advagg_file_create_url($base_url, $_aggregate_settings, $run_file_create_url, 'css');
  return 'url(' . $base_url . $query . $fragment . ')';
}