/modules/shortcodes/vimeo.php

  1. <?php 
  2.  
  3. /** 
  4. [vimeo 141358] 
  5. [vimeo http://vimeo.com/141358] 
  6. [vimeo 141358 h=500&w=350] 
  7. [vimeo id=141358 width=350 height=500] 
  8.   
  9. <iframe src="http://player.vimeo.com/video/18427511" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/18427511">Eskmo 'We Got More' (Official Video)</a> from <a href="http://vimeo.com/ninjatune">Ninja Tune</a> on <a href="http://vimeo.com">Vimeo</a>.</p> 
  10. */ 
  11.  
  12. function jetpack_shortcode_get_vimeo_id( $atts ) { 
  13. if ( isset( $atts[0] ) ) { 
  14. $atts[0] = trim( $atts[0] , '=' ); 
  15. $id = false; 
  16. if ( is_numeric( $atts[0] ) ) 
  17. $id = (int) $atts[0]; 
  18. elseif ( preg_match( '|vimeo\.com/(\d+)/?$|i', $atts[0], $match ) ) 
  19. $id = (int) $match[1]; 
  20. elseif ( preg_match( '|player\.vimeo\.com/video/(\d+)/?$|i', $atts[0], $match ) ) 
  21. $id = (int) $match[1]; 
  22. return $id; 
  23. return 0; 
  24.  
  25. /** 
  26. * Convert a Vimeo shortcode into an embed code. 
  27. * 
  28. * @param array $atts An array of shortcode attributes. 
  29. * @return string The embed code for the Vimeo video. 
  30. */ 
  31. function vimeo_shortcode( $atts ) { 
  32. global $content_width; 
  33.  
  34. extract( array_map( 'intval', shortcode_atts( array( 
  35. 'id' => 0,  
  36. 'width' => 400,  
  37. 'height' => 300,  
  38. 'autoplay' => 0,  
  39. 'loop' => 0,  
  40. ), $atts, 'vimeo' ) ) ); 
  41.  
  42. if ( isset( $atts[0] ) ) { 
  43. $id = jetpack_shortcode_get_vimeo_id( $atts ); 
  44.  
  45. if ( ! $id ) return "<!-- vimeo error: not a vimeo video -->"; 
  46.  
  47. // [vimeo 141358 h=500&w=350] 
  48. $params = shortcode_new_to_old_params( $atts ); // h=500&w=350 
  49. $params = str_replace( array( '&', '&' ), '&', $params ); 
  50. parse_str( $params, $args ); 
  51.  
  52. if ( isset( $args['w'] ) ) { 
  53. $width = (int) $args['w']; 
  54.  
  55. if ( ! isset( $args['h'] ) ) { 
  56. // The case where w=300 is specified without h=200, otherwise $height 
  57. // will always equal the default of 300, no matter what w was set to. 
  58. $height = round( ( $width / 640 ) * 360 ); 
  59.  
  60. if ( isset( $args['h'] ) ) { 
  61. $height = (int) $args['h']; 
  62.  
  63. if ( ! isset( $args['w'] ) ) { 
  64. $width = round( ( $height / 360 ) * 640 ); 
  65.  
  66. if ( ! $width ) { 
  67. $width = absint( $content_width ); 
  68.  
  69. if ( ! $height ) { 
  70. $height = round( ( $width / 640 ) * 360 ); 
  71.  
  72. /** 
  73. * Filter the Vimeo player width. 
  74. * 
  75. * @module shortcodes 
  76. * 
  77. * @since 3.4.0 
  78. * 
  79. * @param int $width Width of the Vimeo player in pixels. 
  80. */ 
  81. $width = (int) apply_filters( 'vimeo_width', $width ); 
  82.  
  83. /** 
  84. * Filter the Vimeo player height. 
  85. * 
  86. * @module shortcodes 
  87. * 
  88. * @since 3.4.0 
  89. * 
  90. * @param int $height Height of the Vimeo player in pixels. 
  91. */ 
  92. $height = (int) apply_filters( 'vimeo_height', $height ); 
  93.  
  94. $url = esc_url( "https://player.vimeo.com/video/$id" ); 
  95.  
  96. // $args['autoplay'] is parsed from the embedded url. 
  97. // $autoplay is parsed from shortcode arguments. 
  98. // in_array( 'autoplay', $atts ) catches the argument passed without a value. 
  99. if ( ! empty( $args['autoplay'] ) || ! empty( $autoplay ) || in_array( 'autoplay', $atts ) ) { 
  100. $url = add_query_arg( 'autoplay', 1, $url ); 
  101.  
  102. if ( ! empty( $args['loop'] ) || ! empty( $loop ) || in_array( 'loop', $atts ) ) { 
  103. $url = add_query_arg( 'loop', 1, $url ); 
  104.  
  105. $html = sprintf( '<div class="embed-vimeo" style="text-align:center;"><iframe src="%1$s" width="%2$u" height="%3$u" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>', esc_url( $url ), $width, $height ); 
  106.  
  107. /** 
  108. * Filter the Vimeo player HTML. 
  109. * 
  110. * @module shortcodes 
  111. * 
  112. * @since 1.2.3 
  113. * 
  114. * @param string $html Embedded Vimeo player HTML. 
  115. */ 
  116. $html = apply_filters( 'video_embed_html', $html ); 
  117.  
  118. return $html; 
  119.  
  120. add_shortcode( 'vimeo', 'vimeo_shortcode' ); 
  121.  
  122. function vimeo_embed_to_shortcode( $content ) { 
  123. if ( false === stripos( $content, 'player.vimeo.com/video/' ) ) 
  124. return $content; 
  125.  
  126. $regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i'; 
  127. $regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); 
  128.  
  129. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { 
  130. if ( !preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) 
  131. continue; 
  132.  
  133. foreach ( $matches as $match ) { 
  134. $id = (int) $match[2]; 
  135.  
  136. $params = $match[3]; 
  137.  
  138. if ( 'regexp_ent' == $reg ) 
  139. $params = html_entity_decode( $params ); 
  140.  
  141. $params = wp_kses_hair( $params, array( 'http' ) ); 
  142.  
  143. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; 
  144. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; 
  145.  
  146. $wh = ''; 
  147. if ( $width && $height ) 
  148. $wh = ' w=' . $width . ' h=' . $height; 
  149.  
  150. $shortcode = '[vimeo ' . $id . $wh . ']'; 
  151. $content = str_replace( $match[0], $shortcode, $content ); 
  152.  
  153. return $content; 
  154.  
  155. add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' ); 
  156.  
  157. /** 
  158. * Replaces plain-text links to Vimeo videos with Vimeo embeds. 
  159. * 
  160. * @since 3.7.0 
  161. * 
  162. * @param string $content HTML content 
  163. * @return string The content with embeds instead of URLs 
  164. */ 
  165. function vimeo_link( $content ) { 
  166. return preg_replace_callback( '#https://vimeo.com/\d*#', 'vimeo_link_callback', $content ); 
  167.  
  168. /** 
  169. * Callback function for the regex that replaces Vimeo URLs with Vimeo embeds. 
  170. * 
  171. * @since 3.7.0 
  172. * 
  173. * @param array $matches An array containing a Vimeo URL. 
  174. * @return string THe Vimeo HTML embed code. 
  175. */ 
  176. function vimeo_link_callback( $matches ) { 
  177. // Grab the Vimeo ID from the URL 
  178. if ( preg_match( '|vimeo\.com/(\d+)/?$|i', $matches[0], $match ) ) { 
  179. $id = (int) $match[1]; 
  180.  
  181. // Pass that ID to the Vimeo shortcode function. 
  182. if ( $id ) { 
  183. $atts = array( 'id' => $id ); 
  184. return "\n" . vimeo_shortcode( $atts ) . "\n"; 
  185.  
  186. /** This filter is documented in modules/shortcodes/youtube.php */ 
  187. if ( apply_filters( 'jetpack_comments_allow_oembed', get_option('embed_autourls') ) ) { 
  188. // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out. 
  189. if ( ! is_admin() ) { 
  190. // Higher priority because we need it before auto-link and autop get to it 
  191. add_filter( 'comment_text', 'vimeo_link', 1 ); 
.