PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.1
Jetpack – WP Security, Backup, Speed, & Growth v12.1
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 in Jetpack – WP Security, Backup, Speed, & Growth 12.1, at modules/shortcodes/ted.php

122 lines 3.3 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 wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/view/id/.+!i', 'https://www.ted.com/talks/oembed.json', true );
17 wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/[a-zA-Z\-\_]+\.html!i', 'https://www.ted.com/talks/oembed.json', true );
18
19 /**
20 * Get the unique ID of a TED video.
21 * Used in Jetpack_Media_Meta_Extractor.
22 *
23 * @param array $atts Shortcode attributes.
24 */
25 function jetpack_shortcode_get_ted_id( $atts ) {
26 return ( ! empty( $atts['id'] ) ? $atts['id'] : 0 );
27 }
28
29 /**
30 * Handle Ted Shortcode.
31 *
32 * @param array $atts Shortcode attributes.
33 */
34 function shortcode_ted( $atts ) {
35 global $wp_embed;
36
37 $defaults = array(
38 'id' => '',
39 'width' => '',
40 'height' => '',
41 'lang' => 'en',
42 );
43 $atts = shortcode_atts( $defaults, $atts, 'ted' );
44
45 if ( empty( $atts['id'] ) ) {
46 return '<!-- Missing TED ID -->';
47 }
48
49 $url = '';
50 if ( preg_match( '#^[\d]+$#', $atts['id'], $matches ) ) {
51 $url = 'https://ted.com/talks/view/id/' . $matches[0];
52 } elseif ( preg_match( '#^https?://(www\.)?ted\.com/talks/view/id/[0-9]+$#', $atts['id'], $matches ) ) {
53 $url = set_url_scheme( $matches[0], 'https' );
54 }
55
56 unset( $atts['id'] );
57
58 $args = array();
59 $embed_size_w = get_option( 'embed_size_w' );
60
61 if ( is_numeric( $atts['width'] ) ) {
62 $args['width'] = $atts['width'];
63 } elseif ( $embed_size_w ) {
64 $args['width'] = $embed_size_w;
65 } elseif ( ! empty( $GLOBALS['content_width'] ) ) {
66 $args['width'] = (int) $GLOBALS['content_width'];
67 } else {
68 $args['width'] = 500;
69 }
70
71 // Default to a 16x9 aspect ratio if there's no height set.
72 if ( is_numeric( $atts['height'] ) ) {
73 $args['height'] = $atts['height'];
74 } else {
75 $args['height'] = $args['width'] * 0.5625;
76 }
77
78 if ( ! empty( $atts['lang'] ) ) {
79 $args['lang'] = sanitize_key( $atts['lang'] );
80 add_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10, 3 );
81 }
82 $retval = $wp_embed->shortcode( $args, $url );
83 remove_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10 );
84
85 return $retval;
86 }
87 add_shortcode( 'ted', 'shortcode_ted' );
88
89 /**
90 * Filter the request URL to also include the $lang parameter
91 *
92 * @param string $provider URL of provider that supplies the tweet we're requesting.
93 * @param string $url URL of tweet to embed.
94 * @param array $args Parameters supplied to shortcode and passed to wp_oembed_get.
95 */
96 function ted_filter_oembed_fetch_url( $provider, $url, $args ) {
97 return add_query_arg( 'lang', $args['lang'], $provider );
98 }
99
100 /**
101 * Filter the oembed html to set the sandbox attribute in the iframe
102 *
103 * @param string|false $cache The cached HTML result, stored in post meta.
104 * @param string $url The attempted embed URL.
105 *
106 * @return string|false
107 */
108 function ted_filter_oembed_amp_iframe( $cache, $url ) {
109 if ( is_string( $cache )
110 && strpos( $url, 'ted.com' )
111 ) {
112 $cache = preg_replace(
113 '/src=[\'"].*?[\'"]/',
114 '$0 sandbox="allow-popups allow-scripts allow-same-origin"',
115 $cache
116 );
117 }
118
119 return $cache;
120 }
121 add_filter( 'embed_oembed_html', 'ted_filter_oembed_amp_iframe', 10, 2 );
122