Geen omschrijving

tweet.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  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. * @package automattic/jetpack
  19. */
  20. add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) );
  21. /**
  22. * Tweet Shortcode class.
  23. */
  24. class Jetpack_Tweet {
  25. /**
  26. * Array of arguments about a tweet.
  27. *
  28. * @var array
  29. */
  30. public static $provider_args;
  31. /**
  32. * Parse shortcode arguments and render its output.
  33. *
  34. * @since 4.5.0
  35. *
  36. * @param array $atts Shortcode parameters.
  37. *
  38. * @return string
  39. */
  40. public static function jetpack_tweet_shortcode( $atts ) {
  41. global $wp_embed;
  42. $default_atts = array(
  43. 'tweet' => '',
  44. 'align' => 'none',
  45. 'width' => '',
  46. 'lang' => 'en',
  47. 'hide_thread' => 'false',
  48. 'hide_media' => 'false',
  49. );
  50. $attr = shortcode_atts( $default_atts, $atts );
  51. self::$provider_args = $attr;
  52. /*
  53. * figure out the tweet id for the requested tweet
  54. * supporting both omitted attributes and tweet="tweet_id"
  55. * and supporting both an id and a URL
  56. */
  57. if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) {
  58. $attr['tweet'] = $atts[0];
  59. }
  60. if ( ctype_digit( $attr['tweet'] ) ) {
  61. $id = 'https://twitter.com/jetpack/status/' . $attr['tweet'];
  62. $tweet_id = (int) $attr['tweet'];
  63. } else {
  64. preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
  65. if ( isset( $urlbits[5] ) && (int) $urlbits[5] ) {
  66. $id = 'https://twitter.com/' . $urlbits[3] . '/status/' . (int) $urlbits[5];
  67. $tweet_id = (int) $urlbits[5];
  68. } else {
  69. return '<!-- Invalid tweet id -->';
  70. }
  71. }
  72. /*
  73. * Fetch tweet.
  74. *
  75. * On WordPress.com, we also cache tweets for better performance and less requests.
  76. */
  77. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  78. /*
  79. * See if we have the tweet stored in our tweet store
  80. * if not get_tweet_store queues up a job to request
  81. */
  82. $data = get_tweet_store( $tweet_id );
  83. if ( $data ) {
  84. $tweet_handler = new Tweet_Handler();
  85. /*
  86. * Replace Unicode characters with ther entities like Blackbird Pie v 0.3.2 did
  87. * to store tweets from other languages (important for non-english bloggers)
  88. */
  89. $data->text = $tweet_handler->unicode_replace_entities( $data->text );
  90. $data->user->screen_name = $tweet_handler->unicode_replace_entities( $data->user->screen_name );
  91. $data->user->name = $tweet_handler->unicode_replace_entities( $data->user->name );
  92. $tweet = esc_html( $data->text );
  93. $tweet = $tweet_handler->expand_tco_links( $tweet, $data );
  94. $tweet = $tweet_handler->autolink( $tweet );
  95. $screen_name = esc_html( $data->user->screen_name );
  96. $name = esc_html( $data->user->name );
  97. $url = 'https://twitter.com/' . $screen_name . '/status/' . (int) $data->id;
  98. // Only show the user's real name if they set it to something different from their screename.
  99. if ( $screen_name !== $name ) {
  100. $real_name = '<br />' . $name;
  101. } else {
  102. $real_name = '<br />&nbsp;';
  103. }
  104. $time = strtotime( $data->created_at );
  105. $human_readable = gmdate( 'F d, Y', $time );
  106. $data_datetime = gmdate( 'Y-m-d\TH:i:sP', $time );
  107. /*
  108. * Additional params.
  109. */
  110. // align (float).
  111. $extra_classes = '';
  112. if ( in_array( $attr['align'], array( 'left', 'right', 'center' ), true ) ) {
  113. $extra_classes = ' tw-align-' . $attr['align'];
  114. }
  115. if ( 'true' === $attr['hide_thread'] ) {
  116. $extra_classes .= ' tw-hide-thread';
  117. }
  118. if ( 'true' === $attr['hide_media'] ) {
  119. $extra_classes .= ' tw-hide-media';
  120. }
  121. // lang.
  122. $lang = substr( $attr['lang'], 0, 2 );
  123. if ( empty( $lang ) ) {
  124. $lang = 'en';
  125. }
  126. // width.
  127. $width_html = '';
  128. $width = (int) $attr['width'];
  129. if ( $width > 100 ) {
  130. $width_html = ' width="' . esc_attr( $width ) . '"';
  131. }
  132. // in reply to id (conversation tweets).
  133. $in_reply_to_html = '';
  134. $in_reply_to = (int) $data->in_reply_to_status_id;
  135. if ( ! empty( $in_reply_to ) && 'false' === $attr['hide_thread'] ) {
  136. $in_reply_to_html = ' data-in-reply-to="' . esc_attr( $in_reply_to ) . '"';
  137. }
  138. // Generate the HTML output.
  139. $output = sprintf(
  140. '<blockquote class="twitter-tweet%1$s"%2$s%3$s lang="%4$s"><p>%5$s</p>&mdash; %6$s (@%7$s) <a href="%8$s" data-datetime="%9$s">%10$s</a></blockquote>',
  141. esc_attr( $extra_classes ),
  142. $width_html,
  143. $in_reply_to_html,
  144. esc_attr( $lang ),
  145. $tweet,
  146. wp_kses( $real_name, array( 'br' => array() ) ),
  147. esc_html( $screen_name ),
  148. esc_url( $url ),
  149. esc_attr( $data_datetime ),
  150. esc_html( $human_readable )
  151. );
  152. } else {
  153. /**
  154. * Filter the default display when a tweet is not available in the store.
  155. * Not available in Jetpack.
  156. *
  157. * @module shortcodes
  158. *
  159. * @since 5.1.0
  160. *
  161. * @param string $message Default display when a tweet is not available.
  162. * @param string $id Twitter URL.
  163. * @param array $attr Shortcode attributes.
  164. */
  165. return apply_filters( 'tweet_shortcode_pending_tweet', '', $id, $attr );
  166. }
  167. } else {
  168. // Add shortcode arguments to provider URL.
  169. add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
  170. /*
  171. * In Jetpack, we use $wp_embed->shortcode() to return the tweet output.
  172. * @see https://github.com/Automattic/jetpack/pull/11173
  173. */
  174. $output = $wp_embed->shortcode( $atts, $id );
  175. // Clean up filter.
  176. remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
  177. }
  178. /** This action is documented in modules/widgets/social-media-icons.php */
  179. do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
  180. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  181. $width = ! empty( $attr['width'] ) ? $attr['width'] : 600;
  182. $height = 480;
  183. $output = sprintf(
  184. '<amp-twitter data-tweetid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-twitter>',
  185. esc_attr( $tweet_id ),
  186. absint( $width ),
  187. absint( $height )
  188. );
  189. } else {
  190. // Add Twitter widgets.js script to the footer.
  191. add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
  192. }
  193. return $output;
  194. }
  195. /**
  196. * Adds parameters to URL used to fetch the tweet.
  197. *
  198. * @since 4.5.0
  199. *
  200. * @param string $provider URL of provider that supplies the tweet we're requesting.
  201. * @param string $url URL of tweet to embed.
  202. * @param array $args Parameters supplied to shortcode and passed to wp_oembed_get.
  203. *
  204. * @return string
  205. */
  206. public static function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  207. foreach ( self::$provider_args as $key => $value ) {
  208. switch ( $key ) {
  209. case 'align':
  210. case 'lang':
  211. case 'hide_thread':
  212. case 'hide_media':
  213. $provider = add_query_arg( $key, $value, $provider );
  214. break;
  215. }
  216. }
  217. // Disable script since we're enqueing it in our own way in the footer.
  218. $provider = add_query_arg( 'omit_script', 'true', $provider );
  219. // Twitter doesn't support maxheight so don't send it.
  220. $provider = remove_query_arg( 'maxheight', $provider );
  221. /**
  222. * Filter the Twitter Partner ID.
  223. *
  224. * @module shortcodes
  225. *
  226. * @since 4.6.0
  227. *
  228. * @param string $partner_id Twitter partner ID.
  229. */
  230. $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
  231. // Add Twitter partner ID to track embeds from Jetpack.
  232. if ( ! empty( $partner ) ) {
  233. $provider = add_query_arg( 'partner', $partner, $provider );
  234. }
  235. return $provider;
  236. }
  237. /**
  238. * Enqueue front end assets.
  239. *
  240. * @since 4.5.0
  241. */
  242. public static function jetpack_tweet_shortcode_script() {
  243. if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
  244. wp_register_script( 'twitter-widgets', 'https://platform.twitter.com/widgets.js', array(), JETPACK__VERSION, true );
  245. wp_print_scripts( 'twitter-widgets' );
  246. }
  247. }
  248. } // class end