twitchtv.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * twitch.tv shortcode
  4. * [twitchtv url='http://www.twitch.tv/paperbat' height='378' width='620' autoplay='false']
  5. * [twitchtv url='http://www.twitch.tv/paperbat/b/323486192' height='378' width='620' autoplay='false']
  6. **/
  7. /**
  8. * (Live URL) http://www.twitch.tv/paperbat
  9. *
  10. * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;channel=paperbat" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
  11. *
  12. * (Archive URL) http://www.twitch.tv/paperbat/v/323486192
  13. *
  14. * <iframe src="https://player.twitch.tv/?autoplay=false&#038;muted=false&#038;video=v323486192" width="620" height="378" frameborder="0" scrolling="no" allowfullscreen></iframe>
  15. *
  16. * @param $atts array User supplied shortcode arguments.
  17. *
  18. * @return string HTML output of the shortcode.
  19. */
  20. function wpcom_twitchtv_shortcode( $atts ) {
  21. $attr = shortcode_atts(
  22. array(
  23. 'height' => 378,
  24. 'width' => 620,
  25. 'url' => '',
  26. 'autoplay' => 'false',
  27. 'muted' => 'false',
  28. 'time' => null
  29. ), $atts
  30. );
  31. if ( empty( $attr['url'] ) ) {
  32. return '<!-- Invalid twitchtv URL -->';
  33. }
  34. preg_match( '|^http://www.twitch.tv/([^/?]+)(/v/(\d+))?|i', $attr['url'], $match );
  35. $url_args = array(
  36. 'autoplay' => ( false !== $attr['autoplay'] && 'false' !== $attr['autoplay'] ) ? 'true' : 'false',
  37. 'muted' => ( false !== $attr['muted'] && 'false' !== $attr['muted'] ) ? 'true' : 'false',
  38. 'time' => $attr['time']
  39. );
  40. $width = intval( $attr['width'] );
  41. $height = intval( $attr['height'] );
  42. $user_id = $match[1];
  43. $video_id = 0;
  44. if ( ! empty( $match[3] ) ) {
  45. $video_id = (int) $match[3];
  46. }
  47. do_action( 'jetpack_bump_stats_extras', 'twitchtv', 'shortcode' );
  48. if ( $video_id > 0 ) {
  49. $url_args['video'] = 'v' . $video_id;
  50. } else {
  51. $url_args['channel'] = $user_id;
  52. }
  53. $url = add_query_arg( $url_args, 'https://player.twitch.tv/' );
  54. return sprintf(
  55. '<iframe src="%s" width="%d" height="%d" frameborder="0" scrolling="no" allowfullscreen></iframe>',
  56. esc_url( $url ),
  57. esc_attr( $width ),
  58. esc_attr( $height )
  59. );
  60. }
  61. add_shortcode( 'twitch', 'wpcom_twitchtv_shortcode' );
  62. add_shortcode( 'twitchtv', 'wpcom_twitchtv_shortcode' );