* */ class YIT_Video { /** * Generate the HTML for a youtube video * * @static * * @param array $args Array of arguments to configure the video to generate * * @return string * @since 1.0 * @author Antonino Scarfi' */ public static function youtube( $args = array() ) { $defaults = array( 'id' => '', 'url' => '', 'width' => 425, 'height' => 356, 'echo' => false ); $args = wp_parse_args( $args, $defaults ); extract( $args ); // get video ID if you have only URL if ( empty( $id ) && ! empty( $url ) ) { $id = self::video_id_by_url( $url ); } elseif ( empty( $id ) && empty( $url ) ) { return; } ob_start(); $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); $id = preg_replace( '/(youtube|vimeo):/', '', $id ); ?>
*/ public static function vimeo( $args = array() ) { $defaults = array( 'id' => '', 'url' => '', 'width' => 425, 'height' => 356, 'echo' => false ); $args = wp_parse_args( $args, $defaults ); extract( $args ); // get video ID if you have only URL if ( empty( $id ) && ! empty( $url ) ) { $id = self::video_id_by_url( $url ); } ob_start(); $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); $id = preg_replace( '/(youtube|vimeo):/', '', $id ); $http = is_ssl()? 'https' : 'http'; ?>
*/ public static function video_id_by_url( $url ) { $parsed = parse_url( esc_url( $url ) ); if ( ! isset( $parsed['host'] ) ) { return false; } switch ( $parsed['host'] ) { case 'youtube.com' : case 'www.youtube.com' : case 'youtu.be' : case 'www.youtu.be' : $id = self::youtube_id_by_url( $url ); return "youtube:$id"; case 'www.vimeo.com' : case 'vimeo.com' : preg_match( '/http(s)?:\/\/(\w+.)?vimeo\.com\/(.*)?(\/[0-9]+)/', $url, $matches ); $id = trim( $matches[4], '/' ); return "vimeo:$id"; default : return false; } } /** * Retrieve video ID from URL * * @static * * @param array $url The URL of video * * @return bool|string * @since 1.0 * @author Antonino Scarfi' */ protected static function youtube_id_by_url( $url ) { if ( preg_match( '/http(s)?:\/\/youtu.be/', $url, $matches) ) { $url = parse_url($url, PHP_URL_PATH); $url = str_replace( '/', '', $url); return $url; } elseif ( preg_match( '/watch/', $url, $matches) ) { $arr = parse_url($url); $url = str_replace( 'v=', '', $arr['query'] ); return $url; } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/v/', $url, $matches) ) { $arr = parse_url($url); $url = str_replace( '/v/', '', $arr['path'] ); return $url; } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/embed/', $url, $matches) ) { $arr = parse_url($url); $url = str_replace( '/embed/', '', $arr['path'] ); return $url; } elseif ( preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) { return $matches[0]; } else { return false; } } } }