vimeo.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /*
  3. [vimeo 141358]
  4. [vimeo http://vimeo.com/141358]
  5. [vimeo 141358 h=500&w=350]
  6. [vimeo id=141358 width=350 height=500]
  7. <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>
  8. */
  9. function jetpack_shortcode_get_vimeo_id( $atts ) {
  10. if ( isset( $atts[0] ) ) {
  11. $atts[0] = trim( $atts[0], '=' );
  12. $id = false;
  13. if ( is_numeric( $atts[0] ) ) {
  14. $id = (int) $atts[0];
  15. } elseif ( preg_match( '|vimeo\.com/(\d+)/?$|i', $atts[0], $match ) ) {
  16. $id = (int) $match[1];
  17. } elseif ( preg_match( '|player\.vimeo\.com/video/(\d+)/?$|i', $atts[0], $match ) ) {
  18. $id = (int) $match[1];
  19. }
  20. return $id;
  21. }
  22. return 0;
  23. }
  24. /**
  25. * Convert a Vimeo shortcode into an embed code.
  26. *
  27. * @param array $atts An array of shortcode attributes.
  28. *
  29. * @return string The embed code for the Vimeo video.
  30. */
  31. function vimeo_shortcode( $atts ) {
  32. global $content_width;
  33. $attr = array_map(
  34. 'intval',
  35. shortcode_atts(
  36. array(
  37. 'id' => 0,
  38. 'width' => 0,
  39. 'height' => 0,
  40. 'autoplay' => 0,
  41. 'loop' => 0,
  42. ), $atts
  43. )
  44. );
  45. if ( isset( $atts[0] ) ) {
  46. $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts );
  47. }
  48. if ( ! $attr['id'] ) {
  49. return '<!-- vimeo error: not a vimeo video -->';
  50. }
  51. // [vimeo 141358 h=500&w=350]
  52. $params = shortcode_new_to_old_params( $atts ); // h=500&w=350
  53. $params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
  54. parse_str( $params, $args );
  55. $width = intval( $attr['width'] );
  56. $height = intval( $attr['height'] );
  57. // Support w and h argument as fallback.
  58. if ( empty( $width ) && isset( $args['w'] ) ) {
  59. $width = intval( $args['w'] );
  60. if ( empty( $height ) && ! isset( $args['h'] ) ) {
  61. // The case where w=300 is specified without h=200, otherwise $height
  62. // will always equal the default of 300, no matter what w was set to.
  63. $height = round( ( $width / 640 ) * 360 );
  64. }
  65. }
  66. if ( empty( $height ) && isset( $args['h'] ) ) {
  67. $height = (int) $args['h'];
  68. if ( ! isset( $args['w'] ) ) {
  69. $width = round( ( $height / 360 ) * 640 );
  70. }
  71. }
  72. if ( ! $width && ! empty( $content_width ) ) {
  73. $width = absint( $content_width );
  74. }
  75. // If setting the width with content_width has failed, defaulting
  76. if ( ! $width ) {
  77. $width = 640;
  78. }
  79. if ( ! $height ) {
  80. $height = round( ( $width / 640 ) * 360 );
  81. }
  82. /**
  83. * Filter the Vimeo player width.
  84. *
  85. * @module shortcodes
  86. *
  87. * @since 3.4.0
  88. *
  89. * @param int $width Width of the Vimeo player in pixels.
  90. */
  91. $width = (int) apply_filters( 'vimeo_width', $width );
  92. /**
  93. * Filter the Vimeo player height.
  94. *
  95. * @module shortcodes
  96. *
  97. * @since 3.4.0
  98. *
  99. * @param int $height Height of the Vimeo player in pixels.
  100. */
  101. $height = (int) apply_filters( 'vimeo_height', $height );
  102. $url = esc_url( 'https://player.vimeo.com/video/' . $attr['id'] );
  103. // Handle autoplay and loop arguments.
  104. if (
  105. isset( $args['autoplay'] ) && '1' === $args['autoplay'] // Parsed from the embedded URL.
  106. || $attr['autoplay'] // Parsed from shortcode arguments.
  107. || in_array( 'autoplay', $atts ) // Catch the argument passed without a value.
  108. ) {
  109. $url = add_query_arg( 'autoplay', 1, $url );
  110. }
  111. if (
  112. isset( $args['loop'] ) && '1' === $args['loop'] // Parsed from the embedded URL.
  113. || $attr['loop'] // Parsed from shortcode arguments.
  114. || in_array( 'loop', $atts ) // Catch the argument passed without a value.
  115. ) {
  116. $url = add_query_arg( 'loop', 1, $url );
  117. }
  118. $html = sprintf(
  119. '<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>',
  120. esc_url( $url ),
  121. esc_attr( $width ),
  122. esc_attr( $height )
  123. );
  124. /**
  125. * Filter the Vimeo player HTML.
  126. *
  127. * @module shortcodes
  128. *
  129. * @since 1.2.3
  130. *
  131. * @param string $html Embedded Vimeo player HTML.
  132. */
  133. $html = apply_filters( 'video_embed_html', $html );
  134. return $html;
  135. }
  136. add_shortcode( 'vimeo', 'vimeo_shortcode' );
  137. /**
  138. * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
  139. *
  140. * @since 3.9
  141. *
  142. * @param array $matches Regex partial matches against the URL passed.
  143. * @param array $attr Attributes received in embed response
  144. * @param array $url Requested URL to be embedded
  145. *
  146. * @return string Return output of Vimeo shortcode with the proper markup.
  147. */
  148. function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
  149. return vimeo_shortcode( array( $url ) );
  150. }
  151. /**
  152. * For bare URLs on their own line of the form
  153. * http://vimeo.com/12345
  154. *
  155. * @since 3.9
  156. *
  157. * @uses wpcom_vimeo_embed_url
  158. */
  159. function wpcom_vimeo_embed_url_init() {
  160. wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(.+\.)?vimeo\.com/#i', 'wpcom_vimeo_embed_url' );
  161. }
  162. // Register handler to modify Vimeo embeds using Jetpack's shortcode output.
  163. add_action( 'init', 'wpcom_vimeo_embed_url_init' );
  164. function vimeo_embed_to_shortcode( $content ) {
  165. if ( ! is_string( $content ) || false === stripos( $content, 'player.vimeo.com/video/' ) ) {
  166. return $content;
  167. }
  168. $regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
  169. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  170. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
  171. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
  172. continue;
  173. }
  174. foreach ( $matches as $match ) {
  175. $id = (int) $match[2];
  176. $params = $match[3];
  177. if ( 'regexp_ent' == $reg ) {
  178. $params = html_entity_decode( $params );
  179. }
  180. $params = wp_kses_hair( $params, array( 'http' ) );
  181. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  182. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  183. $wh = '';
  184. if ( $width && $height ) {
  185. $wh = ' w=' . $width . ' h=' . $height;
  186. }
  187. $shortcode = '[vimeo ' . $id . $wh . ']';
  188. $content = str_replace( $match[0], $shortcode, $content );
  189. }
  190. }
  191. return $content;
  192. }
  193. add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
  194. /**
  195. * Replaces shortcodes and plain-text URLs to Vimeo videos with Vimeo embeds.
  196. * Covers shortcode usage [vimeo 1234] | [vimeo https://vimeo.com/1234] | [vimeo http://vimeo.com/1234]
  197. * Or plain text URLs https://vimeo.com/1234 | vimeo.com/1234 | //vimeo.com/1234
  198. * Links are left intact.
  199. *
  200. * @since 3.7.0
  201. * @since 3.9.5 One regular expression matches shortcodes and plain URLs.
  202. *
  203. * @param string $content HTML content
  204. * @return string The content with embeds instead of URLs
  205. */
  206. function vimeo_link( $content ) {
  207. /**
  208. * [vimeo 12345]
  209. * [vimeo http://vimeo.com/12345]
  210. */
  211. $shortcode = "(?:\[vimeo\s+[^0-9]*)([0-9]+)(?:\])";
  212. /**
  213. * http://vimeo.com/12345
  214. * https://vimeo.com/12345
  215. * //vimeo.com/12345
  216. * vimeo.com/some/descender/12345
  217. *
  218. * Should not capture inside HTML attributes
  219. * [Not] <a href="vimeo.com/12345">Cool Video</a>
  220. * [Not] <a href="https://vimeo.com/12345">vimeo.com/12345</a>
  221. *
  222. * Could erroneously capture:
  223. * <a href="some.link/maybe/even/vimeo">This video (vimeo.com/12345) is teh cat's meow!</a>
  224. */
  225. $plain_url = "(?:[^'\">]?\/?(?:https?:\/\/)?vimeo\.com[^0-9]+)([0-9]+)(?:[^'\"0-9<]|$)";
  226. return jetpack_preg_replace_callback_outside_tags(
  227. sprintf( '#%s|%s#i', $shortcode, $plain_url ),
  228. 'vimeo_link_callback',
  229. $content,
  230. 'vimeo'
  231. );
  232. }
  233. /**
  234. * Callback function for the regex that replaces Vimeo URLs with Vimeo embeds.
  235. *
  236. * @since 3.7.0
  237. *
  238. * @param array $matches An array containing a Vimeo URL.
  239. * @return string The Vimeo HTML embed code.
  240. */
  241. function vimeo_link_callback( $matches ) {
  242. $id = isset( $matches[ 2 ] ) ? $matches[ 2 ] : $matches[ 1 ];
  243. if ( isset( $id ) && ctype_digit( $id ) ) {
  244. return "\n" . vimeo_shortcode( array( 'id' => $id ) ) . "\n";
  245. }
  246. return $matches[ 0 ];
  247. }
  248. /** This filter is documented in modules/shortcodes/youtube.php */
  249. if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
  250. // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out.
  251. // Higher priority because we need it before auto-link and autop get to it
  252. add_filter( 'comment_text', 'vimeo_link', 1 );
  253. }