PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/jetpack/modules/shortcodes/vimeo.php

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