No Description

sharing-service.php 28KB

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