| 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 |
} |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Convert a Vimeo shortcode into an embed code. |
| 29 |
* |
| 30 |
* @param array $atts An array of shortcode attributes. |
| 31 |
* @return string The embed code for the Vimeo video. |
| 32 |
*/ |
| 33 |
function vimeo_shortcode( $atts ) { |
| 34 |
global $content_width; |
| 35 |
|
| 36 |
extract( array_map( 'intval', shortcode_atts( array( |
| 37 |
'id' => 0, |
| 38 |
'width' => 400, |
| 39 |
'height' => 300, |
| 40 |
'autoplay' => 0, |
| 41 |
'loop' => 0, |
| 42 |
), $atts, 'vimeo' ) ) ); |
| 43 |
|
| 44 |
if ( isset( $atts[0] ) ) { |
| 45 |
$id = jetpack_shortcode_get_vimeo_id( $atts ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! $id ) return "<!-- vimeo error: not a vimeo video -->"; |
| 49 |
|
| 50 |
// [vimeo 141358 h=500&w=350] |
| 51 |
$params = shortcode_new_to_old_params( $atts ); // h=500&w=350 |
| 52 |
$params = str_replace( array( '&', '&' ), '&', $params ); |
| 53 |
parse_str( $params, $args ); |
| 54 |
|
| 55 |
if ( isset( $args['w'] ) ) { |
| 56 |
$width = (int) $args['w']; |
| 57 |
|
| 58 |
if ( ! isset( $args['h'] ) ) { |
| 59 |
// The case where w=300 is specified without h=200, otherwise $height |
| 60 |
// will always equal the default of 300, no matter what w was set to. |
| 61 |
$height = round( ( $width / 640 ) * 360 ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( isset( $args['h'] ) ) { |
| 66 |
$height = (int) $args['h']; |
| 67 |
|
| 68 |
if ( ! isset( $args['w'] ) ) { |
| 69 |
$width = round( ( $height / 360 ) * 640 ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! $width ) { |
| 74 |
$width = absint( $content_width ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! $height ) { |
| 78 |
$height = round( ( $width / 640 ) * 360 ); |
| 79 |
} |
| 80 |
|
| 81 |
$url = esc_url( set_url_scheme( "http://player.vimeo.com/video/$id" ) ); |
| 82 |
|
| 83 |
// $args['autoplay'] is parsed from the embedded url. |
| 84 |
// $autoplay is parsed from shortcode arguments. |
| 85 |
// in_array( 'autoplay', $atts ) catches the argument passed without a value. |
| 86 |
if ( ! empty( $args['autoplay'] ) || ! empty( $autoplay ) || in_array( 'autoplay', $atts ) ) { |
| 87 |
$url = add_query_arg( 'autoplay', 1, $url ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! empty( $args['loop'] ) || ! empty( $loop ) || in_array( 'loop', $atts ) ) { |
| 91 |
$url = add_query_arg( 'loop', 1, $url ); |
| 92 |
} |
| 93 |
|
| 94 |
$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 ); |
| 95 |
$html = apply_filters( 'video_embed_html', $html ); |
| 96 |
return $html; |
| 97 |
} |
| 98 |
|
| 99 |
add_shortcode( 'vimeo', 'vimeo_shortcode' ); |
| 100 |
|
| 101 |
function vimeo_embed_to_shortcode( $content ) { |
| 102 |
if ( false === stripos( $content, 'player.vimeo.com/video/' ) ) |
| 103 |
return $content; |
| 104 |
|
| 105 |
$regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i'; |
| 106 |
$regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
| 107 |
|
| 108 |
foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { |
| 109 |
if ( !preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) |
| 110 |
continue; |
| 111 |
|
| 112 |
foreach ( $matches as $match ) { |
| 113 |
$id = (int) $match[2]; |
| 114 |
|
| 115 |
$params = $match[3]; |
| 116 |
|
| 117 |
if ( 'regexp_ent' == $reg ) |
| 118 |
$params = html_entity_decode( $params ); |
| 119 |
|
| 120 |
$params = wp_kses_hair( $params, array( 'http' ) ); |
| 121 |
|
| 122 |
$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
| 123 |
$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; |
| 124 |
|
| 125 |
$wh = ''; |
| 126 |
if ( $width && $height ) |
| 127 |
$wh = ' w=' . $width . ' h=' . $height; |
| 128 |
|
| 129 |
$shortcode = '[vimeo ' . $id . $wh . ']'; |
| 130 |
$content = str_replace( $match[0], $shortcode, $content ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $content; |
| 135 |
} |
| 136 |
|
| 137 |
add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' ); |
| 138 |
|