tweet.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Tweet shortcode.
  4. * Params map to key value pairs, and all but tweet are optional:
  5. * tweet = id or permalink url* (Required)
  6. * align = none|left|right|center
  7. * width = number in pixels example: width="300"
  8. * lang = en|fr|de|ko|etc... language country code.
  9. * hide_thread = true | false **
  10. * hide_media = true | false **
  11. *
  12. * Basic:
  13. * [tweet https://twitter.com/jack/statuses/20 width="350"]
  14. *
  15. * More parameters and another tweet syntax admitted:
  16. * [tweet tweet="https://twitter.com/jack/statuses/20" align="left" width="350" align="center" lang="es"]
  17. */
  18. add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) );
  19. class Jetpack_Tweet {
  20. static $provider_args;
  21. /**
  22. * Parse shortcode arguments and render its output.
  23. *
  24. * @since 4.5.0
  25. *
  26. * @param array $atts Shortcode parameters.
  27. *
  28. * @return string
  29. */
  30. static public function jetpack_tweet_shortcode( $atts ) {
  31. $default_atts = array(
  32. 'tweet' => '',
  33. 'align' => 'none',
  34. 'width' => '',
  35. 'lang' => 'en',
  36. 'hide_thread' => 'false',
  37. 'hide_media' => 'false',
  38. );
  39. $attr = shortcode_atts( $default_atts, $atts );
  40. self::$provider_args = $attr;
  41. // figure out the tweet id for the requested tweet
  42. // supporting both omitted attributes and tweet="tweet_id"
  43. // and supporting both an id and a URL
  44. if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) {
  45. $attr['tweet'] = $atts[0];
  46. }
  47. if ( ctype_digit( $attr['tweet'] ) ) {
  48. $id = 'https://twitter.com/jetpack/status/' . $attr['tweet'];
  49. } else {
  50. preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
  51. if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) {
  52. $id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] );
  53. } else {
  54. return '<!-- Invalid tweet id -->';
  55. }
  56. }
  57. // Add shortcode arguments to provider URL
  58. add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
  59. // Fetch tweet
  60. $output = wp_oembed_get( $id, $atts );
  61. // Clean up filter
  62. remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
  63. // Add Twitter widgets.js script to the footer.
  64. add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
  65. /** This action is documented in modules/widgets/social-media-icons.php */
  66. do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
  67. return $output;
  68. }
  69. /**
  70. * Adds parameters to URL used to fetch the tweet.
  71. *
  72. * @since 4.5.0
  73. *
  74. * @param string $provider URL of provider that supplies the tweet we're requesting.
  75. * @param string $url URL of tweet to embed.
  76. * @param array $args Parameters supplied to shortcode and passed to wp_oembed_get
  77. *
  78. * @return string
  79. */
  80. static public function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) {
  81. foreach ( self::$provider_args as $key => $value ) {
  82. switch ( $key ) {
  83. case 'align':
  84. case 'lang':
  85. case 'hide_thread':
  86. case 'hide_media':
  87. $provider = add_query_arg( $key, $value, $provider );
  88. break;
  89. }
  90. }
  91. // Disable script since we're enqueing it in our own way in the footer
  92. $provider = add_query_arg( 'omit_script', 'true', $provider );
  93. // Twitter doesn't support maxheight so don't send it
  94. $provider = remove_query_arg( 'maxheight', $provider );
  95. /**
  96. * Filter the Twitter Partner ID.
  97. *
  98. * @module shortcodes
  99. *
  100. * @since 4.6.0
  101. *
  102. * @param string $partner_id Twitter partner ID.
  103. */
  104. $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
  105. // Add Twitter partner ID to track embeds from Jetpack
  106. if ( ! empty( $partner ) ) {
  107. $provider = add_query_arg( 'partner', $partner, $provider );
  108. }
  109. return $provider;
  110. }
  111. /**
  112. * Enqueue front end assets.
  113. *
  114. * @since 4.5.0
  115. */
  116. static public function jetpack_tweet_shortcode_script() {
  117. if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
  118. wp_register_script( 'twitter-widgets', 'https://platform.twitter.com/widgets.js', array(), JETPACK__VERSION, true );
  119. wp_print_scripts( 'twitter-widgets' );
  120. }
  121. }
  122. } // class end