ted.php 2.3 KB

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