Brak opisu

twitchtv.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Twitch.tv shortcode
  4. *
  5. * Examples:
  6. * [twitchtv url='https://www.twitch.tv/paperbat' height='378' width='620' autoplay='false']
  7. * [twitchtv url='https://www.twitch.tv/paperbat/b/323486192' height='378' width='620' autoplay='false']
  8. *
  9. * @package automattic/jetpack
  10. */
  11. /**
  12. * (Live URL) https://www.twitch.tv/paperbat
  13. *
  14. * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;channel=paperbat" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
  15. *
  16. * (Archive URL) https://www.twitch.tv/paperbat/v/323486192
  17. *
  18. * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;video=v323486192" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
  19. *
  20. * @param array $atts User supplied shortcode arguments.
  21. *
  22. * @return string HTML output of the shortcode.
  23. */
  24. function wpcom_twitchtv_shortcode( $atts ) {
  25. $attr = shortcode_atts(
  26. array(
  27. 'height' => 378,
  28. 'width' => 620,
  29. 'url' => '',
  30. 'autoplay' => 'false',
  31. 'muted' => 'false',
  32. 'time' => null,
  33. ),
  34. $atts
  35. );
  36. if ( empty( $attr['url'] ) ) {
  37. return '<!-- Invalid twitchtv URL -->';
  38. }
  39. preg_match( '|^https?://www.twitch.tv/([^/?]+)(/v/(\d+))?|i', $attr['url'], $match );
  40. $url_args = array(
  41. 'autoplay' => ( false !== $attr['autoplay'] && 'false' !== $attr['autoplay'] ) ? 'true' : 'false',
  42. 'muted' => ( false !== $attr['muted'] && 'false' !== $attr['muted'] ) ? 'true' : 'false',
  43. 'time' => $attr['time'],
  44. );
  45. $width = (int) $attr['width'];
  46. $height = (int) $attr['height'];
  47. $user_id = $match[1];
  48. $video_id = 0;
  49. if ( ! empty( $match[3] ) ) {
  50. $video_id = (int) $match[3];
  51. }
  52. do_action( 'jetpack_bump_stats_extras', 'twitchtv', 'shortcode' );
  53. if ( $video_id > 0 ) {
  54. $url_args['video'] = 'v' . $video_id;
  55. } else {
  56. $url_args['channel'] = $user_id;
  57. }
  58. // See https://discuss.dev.twitch.tv/t/twitch-embedded-player-updates-in-2020/23956.
  59. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  60. $url_args['parent'] = isset( $_SERVER['HTTP_HOST'] )
  61. ? rawurlencode( wp_unslash( $_SERVER['HTTP_HOST'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  62. : '';
  63. $url = add_query_arg( $url_args, 'https://player.twitch.tv/' );
  64. return sprintf(
  65. '<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen sandbox="allow-popups allow-scripts allow-same-origin allow-presentation"></iframe>',
  66. esc_url( $url ),
  67. esc_attr( $width ),
  68. esc_attr( $height )
  69. );
  70. }
  71. add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' );
  72. add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' );