Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.80% covered (warning)
67.80%
40 / 59
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
jetpack_shortcode_get_ted_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
shortcode_ted
87.88% covered (warning)
87.88%
29 / 33
0.00% covered (danger)
0.00%
0 / 1
9.14
ted_filter_oembed_fetch_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
ted_filter_oembed_amp_iframe
61.11% covered (warning)
61.11%
11 / 18
0.00% covered (danger)
0.00%
0 / 1
4.94
1<?php
2/**
3 * TED Player embed code
4 * http://www.ted.com
5 *
6 * Examples:
7 * http://www.ted.com/talks/view/id/210
8 * http://www.ted.com/talks/marc_goodman_a_vision_of_crimes_in_the_future.html
9 * [ted id="210" lang="en"]
10 * [ted id="http://www.ted.com/talks/view/id/210" lang="en"]
11 * [ted id=1539 lang=fr width=560 height=315]
12 *
13 * @package automattic/jetpack
14 */
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/view/id/.+!i', 'https://www.ted.com/talks/oembed.json', true );
21wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/[a-zA-Z\-\_]+\.html!i', 'https://www.ted.com/talks/oembed.json', true );
22
23/**
24 * Get the unique ID of a TED video.
25 * Used in Jetpack_Media_Meta_Extractor.
26 *
27 * @param array $atts Shortcode attributes.
28 */
29function jetpack_shortcode_get_ted_id( $atts ) {
30    return ( ! empty( $atts['id'] ) ? $atts['id'] : 0 );
31}
32
33/**
34 * Handle Ted Shortcode.
35 *
36 * @param array $atts Shortcode attributes.
37 */
38function shortcode_ted( $atts ) {
39    global $wp_embed;
40
41    $defaults = array(
42        'id'     => '',
43        'width'  => '',
44        'height' => '',
45        'lang'   => 'en',
46    );
47    $atts     = shortcode_atts( $defaults, $atts, 'ted' );
48
49    if ( empty( $atts['id'] ) ) {
50        return '<!-- Missing TED ID -->';
51    }
52
53    $url = '';
54    if ( preg_match( '#^[\d]+$#', $atts['id'], $matches ) ) {
55        $url = 'https://ted.com/talks/view/id/' . $matches[0];
56    } elseif ( preg_match( '#^https?://(www\.)?ted\.com/talks/view/id/[0-9]+$#', $atts['id'], $matches ) ) {
57        $url = set_url_scheme( $matches[0], 'https' );
58    }
59
60    unset( $atts['id'] );
61
62    $args         = array();
63    $embed_size_w = get_option( 'embed_size_w' );
64
65    if ( is_numeric( $atts['width'] ) ) {
66        $args['width'] = $atts['width'];
67    } elseif ( $embed_size_w ) {
68        $args['width'] = $embed_size_w;
69    } elseif ( ! empty( $GLOBALS['content_width'] ) ) {
70        $args['width'] = (int) $GLOBALS['content_width'];
71    } else {
72        $args['width'] = 500;
73    }
74
75    // Default to a 16x9 aspect ratio if there's no height set.
76    if ( is_numeric( $atts['height'] ) ) {
77        $args['height'] = $atts['height'];
78    } else {
79        $args['height'] = $args['width'] * 0.5625;
80    }
81
82    if ( ! empty( $atts['lang'] ) ) {
83        $args['lang'] = sanitize_key( $atts['lang'] );
84        add_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10, 3 );
85    }
86    $retval = $wp_embed->shortcode( $args, $url );
87    remove_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10 );
88
89    return $retval;
90}
91add_shortcode( 'ted', 'shortcode_ted' );
92
93/**
94 * Filter the request URL to also include the $lang parameter
95 *
96 * @param string $provider URL of provider that supplies the tweet we're requesting.
97 * @param string $url      URL of tweet to embed.
98 * @param array  $args     Parameters supplied to shortcode and passed to wp_oembed_get.
99 */
100function ted_filter_oembed_fetch_url( $provider, $url, $args ) {
101    return add_query_arg( 'lang', $args['lang'], $provider );
102}
103
104/**
105 * Filter the oembed html to set the sandbox attribute in the iframe
106 *
107 * @param string|false $cache The cached HTML result, stored in post meta.
108 * @param string       $url   The attempted embed URL.
109 *
110 * @return string|false
111 */
112function ted_filter_oembed_amp_iframe( $cache, $url ) {
113    if ( ! is_string( $cache ) ) {
114        return $cache;
115    }
116
117    $host = wp_parse_url( $url, PHP_URL_HOST );
118    if ( ! $host ) {
119        return $cache;
120    }
121
122    $allowed_hosts = array(
123        'ted.com',
124        'www.ted.com',
125        'embed.ted.com',
126    );
127
128    $host = strtolower( $host );
129    if ( in_array( $host, $allowed_hosts, true ) ) {
130        $cache = preg_replace(
131            '/src=[\'"].*?[\'"]/',
132            '$0 sandbox="allow-popups allow-scripts allow-same-origin"',
133            $cache
134        );
135    }
136
137    return $cache;
138}
139add_filter( 'embed_oembed_html', 'ted_filter_oembed_amp_iframe', 10, 2 );