Sharing_Source

The Jetpack by WordPress.com Sharing Source class.

Defined (1)

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

/modules/sharedaddy/sharing-sources.php  
  1. abstract class Sharing_Source { 
  2. public $button_style; 
  3. public $smart; 
  4. protected $open_link_in_new; 
  5. protected $id; 
  6.  
  7. public function __construct( $id, array $settings ) { 
  8. $this->id = $id; 
  9. /** 
  10. * Filter the way sharing links open. 
  11. * By default, sharing links open in a new window. 
  12. * @module sharedaddy 
  13. * @since 3.4.0 
  14. * @param bool true Should Sharing links open in a new window. Default to true. 
  15. */ 
  16. $this->open_link_in_new = apply_filters( 'jetpack_open_sharing_in_new_window', true ); 
  17.  
  18. if ( isset( $settings['button_style'] ) ) 
  19. $this->button_style = $settings['button_style']; 
  20.  
  21. if ( isset( $settings['smart'] ) ) 
  22. $this->smart = $settings['smart']; 
  23.  
  24. public function http() { 
  25. return is_ssl() ? 'https' : 'http'; 
  26.  
  27. public function get_id() { 
  28. return $this->id; 
  29.  
  30. public function get_class() { 
  31. return $this->id; 
  32.  
  33. public function get_share_url( $post_id ) { 
  34. /** 
  35. * Filter the sharing permalink. 
  36. * @module sharedaddy 
  37. * @since 1.2.0 
  38. * @param string get_permalink( $post_id ) Post Permalink. 
  39. * @param int $post_id Post ID. 
  40. * @param int $this->id Sharing ID. 
  41. */ 
  42. return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id ); 
  43.  
  44. public function get_share_title( $post_id ) { 
  45. $post = get_post( $post_id ); 
  46. /** 
  47. * Filter the sharing title. 
  48. * @module sharedaddy 
  49. * @since 2.8.0 
  50. * @param string $post->post_title Post Title. 
  51. * @param int $post_id Post ID. 
  52. * @param int $this->id Sharing ID. 
  53. */ 
  54. $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id ); 
  55.  
  56. return html_entity_decode( wp_kses( $title, null ) ); 
  57.  
  58. public function has_custom_button_style() { 
  59. return false; 
  60.  
  61. public function get_link( $url, $text, $title, $query = '', $id = false ) { 
  62. $args = func_get_args(); 
  63. $klasses = array( 'share-'.$this->get_class(), 'sd-button' ); 
  64.  
  65. if ( 'icon' == $this->button_style || 'icon-text' == $this->button_style ) 
  66. $klasses[] = 'share-icon'; 
  67.  
  68. if ( 'icon' == $this->button_style ) { 
  69. $text = $title; 
  70. $klasses[] = 'no-text'; 
  71.  
  72. if ( true == $this->open_link_in_new ) 
  73. $text .= __( ' (Opens in new window)', 'jetpack' ); 
  74.  
  75. /** 
  76. * Filter the sharing display ID. 
  77. * @module sharedaddy 
  78. * @since 3.4.0 
  79. * @param int|false $id Sharing ID. 
  80. * @param object $this Sharing service properties. 
  81. * @param array $args Array of sharing service options. 
  82. */ 
  83. $id = apply_filters( 'jetpack_sharing_display_id', $id, $this, $args ); 
  84. /** 
  85. * Filter the sharing display link. 
  86. * @module sharedaddy 
  87. * @since 2.8.0 
  88. * @param string $url Post URL. 
  89. * @param object $this Sharing service properties. 
  90. * @param int|false $id Sharing ID. 
  91. * @param array $args Array of sharing service options. 
  92. */ 
  93. $url = apply_filters( 'sharing_display_link', $url, $this, $id, $args ); // backwards compatibility 
  94. /** 
  95. * Filter the sharing display link. 
  96. * @module sharedaddy 
  97. * @since 2.8.0 
  98. * @param string $url Post URL. 
  99. * @param object $this Sharing service properties. 
  100. * @param int|false $id Sharing ID. 
  101. * @param array $args Array of sharing service options. 
  102. */ 
  103. $url = apply_filters( 'jetpack_sharing_display_link', $url, $this, $id, $args ); 
  104. /** 
  105. * Filter the sharing display query. 
  106. * @module sharedaddy 
  107. * @since 2.8.0 
  108. * @param string $query Sharing service URL parameter. 
  109. * @param object $this Sharing service properties. 
  110. * @param int|false $id Sharing ID. 
  111. * @param array $args Array of sharing service options. 
  112. */ 
  113. $query = apply_filters( 'jetpack_sharing_display_query', $query, $this, $id, $args ); 
  114.  
  115. if ( !empty( $query ) ) { 
  116. if ( false === stripos( $url, '?' ) ) 
  117. $url .= '?'.$query; 
  118. else 
  119. $url .= '&'.$query; 
  120.  
  121. if ( 'text' == $this->button_style ) 
  122. $klasses[] = 'no-icon'; 
  123.  
  124. /** 
  125. * Filter the sharing display classes. 
  126. * @module sharedaddy 
  127. * @since 3.4.0 
  128. * @param array $klasses Sharing service classes. 
  129. * @param object $this Sharing service properties. 
  130. * @param int|false $id Sharing ID. 
  131. * @param array $args Array of sharing service options. 
  132. */ 
  133. $klasses = apply_filters( 'jetpack_sharing_display_classes', $klasses, $this, $id, $args ); 
  134. /** 
  135. * Filter the sharing display title. 
  136. * @module sharedaddy 
  137. * @since 3.4.0 
  138. * @param string $title Sharing service title. 
  139. * @param object $this Sharing service properties. 
  140. * @param int|false $id Sharing ID. 
  141. * @param array $args Array of sharing service options. 
  142. */ 
  143. $title = apply_filters( 'jetpack_sharing_display_title', $title, $this, $id, $args ); 
  144. /** 
  145. * Filter the sharing display text. 
  146. * @module sharedaddy 
  147. * @since 3.4.0 
  148. * @param string $text Sharing service text. 
  149. * @param object $this Sharing service properties. 
  150. * @param int|false $id Sharing ID. 
  151. * @param array $args Array of sharing service options. 
  152. */ 
  153. $text = apply_filters( 'jetpack_sharing_display_text', $text, $this, $id, $args ); 
  154.  
  155. return sprintf( 
  156. '<a rel="nofollow" data-shared="%s" class="%s" href="%s"%s title="%s"><span%s>%s</span></a>',  
  157. ( $id ? esc_attr( $id ) : '' ),  
  158. implode( ' ', $klasses ),  
  159. $url,  
  160. ( true == $this->open_link_in_new ) ? ' target="_blank"' : '',  
  161. $title,  
  162. ( 'icon' == $this->button_style ) ? '></span><span class="sharing-screen-reader-text"' : '',  
  163.  
  164. $text 
  165. ); 
  166.  
  167. /** 
  168. * Get an unfiltered post permalink to use when generating a sharing URL with get_link. 
  169. * Use instead of get_share_url for non-official styles as get_permalink ensures that process_request 
  170. * will be executed more reliably, in the case that the filtered URL uses a service that strips query parameters. 
  171. * @since 3.7.0 
  172. * @param int $post_id Post ID. 
  173. * @uses get_permalink 
  174. * @return string get_permalink( $post_id ) Post permalink. 
  175. */ 
  176. public function get_process_request_url( $post_id ) { 
  177. return get_permalink( $post_id ); 
  178.  
  179. abstract public function get_name(); 
  180. abstract public function get_display( $post ); 
  181.  
  182. public function display_header() { 
  183.  
  184. public function display_footer() { 
  185.  
  186. public function has_advanced_options() { 
  187. return false; 
  188.  
  189. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) { 
  190. $text = ' '; 
  191. $button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style; 
  192. if ( !$this->smart && ! $force_smart ) 
  193. if ( $button_style != 'icon' ) 
  194. $text = $this->get_name(); 
  195.  
  196. $klasses = array( 'share-'.$this->get_class(), 'sd-button' ); 
  197.  
  198. if ( $button_style == 'icon' || $button_style == 'icon-text' ) 
  199. $klasses[] = 'share-icon'; 
  200.  
  201. if ( $button_style == 'icon' ) 
  202. $klasses[] = 'no-text'; 
  203.  
  204. if ( $button_style == 'text' ) 
  205. $klasses[] = 'no-icon'; 
  206.  
  207. $link = sprintf( 
  208. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',  
  209. implode( ' ', $klasses ),  
  210. $this->get_name(),  
  211. $text 
  212. ); 
  213.  
  214. $smart = ( $this->smart || $force_smart ) ? 'on' : 'off'; 
  215. $return = "<div class='option option-smart-$smart'>$link</div>"; 
  216. if ( $echo ) 
  217. echo $return; 
  218.  
  219. return $return; 
  220.  
  221. public function get_total( $post = false ) { 
  222. global $wpdb, $blog_id; 
  223.  
  224. $name = strtolower( $this->get_id() ); 
  225.  
  226. if ( $post == false ) { 
  227. // get total number of shares for service 
  228. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) ); 
  229.  
  230. // get total shares for a post 
  231. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $blog_id, $post->ID, $name ) ); 
  232.  
  233. public function get_posts_total() { 
  234. global $wpdb, $blog_id; 
  235.  
  236. $totals = array(); 
  237. $name = strtolower( $this->get_id() ); 
  238.  
  239. $my_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d AND share_service = %s GROUP BY post_id ORDER BY count DESC ", $blog_id, $name ) ); 
  240.  
  241. if ( !empty( $my_data ) ) 
  242. foreach( $my_data as $row ) 
  243. $totals[] = new Sharing_Post_Total( $row->id, $row->total ); 
  244.  
  245. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) ); 
  246.  
  247. return $totals; 
  248.  
  249. public function process_request( $post, array $post_data ) { 
  250. /** 
  251. * Fires when a post is shared via one of the sharing buttons. 
  252. * @module sharedaddy 
  253. * @since 1.1.0 
  254. * @param array $args Aray of information about the sharing service. 
  255. */ 
  256. do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) ); 
  257.  
  258. public function js_dialog( $name, $params = array() ) { 
  259. if ( true !== $this->open_link_in_new ) 
  260. return; 
  261.  
  262. $defaults = array( 
  263. 'menubar' => 1,  
  264. 'resizable' => 1,  
  265. 'width' => 600,  
  266. 'height' => 400,  
  267. ); 
  268. $params = array_merge( $defaults, $params ); 
  269. $opts = array(); 
  270. foreach( $params as $key => $val ) { 
  271. $opts[] = "$key=$val"; 
  272. $opts = implode( ', ', $opts ); 
  273. ?> 
  274. <script type="text/javascript"> 
  275. var windowOpen; 
  276. jQuery(document).on( 'ready post-load', function() { 
  277. jQuery( 'a.share-<?php echo $name; ?>' ).on( 'click', function() { 
  278. if ( 'undefined' !== typeof windowOpen ) { // If there's another sharing window open, close it. 
  279. windowOpen.close(); 
  280. windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcom<?php echo $name; ?>', '<?php echo $opts; ?>' ); 
  281. return false; 
  282. }); 
  283. }); 
  284. </script> 
  285. <?php