You are here

function filefield_paths_replace_path_callback in File (Field) Paths 7

Callback for regex string replacement functionality.

Parameters

$matches:

$new:

$old:

Return value

string

1 call to filefield_paths_replace_path_callback()
_filefield_paths_replace_path in ./filefield_paths.module
Run regular expression over all available text-based fields.

File

./filefield_paths.module, line 525
Contains core functions for the File (Field) Paths module.

Code

function filefield_paths_replace_path_callback($matches, $new, $old) {
  $prefix = $matches[1];
  $styles = $matches[2];
  $query = isset($matches[6]) ? $matches[6] : '';

  // Get file path info for old file.
  $old_info = parse_url($old);
  if (isset($old_info['path'])) {
    $old_info['host'] .= $old_info['path'];
  }

  // Determine the file path variation type/modifier.
  $old_prefixes = _filefield_paths_replace_path_get_prefixes($old_info['scheme']);
  $modifier = NULL;
  foreach ($old_prefixes as $key => $old_prefix) {
    if ($prefix == $old_prefix) {
      $parts = explode('-', $key);
      $modifier = isset($parts[1]) ? $parts[1] : NULL;
      break;
    }
  }

  // Get file path info for new file.
  $new_info = parse_url($new);
  if (isset($new_info['path'])) {
    $new_info['host'] .= $new_info['path'];
  }

  // Replace prefix.
  $prefixes = _filefield_paths_replace_path_get_prefixes($new_info['scheme']);
  if (isset($key) && isset($prefixes[$key])) {
    $prefix = $prefixes[$key];
  }

  // Replace styles directory.
  if (!empty($styles)) {
    $styles = str_replace($old_info['scheme'], $new_info['scheme'], $styles);

    // Newer versions of the Image module add an 8 character token which is
    // required if the image style hasn't been generated yet.
    if (defined('IMAGE_DERIVATIVE_TOKEN') && isset($matches[7])) {
      $image_style = !empty($matches[3]) ? $matches[3] : $matches[4];

      // Only replace the token if the old one was valid.
      if ($matches[7] == image_style_path_token($image_style, $old)) {
        $query = substr_replace($query, image_style_path_token($image_style, $new), -strlen($matches[7]));
      }
    }
  }

  // Replace path.
  $path = $new_info['host'];
  if (!is_null($modifier) && function_exists($modifier)) {
    $path = call_user_func($modifier, $path);
  }
  return $prefix . $styles . $path . $query;
}