You are here

function oembed_oembed_fetch in oEmbed 7.0

Same name and namespace in other branches
  1. 7 oembed.module \oembed_oembed_fetch()

oEmbed fetcher and parser.

This handles fetching from remote providers and local registered callbacks. It does not cache the responses because they are cached when rendered.

3 calls to oembed_oembed_fetch()
OembedProviderTestCase::testOembedProviders in oembedprovider/oembedprovider.test
oembed_get_data in ./oembed.module
Fetch data for an embeddable URL.
_oembedprovider_handle_request in oembedprovider/oembedprovider.inc
Callback handler for oembed requests.

File

./oembed.module, line 241

Code

function oembed_oembed_fetch($plugin, $url, $matches, $parameters = array()) {
  $embed = FALSE;

  // Normalize the parameters and attributes for better cache performance.
  ksort($parameters);
  $parameters = array_filter($parameters);
  if ($plugin['cache']) {
    $cache_keys = array();

    // Remove trailing slash to normalize URLs.
    $cache_keys[] = hash('sha256', substr($url, -1) == '/' ? substr($url, 0, -1) : $url);

    // Hash and serialize request parameters and display options.
    if (!empty($parameters)) {
      $cache_keys[] = hash('sha256', serialize($parameters));
    }
    $cache_key = implode(':', $cache_keys);
    $cache = cache_get($cache_key, 'cache_oembed');

    // Cache hit.
    if ($cache && isset($cache->data)) {
      return $cache->data;
    }
  }

  // Cache miss.
  drupal_alter('oembed_request', $parameters, $plugin, $url);

  // Drupal oEmbed provider uses function callbacks for internal requests.
  $function = ctools_plugin_get_function($plugin, 'callback');
  if ($function) {
    $embed = call_user_func($function, $plugin, $url, $matches, $parameters);
  }

  // Decorate the oEmbed response object with additional properties that are
  // handy when theming the output.
  if ($embed) {
    $embed['original_url'] = $url;
    $embed['provider'] = $plugin['name'];
    drupal_alter('oembed_response', $embed);
  }
  if ($plugin['cache']) {

    // If expire is not set, use default value and adjust for request time.
    $lifetime = variable_get('oembed_cache_lifetime', 3600);

    // Recalculate cache expire time based on response.
    if ($embed && $lifetime != CACHE_PERMANENT && isset($embed['cache_age'])) {
      $lifetime = max($lifetime, intval($embed['cache_age']));
    }
    else {
      if (!$embed && $lifetime == CACHE_PERMANENT) {
        $lifetime = 3600;
      }
    }
    if ($embed && $lifetime == CACHE_PERMANENT) {
      $expire = $lifetime;
    }
    else {
      $expire = min($lifetime + REQUEST_TIME, 2147483647);
    }

    // Twitter returns an unreasonably high cache_age of 31536000000 seconds,
    // which is longer than the expire column in Drupal cache table supports.
    cache_set($cache_key, $embed, 'cache_oembed', $expire);
  }
  return $embed;
}