You are here

function shorten_url in Shorten URLs 8

Same name and namespace in other branches
  1. 8.2 shorten.module \shorten_url()
  2. 6 shorten.module \shorten_url()
  3. 7.2 shorten.module \shorten_url()
  4. 7 shorten.module \shorten_url()

Retrieves and beautifies the abbreviated URL. This is the main API function of this module.

Parameters

$original: The URL of the page for which to create the abbreviated URL. If not passed uses the current page.

$service: The service to use to abbreviate the URL. For services available by default, see shorten_shorten_service().

Return value

An abbreviated URL.

4 calls to shorten_url()
ShortenFormCurrentPage::buildForm in src/Form/ShortenFormCurrentPage.php
Form constructor.
ShortenShortenForm::submitForm in src/Form/ShortenShortenForm.php
Form submission handler.
shorten_tokens in ./shorten.module
Implements hook_tokens().
UrlShortener::_shortener_url_behavior in modules/shortener/src/Plugin/Filter/UrlShortener.php
Determines the link caption based on the filter behavior setting.

File

./shorten.module, line 61
Shortens URLs via external services.

Code

function shorten_url($original = '', $service = '') {
  if (!$original) {
    $original = Url::fromRoute('<current>', array(), array(
      'absolute' => 'true',
    ))
      ->toString();

    // print $original; die();
  }
  $original_for_caching = urlencode($original);

  // https://www.drupal.org/node/2324925
  if (!$service) {
    $service = \Drupal::config('shorten.settings')
      ->get('shorten_service');
  }

  // $cached = \Drupal::cache('cache_shorten')->get($original_for_caching);
  // if (!empty($cached->data) && REQUEST_TIME < $cached->expire) {
  //   return $cached->data;
  // }
  $services = \Drupal::moduleHandler()
    ->invokeAll('shorten_service');
  if (isset($services[$service])) {
    $url = _shorten_get_url($original, $services[$service], $service);
  }

  // If the primary service fails, try the secondary service.
  if (empty($url)) {
    $service = \Drupal::config('shorten.settings')
      ->get('shorten_service_backup');
    if (isset($services[$service])) {
      $url = _shorten_get_url($original, $services[$service], $service);
    }

    // If the secondary service fails, use the original URL.
    if (empty($url)) {
      $url = $original;
    }
  }
  $url = trim($url);

  // Redundant for most services.
  // Replace "http://" with "www." if the URL is abbreviated because it's shorter.
  if ($url != $original && \Drupal::config('shorten.settings')
    ->get('shorten_www')) {
    if (strpos($url, 'http://') === 0) {
      $url = \Drupal\Component\Utility\Unicode::substr($url, 7);
      if (strpos($url, 'www.') !== 0) {
        $url = 'www.' . $url;
      }
    }
    elseif (strpos($url, 'https://') === 0) {
      $url = \Drupal\Component\Utility\Unicode::substr($url, 8);
      if (strpos($url, 'www.') !== 0) {
        $url = 'www.' . $url;
      }
    }
  }
  $cache_duration = \Drupal::config('shorten.settings')
    ->get('shorten_cache_duration');

  // Only cache failed retrievals for a limited amount of time.
  if ($url == $original) {
    $expire = REQUEST_TIME + \Drupal::config('shorten.settings')
      ->get('shorten_cache_fail_duration');
  }
  elseif (is_numeric($cache_duration)) {
    $expire = REQUEST_TIME + $cache_duration;
  }
  else {
    $expire = \Drupal\Core\Cache\Cache::PERMANENT;
  }

  // \Drupal::cache('cache_shorten')->set($original_for_caching, $url, $expire);
  \Drupal::moduleHandler()
    ->invokeAll('shorten_create', [
    $original,
    $url,
    $service,
  ]);
  return $url;
}