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