You are here

function oembed_oembed_fetch in oEmbed 7

Same name and namespace in other branches
  1. 7.0 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 modules/oembedprovider/oembedprovider.test
oembed_get_data in ./oembed.module
Fetch data for an embeddable URL.
_oembedprovider_handle_request in modules/oembedprovider/oembedprovider.inc
Callback handler for oembed requests.

File

./oembed.module, line 313
Core functionality for oEmbed

Code

function oembed_oembed_fetch($plugin, $url, $matches, $parameters = array()) {
  if ($plugin['cache']) {
    $cache_key = oembed_cache_key($url, $parameters);
    $cache = cache_get($cache_key, 'cache_oembed');

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

  // 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) {
    return FALSE;
  }
  try {
    $embed = call_user_func($function, $plugin, $url, $matches, $parameters);

    // Decorate the oEmbed response object with additional properties that are
    // handy when theming the output.
    $embed['original_url'] = $url;
    $embed['provider'] = $plugin['name'];
    drupal_alter('oembed_response', $embed);
    if ($plugin['cache']) {
      $expire = oembed_cache_expire($embed);
      cache_set($cache_key, $embed, 'cache_oembed', $expire);
    }
    return $embed;
  } catch (RuntimeException $e) {
    if ($plugin['cache'] && ($e
      ->getCode() === 404 || $e
      ->getCode() === 501 || $e
      ->getCode() === 401)) {

      // If expire is not set, use default value and adjust for request time.
      $lifetime = variable_get('oembed_cache_lifetime', 3600);
      if ($lifetime == CACHE_PERMANENT) {
        $lifetime = 3600;
      }
      cache_set($cache_key, NULL, 'cache_oembed', $lifetime + REQUEST_TIME);
    }
  }
  return FALSE;
}