YIT_Video

YIT Video.

Defined (1)

The class is defined in the following location(s).

/plugin-fw/lib/yit-video.php  
  1. /* @class YIT_Video 
  2. * @package Yithemes 
  3. * @since 1.0.0 
  4. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  5. */ 
  6.  
  7. class YIT_Video { 
  8.  
  9. /** 
  10. * Generate the HTML for a youtube video 
  11. * @static 
  12. * @param array $args Array of arguments to configure the video to generate 
  13. * @return string 
  14. * @since 1.0 
  15. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  16. */ 
  17. public static function youtube( $args = array() ) { 
  18. $defaults = array( 
  19. 'id' => '',  
  20. 'url' => '',  
  21. 'width' => 425,  
  22. 'height' => 356,  
  23. 'echo' => false 
  24. ); 
  25. $args = wp_parse_args( $args, $defaults ); 
  26. extract( $args ); 
  27.  
  28. // get video ID if you have only URL 
  29. if ( empty( $id ) && ! empty( $url ) ) { 
  30. $id = self::video_id_by_url( $url ); 
  31. } elseif ( empty( $id ) && empty( $url ) ) { 
  32. return; 
  33.  
  34. ob_start(); 
  35.  
  36. $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); 
  37. $id = preg_replace( '/(youtube|vimeo):/', '', $id ); ?> 
  38.  
  39. <div class="post_video youtube"> 
  40. <iframe wmode="transparent" width="<?php echo $width; ?>" height="<?php echo $height; ?>" src="https://www.youtube.com/embed/<?php echo $id; ?>?wmode=transparent" frameborder="0" allowfullscreen></iframe> 
  41. </div> 
  42.  
  43. <?php 
  44. $html = apply_filters( 'yit_video_youtube', ob_get_clean() ); 
  45.  
  46. if( $echo ) echo $html; 
  47.  
  48. return $html; 
  49.  
  50. /** 
  51. * Generate the HTML for a vimeo video 
  52. * @static 
  53. * @param array $args Array of arguments to configure the video to generate 
  54. * @return string 
  55. * @since 1.0 
  56. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  57. */ 
  58. public static function vimeo( $args = array() ) { 
  59. $defaults = array( 
  60. 'id' => '',  
  61. 'url' => '',  
  62. 'width' => 425,  
  63. 'height' => 356,  
  64. 'echo' => false 
  65. ); 
  66. $args = wp_parse_args( $args, $defaults ); 
  67. extract( $args ); 
  68.  
  69. // get video ID if you have only URL 
  70. if ( empty( $id ) && ! empty( $url ) ) { 
  71. $id = self::video_id_by_url( $url ); 
  72.  
  73. ob_start(); 
  74.  
  75. $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); 
  76. $id = preg_replace( '/(youtube|vimeo):/', '', $id ); 
  77. $http = is_ssl()? 'https' : 'http'; 
  78. ?> 
  79.  
  80.  
  81. <div class="post_video vimeo"> 
  82. <iframe wmode="transparent" src="<?php echo $http;?>://player.vimeo.com/video/<?php echo $id; ?>?title=0&byline=0&portrait=0" width="<?php echo $width; ?>" height="<?php echo $height; ?>" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> 
  83. </div> 
  84.  
  85. <?php 
  86. $html = apply_filters( 'yit_video_vimeo', ob_get_clean() ); 
  87.  
  88. if( $echo ) echo $html; 
  89.  
  90. return $html; 
  91.  
  92. /** 
  93. * Retrieve video ID from URL 
  94. * @static 
  95. * @param array $url The URL of video 
  96. * @return bool|string 
  97. * @since 1.0 
  98. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  99. */ 
  100. public static function video_id_by_url( $url ) { 
  101. $parsed = parse_url( esc_url( $url ) ); 
  102.  
  103. if ( ! isset( $parsed['host'] ) ) { 
  104. return false; 
  105.  
  106.  
  107. switch ( $parsed['host'] ) { 
  108. case 'youtube.com' : 
  109. case 'www.youtube.com' : 
  110. case 'youtu.be' : 
  111. case 'www.youtu.be' : 
  112. $id = self::youtube_id_by_url( $url ); 
  113. return "youtube:$id"; 
  114.  
  115. case 'www.vimeo.com' : 
  116. case 'vimeo.com' : 
  117. preg_match( '/http(s)?:\/\/(\w+.)?vimeo\.com\/(.*)?(\/[0-9]+)/', $url, $matches ); 
  118. $id = trim( $matches[4], '/' ); 
  119. return "vimeo:$id"; 
  120.  
  121. default : 
  122. return false; 
  123.  
  124.  
  125. /** 
  126. * Retrieve video ID from URL 
  127. * @static 
  128. * @param array $url The URL of video 
  129. * @return bool|string 
  130. * @since 1.0 
  131. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  132. */ 
  133. protected static function youtube_id_by_url( $url ) { 
  134. if ( preg_match( '/http(s)?:\/\/youtu.be/', $url, $matches) ) { 
  135. $url = parse_url($url, PHP_URL_PATH); 
  136. $url = str_replace( '/', '', $url); 
  137. return $url; 
  138.  
  139. } elseif ( preg_match( '/watch/', $url, $matches) ) { 
  140. $arr = parse_url($url); 
  141. $url = str_replace( 'v=', '', $arr['query'] ); 
  142. return $url; 
  143.  
  144. } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/v/', $url, $matches) ) { 
  145. $arr = parse_url($url); 
  146. $url = str_replace( '/v/', '', $arr['path'] ); 
  147. return $url; 
  148.  
  149. } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/embed/', $url, $matches) ) { 
  150. $arr = parse_url($url); 
  151. $url = str_replace( '/embed/', '', $arr['path'] ); 
  152. return $url; 
  153.  
  154. } elseif ( preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) { 
  155. return $matches[0];