暫無描述

vimeo.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /**
  3. * Vimeo Shortcode.
  4. *
  5. * Examples:
  6. * [vimeo 141358]
  7. * [vimeo http://vimeo.com/141358]
  8. * [vimeo 141358 h=500&w=350]
  9. * [vimeo 141358 h=500 w=350]
  10. * [vimeo id=141358 width=350 height=500]
  11. *
  12. * <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>
  13. *
  14. * @package automattic/jetpack
  15. */
  16. /**
  17. * Extract Vimeo ID from shortcode.
  18. *
  19. * @param array $atts Shortcode attributes.
  20. */
  21. function jetpack_shortcode_get_vimeo_id( $atts ) {
  22. if ( isset( $atts[0] ) ) {
  23. $atts[0] = trim( $atts[0], '=' );
  24. $id = false;
  25. if ( is_numeric( $atts[0] ) ) {
  26. $id = (int) $atts[0];
  27. } elseif ( preg_match( '|vimeo\.com/(\d+)/?$|i', $atts[0], $match ) ) {
  28. $id = (int) $match[1];
  29. } elseif ( preg_match( '|player\.vimeo\.com/video/(\d+)/?$|i', $atts[0], $match ) ) {
  30. $id = (int) $match[1];
  31. }
  32. return $id;
  33. }
  34. return 0;
  35. }
  36. /**
  37. * Get video dimensions.
  38. *
  39. * @since 8.0.0
  40. *
  41. * @param array $attr The attributes of the shortcode.
  42. * @param array $old_attr Optional array of attributes from the old shortcode format.
  43. *
  44. * @return array Width and height.
  45. */
  46. function jetpack_shortcode_get_vimeo_dimensions( $attr, $old_attr = array() ) {
  47. global $content_width;
  48. $default_width = 600;
  49. $default_height = 338;
  50. $aspect_ratio = $default_height / $default_width;
  51. /*
  52. * For width and height, we want to support both formats
  53. * that can be provided in the new shortcode format:
  54. * - for width: width or w
  55. * - for height: height or h
  56. *
  57. * For each variation, the full word takes priority.
  58. *
  59. * If no variation is set, we default to the default width and height values set above.
  60. */
  61. if ( ! empty( $attr['width'] ) ) {
  62. $width = absint( $attr['width'] );
  63. } elseif ( ! empty( $attr['w'] ) ) {
  64. $width = absint( $attr['w'] );
  65. } else {
  66. $width = $default_width;
  67. }
  68. if ( ! empty( $attr['height'] ) ) {
  69. $height = absint( $attr['height'] );
  70. } elseif ( ! empty( $attr['h'] ) ) {
  71. $height = absint( $attr['h'] );
  72. } else {
  73. $height = $default_height;
  74. }
  75. /*
  76. * Support w and h argument as fallbacks in old shortcode format.
  77. */
  78. if (
  79. $default_width === $width
  80. && ! empty( $old_attr['w'] )
  81. ) {
  82. $width = absint( $old_attr['w'] );
  83. if (
  84. $default_width === $width
  85. && empty( $old_attr['h'] )
  86. ) {
  87. $height = round( $width * $aspect_ratio );
  88. }
  89. }
  90. if (
  91. $default_height === $height
  92. && ! empty( $old_attr['h'] )
  93. ) {
  94. $height = absint( $old_attr['h'] );
  95. if ( empty( $old_attr['w'] ) ) {
  96. $width = round( $height * $aspect_ratio );
  97. }
  98. }
  99. /*
  100. * If we have a content width defined, let it be the new default.
  101. */
  102. if (
  103. $default_width === $width
  104. && ! empty( $content_width )
  105. ) {
  106. $width = absint( $content_width );
  107. }
  108. /*
  109. * If we have a custom width, we need a custom height as well
  110. * to maintain aspect ratio.
  111. */
  112. if (
  113. $default_width !== $width
  114. && $default_height === $height
  115. ) {
  116. $height = round( ( $width / 640 ) * 360 );
  117. }
  118. /**
  119. * Filter the Vimeo player width.
  120. *
  121. * @module shortcodes
  122. *
  123. * @since 3.4.0
  124. *
  125. * @param int $width Width of the Vimeo player in pixels.
  126. */
  127. $width = (int) apply_filters( 'vimeo_width', $width );
  128. /**
  129. * Filter the Vimeo player height.
  130. *
  131. * @module shortcodes
  132. *
  133. * @since 3.4.0
  134. *
  135. * @param int $height Height of the Vimeo player in pixels.
  136. */
  137. $height = (int) apply_filters( 'vimeo_height', $height );
  138. return array( $width, $height );
  139. }
  140. /**
  141. * Convert a Vimeo shortcode into an embed code.
  142. *
  143. * @param array $atts An array of shortcode attributes.
  144. *
  145. * @return string The embed code for the Vimeo video.
  146. */
  147. function vimeo_shortcode( $atts ) {
  148. $attr = array_map(
  149. 'intval',
  150. shortcode_atts(
  151. array(
  152. 'id' => 0,
  153. 'width' => 0,
  154. 'height' => 0,
  155. 'autoplay' => 0,
  156. 'loop' => 0,
  157. 'w' => 0,
  158. 'h' => 0,
  159. ),
  160. $atts
  161. )
  162. );
  163. if ( isset( $atts[0] ) ) {
  164. $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts );
  165. }
  166. if ( ! $attr['id'] ) {
  167. return '<!-- vimeo error: not a vimeo video -->';
  168. }
  169. // Handle old shortcode params such as h=500&w=350.
  170. $params = shortcode_new_to_old_params( $atts );
  171. $params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
  172. parse_str( $params, $args );
  173. list( $width, $height ) = jetpack_shortcode_get_vimeo_dimensions( $attr, $args );
  174. $url = esc_url( 'https://player.vimeo.com/video/' . $attr['id'] );
  175. // Handle autoplay and loop arguments.
  176. if (
  177. isset( $args['autoplay'] ) && '1' === $args['autoplay'] // Parsed from the embedded URL.
  178. || $attr['autoplay'] // Parsed from shortcode arguments.
  179. || in_array( 'autoplay', $atts, true ) // Catch the argument passed without a value.
  180. ) {
  181. $url = add_query_arg( 'autoplay', 1, $url );
  182. }
  183. if (
  184. isset( $args['loop'] ) && '1' === $args['loop'] // Parsed from the embedded URL.
  185. || $attr['loop'] // Parsed from shortcode arguments.
  186. || in_array( 'loop', $atts, true ) // Catch the argument passed without a value.
  187. ) {
  188. $url = add_query_arg( 'loop', 1, $url );
  189. }
  190. if (
  191. class_exists( 'Jetpack_AMP_Support' )
  192. && Jetpack_AMP_Support::is_amp_request()
  193. ) {
  194. $html = sprintf(
  195. '<amp-vimeo data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-vimeo>',
  196. esc_attr( $attr['id'] ),
  197. absint( $width ),
  198. absint( $height )
  199. );
  200. } else {
  201. $html = sprintf(
  202. '<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>',
  203. esc_url( $url ),
  204. esc_attr( $width ),
  205. esc_attr( $height )
  206. );
  207. }
  208. /**
  209. * Filter the Vimeo player HTML.
  210. *
  211. * @module shortcodes
  212. *
  213. * @since 1.2.3
  214. *
  215. * @param string $html Embedded Vimeo player HTML.
  216. */
  217. $html = apply_filters( 'video_embed_html', $html );
  218. return $html;
  219. }
  220. add_shortcode( 'vimeo', 'vimeo_shortcode' );
  221. /**
  222. * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
  223. *
  224. * @since 3.9
  225. *
  226. * @param array $matches Regex partial matches against the URL passed.
  227. * @param array $attr Attributes received in embed response.
  228. * @param array $url Requested URL to be embedded.
  229. *
  230. * @return string Return output of Vimeo shortcode with the proper markup.
  231. */
  232. function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
  233. return vimeo_shortcode( array( $url ) );
  234. }
  235. /**
  236. * For bare URLs on their own line of the form
  237. * http://vimeo.com/12345
  238. *
  239. * @since 3.9
  240. *
  241. * @uses wpcom_vimeo_embed_url
  242. */
  243. function wpcom_vimeo_embed_url_init() {
  244. wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(.+\.)?vimeo\.com/#i', 'wpcom_vimeo_embed_url' );
  245. }
  246. /*
  247. * Register handler to modify Vimeo embeds using Jetpack's shortcode output.
  248. * This does not happen on WordPress.com, since embeds are handled by core there.
  249. */
  250. if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
  251. add_action( 'init', 'wpcom_vimeo_embed_url_init' );
  252. }
  253. /**
  254. * Transform a Vimeo embed iFrame into a Vimeo shortcode.
  255. *
  256. * @param string $content Post content.
  257. */
  258. function vimeo_embed_to_shortcode( $content ) {
  259. if ( ! is_string( $content ) || false === stripos( $content, 'player.vimeo.com/video/' ) ) {
  260. return $content;
  261. }
  262. $regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
  263. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  264. foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
  265. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  266. continue;
  267. }
  268. foreach ( $matches as $match ) {
  269. $id = (int) $match[2];
  270. $params = $match[3];
  271. if ( 'regexp_ent' === $reg ) {
  272. $params = html_entity_decode( $params );
  273. }
  274. $params = wp_kses_hair( $params, array( 'http' ) );
  275. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  276. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  277. $wh = '';
  278. if ( $width && $height ) {
  279. $wh = ' w=' . $width . ' h=' . $height;
  280. }
  281. $shortcode = '[vimeo ' . $id . $wh . ']';
  282. $content = str_replace( $match[0], $shortcode, $content );
  283. }
  284. }
  285. return $content;
  286. }
  287. add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
  288. /**
  289. * Replaces shortcodes and plain-text URLs to Vimeo videos with Vimeo embeds.
  290. * Covers shortcode usage [vimeo 1234] | [vimeo https://vimeo.com/1234] | [vimeo http://vimeo.com/1234]
  291. * Or plain text URLs https://vimeo.com/1234 | vimeo.com/1234 | //vimeo.com/1234
  292. * Links are left intact.
  293. *
  294. * @since 3.7.0
  295. * @since 3.9.5 One regular expression matches shortcodes and plain URLs.
  296. *
  297. * @param string $content HTML content.
  298. *
  299. * @return string The content with embeds instead of URLs
  300. */
  301. function vimeo_link( $content ) {
  302. /**
  303. * [vimeo 12345]
  304. * [vimeo http://vimeo.com/12345]
  305. */
  306. $shortcode = '(?:\[vimeo\s+[^0-9]*)([0-9]+)(?:\])';
  307. /**
  308. * Regex to look for a Vimeo link.
  309. *
  310. * - http://vimeo.com/12345
  311. * - https://vimeo.com/12345
  312. * - //vimeo.com/12345
  313. * - vimeo.com/some/descender/12345
  314. *
  315. * Should not capture inside HTML attributes
  316. * [Not] <a href="vimeo.com/12345">Cool Video</a>
  317. * [Not] <a href="https://vimeo.com/12345">vimeo.com/12345</a>
  318. *
  319. * Could erroneously capture:
  320. * <a href="some.link/maybe/even/vimeo">This video (vimeo.com/12345) is teh cat's meow!</a>
  321. */
  322. $plain_url = "(?:[^'\">]?\/?(?:https?:\/\/)?vimeo\.com[^0-9]+)([0-9]+)(?:[^'\"0-9<]|$)";
  323. return jetpack_preg_replace_callback_outside_tags(
  324. sprintf( '#%s|%s#i', $shortcode, $plain_url ),
  325. 'vimeo_link_callback',
  326. $content,
  327. 'vimeo'
  328. );
  329. }
  330. /**
  331. * Callback function for the regex that replaces Vimeo URLs with Vimeo embeds.
  332. *
  333. * @since 3.7.0
  334. *
  335. * @param array $matches An array containing a Vimeo URL.
  336. * @return string The Vimeo HTML embed code.
  337. */
  338. function vimeo_link_callback( $matches ) {
  339. $id = isset( $matches[2] ) ? $matches[2] : $matches[1];
  340. if ( isset( $id ) && ctype_digit( $id ) ) {
  341. return "\n" . vimeo_shortcode( array( 'id' => $id ) ) . "\n";
  342. }
  343. return $matches[0];
  344. }
  345. if (
  346. ! is_admin()
  347. /** This filter is documented in modules/shortcodes/youtube.php */
  348. && apply_filters( 'jetpack_comments_allow_oembed', true )
  349. // No need for this on WordPress.com, this is done for multiple shortcodes at a time there.
  350. && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
  351. ) {
  352. /*
  353. * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
  354. * so the iframe gets filtered out.
  355. * Higher priority because we need it before auto-link and autop get to it
  356. */
  357. add_filter( 'comment_text', 'vimeo_link', 1 );
  358. }