From d0e02cb448438d006aa08530ca6ac2d1633c9052 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Wed, 7 Feb 2024 16:28:28 +0100 Subject: [PATCH 1/6] Sharing Block: automatically add block to templates in block themes Under a few specific conditions, we want to automatically add a sharing buttons block (and 3 inner blocks, Facebook, X, and Mastodon) to the bottom of the post content. Site owners can then go to Appearance > Editor if they want to remove or customize that block. For now, we're trying to automatically add the block right after the post content block, in post and page templates (that includes templates like `page-wide`, `page-no-sidebar`, etc). Here are the conditions: - The site must be using a block theme. - An option (`jetpack_sharing_buttons_auto_add`) must be set. Later on we can add UI to set that option under Jetpack > Settings > Social. - The sharing module must be deactivated. We wouldn't want to automatically add sharing buttons to the post content twice. - On WordPress.com Simple, the module is always active. There, we check if sharing posts are enabled on single posts and / or pages, since that matches the template where we'd like to automatically add the block. Resources: - https://make.wordpress.org/core/2023/10/15/introducing-block-hooks-for-dynamic-blocks/ - https://github.com/ockham/like-button - https://core.trac.wordpress.org/ticket/59572 - https://core.trac.wordpress.org/ticket/60126 Internal references: - p1707216098587689-slack-C065QF2R1B6 --- .../jetpack/changelog/add-hook-sharing-block | 4 + .../blocks/sharing-button/sharing-button.php | 139 +++++++++++++++++- 2 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/add-hook-sharing-block diff --git a/projects/plugins/jetpack/changelog/add-hook-sharing-block b/projects/plugins/jetpack/changelog/add-hook-sharing-block new file mode 100644 index 000000000000..a036c3abf71f --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-hook-sharing-block @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Sharing: automatically add the Sharing Buttons block to the single post and page templates on sites using a block-based theme. diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index 96a515efe255..458792c6e1a3 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -2,7 +2,7 @@ /** * Sharing Buttons Block. * - * @since 11.x + * @since 13.1 * * @package automattic/jetpack */ @@ -10,11 +10,16 @@ namespace Automattic\Jetpack\Extensions\Sharing_Button_Block; use Automattic\Jetpack\Blocks; +use Automattic\Jetpack\Modules; +use Automattic\Jetpack\Status\Host; use Jetpack_Gutenberg; require_once __DIR__ . '/class-sharing-source-block.php'; require_once __DIR__ . '/components/social-icons.php'; +const PARENT_BLOCK_NAME = 'jetpack/sharing-buttons'; +const INNER_BLOCK_NAME = 'jetpack/sharing-button'; + /** * Registers the block for use in Gutenberg * This is done via an action so that we can disable @@ -156,5 +161,135 @@ function sharing_process_requests() { } } } - add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 ); + +/** + * Automatically add the Sharing Buttons block to the end of the Single Posts template. + * + * @since $$next-version$$ + * + * @param array $hooked_block_types The list of hooked block types. + * @param string $relative_position The relative position of the hooked blocks. Can be one of 'before', 'after', 'first_child', or 'last_child'. + * @param string $anchor_block_type The anchor block type. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + * + * @return array + */ +function add_block_to_single_posts_template( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { + // Only automate the addition of the block in block-based themes. + if ( ! wp_is_block_theme() ) { + return $hooked_block_types; + } + + // Proceed if the user has toggle the auto-addition in Jetpack settings. + if ( ! get_option( 'jetpack_sharing_buttons_auto_add' ) ) { + return $hooked_block_types; + } + + /* + * The Sharing module must be disabled. + * We do not want to automatically insert sharing buttons twice. + * On WordPress.com Simple the module is always active so we must check differently. + * There, we check if buttons are enabled on single posts and pages. + */ + if ( ( new Host() )->is_wpcom_simple() ) { + if ( ! class_exists( 'Sharing_Service' ) ) { + include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php'; + } + + $sharer = new \Sharing_Service(); + $global = $sharer->get_global_options(); + if ( + ! $global['show'] + || in_array( 'post', $global['show'], true ) + || in_array( 'page', $global['show'], true ) + ) { + return $hooked_block_types; + } + } elseif ( ( new Modules() )->is_active( 'sharedaddy' ) ) { + return $hooked_block_types; + } + + // Only hook into page and single post templates. + if ( + ! $context instanceof \WP_Block_Template + || ! property_exists( $context, 'slug' ) + || empty( $context->slug ) + || ! preg_match( '/^(page|single)/', $context->slug ) + ) { + return $hooked_block_types; + } + + $content = $context->content ?? ''; + // Check if the block is already in the template. If so, abort. + if ( false !== strpos( $content, 'wp:' . PARENT_BLOCK_NAME ) ) { + return $hooked_block_types; + } + + // Add the block at the end of the post content. + if ( + 'after' === $relative_position + && 'core/post-content' === $anchor_block_type + ) { + $hooked_block_types[] = PARENT_BLOCK_NAME; + } + + return $hooked_block_types; +} +add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 ); + +/** + * Add default services to the block we add to the post content by default. + * + * @since $$next-version$$ + * + * @param array $parsed_hooked_block The parsed block array for the given hooked block type. + * @param string $relative_position The relative position of the hooked block. + * @param array $parsed_anchor_block The anchor block, in parsed block array format. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block. + * + * @return array + */ +function add_default_services_to_block( $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // Is the hooked block adjacent to the anchor block? + if ( 'after' !== $relative_position ) { + return $parsed_hooked_block; + } + + // Use the icon style by default. + $parsed_hooked_block['attrs']['styleType'] = 'icon'; + + // Does the anchor block have a layout attribute? + if ( isset( $anchor_block['attrs']['layout'] ) ) { + // Copy the anchor block's layout attribute to the hooked block. + $parsed_hooked_block['attrs']['layout'] = $parsed_anchor_block['attrs']['layout']; + } + + // Add default services (inner blocks) to the block. + $parsed_hooked_block['innerBlocks'] = array( + array( + 'blockName' => INNER_BLOCK_NAME, + 'attrs' => array( + 'service' => 'facebook', + 'label' => esc_html__( 'Facebook', 'jetpack' ), + ), + ), + array( + 'blockName' => INNER_BLOCK_NAME, + 'attrs' => array( + 'service' => 'x', + 'label' => esc_html__( 'X', 'jetpack' ), + ), + ), + array( + 'blockName' => INNER_BLOCK_NAME, + 'attrs' => array( + 'service' => 'mastodon', + 'label' => esc_html__( 'Mastodon', 'jetpack' ), + ), + ), + ); + + return $parsed_hooked_block; +} +add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 4 ); From c0445f06fab289408d7b0cebe873bc4a2a7dd4ba Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 8 Feb 2024 16:34:15 +0100 Subject: [PATCH 2/6] Wrap inner blocks in our sharing buttons markup. See https://github.com/Automattic/jetpack/pull/35542/files#r1483160941 Co-authored-by: Bernie Reiter --- .../extensions/blocks/sharing-button/sharing-button.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index 458792c6e1a3..81d31275d766 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -290,6 +290,15 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position ), ); + // Wrap inner blocks in our sharing buttons markup. + $parsed_hooked_block['innerContent'] = array( + '
    ', + null, + null, + null, + '
