sharing-service.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. <?php
  2. include_once dirname( __FILE__ ).'/sharing-sources.php';
  3. define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION );
  4. class Sharing_Service {
  5. private $global = false;
  6. public $default_sharing_label = '';
  7. public function __construct() {
  8. $this->default_sharing_label = __( 'Share this:', 'jetpack' );
  9. }
  10. /**
  11. * Gets a generic list of all services, without any config
  12. */
  13. public function get_all_services_blog() {
  14. $options = get_option( 'sharing-options' );
  15. $all = $this->get_all_services();
  16. $services = array();
  17. foreach ( $all AS $id => $name ) {
  18. if ( isset( $all[$id] ) ) {
  19. $config = array();
  20. // Pre-load custom modules otherwise they won't know who they are
  21. if ( substr( $id, 0, 7 ) == 'custom-' && is_array( $options[$id] ) )
  22. $config = $options[$id];
  23. $services[$id] = new $all[$id]( $id, $config );
  24. }
  25. }
  26. return $services;
  27. }
  28. /**
  29. * Gets a list of all available service names and classes
  30. */
  31. public function get_all_services( $include_custom = true ) {
  32. global $wp_version;
  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. 'print' => 'Share_Print',
  38. 'facebook' => 'Share_Facebook',
  39. 'linkedin' => 'Share_LinkedIn',
  40. 'reddit' => 'Share_Reddit',
  41. 'twitter' => 'Share_Twitter',
  42. 'google-plus-1' => 'Share_GooglePlus1',
  43. 'tumblr' => 'Share_Tumblr',
  44. 'pinterest' => 'Share_Pinterest',
  45. 'pocket' => 'Share_Pocket',
  46. 'telegram' => 'Share_Telegram',
  47. 'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp',
  48. 'skype' => 'Share_Skype',
  49. );
  50. /**
  51. * Filters if Email Sharing is enabled.
  52. *
  53. * E-Mail sharing is often problematic due to spam concerns, so this filter enables it to be quickly and simply toggled.
  54. * @module sharedaddy
  55. *
  56. * @since 5.1.0
  57. *
  58. * @param bool $email Is e-mail sharing enabled? Default false if Akismet is not active or true if Akismet is active.
  59. */
  60. if ( apply_filters( 'sharing_services_email', Jetpack::is_akismet_active() ) ) {
  61. $services['email'] = 'Share_Email';
  62. }
  63. if ( is_multisite() && ( version_compare( $wp_version, '4.9-RC1-42107', '<' ) || is_plugin_active( 'press-this/press-this-plugin.php' ) ) ) {
  64. $services['press-this'] = 'Share_PressThis';
  65. }
  66. if ( $include_custom ) {
  67. // Add any custom services in
  68. $options = $this->get_global_options();
  69. foreach ( (array) $options['custom'] AS $custom_id ) {
  70. $services[$custom_id] = 'Share_Custom';
  71. }
  72. }
  73. /**
  74. * Filters the list of available Sharing Services.
  75. *
  76. * @module sharedaddy
  77. *
  78. * @since 1.1.0
  79. *
  80. * @param array $services Array of all available Sharing Services.
  81. */
  82. return apply_filters( 'sharing_services', $services );
  83. }
  84. public function new_service( $label, $url, $icon ) {
  85. // Validate
  86. $label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
  87. $url = trim( esc_url_raw( $url ) );
  88. $icon = trim( esc_url_raw( $icon ) );
  89. if ( $label && $url && $icon ) {
  90. $options = get_option( 'sharing-options' );
  91. if ( !is_array( $options ) )
  92. $options = array();
  93. $service_id = 'custom-'.time();
  94. // Add a new custom service
  95. $options['global']['custom'][] = $service_id;
  96. if ( false !== $this->global ) {
  97. $this->global['custom'][] = $service_id;
  98. }
  99. update_option( 'sharing-options', $options );
  100. // Create a custom service and set the options for it
  101. $service = new Share_Custom( $service_id, array( 'name' => $label, 'url' => $url, 'icon' => $icon ) );
  102. $this->set_service( $service_id, $service );
  103. // Return the service
  104. return $service;
  105. }
  106. return false;
  107. }
  108. public function delete_service( $service_id ) {
  109. $options = get_option( 'sharing-options' );
  110. if ( isset( $options[$service_id] ) )
  111. unset( $options[$service_id] );
  112. $key = array_search( $service_id, $options['global']['custom'] );
  113. if ( $key !== false )
  114. unset( $options['global']['custom'][$key] );
  115. update_option( 'sharing-options', $options );
  116. return true;
  117. }
  118. public function set_blog_services( array $visible, array $hidden ) {
  119. $services = $this->get_all_services();
  120. // Validate the services
  121. $available = array_keys( $services );
  122. // Only allow services that we have defined
  123. $hidden = array_intersect( $hidden, $available );
  124. $visible = array_intersect( $visible, $available );
  125. // Ensure we don't have the same ones in hidden and visible
  126. $hidden = array_diff( $hidden, $visible );
  127. /**
  128. * Control the state of the list of sharing services.
  129. *
  130. * @module sharedaddy
  131. *
  132. * @since 1.1.0
  133. *
  134. * @param array $args {
  135. * Array of options describing the state of the sharing services.
  136. *
  137. * @type array $services List of all available service names and classes.
  138. * @type array $available Validated list of all available service names and classes.
  139. * @type array $hidden List of services hidden behind a "More" button.
  140. * @type array $visible List of visible services.
  141. * @type array $this->get_blog_services() Array of Sharing Services currently enabled.
  142. * }
  143. */
  144. do_action( 'sharing_get_services_state', array(
  145. 'services' => $services,
  146. 'available' => $available,
  147. 'hidden' => $hidden,
  148. 'visible' => $visible,
  149. 'currently_enabled' => $this->get_blog_services()
  150. ) );
  151. return update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
  152. }
  153. public function get_blog_services() {
  154. $options = get_option( 'sharing-options' );
  155. $enabled = get_option( 'sharing-services' );
  156. $services = $this->get_all_services();
  157. /**
  158. * Check if options exist and are well formatted.
  159. * This avoids issues on sites with corrupted options.
  160. * @see https://github.com/Automattic/jetpack/issues/6121
  161. */
  162. if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) {
  163. $global_options = array( 'global' => $this->get_global_options() );
  164. $options = is_array( $options )
  165. ? array_merge( $options, $global_options )
  166. : $global_options;
  167. }
  168. $global = $options['global'];
  169. // Default services
  170. if ( ! is_array( $enabled ) ) {
  171. $enabled = array(
  172. 'visible' => array(),
  173. 'hidden' => array()
  174. );
  175. /**
  176. * Filters the list of default Sharing Services.
  177. *
  178. * @module sharedaddy
  179. *
  180. * @since 1.1.0
  181. *
  182. * @param array $enabled Array of default Sharing Services.
  183. */
  184. $enabled = apply_filters( 'sharing_default_services', $enabled );
  185. }
  186. // Cleanup after any filters that may have produced duplicate services
  187. if ( is_array( $enabled['visible'] ) ) {
  188. $enabled['visible'] = array_unique( $enabled['visible'] );
  189. } else {
  190. $enabled['visible'] = array();
  191. }
  192. if ( is_array( $enabled['hidden'] ) ) {
  193. $enabled['hidden'] = array_unique( $enabled['hidden'] );
  194. } else {
  195. $enabled['hidden'] = array();
  196. }
  197. // Form the enabled services
  198. $blog = array( 'visible' => array(), 'hidden' => array() );
  199. foreach ( $blog AS $area => $stuff ) {
  200. foreach ( (array)$enabled[$area] AS $service ) {
  201. if ( isset( $services[$service] ) ) {
  202. if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) {
  203. $options[ $service ] = array();
  204. }
  205. $blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) );
  206. }
  207. }
  208. }
  209. /**
  210. * Filters the list of enabled Sharing Services.
  211. *
  212. * @module sharedaddy
  213. *
  214. * @since 1.1.0
  215. *
  216. * @param array $blog Array of enabled Sharing Services.
  217. */
  218. $blog = apply_filters( 'sharing_services_enabled', $blog );
  219. // Add CSS for NASCAR
  220. if ( count( $blog['visible'] ) || count( $blog['hidden'] ) )
  221. add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
  222. // Convenience for checking if a service is present
  223. $blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
  224. return $blog;
  225. }
  226. public function get_service( $service_name ) {
  227. $services = $this->get_blog_services();
  228. if ( isset( $services['visible'][$service_name] ) )
  229. return $services['visible'][$service_name];
  230. if ( isset( $services['hidden'][$service_name] ) )
  231. return $services['hidden'][$service_name];
  232. return false;
  233. }
  234. public function set_global_options( $data ) {
  235. $options = get_option( 'sharing-options' );
  236. // No options yet
  237. if ( !is_array( $options ) )
  238. $options = array();
  239. // Defaults
  240. $options['global'] = array(
  241. 'button_style' => 'icon-text',
  242. 'sharing_label' => $this->default_sharing_label,
  243. 'open_links' => 'same',
  244. 'show' => array(),
  245. 'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array()
  246. );
  247. /**
  248. * Filters global sharing settings.
  249. *
  250. * @module sharedaddy
  251. *
  252. * @since 1.1.0
  253. *
  254. * @param array $options['global'] Array of global sharing settings.
  255. */
  256. $options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
  257. // Validate options and set from our data
  258. if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) )
  259. $options['global']['button_style'] = $data['button_style'];
  260. if ( isset( $data['sharing_label'] ) ) {
  261. if ( $this->default_sharing_label === $data['sharing_label'] ) {
  262. $options['global']['sharing_label'] = false;
  263. } else {
  264. $options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
  265. }
  266. }
  267. if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) )
  268. $options['global']['open_links'] = $data['open_links'];
  269. $shows = array_values( get_post_types( array( 'public' => true ) ) );
  270. $shows[] = 'index';
  271. if ( isset( $data['show'] ) ) {
  272. if ( is_scalar( $data['show'] ) ) {
  273. switch ( $data['show'] ) {
  274. case 'posts' :
  275. $data['show'] = array( 'post', 'page' );
  276. break;
  277. case 'index' :
  278. $data['show'] = array( 'index' );
  279. break;
  280. case 'posts-index' :
  281. $data['show'] = array( 'post', 'page', 'index' );
  282. break;
  283. }
  284. }
  285. if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
  286. $options['global']['show'] = $data['show'];
  287. }
  288. }
  289. update_option( 'sharing-options', $options );
  290. return $options['global'];
  291. }
  292. public function get_global_options() {
  293. if ( $this->global === false ) {
  294. $options = get_option( 'sharing-options' );
  295. if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) {
  296. $this->global = $options['global'];
  297. } else {
  298. $this->global = $this->set_global_options( $options['global'] );
  299. }
  300. }
  301. if ( ! isset( $this->global['show'] ) ) {
  302. $this->global['show'] = array( 'post', 'page' );
  303. } elseif ( is_scalar( $this->global['show'] ) ) {
  304. switch ( $this->global['show'] ) {
  305. case 'posts' :
  306. $this->global['show'] = array( 'post', 'page' );
  307. break;
  308. case 'index' :
  309. $this->global['show'] = array( 'index' );
  310. break;
  311. case 'posts-index' :
  312. $this->global['show'] = array( 'post', 'page', 'index' );
  313. break;
  314. }
  315. }
  316. if ( false === $this->global['sharing_label'] ) {
  317. $this->global['sharing_label'] = $this->default_sharing_label;
  318. }
  319. return $this->global;
  320. }
  321. public function set_service( $id, Sharing_Source $service ) {
  322. // Update the options for this service
  323. $options = get_option( 'sharing-options' );
  324. // No options yet
  325. if ( ! is_array( $options ) ) {
  326. $options = array();
  327. }
  328. /**
  329. * Get the state of a sharing button.
  330. *
  331. * @module sharedaddy
  332. *
  333. * @since 1.1.0
  334. *
  335. * @param array $args {
  336. * State of a sharing button.
  337. *
  338. * @type string $id Service ID.
  339. * @type array $options Array of all sharing options.
  340. * @type array $service Details about a service.
  341. * }
  342. */
  343. do_action( 'sharing_get_button_state', array( 'id' => $id, 'options' => $options, 'service' => $service ) );
  344. $options[$id] = $service->get_options();
  345. update_option( 'sharing-options', array_filter( $options ) );
  346. }
  347. // Soon to come to a .org plugin near you!
  348. public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
  349. global $wpdb, $blog_id;
  350. if ( !$_blog_id ) {
  351. $_blog_id = $blog_id;
  352. }
  353. if ( $service_name == false ) {
  354. if ( $post_id > 0 ) {
  355. // total number of shares for this post
  356. 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 ) );
  357. } else {
  358. // total number of shares for this blog
  359. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d", $_blog_id ) );
  360. }
  361. }
  362. if ( $post_id > 0 )
  363. 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 ) );
  364. else
  365. 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 ) );
  366. }
  367. public function get_services_total( $post_id = false ) {
  368. $totals = array();
  369. $services = $this->get_blog_services();
  370. if ( !empty( $services ) && isset( $services[ 'all' ] ) )
  371. foreach( $services[ 'all' ] as $key => $value ) {
  372. $totals[$key] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) );
  373. }
  374. usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
  375. return $totals;
  376. }
  377. public function get_posts_total() {
  378. $totals = array();
  379. global $wpdb, $blog_id;
  380. $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 ) );
  381. if ( !empty( $my_data ) )
  382. foreach( $my_data as $row )
  383. $totals[] = new Sharing_Post_Total( $row->id, $row->total );
  384. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
  385. return $totals;
  386. }
  387. }
  388. class Sharing_Service_Total {
  389. public $id = '';
  390. public $name = '';
  391. public $service = '';
  392. public $total = 0;
  393. public function __construct( $id, $total ) {
  394. $services = new Sharing_Service();
  395. $this->id = esc_html( $id );
  396. $this->service = $services->get_service( $id );
  397. $this->total = (int) $total;
  398. $this->name = $this->service->get_name();
  399. }
  400. static function cmp( $a, $b ) {
  401. if ( $a->total == $b->total )
  402. return $a->name < $b->name;
  403. return $a->total < $b->total;
  404. }
  405. }
  406. class Sharing_Post_Total {
  407. public $id = 0;
  408. public $total = 0;
  409. public $title = '';
  410. public $url = '';
  411. public function __construct( $id, $total ) {
  412. $this->id = (int) $id;
  413. $this->total = (int) $total;
  414. $this->title = get_the_title( $this->id );
  415. $this->url = get_permalink( $this->id );
  416. }
  417. static function cmp( $a, $b ) {
  418. if ( $a->total == $b->total )
  419. return $a->id < $b->id;
  420. return $a->total < $b->total;
  421. }
  422. }
  423. function sharing_register_post_for_share_counts( $post_id ) {
  424. global $jetpack_sharing_counts;
  425. if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) )
  426. $jetpack_sharing_counts = array();
  427. $jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
  428. }
  429. function sharing_maybe_enqueue_scripts() {
  430. $sharer = new Sharing_Service();
  431. $global_options = $sharer->get_global_options();
  432. $enqueue = false;
  433. if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) {
  434. $enqueue = true;
  435. } elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) {
  436. $enqueue = true;
  437. }
  438. /**
  439. * Filter to decide when sharing scripts should be enqueued.
  440. *
  441. * @module sharedaddy
  442. *
  443. * @since 3.2.0
  444. *
  445. * @param bool $enqueue Decide if the sharing scripts should be enqueued.
  446. */
  447. return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
  448. }
  449. function sharing_add_footer() {
  450. global $jetpack_sharing_counts;
  451. /**
  452. * Filter all JavaScript output by the sharing module.
  453. *
  454. * @module sharedaddy
  455. *
  456. * @since 1.1.0
  457. *
  458. * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
  459. */
  460. if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) {
  461. /**
  462. * Filter the display of sharing counts next to the sharing buttons.
  463. *
  464. * @module sharedaddy
  465. *
  466. * @since 3.2.0
  467. *
  468. * @param bool true Control the display of counters next to the sharing buttons. Default to true.
  469. */
  470. if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) :
  471. $sharing_post_urls = array_filter( $jetpack_sharing_counts );
  472. if ( $sharing_post_urls ) :
  473. ?>
  474. <script type="text/javascript">
  475. window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>;
  476. </script>
  477. <?php
  478. endif;
  479. endif;
  480. wp_enqueue_script( 'sharing-js' );
  481. $sharing_js_options = array(
  482. 'lang' => get_base_recaptcha_lang_code(),
  483. /** This filter is documented in modules/sharedaddy/sharing-service.php */
  484. 'counts' => apply_filters( 'jetpack_sharing_counts', true )
  485. );
  486. wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options);
  487. }
  488. $sharer = new Sharing_Service();
  489. $enabled = $sharer->get_blog_services();
  490. foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
  491. $service->display_footer();
  492. }
  493. }
  494. function sharing_add_header() {
  495. $sharer = new Sharing_Service();
  496. $enabled = $sharer->get_blog_services();
  497. foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
  498. $service->display_header();
  499. }
  500. if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
  501. wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) .'sharing.css', array(), JETPACK__VERSION );
  502. wp_enqueue_style( 'social-logos' );
  503. }
  504. }
  505. add_action( 'wp_head', 'sharing_add_header', 1 );
  506. function sharing_process_requests() {
  507. global $post;
  508. // Only process if: single post and share=X defined
  509. if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) {
  510. $sharer = new Sharing_Service();
  511. $service = $sharer->get_service( $_GET['share'] );
  512. if ( $service ) {
  513. $service->process_request( $post, $_POST );
  514. }
  515. }
  516. }
  517. add_action( 'template_redirect', 'sharing_process_requests', 9 );
  518. function sharing_display( $text = '', $echo = false ) {
  519. global $post, $wp_current_filter;
  520. require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
  521. if ( Jetpack_Sync_Settings::is_syncing() ) {
  522. return $text;
  523. }
  524. if ( empty( $post ) )
  525. return $text;
  526. if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
  527. return $text;
  528. }
  529. // Don't output flair on excerpts
  530. if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
  531. return $text;
  532. }
  533. // Don't allow flair to be added to the_content more than once (prevent infinite loops)
  534. $done = false;
  535. foreach ( $wp_current_filter as $filter ) {
  536. if ( 'the_content' == $filter ) {
  537. if ( $done )
  538. return $text;
  539. else
  540. $done = true;
  541. }
  542. }
  543. // check whether we are viewing the front page and whether the front page option is checked
  544. $options = get_option( 'sharing-options' );
  545. $display_options = $options['global']['show'];
  546. if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) )
  547. return $text;
  548. if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) {
  549. // Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
  550. // We only want to output the sharing buttons once. Let's stick with the_content().
  551. return $text;
  552. }
  553. $sharer = new Sharing_Service();
  554. $global = $sharer->get_global_options();
  555. $show = false;
  556. if ( !is_feed() ) {
  557. if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) {
  558. $show = true;
  559. } elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) {
  560. $show = true;
  561. }
  562. }
  563. /**
  564. * Filter to decide if sharing buttons should be displayed.
  565. *
  566. * @module sharedaddy
  567. *
  568. * @since 1.1.0
  569. *
  570. * @param bool $show Should the sharing buttons be displayed.
  571. * @param WP_Post $post The post to share.
  572. */
  573. $show = apply_filters( 'sharing_show', $show, $post );
  574. // Disabled for this post?
  575. $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
  576. if ( !empty( $switched_status ) ) {
  577. $show = false;
  578. }
  579. // Private post?
  580. $post_status = get_post_status( $post->ID );
  581. if ( 'private' === $post_status ) {
  582. $show = false;
  583. }
  584. // Allow to be used on P2 ajax requests for latest posts.
  585. if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'get_latest_posts' == $_REQUEST['action'] )
  586. $show = true;
  587. $sharing_content = '';
  588. $enabled = false;
  589. if ( $show ) {
  590. /**
  591. * Filters the list of enabled Sharing Services.
  592. *
  593. * @module sharedaddy
  594. *
  595. * @since 2.2.3
  596. *
  597. * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
  598. */
  599. $enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
  600. if ( count( $enabled['all'] ) > 0 ) {
  601. global $post;
  602. $dir = get_option( 'text_direction' );
  603. // Wrapper
  604. $sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
  605. if ( $global['sharing_label'] != '' ) {
  606. $sharing_content .= sprintf(
  607. /**
  608. * Filter the sharing buttons' headline structure.
  609. *
  610. * @module sharedaddy
  611. *
  612. * @since 4.4.0
  613. *
  614. * @param string $sharing_headline Sharing headline structure.
  615. * @param string $global['sharing_label'] Sharing title.
  616. * @param string $sharing Module name.
  617. */
  618. apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
  619. esc_html( $global['sharing_label'] )
  620. );
  621. }
  622. $sharing_content .= '<div class="sd-content"><ul>';
  623. // Visible items
  624. $visible = '';
  625. foreach ( $enabled['visible'] as $id => $service ) {
  626. // Individual HTML for sharing service
  627. $visible .= '<li class="share-' . $service->get_class() . '">' . $service->get_display( $post ) . '</li>';
  628. }
  629. $parts = array();
  630. $parts[] = $visible;
  631. if ( count( $enabled['hidden'] ) > 0 ) {
  632. if ( count( $enabled['visible'] ) > 0 )
  633. $expand = __( 'More', 'jetpack' );
  634. else
  635. $expand = __( 'Share', 'jetpack' );
  636. $parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>'.$expand.'</span></a></li>';
  637. }
  638. if ( $dir == 'rtl' )
  639. $parts = array_reverse( $parts );
  640. $sharing_content .= implode( '', $parts );
  641. $sharing_content .= '<li class="share-end"></li></ul>';
  642. if ( count( $enabled['hidden'] ) > 0 ) {
  643. $sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
  644. if ( count( $enabled['hidden'] ) == 1 )
  645. $sharing_content .= 'width:150px;';
  646. $sharing_content .= '">';
  647. if ( count( $enabled['hidden'] ) == 1 )
  648. $sharing_content .= '<ul style="background-image:none;">';
  649. else
  650. $sharing_content .= '<ul>';
  651. $count = 1;
  652. foreach ( $enabled['hidden'] as $id => $service ) {
  653. // Individual HTML for sharing service
  654. $sharing_content .= '<li class="share-'.$service->get_class().'">';
  655. $sharing_content .= $service->get_display( $post );
  656. $sharing_content .= '</li>';
  657. if ( ( $count % 2 ) == 0 )
  658. $sharing_content .= '<li class="share-end"></li>';
  659. $count ++;
  660. }
  661. // End of wrapper
  662. $sharing_content .= '<li class="share-end"></li></ul></div></div>';
  663. }
  664. $sharing_content .= '</div></div></div>';
  665. // Register our JS
  666. if ( defined( 'JETPACK__VERSION' ) ) {
  667. $ver = JETPACK__VERSION;
  668. } else {
  669. $ver = '20141212';
  670. }
  671. wp_register_script(
  672. 'sharing-js',
  673. Jetpack::get_file_url_for_environment(
  674. '_inc/build/sharedaddy/sharing.min.js',
  675. 'modules/sharedaddy/sharing.js'
  676. ),
  677. array( 'jquery' ),
  678. $ver
  679. );
  680. // Enqueue scripts for the footer
  681. add_action( 'wp_footer', 'sharing_add_footer' );
  682. }
  683. }
  684. /**
  685. * Filters the content markup of the Jetpack sharing links
  686. *
  687. * @module sharedaddy
  688. *
  689. * @since 3.8.0
  690. * @since 6.2.0 Started sending $enabled as a second parameter.
  691. *
  692. * @param string $sharing_content Content markup of the Jetpack sharing links
  693. * @param array $enabled Array of Sharing Services currently enabled.
  694. */
  695. $sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content, $enabled );
  696. if ( $echo )
  697. echo $text . $sharing_markup;
  698. else
  699. return $text . $sharing_markup;
  700. }
  701. add_filter( 'the_content', 'sharing_display', 19 );
  702. add_filter( 'the_excerpt', 'sharing_display', 19 );
  703. function get_base_recaptcha_lang_code() {
  704. $base_recaptcha_lang_code_mapping = array(
  705. 'en' => 'en',
  706. 'nl' => 'nl',
  707. 'fr' => 'fr',
  708. 'fr-be' => 'fr',
  709. 'fr-ca' => 'fr',
  710. 'fr-ch' => 'fr',
  711. 'de' => 'de',
  712. 'pt' => 'pt',
  713. 'pt-br' => 'pt',
  714. 'ru' => 'ru',
  715. 'es' => 'es',
  716. 'tr' => 'tr'
  717. );
  718. $blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' );
  719. if( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) )
  720. return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
  721. // if no base mapping is found return default 'en'
  722. return 'en';
  723. }