<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* Utilities to register and interact with a sharing service.
* Sharing_Service gets info about a service.
* Sharing_Service_Total and Sharing_Post_Total get stats data.
* @package automattic/jetpack
* phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Sync\Settings;
if ( ! defined( 'ABSPATH' ) ) {
require_once __DIR__ . '/sharing-sources.php';
define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION );
* Interact with a sharing service.
* Should the service be available globally?
public $default_sharing_label = '';
* Initialize the sharing service.
* Only run this method once upon module loading.
public static function init() {
add_filter( 'the_content', 'sharing_display', 19 );
add_filter( 'the_excerpt', 'sharing_display', 19 );
public function __construct() {
$this->default_sharing_label = __( 'Share this:', 'jetpack' );
* Gets a generic list of all services, without any config
public function get_all_services_blog() {
$options = get_option( 'sharing-options' );
$all = $this->get_all_services();
foreach ( $all as $id => $name ) {
if ( isset( $all[ $id ] ) ) {
// Pre-load custom modules otherwise they won't know who they are
if ( str_starts_with( $id, 'custom-' ) && is_array( $options[ $id ] ) ) {
$config = $options[ $id ];
$services[ $id ] = new $all[ $id ]( $id, $config );
* Gets a list of all available service names and classes
* @param bool $include_custom Include custom sharing services.
public function get_all_services( $include_custom = true ) {
// if you update this list, please update the REST API tests
// in bin/tests/api/suites/SharingTest.php
'print' => 'Share_Print',
'email' => 'Share_Email',
'facebook' => 'Share_Facebook',
'linkedin' => 'Share_LinkedIn',
'reddit' => 'Share_Reddit',
'twitter' => 'Share_Twitter',
'tumblr' => 'Share_Tumblr',
'pinterest' => 'Share_Pinterest',
'pocket' => 'Share_Pocket',
'telegram' => 'Share_Telegram',
'threads' => 'Share_Threads',
'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp',
'mastodon' => 'Share_Mastodon',
'nextdoor' => 'Share_Nextdoor',
'bluesky' => 'Share_Bluesky',
if ( is_multisite() && is_plugin_active( 'press-this/press-this-plugin.php' ) ) {
$services['press-this'] = 'Share_PressThis';
// Add any custom services in
$options = $this->get_global_options();
if ( isset( $options['custom'] ) ) {
foreach ( $options['custom'] as $custom_id ) {
$services[ $custom_id ] = 'Share_Custom';
* Filters the list of available Sharing Services.
* @param array $services Array of all available Sharing Services.
return apply_filters( 'sharing_services', $services );
* Save a new custom sharing service.
* @param string $label Service name.
* @param string $url Service sharing URL.
* @param string $icon Service icon.
* @return bool|Share_Custom
public function new_service( $label, $url, $icon ) {
$label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
$url = trim( esc_url_raw( $url ) );
$icon = trim( esc_url_raw( $icon ) );
if ( $label && $url && $icon ) {
$options = get_option( 'sharing-options' );
if ( ! is_array( $options ) ) {
$service_id = 'custom-' . time();
// Add a new custom service
$options['global']['custom'][] = $service_id;
if ( false !== $this->global ) {
$this->global['custom'][] = $service_id;
update_option( 'sharing-options', $options );
// Create a custom service and set the options for it
$service = new Share_Custom(
$this->set_service( $service_id, $service );
* Delete a sharing service.
* @param string $service_id Service ID.
public function delete_service( $service_id ) {
$options = get_option( 'sharing-options' );
if ( isset( $options[ $service_id ] ) ) {
unset( $options[ $service_id ] );
$key = array_search( $service_id, $options['global']['custom'], true );
unset( $options['global']['custom'][ $key ] );
update_option( 'sharing-options', $options );
* Save enabled sharing services.
* @param array $visible Visible sharing services.
* @param array $hidden Hidden sharing services (available under a dropdown).
public function set_blog_services( array $visible, array $hidden ) {
$services = $this->get_all_services();
$available = array_keys( $services );
// Only allow services that we have defined
$hidden = array_intersect( $hidden, $available );
$visible = array_intersect( $visible, $available );
// Ensure we don't have the same ones in hidden and visible
$hidden = array_diff( $hidden, $visible );
* Control the state of the list of sharing services.
* Array of options describing the state of the sharing services.
* @type array $services List of all available service names and classes.
* @type array $available Validated list of all available service names and classes.
* @type array $hidden List of services hidden behind a "More" button.
* @type array $visible List of visible services.
* @type array $this->get_blog_services() Array of Sharing Services currently enabled.
'sharing_get_services_state',
'available' => $available,
'currently_enabled' => $this->get_blog_services(),
* Get information about enabled sharing services on the site.
public function get_blog_services() {
$options = get_option( 'sharing-options' );
$enabled = get_option( 'sharing-services' );
$services = $this->get_all_services();
* Check if options exist and are well formatted.
* This avoids issues on sites with corrupted options.
* @see https://github.com/Automattic/jetpack/issues/6121
if ( ! is_array( $options ) || ! isset( $options['button_style'] ) || ! isset( $options['global'] ) ) {
$global_options = array( 'global' => $this->get_global_options() );
$options = is_array( $options )
? array_merge( $options, $global_options )
$global = $options['global'];
if ( ! is_array( $enabled ) ) {
* Filters the list of default Sharing Services.
* @param array $enabled Array of default Sharing Services.
$enabled = apply_filters( 'sharing_default_services', $enabled );
// Cleanup after any filters that may have produced duplicate services
if ( isset( $enabled['visible'] ) && is_array( $enabled['visible'] ) ) {
$enabled['visible'] = array_unique( $enabled['visible'] );
$enabled['visible'] = array();
if ( isset( $enabled['hidden'] ) && is_array( $enabled['hidden'] ) ) {
$enabled['hidden'] = array_unique( $enabled['hidden'] );
$enabled['hidden'] = array();
// Form the enabled services
foreach ( $blog as $area => $stuff ) {
foreach ( (array) $enabled[ $area ] as $service ) {
if ( isset( $services[ $service ] ) ) {
if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) {
$options[ $service ] = array();
$blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) );
* Filters the list of enabled Sharing Services.
* @param array $blog Array of enabled Sharing Services.
$blog = apply_filters( 'sharing_services_enabled', $blog );
if ( ( is_countable( $blog['visible'] ) && count( $blog['visible'] ) ) || ( is_countable( $blog['hidden'] ) && count( $blog['hidden'] ) ) ) {
add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
// Convenience for checking if a service is present
$blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
* Get information about a specific enabled sharing service.
* @param string $service_name Service name.
* @return bool|Sharing_Source
public function get_service( $service_name ) {
$services = $this->get_blog_services();
if ( isset( $services['visible'][ $service_name ] ) ) {
return $services['visible'][ $service_name ];
if ( isset( $services['hidden'][ $service_name ] ) ) {
return $services['hidden'][ $service_name ];
* Update global sharing options.
* @param array $data Array of new sharing options to save.
public function set_global_options( $data ) {
$options = get_option( 'sharing-options' );
if ( ! is_array( $options ) ) {
$options['global'] = array(
'button_style' => 'icon-text',
'sharing_label' => $this->default_sharing_label,
'show' => ! isset( $options['global'] ) ? array( 'post', 'page' ) : array(),
'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array(),
* Filters global sharing settings.
* @param array $options['global'] Array of global sharing settings.
$options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
// Validate options and set from our data
isset( $data['button_style'] )
&& in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ), true )
$options['global']['button_style'] = $data['button_style'];
if ( isset( $data['sharing_label'] ) ) {
if ( $this->default_sharing_label === $data['sharing_label'] ) {
$options['global']['sharing_label'] = false;
$options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
isset( $data['open_links'] )
&& in_array( $data['open_links'], array( 'new', 'same' ), true )
$options['global']['open_links'] = $data['open_links'];
$shows = array_values( get_post_types( array( 'public' => true ) ) );
if ( isset( $data['show'] ) ) {
if ( is_scalar( $data['show'] ) ) {
switch ( $data['show'] ) {
$data['show'] = array( 'post', 'page' );
$data['show'] = array( 'index' );
$data['show'] = array( 'post', 'page', 'index' );
$data['show'] = array_intersect( $data['show'], $shows );
$options['global']['show'] = $data['show'];
update_option( 'sharing-options', $options );
return $options['global'];
* Get global sharing options for the site.
public function get_global_options() {
if ( $this->global === false ) {
$options = get_option( 'sharing-options' );
if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) {
$this->global = $options['global'];
$this->global = $this->set_global_options( $options );
if ( ! isset( $this->global['show'] ) ) {
$this->global['show'] = array( 'post', 'page' );
} elseif ( is_scalar( $this->global['show'] ) ) {
switch ( $this->global['show'] ) {
$this->global['show'] = array( 'post', 'page' );
$this->global['show'] = array( 'index' );