PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.0
Jetpack – WP Security, Backup, Speed, & Growth v16.0
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / ted.php
ted.php
140 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit( 0 );
18 }
19
20 wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/view/id/.+!i', 'https://www.ted.com/talks/oembed.json', true );
21 wp_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 */
29 function 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 */
38 function 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 }
91 add_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 */
100 function 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 */
112 function 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 }
139 add_filter( 'embed_oembed_html', 'ted_filter_oembed_amp_iframe', 10, 2 );
140