PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.8
Jetpack – WP Security, Backup, Speed, & Growth v14.8
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / extensions / blocks / sharing-button / sharing-button.php
sharing-button.php
356 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sharing Buttons Block.
4 *
5 * @since 13.1
6 *
7 * @package automattic/jetpack
8 */
9
10 namespace Automattic\Jetpack\Extensions\Sharing_Button_Block;
11
12 use Automattic\Jetpack\Blocks;
13 use Automattic\Jetpack\Modules;
14 use Automattic\Jetpack\Status\Host;
15 use Jetpack_Gutenberg;
16 use WP_Block_Template;
17
18 require_once __DIR__ . '/class-sharing-source-block.php';
19 require_once __DIR__ . '/components/social-icons.php';
20
21 const PARENT_BLOCK_NAME = 'jetpack/sharing-buttons';
22 const INNER_BLOCK_NAME = 'jetpack/sharing-button';
23
24 /**
25 * Registers the block for use in Gutenberg
26 * This is done via an action so that we can disable
27 * registration if we need to.
28 */
29 function register_block() {
30 Blocks::jetpack_register_block(
31 __DIR__,
32 array( 'render_callback' => __NAMESPACE__ . '\render_block' )
33 );
34
35 /*
36 * Automatically add the sharing block to the end of single posts.
37 */
38 add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 );
39 add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 );
40 }
41 add_action( 'init', __NAMESPACE__ . '\register_block' );
42
43 /**
44 * Sharing Buttons block registration/dependency declaration.
45 *
46 * @param array $attr Array containing the Sharing Buttons block attributes.
47 * @param string $content String containing the Sharing Buttons block content.
48 * @param object $block Object containing block data.
49 *
50 * @return string
51 */
52 function render_block( $attr, $content, $block ) {
53 $service_name = $attr['service'];
54 $title = $attr['label'] ?? $service_name;
55 $icon = get_social_logo( $service_name );
56 $style_type = $block->context['styleType'] ?? 'icon-text';
57 $post_id = $block->context['postId'] ?? 0;
58 $data_shared = sprintf(
59 'sharing-%1$s-%2$d',
60 $service_name,
61 $post_id
62 );
63
64 $services = get_services();
65 if ( ! array_key_exists( $service_name, $services ) ) {
66 return $content;
67 }
68
69 $service = new $services[ $service_name ]( $service_name, array() );
70 $link_props = $service->get_link(
71 $post_id,
72 'share=' . esc_attr( $service_name ) . '&nb=1',
73 esc_attr( $data_shared )
74 );
75 $link_url = $link_props['url'];
76 $link_classes = sprintf(
77 'jetpack-sharing-button__button style-%1$s share-%2$s',
78 $style_type,
79 $service_name
80 );
81
82 $accessible_name = sprintf(
83 /* translators: %s refers to a string representation of sharing service, e.g. Facebook */
84 esc_html__( 'Click to share on %s', 'jetpack' ),
85 esc_html( $title )
86 );
87
88 $accessible_name .= __( ' (Opens in new window)', 'jetpack' );
89
90 $block_class_name = 'jetpack-sharing-button__list-item';
91
92 if ( $service_name === 'share' ) {
93 $block_class_name .= ' tooltip';
94 /* translators: aria label for SMS sharing button */
95 $accessible_name = esc_attr__( 'Share using Native tools', 'jetpack' );
96 $link_props = $service->get_link( $post_id );
97 }
98
99 $link_url = $link_props['url'];
100 $link_classes = sprintf(
101 'jetpack-sharing-button__button style-%1$s share-%2$s',
102 $style_type,
103 $service_name
104 );
105
106 $styles = array();
107 if (
108 array_key_exists( 'iconColorValue', $block->context )
109 && ! empty( $block->context['iconColorValue'] )
110 ) {
111 $styles['color'] = $block->context['iconColorValue'];
112 }
113 if (
114 array_key_exists( 'iconBackgroundColorValue', $block->context )
115 && ! empty( $block->context['iconBackgroundColorValue'] )
116 ) {
117 $styles['background-color'] = $block->context['iconBackgroundColorValue'];
118 }
119 $link_styles = '';
120 foreach ( $styles as $property => $value ) {
121 $link_styles .= $property . ':' . $value . ';';
122 }
123
124 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
125
126 $component = '<li class="' . esc_attr( $block_class_name ) . '">';
127 $component .= sprintf(
128 '<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">',
129 esc_url( $link_url ),
130 esc_attr( $link_classes ),
131 esc_attr( $link_styles ),
132 esc_attr( $service_name ),
133 esc_attr( $data_shared )
134 );
135
136 // Add hidden span with accessible name
137 $component .= sprintf(
138 '<span id="%s" hidden>%s</span>',
139 esc_attr( $data_shared ),
140 esc_html( $accessible_name )
141 );
142
143 $component .= $style_type !== 'text' ? $icon : '';
144 $component .= '<span class="jetpack-sharing-button__service-label" aria-hidden="true">' . esc_html( $title ) . '</span>';
145 if ( $service_name === 'share' ) {
146 $component .= '<span class="tooltiptext" aria-live="assertive">' . esc_html__( 'Copied to clipboard', 'jetpack' ) . '</span>';
147 }
148 $component .= '</a>';
149 $component .= '</li>';
150
151 return $component;
152 }
153
154 /**
155 * Get services for the Sharing Buttons block.
156 *
157 * @return array Array of services.
158 */
159 function get_services() {
160 $services = array(
161 'print' => Share_Print_Block::class,
162 'mail' => Share_Email_Block::class,
163 'facebook' => Share_Facebook_Block::class,
164 'linkedin' => Share_LinkedIn_Block::class,
165 'reddit' => Share_Reddit_Block::class,
166 'twitter' => Share_Twitter_Block::class,
167 'tumblr' => Share_Tumblr_Block::class,
168 'pinterest' => Share_Pinterest_Block::class,
169 'pocket' => Share_Pocket_Block::class,
170 'telegram' => Share_Telegram_Block::class,
171 'threads' => Share_Threads_Block::class,
172 'whatsapp' => Jetpack_Share_WhatsApp_Block::class,
173 'mastodon' => Share_Mastodon_Block::class,
174 'nextdoor' => Share_Nextdoor_Block::class,
175 'bluesky' => Share_Bluesky_Block::class,
176 'x' => Share_X_Block::class,
177 'share' => Share_Native_Block::class,
178 );
179
180 return $services;
181 }
182
183 /**
184 * Launch sharing requests on page load when a specific query string is used.
185 *
186 * @return void
187 */
188 function sharing_process_requests() {
189 global $post;
190
191 // Only process if: single post and share={service} defined
192 if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
193 $services = get_services();
194 $service_name = sanitize_text_field( wp_unslash( $_GET['share'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
195
196 // Only allow services that have been defined in get_services().
197 if ( ! array_key_exists( $service_name, $services ) ) {
198 return;
199 }
200
201 $service = new $services[ ( $service_name ) ]( $service_name, array() ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
202 if ( $service ) {
203 $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
204 }
205 }
206 }
207
208 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking for the data being present.
209 if ( isset( $_GET['share'] ) ) {
210 add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 );
211 }
212
213 /**
214 * Automatically add the Sharing Buttons block to the end of the Single Posts template.
215 *
216 * @since 13.2
217 *
218 * @param array $hooked_block_types The list of hooked block types.
219 * @param string $relative_position The relative position of the hooked blocks. Can be one of 'before', 'after', 'first_child', or 'last_child'.
220 * @param string $anchor_block_type The anchor block type.
221 * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
222 *
223 * @return array
224 */
225 function add_block_to_single_posts_template( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {
226 // Add the block at the end of the post content.
227 if (
228 'after' !== $relative_position
229 || 'core/post-content' !== $anchor_block_type
230 ) {
231 return $hooked_block_types;
232 }
233
234 // Only automate the addition of the block in block-based themes.
235 if ( ! wp_is_block_theme() ) {
236 return $hooked_block_types;
237 }
238
239 // Proceed if the user has toggled the auto-addition in Jetpack settings.
240 if ( ! get_option( 'jetpack_sharing_buttons_auto_add' ) ) {
241 return $hooked_block_types;
242 }
243
244 /*
245 * The Sharing module must be disabled.
246 * We do not want to automatically insert sharing buttons twice.
247 * On WordPress.com Simple the module is always active so we must check differently.
248 * There, we check if buttons are enabled on single posts and pages.
249 */
250 if ( ( new Host() )->is_wpcom_simple() ) {
251 if ( ! class_exists( 'Sharing_Service' ) ) {
252 include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php';
253 }
254
255 $sharer = new \Sharing_Service();
256 $global = $sharer->get_global_options();
257 if (
258 ! $global['show']
259 || in_array( 'post', $global['show'], true )
260 || in_array( 'page', $global['show'], true )
261 ) {
262 return $hooked_block_types;
263 }
264 } elseif ( ( new Modules() )->is_active( 'sharedaddy' ) ) {
265 return $hooked_block_types;
266 }
267
268 // Only hook into page and single post templates.
269 if (
270 ! $context instanceof WP_Block_Template
271 || ! property_exists( $context, 'slug' )
272 || empty( $context->slug )
273 || ! preg_match( '/^(page|single)/', $context->slug )
274 ) {
275 return $hooked_block_types;
276 }
277
278 $hooked_block_types[] = PARENT_BLOCK_NAME;
279 return $hooked_block_types;
280 }
281
282 /**
283 * Add default services to the block we add to the post content by default.
284 *
285 * @since 13.2
286 *
287 * @param array $parsed_hooked_block The parsed block array for the given hooked block type.
288 * @param string $hooked_block_type The hooked block type name.
289 * @param string $relative_position The relative position of the hooked block.
290 * @param array $parsed_anchor_block The anchor block, in parsed block array format.
291 * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block.
292 *
293 * @return array
294 */
295 function add_default_services_to_block( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
296 // Is the hooked block adjacent to the anchor block?
297 if ( 'after' !== $relative_position ) {
298 return $parsed_hooked_block;
299 }
300
301 // Use the icon style by default.
302 $parsed_hooked_block['attrs']['styleType'] = 'icon';
303
304 // Add default services (inner blocks) to the block.
305 $parsed_hooked_block['innerBlocks'] = array(
306 array(
307 'blockName' => INNER_BLOCK_NAME,
308 'innerContent' => array(),
309 'attrs' => array(
310 'service' => 'facebook',
311 'label' => esc_html__( 'Facebook', 'jetpack' ),
312 ),
313 ),
314 array(
315 'blockName' => INNER_BLOCK_NAME,
316 'innerContent' => array(),
317 'attrs' => array(
318 'service' => 'x',
319 'label' => esc_html__( 'X', 'jetpack' ),
320 ),
321 ),
322 array(
323 'blockName' => INNER_BLOCK_NAME,
324 'innerContent' => array(),
325 'attrs' => array(
326 'service' => 'mastodon',
327 'label' => esc_html__( 'Mastodon', 'jetpack' ),
328 ),
329 ),
330 );
331
332 // Wrap inner blocks in our sharing buttons markup.
333 $parsed_hooked_block['innerContent'] = array(
334 '<ul class="wp-block-jetpack-sharing-buttons has-normal-icon-size jetpack-sharing-buttons__services-list" id="jetpack-sharing-serivces-list">',
335 null,
336 null,
337 null,
338 '</ul>',
339 );
340
341 // Wrap the whole thing in a group block.
342 return array(
343 'blockName' => 'core/group',
344 'attrs' => array(
345 // Does the anchor block have a layout attribute? If so, use it in the group to maintain the same alignment.
346 'layout' => $parsed_anchor_block['attrs']['layout'] ?? 'null',
347 ),
348 'innerBlocks' => array( $parsed_hooked_block ),
349 'innerContent' => array(
350 '<div class="wp-block-group">',
351 null,
352 '</div>',
353 ),
354 );
355 }
356