Jetpack_Tweet

The WordPress Core Jetpack Tweet class.

Defined (1)

The class is defined in the following location(s).

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