Sharing_Service

The Jetpack by WordPress.com Sharing Service class.

Defined (1)

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

/modules/sharedaddy/sharing-service.php  
  1. class Sharing_Service { 
  2. private $global = false; 
  3. public $default_sharing_label = ''; 
  4.  
  5. public function __construct() { 
  6. $this->default_sharing_label = __( 'Share this:', 'jetpack' ); 
  7.  
  8. /** 
  9. * Gets a generic list of all services, without any config 
  10. */ 
  11. public function get_all_services_blog() { 
  12. $options = get_option( 'sharing-options' ); 
  13.  
  14. $all = $this->get_all_services(); 
  15. $services = array(); 
  16.  
  17. foreach ( $all AS $id => $name ) { 
  18. if ( isset( $all[$id] ) ) { 
  19. $config = array(); 
  20.  
  21. // Pre-load custom modules otherwise they won't know who they are 
  22. if ( substr( $id, 0, 7 ) == 'custom-' && is_array( $options[$id] ) ) 
  23. $config = $options[$id]; 
  24.  
  25. $services[$id] = new $all[$id]( $id, $config ); 
  26.  
  27. return $services; 
  28.  
  29. /** 
  30. * Gets a list of all available service names and classes 
  31. */ 
  32. public function get_all_services( $include_custom = true ) { 
  33. // Default services 
  34. // if you update this list, please update the REST API tests 
  35. // in bin/tests/api/suites/SharingTest.php 
  36. $services = array( 
  37. 'email' => 'Share_Email',  
  38. 'print' => 'Share_Print',  
  39. 'facebook' => 'Share_Facebook',  
  40. 'linkedin' => 'Share_LinkedIn',  
  41. 'reddit' => 'Share_Reddit',  
  42. 'twitter' => 'Share_Twitter',  
  43. 'press-this' => 'Share_PressThis',  
  44. 'google-plus-1' => 'Share_GooglePlus1',  
  45. 'tumblr' => 'Share_Tumblr',  
  46. 'pinterest' => 'Share_Pinterest',  
  47. 'pocket' => 'Share_Pocket',  
  48. ); 
  49.  
  50. if ( $include_custom ) { 
  51. // Add any custom services in 
  52. $options = $this->get_global_options(); 
  53. foreach ( (array) $options['custom'] AS $custom_id ) { 
  54. $services[$custom_id] = 'Share_Custom'; 
  55.  
  56. /** 
  57. * Filters the list of available Sharing Services. 
  58. * @module sharedaddy 
  59. * @since 1.1.0 
  60. * @param array $services Array of all available Sharing Services. 
  61. */ 
  62. return apply_filters( 'sharing_services', $services ); 
  63.  
  64. public function new_service( $label, $url, $icon ) { 
  65. // Validate 
  66. $label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) ); 
  67. $url = trim( esc_url_raw( $url ) ); 
  68. $icon = trim( esc_url_raw( $icon ) ); 
  69.  
  70. if ( $label && $url && $icon ) { 
  71. $options = get_option( 'sharing-options' ); 
  72. if ( !is_array( $options ) ) 
  73. $options = array(); 
  74.  
  75. $service_id = 'custom-'.time(); 
  76.  
  77. // Add a new custom service 
  78. $options['global']['custom'][] = $service_id; 
  79. if ( false !== $this->global ) { 
  80. $this->global['custom'][] = $service_id; 
  81.  
  82. update_option( 'sharing-options', $options ); 
  83.  
  84. // Create a custom service and set the options for it 
  85. $service = new Share_Custom( $service_id, array( 'name' => $label, 'url' => $url, 'icon' => $icon ) ); 
  86. $this->set_service( $service_id, $service ); 
  87.  
  88. // Return the service 
  89. return $service; 
  90.  
  91. return false; 
  92.  
  93. public function delete_service( $service_id ) { 
  94. $options = get_option( 'sharing-options' ); 
  95. if ( isset( $options[$service_id] ) ) 
  96. unset( $options[$service_id] ); 
  97.  
  98. $key = array_search( $service_id, $options['global']['custom'] ); 
  99. if ( $key !== false ) 
  100. unset( $options['global']['custom'][$key] ); 
  101.  
  102. update_option( 'sharing-options', $options ); 
  103. return true; 
  104.  
  105. public function set_blog_services( array $visible, array $hidden ) { 
  106. $services = $this->get_all_services(); 
  107. // Validate the services 
  108. $available = array_keys( $services ); 
  109.  
  110. // Only allow services that we have defined 
  111. $hidden = array_intersect( $hidden, $available ); 
  112. $visible = array_intersect( $visible, $available ); 
  113.  
  114. // Ensure we don't have the same ones in hidden and visible 
  115. $hidden = array_diff( $hidden, $visible ); 
  116.  
  117. /** 
  118. * Control the state of the list of sharing services. 
  119. * @module sharedaddy 
  120. * @since 1.1.0 
  121. * @param array $args { 
  122. * Array of options describing the state of the sharing services. 
  123. * @type array $services List of all available service names and classes. 
  124. * @type array $available Validated list of all available service names and classes. 
  125. * @type array $hidden List of services hidden behind a "More" button. 
  126. * @type array $visible List of visible services. 
  127. * @type array $this->get_blog_services() Array of Sharing Services currently enabled. 
  128. * } 
  129. */ 
  130. do_action( 'sharing_get_services_state', array( 
  131. 'services' => $services,  
  132. 'available' => $available,  
  133. 'hidden' => $hidden,  
  134. 'visible' => $visible,  
  135. 'currently_enabled' => $this->get_blog_services() 
  136. ) ); 
  137.  
  138. update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) ); 
  139.  
  140. public function get_blog_services() { 
  141. $options = get_option( 'sharing-options' ); 
  142. $enabled = get_option( 'sharing-services' ); 
  143. $services = $this->get_all_services(); 
  144.  
  145. if ( !is_array( $options ) ) 
  146. $options = array( 'global' => $this->get_global_options() ); 
  147.  
  148. $global = $options['global']; 
  149.  
  150. // Default services 
  151. if ( ! is_array( $enabled ) ) { 
  152. $enabled = array( 
  153. 'visible' => array(),  
  154. 'hidden' => array() 
  155. ); 
  156.  
  157. /** 
  158. * Filters the list of default Sharing Services. 
  159. * @module sharedaddy 
  160. * @since 1.1.0 
  161. * @param array $enabled Array of default Sharing Services. 
  162. */ 
  163. $enabled = apply_filters( 'sharing_default_services', $enabled ); 
  164.  
  165. // Cleanup after any filters that may have produced duplicate services 
  166. $enabled['visible'] = array_unique( $enabled['visible'] ); 
  167. $enabled['hidden'] = array_unique( $enabled['hidden'] ); 
  168.  
  169. // Form the enabled services 
  170. $blog = array( 'visible' => array(), 'hidden' => array() ); 
  171.  
  172. foreach ( $blog AS $area => $stuff ) { 
  173. foreach ( (array)$enabled[$area] AS $service ) { 
  174. if ( isset( $services[$service] ) ) { 
  175. $blog[$area][$service] = new $services[$service]( $service, array_merge( $global, isset( $options[$service] ) ? $options[$service] : array() ) ); 
  176.  
  177. /** 
  178. * Filters the list of enabled Sharing Services. 
  179. * @module sharedaddy 
  180. * @since 1.1.0 
  181. * @param array $blog Array of enabled Sharing Services. 
  182. */ 
  183. $blog = apply_filters( 'sharing_services_enabled', $blog ); 
  184.  
  185. // Add CSS for NASCAR 
  186. if ( count( $blog['visible'] ) || count( $blog['hidden'] ) ) 
  187. add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' ); 
  188.  
  189. // Convenience for checking if a service is present 
  190. $blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) ); 
  191. return $blog; 
  192.  
  193. public function get_service( $service_name ) { 
  194. $services = $this->get_blog_services(); 
  195.  
  196. if ( isset( $services['visible'][$service_name] ) ) 
  197. return $services['visible'][$service_name]; 
  198.  
  199. if ( isset( $services['hidden'][$service_name] ) ) 
  200. return $services['hidden'][$service_name]; 
  201.  
  202. return false; 
  203.  
  204. public function set_global_options( $data ) { 
  205. $options = get_option( 'sharing-options' ); 
  206.  
  207. // No options yet 
  208. if ( !is_array( $options ) ) 
  209. $options = array(); 
  210.  
  211. // Defaults 
  212. $options['global'] = array( 
  213. 'button_style' => 'icon-text',  
  214. 'sharing_label' => $this->default_sharing_label,  
  215. 'open_links' => 'same',  
  216. 'show' => array(),  
  217. 'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array() 
  218. ); 
  219.  
  220. /** 
  221. * Filters global sharing settings. 
  222. * @module sharedaddy 
  223. * @since 1.1.0 
  224. * @param array $options['global'] Array of global sharing settings. 
  225. */ 
  226. $options['global'] = apply_filters( 'sharing_default_global', $options['global'] ); 
  227.  
  228. // Validate options and set from our data 
  229. if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) ) 
  230. $options['global']['button_style'] = $data['button_style']; 
  231.  
  232. if ( isset( $data['sharing_label'] ) ) { 
  233. if ( $this->default_sharing_label === $data['sharing_label'] ) { 
  234. $options['global']['sharing_label'] = false; 
  235. } else { 
  236. $options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) ); 
  237.  
  238. if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) ) 
  239. $options['global']['open_links'] = $data['open_links']; 
  240.  
  241. $shows = array_values( get_post_types( array( 'public' => true ) ) ); 
  242. $shows[] = 'index'; 
  243. if ( isset( $data['show'] ) ) { 
  244. if ( is_scalar( $data['show'] ) ) { 
  245. switch ( $data['show'] ) { 
  246. case 'posts' : 
  247. $data['show'] = array( 'post', 'page' ); 
  248. break; 
  249. case 'index' : 
  250. $data['show'] = array( 'index' ); 
  251. break; 
  252. case 'posts-index' : 
  253. $data['show'] = array( 'post', 'page', 'index' ); 
  254. break; 
  255.  
  256. if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { 
  257. $options['global']['show'] = $data['show']; 
  258.  
  259. update_option( 'sharing-options', $options ); 
  260. return $options['global']; 
  261.  
  262. public function get_global_options() { 
  263. if ( $this->global === false ) { 
  264. $options = get_option( 'sharing-options' ); 
  265.  
  266. if ( is_array( $options ) && isset( $options['global'] ) ) 
  267. $this->global = $options['global']; 
  268. else 
  269. $this->global = $this->set_global_options( $options['global'] ); 
  270.  
  271. if ( ! isset( $this->global['show'] ) ) { 
  272. $this->global['show'] = array( 'post', 'page' ); 
  273. } elseif ( is_scalar( $this->global['show'] ) ) { 
  274. switch ( $this->global['show'] ) { 
  275. case 'posts' : 
  276. $this->global['show'] = array( 'post', 'page' ); 
  277. break; 
  278. case 'index' : 
  279. $this->global['show'] = array( 'index' ); 
  280. break; 
  281. case 'posts-index' : 
  282. $this->global['show'] = array( 'post', 'page', 'index' ); 
  283. break; 
  284.  
  285. if ( false === $this->global['sharing_label'] ) { 
  286. $this->global['sharing_label'] = $this->default_sharing_label; 
  287.  
  288. return $this->global; 
  289.  
  290. public function set_service( $id, Sharing_Source $service ) { 
  291. // Update the options for this service 
  292. $options = get_option( 'sharing-options' ); 
  293.  
  294. // No options yet 
  295. if ( ! is_array( $options ) ) { 
  296. $options = array(); 
  297.  
  298. /** 
  299. * Get the state of a sharing button. 
  300. * @module sharedaddy 
  301. * @since 1.1.0 
  302. * @param array $args { 
  303. * State of a sharing button. 
  304. * @type string $id Service ID. 
  305. * @type array $options Array of all sharing options. 
  306. * @type array $service Details about a service. 
  307. * } 
  308. */ 
  309. do_action( 'sharing_get_button_state', array( 'id' => $id, 'options' => $options, 'service' => $service ) ); 
  310.  
  311. $options[$id] = $service->get_options(); 
  312.  
  313. update_option( 'sharing-options', array_filter( $options ) ); 
  314.  
  315. // Soon to come to a .org plugin near you! 
  316. public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) { 
  317. global $wpdb, $blog_id; 
  318. if ( !$_blog_id ) { 
  319. $_blog_id = $blog_id; 
  320. if ( $service_name == false ) { 
  321. if ( $post_id > 0 ) { 
  322. // total number of shares for this post 
  323. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d", $_blog_id, $post_id ) ); 
  324. } else { 
  325. // total number of shares for this blog 
  326. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d", $_blog_id ) ); 
  327.  
  328. if ( $post_id > 0 ) 
  329. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $_blog_id, $post_id, $service_name ) ); 
  330. else 
  331. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $_blog_id, $service_name ) ); 
  332.  
  333. public function get_services_total( $post_id = false ) { 
  334. $totals = array(); 
  335. $services = $this->get_blog_services(); 
  336.  
  337. if ( !empty( $services ) && isset( $services[ 'all' ] ) ) 
  338. foreach( $services[ 'all' ] as $key => $value ) { 
  339. $totals[$key] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) ); 
  340. usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) ); 
  341.  
  342. return $totals; 
  343.  
  344. public function get_posts_total() { 
  345. $totals = array(); 
  346. global $wpdb, $blog_id; 
  347.  
  348. $my_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d GROUP BY post_id ORDER BY count DESC ", $blog_id ) ); 
  349.  
  350. if ( !empty( $my_data ) ) 
  351. foreach( $my_data as $row ) 
  352. $totals[] = new Sharing_Post_Total( $row->id, $row->total ); 
  353.  
  354. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) ); 
  355.  
  356. return $totals;