You are here

function media_youtube_valid_id in Media: YouTube 7.2

Check to ensure that a given id is valid.

@TODO: How does this compare to MediaInternetYouTubeHandler's validId method, and can we refactor the code to rely on only one of them?

Parameters

string $id: The YouTube video id.

boolean $refresh: (Defaults to FALSE) If TRUE, then reset the value from the cache.

Return value

boolean Returns TRUE if the video is valid.

File

./media_youtube.module, line 420
Provides a stream wrapper and formatters appropriate for accessing and displaying YouTube videos.

Code

function media_youtube_valid_id($id, $refresh = FALSE) {
  $ids =& drupal_static(__FUNCTION__, array());

  // Return our cached id if allowed, and it exists.
  if (!$refresh && isset($ids[$id])) {
    return $ids[$id];
  }
  elseif (!$refresh && !isset($ids[$id])) {
    return $id;
  }
  elseif (!$refresh && ($cache = cache_get('media_youtube:id:' . $id, 'cache_media_xml'))) {
    $ids[$id] = $cache->data;
    return $ids[$id];
  }
  $url = url(MEDIA_YOUTUBE_REST_API . '/' . $id);
  $response = drupal_http_request($url, array(
    'method' => 'HEAD',
  ));
  $ids[$id] = $response->code == 200;
  cache_set('media_youtube:id:' . $id, $ids[$id], 'cache_media_xml', media_variable_get('xml_cache_expire', 3600));
  return $ids[$id];
}