Skip to content

Commit c6805fb

Browse files
authored
Sharing: only hook block to single post template on WP 6.5+ (#35905)
Follow-up to #35542 Core recently changed the definition of the filter we're using to automatically add the sharing block to post templates. As a result, our code is not compatible with WordPress 6.4 anymore: ``` PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Automattic\Jetpack\Extensions\Sharing_Button_Block\add_default_services_to_block(), 4 passed in /var/www/html/wp-includes/class-wp-hook.php on line 324 and exactly 5 expected in /usr/local/src/jetpack-monorepo/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php:254 ``` Let's gate that functionality to WordPress 6.5+ to avoid that error.
1 parent c8b3b5b commit c6805fb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: other
3+
4+
Sharing Block: only hook block on WordPress 6.5+

projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ function register_block() {
3030
__DIR__,
3131
array( 'render_callback' => __NAMESPACE__ . '\render_block' )
3232
);
33+
34+
/*
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.
38+
*/
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+
}
3344
}
3445
add_action( 'init', __NAMESPACE__ . '\register_block' );
3546

@@ -236,7 +247,6 @@ function add_block_to_single_posts_template( $hooked_block_types, $relative_posi
236247

237248
return $hooked_block_types;
238249
}
239-
add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 );
240250

241251
/**
242252
* Add default services to the block we add to the post content by default.
@@ -312,4 +322,3 @@ function add_default_services_to_block( $parsed_hooked_block, $hooked_block_type
312322
),
313323
);
314324
}
315-
add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 );

0 commit comments

Comments
 (0)