', + ); + return $parsed_hooked_block; } add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 4 ); From 9eb3facfe1306f70fe14ab0ff982e9b28af75268 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 8 Feb 2024 16:38:20 +0100 Subject: [PATCH 3/6] Fix anchor block reference --- .../jetpack/extensions/blocks/sharing-button/sharing-button.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index 81d31275d766..f2c51e28384b 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -260,7 +260,7 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position $parsed_hooked_block['attrs']['styleType'] = 'icon'; // Does the anchor block have a layout attribute? - if ( isset( $anchor_block['attrs']['layout'] ) ) { + if ( isset( $parsed_anchor_block['attrs']['layout'] ) ) { // Copy the anchor block's layout attribute to the hooked block. $parsed_hooked_block['attrs']['layout'] = $parsed_anchor_block['attrs']['layout']; } From 4c9112b300d4308d1dbce62d8d603375d203d16f Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 8 Feb 2024 16:54:03 +0100 Subject: [PATCH 4/6] Wrap the whole thing in a group block --- .../blocks/sharing-button/sharing-button.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index f2c51e28384b..388ec64ca324 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -259,12 +259,6 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position // Use the icon style by default. $parsed_hooked_block['attrs']['styleType'] = 'icon'; - // Does the anchor block have a layout attribute? - if ( isset( $parsed_anchor_block['attrs']['layout'] ) ) { - // Copy the anchor block's layout attribute to the hooked block. - $parsed_hooked_block['attrs']['layout'] = $parsed_anchor_block['attrs']['layout']; - } - // Add default services (inner blocks) to the block. $parsed_hooked_block['innerBlocks'] = array( array( @@ -299,6 +293,19 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position '', ); - return $parsed_hooked_block; + // Wrap the whole thing in a group block. + return array( + 'blockName' => 'core/group', + 'attrs' => array( + // Does the anchor block have a layout attribute? If so, use it in the group to maintain the same alignment. + 'layout' => $parsed_anchor_block['attrs']['layout'] ?? 'null', + ), + 'innerBlocks' => array( $parsed_hooked_block ), + 'innerContent' => array( + '
', + null, + '
', + ), + ); } add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 4 ); From 228608772fca66216e5ab93726b223a3d9143c4d Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 8 Feb 2024 18:14:09 +0100 Subject: [PATCH 5/6] Avoid PHP warning --- .../blocks/sharing-button/sharing-button.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index 388ec64ca324..f37502fad483 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -262,22 +262,25 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position // Add default services (inner blocks) to the block. $parsed_hooked_block['innerBlocks'] = array( array( - 'blockName' => INNER_BLOCK_NAME, - 'attrs' => array( + 'blockName' => INNER_BLOCK_NAME, + 'innerContent' => array(), + 'attrs' => array( 'service' => 'facebook', 'label' => esc_html__( 'Facebook', 'jetpack' ), ), ), array( - 'blockName' => INNER_BLOCK_NAME, - 'attrs' => array( + 'blockName' => INNER_BLOCK_NAME, + 'innerContent' => array(), + 'attrs' => array( 'service' => 'x', 'label' => esc_html__( 'X', 'jetpack' ), ), ), array( - 'blockName' => INNER_BLOCK_NAME, - 'attrs' => array( + 'blockName' => INNER_BLOCK_NAME, + 'innerContent' => array(), + 'attrs' => array( 'service' => 'mastodon', 'label' => esc_html__( 'Mastodon', 'jetpack' ), ), From 877639d7278b017c0f6312d43921435bebcc816e Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Wed, 21 Feb 2024 15:30:39 +0100 Subject: [PATCH 6/6] Update hook to match updated filter signature in Core See https://core.trac.wordpress.org/changeset/57660 --- .../extensions/blocks/sharing-button/sharing-button.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php index f37502fad483..e4e8ffcfa5d1 100644 --- a/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php +++ b/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php @@ -181,7 +181,7 @@ function add_block_to_single_posts_template( $hooked_block_types, $relative_posi return $hooked_block_types; } - // Proceed if the user has toggle the auto-addition in Jetpack settings. + // Proceed if the user has toggled the auto-addition in Jetpack settings. if ( ! get_option( 'jetpack_sharing_buttons_auto_add' ) ) { return $hooked_block_types; } @@ -244,13 +244,14 @@ function add_block_to_single_posts_template( $hooked_block_types, $relative_posi * @since $$next-version$$ * * @param array $parsed_hooked_block The parsed block array for the given hooked block type. + * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block. * * @return array */ -function add_default_services_to_block( $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable +function add_default_services_to_block( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // Is the hooked block adjacent to the anchor block? if ( 'after' !== $relative_position ) { return $parsed_hooked_block; @@ -311,4 +312,4 @@ function add_default_services_to_block( $parsed_hooked_block, $relative_position ), ); } -add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 4 ); +add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 );