You are here

function shortcode_video_macro_process in Shortcode Video 7

Provides Youtube video embedding ShortCode macro.

<iframe width="420" height="315" title="Foo" src="http://www.youtube.com/embed/EJu8ihVdygY?rel=0" frameborder="0" allowfullscreen></iframe>

1 string reference to 'shortcode_video_macro_process'
shortcode_video_shortcode_info in ./shortcode_video.module
Implements hook_shortcode_info().

File

./shortcode_video.module, line 96
ShortCode for embedding videos.

Code

function shortcode_video_macro_process($attrs, $text) {
  $attrs = shortcode_attrs(array(
    'class' => '',
    'width' => '',
    'height' => '',
    'showlink' => FALSE,
    'sub' => '',
    'forcesub' => FALSE,
    'title' => '',
  ), $attrs);
  $text = decode_entities($text);
  $url = drupal_parse_url($text);

  // Custom url parser regexp:
  // preg_match('!^.*\:\/\/(www\.)?([^\.]*)(\.)([^\/\?]*)([\/]?)([^\?\"]*)\??([^\"\>]?.*)?$!', $text, $m);
  // Get provider.
  $m = [];
  preg_match('!^.*\\:\\/\\/(www\\.)?([^\\?\\/]*)(.*)$!', $url['path'], $m);
  $provider = $m[2];
  $output = '';
  switch ($provider) {
    case 'youtu.be':
    case 'youtube.com':
      if (!empty($url['query']['v'])) {
        $params['video_id'] = $url['query']['v'];
        unset($params['v']);
        unset($url['query']['v']);
      }
      else {
        $id = trim($m[3], '/');
        switch ($id) {
          case 'watch':

            // todo: try special parsing.
            // $params = _shortcode_video_parse_params($m);
            break;
          default:
            $params['video_id'] = $id;
            break;
        }
      }
      $embed_url = '//www.youtube.com/embed/';
      $params['attributes'] = $attrs;
      if (!empty($params['video_id'])) {
        if ($attrs['sub']) {
          $url['query']['hl'] = $attrs['sub'];
        }
        if ($attrs['forcesub']) {
          $url['query']['fs'] = 1;
        }
        if (!empty($url['query']['t'])) {
          $t = explode('m', $url['query']['t']);
          $start = intval(trim($t[0])) * 60;
          $start += intval($t[1]);
          unset($url['query']['t']);
          $url['query']['start'] = $start;
        }
        $url['external'] = TRUE;
        $params['video_url'] = url($embed_url . $params['video_id'], $url);
        $output = theme('shortcode_video_embed_youtube', $params);
      }
      break;
    case 'vimeo.com':
      $id = trim($m[3], '/');
      $params['video_id'] = $id;
      $embed_url = '//player.vimeo.com/video/';
      $params['external'] = TRUE;
      $params['video_url'] = url($embed_url . $params['video_id'], $params);
      if (!empty($params['video_id'])) {
        $params['attributes'] = $attrs;
        $output = theme('shortcode_video_embed_vimeo', $params);
      }
      break;
    default:

      // Alter the output.
      // $context1 = $text.
      // $context2 = $attrs - the attributes array
      // $context3 = $url - The parsed URL by Drupal.
      drupal_alter('shortcode_info', $output, $text, $attrs, $url);
      if (empty($output)) {

        // Default behaviour when no custom provider.
        $pattern = array(
          '!http://!',
          '!https://!',
        );
        $text = preg_replace($pattern, '//', $text);
        $output = theme('shortcode_video_embed_no_provider', array(
          'video_url' => $text,
        ));
      }
      break;
  }
  return $output;
}