Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress 5.8 Compatibility: Add support for block_editor_settings_all #20201

Merged
merged 11 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: compat

Compatibility: Adds support for block_editor_settings_all when running WordPress 5.8 or higher
12 changes: 10 additions & 2 deletions projects/plugins/jetpack/modules/copy-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct() {
* @return void
*/
public function update_post_data( $target_post_id, $post, $update ) {
global $wp_version;
// This `$update` check avoids infinite loops of trying to update our updated post.
if ( $update ) {
return;
Expand All @@ -69,8 +70,15 @@ public function update_post_data( $target_post_id, $post, $update ) {
add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );

// Required to avoid the block editor from adding default blocks according to post format.
add_filter( 'block_editor_settings', array( $this, 'remove_post_format_template' ) );
/*
* Required to avoid the block editor from adding default blocks according to post format.
* @todo: simplify once WordPress 5.8 is the minimum required version.
*/
if ( version_compare( $wp_version, '5.8', '>=' ) ) {
add_filter( 'block_editor_settings_all', array( $this, 'remove_post_format_template' ) );
} else {
add_filter( 'block_editor_settings', array( $this, 'remove_post_format_template' ) );
}

/**
* Fires after all updates have been performed, and default content filters have been added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private function __construct() {
* Add in all hooks.
*/
public function init_actions() {
global $wp_version;
// Bail early if Jetpack's block editor extensions are disabled on the site.
/* This filter is documented in class.jetpack-gutenberg.php */
if ( ! apply_filters( 'jetpack_gutenberg', true ) ) {
Expand All @@ -64,11 +65,16 @@ public function init_actions() {

require_once __DIR__ . '/functions.editor-type.php';
add_action( 'edit_form_top', 'Jetpack\EditorType\remember_classic_editor' );
add_filter( 'block_editor_settings', 'Jetpack\EditorType\remember_block_editor', 10, 2 );
add_action( 'login_init', array( $this, 'allow_block_editor_login' ), 1 );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 9 );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) );
add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) );
// @todo simplify once 5.8 is the minimum supported version.
if ( version_compare( $wp_version, '5.8', '>=' ) ) {
add_filter( 'block_editor_settings_all', 'Jetpack\EditorType\remember_block_editor', 10, 2 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's going to work as well in WordPress 5.8.

In WordPress 5.7, the block_editor_settings filter's second parameter was an instance of WP_Post. We use it here:

In WordPress 5.8, the block_editor_settings_all filter's second parameter is different: it is now an instance of WP_Block_Editor_Context.

} else {
add_filter( 'block_editor_settings', 'Jetpack\EditorType\remember_block_editor', 10, 2 );
}

$this->enable_cross_site_auth_cookies();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@ function remember_classic_editor( $post ) {
/**
* Remember when the block editor was used to edit a post.
*
* @param object $editor_settings This is hooked into a filter and this is the settings that are passed in.
* @param object $post The post being editted.
* @return object The unmodified $editor_settings parameter.
* @todo: simplify once WordPress 5.8 is the minimum required version.
*
* @param array $editor_settings This is hooked into a filter and this is the settings that are passed in.
* @param WP_Post|WP_Block_Editor_Context $post In WP 5.7- the post being editted. In WP 5.8+, the block editor context.
*
* @return array The unmodified $editor_settings parameter.
*/
function remember_block_editor( $editor_settings, $post ) {
$post_type = get_post_type( $post );
if (
! empty( $post->post ) &&
is_a( $post, 'WP_Block_Editor_Context' )
) {
$post = $post->post;
}

if ( empty( $post ) ) {
return $editor_settings;
}

$post_type = get_post_type( $post );
if ( $post_type && can_edit_post_type( $post_type ) ) {
remember_editor( $post->ID, 'block-editor' );
}
Expand Down