| 1 |
<?php |
| 2 |
/** |
| 3 |
* Vine shortcode |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Vine embed code: |
| 8 |
* <iframe class="vine-embed" src="https://vine.co/v/bjHh0zHdgZT" width="600" height="600" frameborder="0"></iframe> |
| 9 |
* <script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script> |
| 10 |
* |
| 11 |
* URL example: |
| 12 |
* https://vine.co/v/bjHh0zHdgZT/ |
| 13 |
* |
| 14 |
* Embed shortcode examples: |
| 15 |
* [embed]https://vine.co/v/bjHh0zHdgZT[/embed] |
| 16 |
* [embed width="300"]https://vine.co/v/bjHh0zHdgZT[/embed] |
| 17 |
* [embed type="postcard" width="300"]https://vine.co/v/bjHh0zHdgZT[/embed] |
| 18 |
**/ |
| 19 |
|
| 20 |
function vine_embed_video( $matches, $attr, $url, $rawattr ) { |
| 21 |
static $vine_flag_embedded_script; |
| 22 |
|
| 23 |
$max_height = 300; |
| 24 |
$type = 'simple'; |
| 25 |
|
| 26 |
// Only allow 'postcard' or 'simple' types |
| 27 |
if ( isset( $rawattr['type'] ) && $rawattr['type'] === 'postcard' ) |
| 28 |
$type = 'postcard'; |
| 29 |
|
| 30 |
$vine_size = Jetpack::get_content_width(); |
| 31 |
|
| 32 |
// If the user enters a value for width or height, we ignore the Jetpack::get_content_width() |
| 33 |
if ( isset( $rawattr['width'] ) || isset( $rawattr['height'] ) ) { |
| 34 |
// 300 is the minimum size that Vine provides for embeds. Lower than that, the postcard embeds looks weird. |
| 35 |
$vine_size = max( $max_height, min( $attr['width'], $attr['height'] ) ); |
| 36 |
} |
| 37 |
|
| 38 |
if ( empty( $vine_size ) ) { |
| 39 |
$vine_size = $max_height; |
| 40 |
} |
| 41 |
|
| 42 |
$url = 'https://vine.co/v/' . $matches[1] . '/embed/' . $type; |
| 43 |
$vine_html = sprintf( '<iframe class="vine-embed" src="%s" width="%s" height="%s" frameborder="0"></iframe>', esc_url( $url ), (int) $vine_size, (int) $vine_size ); |
| 44 |
|
| 45 |
if ( $vine_flag_embedded_script !== true ) { |
| 46 |
$vine_html .= '<script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>'; |
| 47 |
$vine_flag_embedded_script = true; |
| 48 |
} |
| 49 |
|
| 50 |
return $vine_html; |
| 51 |
} |
| 52 |
wp_embed_register_handler( 'jetpack_vine', '#https?://vine.co/v/([a-z0-9]+).*#i', 'vine_embed_video' ); |
| 53 |
|
| 54 |
function vine_shortcode( $atts ) { |
| 55 |
global $wp_embed; |
| 56 |
|
| 57 |
if ( empty( $atts['url'] ) ) |
| 58 |
return ''; |
| 59 |
|
| 60 |
if ( ! preg_match( '#https?://vine.co/v/([a-z0-9]+).*#i', $atts['url'] ) ) |
| 61 |
return ''; |
| 62 |
|
| 63 |
return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 64 |
} |
| 65 |
add_shortcode( 'vine', 'vine_shortcode' ); |
| 66 |
|