| 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 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 |
|