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