← All changes
|
extensions/blocks/sharing-button/sharing-button.php
+47
-49
13.3.3
→
16.3-a.1
View file →
| @@ -12,9 +12,14 @@ | ||
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | 13 | use Automattic\Jetpack\Modules; |
| 14 | 14 | use Automattic\Jetpack\Status\Host; |
| 15 | 15 | use Jetpack_Gutenberg; |
| 16 | +use WP_Block_Template; | |
| 16 | 17 | |
| 18 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | + exit( 0 ); | |
| 20 | +} | |
| 21 | + | |
| 17 | 22 | require_once __DIR__ . '/class-sharing-source-block.php'; |
| 18 | 23 | require_once __DIR__ . '/components/social-icons.php'; |
| 19 | 24 | |
| 20 | 25 | const PARENT_BLOCK_NAME = 'jetpack/sharing-buttons'; |
| @@ -31,17 +36,12 @@ | ||
| 31 | 36 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 32 | 37 | ); |
| 33 | 38 | |
| 34 | 39 | /* |
| 35 | - * Automatically add the sharing block to the end of single posts | |
| 36 | - * only when running WordPress 6.5 or later. | |
| 37 | - * @todo: remove when WordPress 6.5 is the minimum required version. | |
| 40 | + * Automatically add the sharing block to the end of single posts. | |
| 38 | 41 | */ |
| 39 | - global $wp_version; | |
| 40 | - if ( version_compare( $wp_version, '6.5-beta2', '>=' ) ) { | |
| 41 | - add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 ); | |
| 42 | - add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 ); | |
| 43 | - } | |
| 42 | + add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 ); | |
| 43 | + add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 ); | |
| 44 | 44 | } |
| 45 | 45 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 46 | 46 | |
| 47 | 47 | /** |
| @@ -53,8 +53,13 @@ | ||
| 53 | 53 | * |
| 54 | 54 | * @return string |
| 55 | 55 | */ |
| 56 | 56 | function render_block( $attr, $content, $block ) { |
| 57 | + $services = get_services(); | |
| 58 | + if ( ! isset( $attr['service'] ) || ! array_key_exists( $attr['service'], $services ) ) { | |
| 59 | + return $content; | |
| 60 | + } | |
| 61 | + | |
| 57 | 62 | $service_name = $attr['service']; |
| 58 | 63 | $title = $attr['label'] ?? $service_name; |
| 59 | 64 | $icon = get_social_logo( $service_name ); |
| 60 | 65 | $style_type = $block->context['styleType'] ?? 'icon-text'; |
| @@ -64,37 +69,28 @@ | ||
| 64 | 69 | $service_name, |
| 65 | 70 | $post_id |
| 66 | 71 | ); |
| 67 | 72 | |
| 68 | - $services = get_services(); | |
| 69 | - if ( ! array_key_exists( $service_name, $services ) ) { | |
| 70 | - return $content; | |
| 71 | - } | |
| 72 | - | |
| 73 | - $service = new $services[ $service_name ]( $service_name, array() ); | |
| 74 | - $link_props = $service->get_link( | |
| 73 | + $service = new $services[ $service_name ]( $service_name, array() ); | |
| 74 | + $link_props = $service->get_link( | |
| 75 | 75 | $post_id, |
| 76 | 76 | 'share=' . esc_attr( $service_name ) . '&nb=1', |
| 77 | 77 | esc_attr( $data_shared ) |
| 78 | 78 | ); |
| 79 | - $link_url = $link_props['url']; | |
| 80 | - $link_classes = sprintf( | |
| 79 | + $link_url = $link_props['url']; | |
| 80 | + $link_classes = sprintf( | |
| 81 | 81 | 'jetpack-sharing-button__button style-%1$s share-%2$s', |
| 82 | 82 | $style_type, |
| 83 | 83 | $service_name |
| 84 | 84 | ); |
| 85 | - $link_aria_label = sprintf( | |
| 86 | - /* translators: %s refers to a string representation of sharing service, e.g. Facebook */ | |
| 85 | + | |
| 86 | + $accessible_name = sprintf( | |
| 87 | + /* translators: %s refers to a string representation of sharing service, e.g. Facebook */ | |
| 87 | 88 | esc_html__( 'Share on %s', 'jetpack' ), |
| 88 | 89 | esc_html( $title ) |
| 89 | 90 | ); |
| 90 | 91 | |
| 91 | - $link_props = $service->get_link( | |
| 92 | - $post_id, | |
| 93 | - 'share=' . esc_attr( $service_name ) . '&nb=1', | |
| 94 | - false, | |
| 95 | - esc_attr( $data_shared ) | |
| 96 | - ); | |
| 92 | + $accessible_name .= __( ' (Opens in new window)', 'jetpack' ); | |
| 97 | 93 | |
| 98 | 94 | $block_class_name = 'jetpack-sharing-button__list-item'; |
| 99 | 95 | |
| 100 | 96 | if ( $service_name === 'share' ) { |
| @@ -99,9 +95,9 @@ | ||
| 99 | 95 | |
| 100 | 96 | if ( $service_name === 'share' ) { |
| 101 | 97 | $block_class_name .= ' tooltip'; |
| 102 | 98 | /* translators: aria label for SMS sharing button */ |
| 103 | - $link_aria_label = esc_attr__( 'Share using Native tools', 'jetpack' ); | |
| 99 | + $accessible_name = esc_attr__( 'Share using Native tools', 'jetpack' ); | |
| 104 | 100 | $link_props = $service->get_link( $post_id ); |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | 103 | $link_url = $link_props['url']; |
| @@ -132,15 +128,21 @@ | ||
| 132 | 128 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 133 | 129 | |
| 134 | 130 | $component = '<li class="' . esc_attr( $block_class_name ) . '">'; |
| 135 | 131 | $component .= sprintf( |
| 136 | - '<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-label="%6$s">', | |
| 132 | + '<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">', | |
| 137 | 133 | esc_url( $link_url ), |
| 138 | 134 | esc_attr( $link_classes ), |
| 139 | 135 | esc_attr( $link_styles ), |
| 140 | 136 | esc_attr( $service_name ), |
| 137 | + esc_attr( $data_shared ) | |
| 138 | + ); | |
| 139 | + | |
| 140 | + // Add hidden span with accessible name | |
| 141 | + $component .= sprintf( | |
| 142 | + '<span id="%s" hidden>%s</span>', | |
| 141 | 143 | esc_attr( $data_shared ), |
| 142 | - esc_attr( $link_aria_label ) | |
| 144 | + esc_html( $accessible_name ) | |
| 143 | 145 | ); |
| 144 | 146 | |
| 145 | 147 | $component .= $style_type !== 'text' ? $icon : ''; |
| 146 | 148 | $component .= '<span class="jetpack-sharing-button__service-label" aria-hidden="true">' . esc_html( $title ) . '</span>'; |
| @@ -167,9 +169,8 @@ | ||
| 167 | 169 | 'reddit' => Share_Reddit_Block::class, |
| 168 | 170 | 'twitter' => Share_Twitter_Block::class, |
| 169 | 171 | 'tumblr' => Share_Tumblr_Block::class, |
| 170 | 172 | 'pinterest' => Share_Pinterest_Block::class, |
| 171 | - 'pocket' => Share_Pocket_Block::class, | |
| 172 | 173 | 'telegram' => Share_Telegram_Block::class, |
| 173 | 174 | 'threads' => Share_Threads_Block::class, |
| 174 | 175 | 'whatsapp' => Jetpack_Share_WhatsApp_Block::class, |
| 175 | 176 | 'mastodon' => Share_Mastodon_Block::class, |
| @@ -199,16 +200,18 @@ | ||
| 199 | 200 | if ( ! array_key_exists( $service_name, $services ) ) { |
| 200 | 201 | return; |
| 201 | 202 | } |
| 202 | 203 | |
| 203 | - $service = new $services[ ( $service_name ) ]( $service_name, array() ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 204 | - if ( $service ) { | |
| 205 | - $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 206 | - } | |
| 204 | + $service = new $services[ ( $service_name ) ]( $service_name, array() ); | |
| 205 | + $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 207 | 206 | } |
| 208 | 207 | } |
| 209 | -add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 ); | |
| 210 | 208 | |
| 209 | +// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking for the data being present. | |
| 210 | +if ( isset( $_GET['share'] ) ) { | |
| 211 | + add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 ); | |
| 212 | +} | |
| 213 | + | |
| 211 | 214 | /** |
| 212 | 215 | * Automatically add the Sharing Buttons block to the end of the Single Posts template. |
| 213 | 216 | * |
| 214 | 217 | * @since 13.2 |
| @@ -220,8 +223,16 @@ | ||
| 220 | 223 | * |
| 221 | 224 | * @return array |
| 222 | 225 | */ |
| 223 | 226 | function add_block_to_single_posts_template( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { |
| 227 | + // Add the block at the end of the post content. | |
| 228 | + if ( | |
| 229 | + 'after' !== $relative_position | |
| 230 | + || 'core/post-content' !== $anchor_block_type | |
| 231 | + ) { | |
| 232 | + return $hooked_block_types; | |
| 233 | + } | |
| 234 | + | |
| 224 | 235 | // Only automate the addition of the block in block-based themes. |
| 225 | 236 | if ( ! wp_is_block_theme() ) { |
| 226 | 237 | return $hooked_block_types; |
| 227 | 238 | } |
| @@ -256,9 +267,9 @@ | ||
| 256 | 267 | } |
| 257 | 268 | |
| 258 | 269 | // Only hook into page and single post templates. |
| 259 | 270 | if ( |
| 260 | - ! $context instanceof \WP_Block_Template | |
| 271 | + ! $context instanceof WP_Block_Template | |
| 261 | 272 | || ! property_exists( $context, 'slug' ) |
| 262 | 273 | || empty( $context->slug ) |
| 263 | 274 | || ! preg_match( '/^(page|single)/', $context->slug ) |
| 264 | 275 | ) { |
| @@ -264,22 +275,9 @@ | ||
| 264 | 275 | ) { |
| 265 | 276 | return $hooked_block_types; |
| 266 | 277 | } |
| 267 | 278 | |
| 268 | - $content = $context->content ?? ''; | |
| 269 | - // Check if the block is already in the template. If so, abort. | |
| 270 | - if ( false !== strpos( $content, 'wp:' . PARENT_BLOCK_NAME ) ) { | |
| 271 | - return $hooked_block_types; | |
| 272 | - } | |
| 273 | - | |
| 274 | - // Add the block at the end of the post content. | |
| 275 | - if ( | |
| 276 | - 'after' === $relative_position | |
| 277 | - && 'core/post-content' === $anchor_block_type | |
| 278 | - ) { | |
| 279 | - $hooked_block_types[] = PARENT_BLOCK_NAME; | |
| 280 | - } | |
| 281 | - | |
| 279 | + $hooked_block_types[] = PARENT_BLOCK_NAME; | |
| 282 | 280 | return $hooked_block_types; |
| 283 | 281 | } |
| 284 | 282 | |
| 285 | 283 | /** |