| 1 |
<?php |
| 2 |
/** |
| 3 |
* Vine shortcode |
| 4 |
* The service is now archived, but existing embeds are still accessible. |
| 5 |
* |
| 6 |
* Examples: |
| 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 |
* @package automattic/jetpack |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* Handle Vine embeds. |
| 24 |
* |
| 25 |
* @param array $matches Results after parsing the URL using the regex in wp_embed_register_handler(). |
| 26 |
* @param array $attr Embed attributes. |
| 27 |
* @param string $url The original URL that was matched by the regex. |
| 28 |
* @param array $rawattr The original unmodified attributes. |
| 29 |
* @return string The embed HTML. |
| 30 |
*/ |
| 31 |
function vine_embed_video( $matches, $attr, $url, $rawattr ) { |
| 32 |
$max_height = 300; |
| 33 |
$type = 'simple'; |
| 34 |
|
| 35 |
// Only allow 'postcard' or 'simple' types. |
| 36 |
if ( |
| 37 |
isset( $rawattr['type'] ) |
| 38 |
&& 'postcard' === $rawattr['type'] |
| 39 |
) { |
| 40 |
$type = 'postcard'; |
| 41 |
} |
| 42 |
|
| 43 |
$vine_size = Jetpack::get_content_width(); |
| 44 |
|
| 45 |
// If the user enters a value for width or height, we ignore the Jetpack::get_content_width(). |
| 46 |
if ( isset( $rawattr['width'] ) || isset( $rawattr['height'] ) ) { |
| 47 |
// 300 is the minimum size that Vine provides for embeds. Lower than that, the postcard embeds looks weird. |
| 48 |
$vine_size = max( $max_height, min( $attr['width'], $attr['height'] ) ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( empty( $vine_size ) ) { |
| 52 |
$vine_size = $max_height; |
| 53 |
} |
| 54 |
|
| 55 |
$url = 'https://vine.co/v/' . $matches[1] . '/embed/' . $type; |
| 56 |
$vine_html = sprintf( |
| 57 |
'<span class="embed-vine" style="display: block;"><iframe class="vine-embed" src="%1$s" width="%2$d" height="%3$d" frameborder="0"></iframe></span>', |
| 58 |
esc_url( $url ), |
| 59 |
(int) $vine_size, |
| 60 |
(int) $vine_size |
| 61 |
); |
| 62 |
|
| 63 |
wp_enqueue_script( |
| 64 |
'vine-embed', |
| 65 |
'https://platform.vine.co/static/scripts/embed.js', |
| 66 |
array(), |
| 67 |
JETPACK__VERSION, |
| 68 |
true |
| 69 |
); |
| 70 |
|
| 71 |
return $vine_html; |
| 72 |
} |
| 73 |
wp_embed_register_handler( 'jetpack_vine', '#https?://vine.co/v/([a-z0-9]+).*#i', 'vine_embed_video' ); |
| 74 |
|
| 75 |
/** |
| 76 |
* Display the Vine shortcode. |
| 77 |
* |
| 78 |
* @param array $atts Shortcode attributes. |
| 79 |
*/ |
| 80 |
function vine_shortcode( $atts ) { |
| 81 |
global $wp_embed; |
| 82 |
|
| 83 |
if ( empty( $atts['url'] ) ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! preg_match( '#https?://vine.co/v/([a-z0-9]+).*#i', $atts['url'] ) ) { |
| 88 |
return ''; |
| 89 |
} |
| 90 |
|
| 91 |
return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 92 |
} |
| 93 |
add_shortcode( 'vine', 'vine_shortcode' ); |
| 94 |
|