Share_Twitter

The Jetpack by WordPress.com Share Twitter class.

Defined (1)

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

/modules/sharedaddy/sharing-sources.php  
  1. class Share_Twitter extends Sharing_Source { 
  2. public $shortname = 'twitter'; 
  3. public $genericon = '\f202'; 
  4. // 'https://dev.twitter.com/rest/reference/get/help/configuration' ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23 
  5. public $short_url_length = 24; 
  6.  
  7. public function __construct( $id, array $settings ) { 
  8. parent::__construct( $id, $settings ); 
  9.  
  10. if ( 'official' == $this->button_style ) 
  11. $this->smart = true; 
  12. else 
  13. $this->smart = false; 
  14.  
  15. public function get_name() { 
  16. return __( 'Twitter', 'jetpack' ); 
  17.  
  18. function sharing_twitter_via( $post ) { 
  19. /** 
  20. * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag. 
  21. * @module sharedaddy 
  22. * @since 3.0.0 
  23. * @param string $string Twitter Username. 
  24. * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags. 
  25. */ 
  26. $twitter_site_tag_value = apply_filters( 'jetpack_twitter_cards_site_tag', '', array() ); 
  27.  
  28. /** 
  29. * Hack to remove the unwanted behavior of adding 'via @jetpack' which 
  30. * was introduced with the adding of the Twitter cards. 
  31. * This should be a temporary solution until a better method is setup. 
  32. */ 
  33. if( 'jetpack' == $twitter_site_tag_value ) { 
  34. $twitter_site_tag_value = ''; 
  35.  
  36. /** 
  37. * Filters the Twitter username used as "via" in the Twitter sharing button. 
  38. * @module sharedaddy 
  39. * @since 1.7.0 
  40. * @param string $twitter_site_tag_value Twitter Username. 
  41. * @param int $post->ID Post ID. 
  42. */ 
  43. $twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID ); 
  44.  
  45. // Strip out anything other than a letter, number, or underscore. 
  46. // This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle. 
  47. return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value ); 
  48.  
  49. public function get_related_accounts( $post ) { 
  50. /** 
  51. * Filter the list of related Twitter accounts added to the Twitter sharing button. 
  52. * @module sharedaddy 
  53. * @since 1.7.0 
  54. * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description' 
  55. * @param int $post->ID Post ID. 
  56. */ 
  57. $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID ); 
  58.  
  59. // Example related string: account1, account2:Account 2 description, account3 
  60. $related = array(); 
  61.  
  62. foreach ( $related_accounts as $related_account_username => $related_account_description ) { 
  63. // Join the description onto the end of the username 
  64. if ( $related_account_description ) 
  65. $related_account_username .= ':' . $related_account_description; 
  66.  
  67. $related[] = $related_account_username; 
  68.  
  69. return implode( ', ', $related ); 
  70.  
  71. public function get_display( $post ) { 
  72. $via = $this->sharing_twitter_via( $post ); 
  73.  
  74. if ( $via ) { 
  75. $via = '&via=' . rawurlencode( $via ); 
  76. } else { 
  77. $via = ''; 
  78.  
  79. $related = $this->get_related_accounts( $post ); 
  80. if ( ! empty( $related ) && $related !== $via ) { 
  81. $via .= '&related=' . rawurlencode( $related ); 
  82.  
  83. if ( $this->smart ) { 
  84. $share_url = $this->get_share_url( $post->ID ); 
  85. $post_title = $this->get_share_title( $post->ID ); 
  86. return '<div class="twitter_button"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="' . esc_url( $this->http() . '://platform.twitter.com/widgets/tweet_button.html?url=' . rawurlencode( $share_url ) . '&counturl=' . rawurlencode( get_permalink( $post->ID ) ) . '&count=horizontal&text=' . rawurlencode( $post_title . ':' ) . $via ) . '" style="width:101px; height:20px;"></iframe></div>'; 
  87. } else { 
  88. if ( 
  89. /** 
  90. * Allow plugins to disable sharing counts for specific sharing services. 
  91. * @module sharedaddy 
  92. * @since 3.0.0 
  93. * @param bool true Should sharing counts be enabled for this specific service. Default to true. 
  94. * @param int $post->ID Post ID. 
  95. * @param string $str Sharing service name. 
  96. */ 
  97. apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' ) 
  98. ) { 
  99. sharing_register_post_for_share_counts( $post->ID ); 
  100. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID ); 
  101.  
  102. public function process_request( $post, array $post_data ) { 
  103. $post_title = $this->get_share_title( $post->ID ); 
  104. $post_link = $this->get_share_url( $post->ID ); 
  105.  
  106. if ( function_exists( 'mb_stripos' ) ) { 
  107. $strlen = 'mb_strlen'; 
  108. $substr = 'mb_substr'; 
  109. } else { 
  110. $strlen = 'strlen'; 
  111. $substr = 'substr'; 
  112.  
  113. $via = $this->sharing_twitter_via( $post ); 
  114. $related = $this->get_related_accounts( $post ); 
  115. if ( $via ) { 
  116. $sig = " via @$via"; 
  117. if ( $related === $via ) { 
  118. $related = false; 
  119. } else { 
  120. $via = false; 
  121. $sig = ''; 
  122.  
  123. $suffix_length = $this->short_url_length + $strlen( $sig ); 
  124. // $sig is handled by twitter in their 'via' argument. 
  125. // $post_link is handled by twitter in their 'url' argument. 
  126. if ( 140 < $strlen( $post_title ) + $suffix_length ) { 
  127. // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis. 
  128. $text = $substr( $post_title, 0, 140 - $suffix_length - 1 ) . "\xE2\x80\xA6"; 
  129. } else { 
  130. $text = $post_title; 
  131.  
  132. // Record stats 
  133. parent::process_request( $post, $post_data ); 
  134.  
  135. $url = $post_link; 
  136. $twitter_url = add_query_arg( 
  137. rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),  
  138. 'https://twitter.com/intent/tweet' 
  139. ); 
  140.  
  141. // Redirect to Twitter 
  142. wp_redirect( $twitter_url ); 
  143. die(); 
  144.  
  145. public function has_custom_button_style() { 
  146. return $this->smart; 
  147.  
  148. public function display_footer() { 
  149. $this->js_dialog( $this->shortname, array( 'height' => 350 ) );