/plugin-fw/lib/yit-video.php

  1. <?php 
  2. /** 
  3. * This file belongs to the YIT Framework. 
  4. * 
  5. * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0) 
  6. * that is bundled with this package in the file LICENSE.txt. 
  7. * It is also available through the world-wide-web at this URL: 
  8. * http://www.gnu.org/licenses/gpl-3.0.txt 
  9. */ 
  10.  
  11.  
  12. if ( ! defined( 'ABSPATH' ) ) { 
  13. exit; 
  14. } // Exit if accessed directly 
  15.  
  16. if ( ! class_exists( 'YIT_Video' ) ) { 
  17. /** 
  18. * YIT Video 
  19. * 
  20. * Class to manage the video from youtube and vimeo or other services 
  21. * 
  22. * @class YIT_Video 
  23. * @package Yithemes 
  24. * @since 1.0.0 
  25. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  26. * 
  27. */ 
  28.  
  29. class YIT_Video { 
  30.  
  31. /** 
  32. * Generate the HTML for a youtube video 
  33. * 
  34. * @static 
  35. * 
  36. * @param array $args Array of arguments to configure the video to generate 
  37. * 
  38. * @return string 
  39. * @since 1.0 
  40. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  41. */ 
  42. public static function youtube( $args = array() ) { 
  43. $defaults = array( 
  44. 'id' => '',  
  45. 'url' => '',  
  46. 'width' => 425,  
  47. 'height' => 356,  
  48. 'echo' => false 
  49. ); 
  50. $args = wp_parse_args( $args, $defaults ); 
  51. extract( $args ); 
  52.  
  53. // get video ID if you have only URL 
  54. if ( empty( $id ) && ! empty( $url ) ) { 
  55. $id = self::video_id_by_url( $url ); 
  56. } elseif ( empty( $id ) && empty( $url ) ) { 
  57. return; 
  58.  
  59. ob_start(); 
  60.  
  61. $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); 
  62. $id = preg_replace( '/(youtube|vimeo):/', '', $id ); ?> 
  63.  
  64. <div class="post_video youtube"> 
  65. <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> 
  66. </div> 
  67.  
  68. <?php 
  69. $html = apply_filters( 'yit_video_youtube', ob_get_clean() ); 
  70.  
  71. if( $echo ) echo $html; 
  72.  
  73. return $html; 
  74.  
  75. /** 
  76. * Generate the HTML for a vimeo video 
  77. * 
  78. * @static 
  79. * 
  80. * @param array $args Array of arguments to configure the video to generate 
  81. * 
  82. * @return string 
  83. * @since 1.0 
  84. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  85. */ 
  86. public static function vimeo( $args = array() ) { 
  87. $defaults = array( 
  88. 'id' => '',  
  89. 'url' => '',  
  90. 'width' => 425,  
  91. 'height' => 356,  
  92. 'echo' => false 
  93. ); 
  94. $args = wp_parse_args( $args, $defaults ); 
  95. extract( $args ); 
  96.  
  97. // get video ID if you have only URL 
  98. if ( empty( $id ) && ! empty( $url ) ) { 
  99. $id = self::video_id_by_url( $url ); 
  100.  
  101. ob_start(); 
  102.  
  103. $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); 
  104. $id = preg_replace( '/(youtube|vimeo):/', '', $id ); 
  105. $http = is_ssl()? 'https' : 'http'; 
  106. ?> 
  107.  
  108.  
  109. <div class="post_video vimeo"> 
  110. <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> 
  111. </div> 
  112.  
  113. <?php 
  114. $html = apply_filters( 'yit_video_vimeo', ob_get_clean() ); 
  115.  
  116. if( $echo ) echo $html; 
  117.  
  118. return $html; 
  119.  
  120. /** 
  121. * Retrieve video ID from URL 
  122. * 
  123. * @static 
  124. * 
  125. * @param array $url The URL of video 
  126. * 
  127. * @return bool|string 
  128. * @since 1.0 
  129. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  130. */ 
  131. public static function video_id_by_url( $url ) { 
  132. $parsed = parse_url( esc_url( $url ) ); 
  133.  
  134. if ( ! isset( $parsed['host'] ) ) { 
  135. return false; 
  136.  
  137.  
  138. switch ( $parsed['host'] ) { 
  139. case 'youtube.com' : 
  140. case 'www.youtube.com' : 
  141. case 'youtu.be' : 
  142. case 'www.youtu.be' : 
  143. $id = self::youtube_id_by_url( $url ); 
  144. return "youtube:$id"; 
  145.  
  146. case 'www.vimeo.com' : 
  147. case 'vimeo.com' : 
  148. preg_match( '/http(s)?:\/\/(\w+.)?vimeo\.com\/(.*)?(\/[0-9]+)/', $url, $matches ); 
  149. $id = trim( $matches[4], '/' ); 
  150. return "vimeo:$id"; 
  151.  
  152. default : 
  153. return false; 
  154.  
  155.  
  156. /** 
  157. * Retrieve video ID from URL 
  158. * 
  159. * @static 
  160. * 
  161. * @param array $url The URL of video 
  162. * 
  163. * @return bool|string 
  164. * @since 1.0 
  165. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  166. */ 
  167. protected static function youtube_id_by_url( $url ) { 
  168. if ( preg_match( '/http(s)?:\/\/youtu.be/', $url, $matches) ) { 
  169. $url = parse_url($url, PHP_URL_PATH); 
  170. $url = str_replace( '/', '', $url); 
  171. return $url; 
  172.  
  173. } elseif ( preg_match( '/watch/', $url, $matches) ) { 
  174. $arr = parse_url($url); 
  175. $url = str_replace( 'v=', '', $arr['query'] ); 
  176. return $url; 
  177.  
  178. } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/v/', $url, $matches) ) { 
  179. $arr = parse_url($url); 
  180. $url = str_replace( '/v/', '', $arr['path'] ); 
  181. return $url; 
  182.  
  183. } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/embed/', $url, $matches) ) { 
  184. $arr = parse_url($url); 
  185. $url = str_replace( '/embed/', '', $arr['path'] ); 
  186. return $url; 
  187.  
  188. } elseif ( preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) { 
  189. return $matches[0]; 
  190.  
  191. } else { 
  192. return false; 
  193.  
.