| 1 |
<?php |
| 2 |
/* |
| 3 |
* TED Player embed code |
| 4 |
* http://www.ted.com |
| 5 |
* |
| 6 |
* http://www.ted.com/talks/view/id/210 |
| 7 |
* http://www.ted.com/talks/marc_goodman_a_vision_of_crimes_in_the_future.html |
| 8 |
* [ted id="210" lang="en"] |
| 9 |
* [ted id="http://www.ted.com/talks/view/id/210" lang="en"] |
| 10 |
* [ted id=1539 lang=fr width=560 height=315] |
| 11 |
*/ |
| 12 |
|
| 13 |
wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/view/id/.+!i', 'http://www.ted.com/talks/oembed.json', true ); |
| 14 |
wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/[a-zA-Z\-\_]+\.html!i', 'http://www.ted.com/talks/oembed.json', true ); |
| 15 |
|
| 16 |
function jetpack_shortcode_get_ted_id( $atts ) { |
| 17 |
return ( ! empty( $atts['id'] ) ? $atts['id'] : 0 ); |
| 18 |
} |
| 19 |
|
| 20 |
add_shortcode( 'ted', 'shortcode_ted' ); |
| 21 |
function shortcode_ted( $atts ) { |
| 22 |
global $wp_embed; |
| 23 |
|
| 24 |
$defaults = array( |
| 25 |
'id' => '', |
| 26 |
'width' => '', |
| 27 |
'height' => '', |
| 28 |
'lang' => 'en', |
| 29 |
); |
| 30 |
$atts = shortcode_atts( $defaults, $atts, 'ted' ); |
| 31 |
|
| 32 |
if ( empty( $atts['id'] ) ) { |
| 33 |
return '<!-- Missing TED ID -->'; |
| 34 |
} |
| 35 |
|
| 36 |
$url = ''; |
| 37 |
if ( preg_match( '#^[\d]+$#', $atts['id'], $matches ) ) { |
| 38 |
$url = 'http://ted.com/talks/view/id/' . $matches[0]; |
| 39 |
} elseif ( preg_match( '#^https?://(www\.)?ted\.com/talks/view/id/[0-9]+$#', $atts['id'], $matches ) ) { |
| 40 |
$url = $matches[0]; |
| 41 |
} |
| 42 |
|
| 43 |
unset( $atts['id'] ); |
| 44 |
|
| 45 |
$args = array(); |
| 46 |
if ( is_numeric( $atts['width'] ) ) { |
| 47 |
$args['width'] = $atts['width']; |
| 48 |
} else if ( $embed_size_w = get_option( 'embed_size_w' ) ) { |
| 49 |
$args['width'] = $embed_size_w; |
| 50 |
} else if ( ! empty( $GLOBALS['content_width'] ) ) { |
| 51 |
$args['width'] = (int) $GLOBALS['content_width']; |
| 52 |
} else { |
| 53 |
$args['width'] = 500; |
| 54 |
} |
| 55 |
|
| 56 |
// Default to a 16x9 aspect ratio if there's no height set |
| 57 |
if ( is_numeric( $atts['height'] ) ) { |
| 58 |
$args['height'] = $atts['height']; |
| 59 |
} else { |
| 60 |
$args['height'] = $args['width'] * 0.5625; |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! empty( $atts['lang'] ) ) { |
| 64 |
$args['lang'] = sanitize_key( $atts['lang'] ); |
| 65 |
add_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10, 3 ); |
| 66 |
} |
| 67 |
$retval = $wp_embed->shortcode( $args, $url ); |
| 68 |
remove_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10 ); |
| 69 |
|
| 70 |
return $retval; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Filter the request URL to also include the $lang parameter |
| 75 |
*/ |
| 76 |
function ted_filter_oembed_fetch_url( $provider, $url, $args ) { |
| 77 |
return add_query_arg( 'lang', $args['lang'], $provider ); |
| 78 |
} |
| 79 |
|