Нет описания

ted.php 3.3KB

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