Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.36% covered (danger)
5.36%
6 / 112
25.00% covered (danger)
25.00%
1 / 4
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Sharing_Button_Block\register_block
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
Automattic\Jetpack\Extensions\Sharing_Button_Block\render_block
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 1
132
Automattic\Jetpack\Extensions\Sharing_Button_Block\get_services
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Extensions\Sharing_Button_Block\sharing_process_requests
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Sharing Buttons Block.
4 *
5 * @since 13.1
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\Sharing_Button_Block;
11
12use Automattic\Jetpack\Blocks;
13use Automattic\Jetpack\Sharing_Likes\Hooked_Blocks\Hooked_Blocks;
14use Automattic\Jetpack\Status\Host;
15use Jetpack_Gutenberg;
16
17if ( ! defined( 'ABSPATH' ) ) {
18    exit( 0 );
19}
20
21require_once __DIR__ . '/class-sharing-source-block.php';
22require_once __DIR__ . '/components/social-icons.php';
23
24const PARENT_BLOCK_NAME = 'jetpack/sharing-buttons';
25const INNER_BLOCK_NAME  = 'jetpack/sharing-button';
26
27/**
28 * Registers the block for use in Gutenberg
29 * This is done via an action so that we can disable
30 * registration if we need to.
31 */
32function register_block() {
33    Blocks::jetpack_register_block(
34        __DIR__,
35        array( 'render_callback' => __NAMESPACE__ . '\render_block' )
36    );
37
38    // load-jetpack.php sets the package up everywhere else, but Simple's front end loads neither it nor sharing.php.
39    if ( ( new Host() )->is_wpcom_simple() && class_exists( Hooked_Blocks::class ) ) {
40        Hooked_Blocks::init();
41    }
42}
43add_action( 'init', __NAMESPACE__ . '\register_block' );
44
45/**
46 * Sharing Buttons block registration/dependency declaration.
47 *
48 * @param array  $attr    Array containing the Sharing Buttons block attributes.
49 * @param string $content String containing the Sharing Buttons block content.
50 * @param object $block   Object containing block data.
51 *
52 * @return string
53 */
54function render_block( $attr, $content, $block ) {
55    $services = get_services();
56    if ( ! isset( $attr['service'] ) || ! array_key_exists( $attr['service'], $services ) ) {
57        return $content;
58    }
59
60    $service_name = $attr['service'];
61    $title        = $attr['label'] ?? $service_name;
62    $icon         = get_social_logo( $service_name );
63    $style_type   = $block->context['styleType'] ?? 'icon-text';
64    $post_id      = $block->context['postId'] ?? 0;
65    $data_shared  = sprintf(
66        'sharing-%1$s-%2$d',
67        $service_name,
68        $post_id
69    );
70
71    $service      = new $services[ $service_name ]( $service_name, array() );
72    $link_props   = $service->get_link(
73        $post_id,
74        'share=' . esc_attr( $service_name ) . '&nb=1',
75        esc_attr( $data_shared )
76    );
77    $link_url     = $link_props['url'];
78    $link_classes = sprintf(
79        'jetpack-sharing-button__button style-%1$s share-%2$s',
80        $style_type,
81        $service_name
82    );
83
84    $accessible_name = sprintf(
85        /* translators: %s refers to a string representation of sharing service, e.g. Facebook */
86        esc_html__( 'Share on %s', 'jetpack' ),
87        esc_html( $title )
88    );
89
90    $accessible_name .= __( ' (Opens in new window)', 'jetpack' );
91
92    $block_class_name = 'jetpack-sharing-button__list-item';
93
94    if ( $service_name === 'share' ) {
95        $block_class_name .= ' tooltip';
96        /* translators: aria label for SMS sharing button */
97        $accessible_name = esc_attr__( 'Share using Native tools', 'jetpack' );
98        $link_props      = $service->get_link( $post_id );
99    }
100
101    $link_url     = $link_props['url'];
102    $link_classes = sprintf(
103        'jetpack-sharing-button__button style-%1$s share-%2$s',
104        $style_type,
105        $service_name
106    );
107
108    $styles = array();
109    if (
110        array_key_exists( 'iconColorValue', $block->context )
111        && ! empty( $block->context['iconColorValue'] )
112    ) {
113        $styles['color'] = $block->context['iconColorValue'];
114    }
115    if (
116        array_key_exists( 'iconBackgroundColorValue', $block->context )
117        && ! empty( $block->context['iconBackgroundColorValue'] )
118    ) {
119        $styles['background-color'] = $block->context['iconBackgroundColorValue'];
120    }
121    $link_styles = '';
122    foreach ( $styles as $property => $value ) {
123        $link_styles .= $property . ':' . $value . ';';
124    }
125
126    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
127
128    $component  = '<li class="' . esc_attr( $block_class_name ) . '">';
129    $component .= sprintf(
130        '<a href="%1$s" target="_blank" rel="nofollow noopener noreferrer" class="%2$s" style="%3$s" data-service="%4$s" data-shared="%5$s" aria-labelledby="%5$s">',
131        esc_url( $link_url ),
132        esc_attr( $link_classes ),
133        esc_attr( $link_styles ),
134        esc_attr( $service_name ),
135        esc_attr( $data_shared )
136    );
137
138    // Add hidden span with accessible name
139    $component .= sprintf(
140        '<span id="%s" hidden>%s</span>',
141        esc_attr( $data_shared ),
142        esc_html( $accessible_name )
143    );
144
145    $component .= $style_type !== 'text' ? $icon : '';
146    $component .= '<span class="jetpack-sharing-button__service-label" aria-hidden="true">' . esc_html( $title ) . '</span>';
147    if ( $service_name === 'share' ) {
148        $component .= '<span class="tooltiptext" aria-live="assertive">' . esc_html__( 'Copied to clipboard', 'jetpack' ) . '</span>';
149    }
150    $component .= '</a>';
151    $component .= '</li>';
152
153    return $component;
154}
155
156/**
157 * Get services for the Sharing Buttons block.
158 *
159 * @return array Array of services.
160 */
161function get_services() {
162    $services = array(
163        'print'     => Share_Print_Block::class,
164        'mail'      => Share_Email_Block::class,
165        'facebook'  => Share_Facebook_Block::class,
166        'linkedin'  => Share_LinkedIn_Block::class,
167        'reddit'    => Share_Reddit_Block::class,
168        'twitter'   => Share_Twitter_Block::class,
169        'tumblr'    => Share_Tumblr_Block::class,
170        'pinterest' => Share_Pinterest_Block::class,
171        'telegram'  => Share_Telegram_Block::class,
172        'threads'   => Share_Threads_Block::class,
173        'whatsapp'  => Jetpack_Share_WhatsApp_Block::class,
174        'mastodon'  => Share_Mastodon_Block::class,
175        'nextdoor'  => Share_Nextdoor_Block::class,
176        'bluesky'   => Share_Bluesky_Block::class,
177        'x'         => Share_X_Block::class,
178        'share'     => Share_Native_Block::class,
179    );
180
181    return $services;
182}
183
184/**
185 * Launch sharing requests on page load when a specific query string is used.
186 *
187 * @return void
188 */
189function sharing_process_requests() {
190    global $post;
191
192    // Only process if: single post and share={service} defined
193    if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
194        $services     = get_services();
195        $service_name = sanitize_text_field( wp_unslash( $_GET['share'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
196
197        // Only allow services that have been defined in get_services().
198        if ( ! array_key_exists( $service_name, $services ) ) {
199            return;
200        }
201
202        $service = new $services[ ( $service_name ) ]( $service_name, array() );
203        $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
204    }
205}
206
207// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking for the data being present.
208if ( isset( $_GET['share'] ) ) {
209    add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 );
210}