t('Video macro'), 'description' => t('Inserts embedded video code.'), 'process callback' => 'shortcode_video_macro_process', //'attributes callback' => "shortcode_basic_tags_quote_attributes", 'tips callback' => 'shortcode_video_tip', ); return $shortcodes; } /** * Implementation of hook_theme(). */ function shortcode_video_theme() { return array( 'shortcode_video_embed_error' => array( 'variables' => array('video_url' => '', 'class' => 'video-embedding-error'), ), 'shortcode_video_embed_no_provider' => array( 'variables' => array('video_url' => '', 'class' => 'video-embedding-no-provider'), ), 'shortcode_video_embed_youtube' => array( 'variables' => array('video_url' => '', 'attributes' => array()), ), ); } /* * Youtube embed code * * */ function shortcode_video_macro_process($attrs, $text) { $attrs = shortcode_attrs(array( 'class' => '', 'width' => '', 'height' => '', ), $attrs ); // which provider given $m = array(); preg_match('!^.*\://(.*?\.)?([^\.]*?)\.([^\.]*?)\/(.*?)$!', $text, $m); if (empty($m[1])) { // something wrong with the text return theme('shortcode_video_embed_error', array('video_url' => $text)); } $provider = strtolower($m[2]); $output = ''; switch($provider) { case 'youtube': $params = _shortcode_video_get_youtube_videoid($m); if (!empty($params['video_id'])) { $params['attributes'] = $attrs; $output = theme('shortcode_video_embed_youtube', $params); } break; default: $output = theme('shortcode_video_embed_no_provider', array('video_url' => $text)); break; } return $output; } function shortcode_video_tip($format, $long) { $output = array(); $output[] = '
' . t('[video]video url[/video]') . ' '; if ($long) { $output[] = t('Embeds the video into the text.') . '
'; } else { $output[] = t('Embeds the video into the content text. With the width and height parameter you can specify the video size.') . ''; $output[] = '' . t('For YouTube videos you can specify the width with 420, 480, 640, 960 as the default videos sizes, then the height of the video will be added according to the default embed sizes. The default video size is 480x360px.') . '
'; } return implode('', $output); } /** * Gets the youtube video id from the url part * @param $m - exploded give video text * $m[0] contains the full url of the video * @return array - the video parameters * * Url versions * 1; simple youtube link * http://www.youtube.com/watch?v=EJu8ihVdygY&feature=g-vrec&context=G25615bbRVAAAAAAAAAg * * 2; embed link * * * 3; embed url * http://www.youtube.com/embed/EJu8ihVdygY?rel=0 */ function _shortcode_video_get_youtube_videoid($m) { $params = array(); // $m[4] contains the video url from the end of the provider url to the end of the given text // maybe contains html () $type = substr($m[4], 0, 5); if ('watch' == $type) { $mm = array(); preg_match('!\?v=([^\&]*?)$!', $m[4], $mm); if (!empty($mm[1])) { $params['video_id'] = $mm[1]; $params['video_url'] = 'http://www.youtube.com/embed/' . $mm[1] . '?rel=0'; } } elseif ('embed' == $type) { $mm = array(); preg_match('!^embed\/(.*?)\?!', $m[4], $mm); if (!empty($mm[1])) { $params['video_id'] = $mm[1]; $params['video_url'] = 'http://www.youtube.com/embed/' . $mm[1] . '?rel=0'; } } else { $params['video_id'] = ''; $params['video_url'] = ''; } return $params; } /* * Theme functions */ function theme_shortcode_video_embed_error($variables) { return '' . check_plain($variables['video_url']) . ''; } function theme_shortcode_video_embed_no_provider($variables) { return '' . check_plain($variables['video_url']) . ''; } function theme_shortcode_video_embed_youtube($variables) { $attrs = $variables['attributes']; $height = empty($attrs['height']) ? 0 : intval($attrs['height']); $width = empty($attrs['width']) ? 0 : intval($attrs['width']); if (empty($width) && empty($height)) { $width = 480; $height = 360; } elseif ($width && empty($height)) { switch($width) { case 450: $height = 315; break; case 480: $height = 360; break; case 640: $height = 480; break; case 960: $height = 720; break; default: $height = intval($width * 0.75); break; } } else { $width = intval($height * 1.33); } $width = $width ? ' width="' . $width . '"' : ''; $height = $height ? ' height="' . $height . '"' : ''; return ''; }