From 8e0718df53752dbc57a804b68af96a99f891f8e2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 11 Dec 2023 17:04:23 +0000 Subject: [PATCH 01/12] call variation through callback so it's only loaded when needed. --- lib/compat/wordpress-6.4/block-hooks.php | 5 +++++ .../src/navigation-link/index.php | 19 ++++++++++++++----- .../block-library/src/post-terms/index.php | 15 ++++++++++++--- .../block-library/src/template-part/index.php | 2 +- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index 9cf6d2414926a5..536169a2621cf2 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -79,6 +79,11 @@ function gutenberg_add_hooked_blocks( $settings, $metadata ) { $exposed_settings = array_intersect_key( $settings, $fields_to_pick ); + // If the block has a variations callback, call it and add the variations to the block. + if ( isset( $exposed_settings['variations'] ) && is_callable( $exposed_settings['variations'] ) ) { + $exposed_settings['variations'] = call_user_func( $exposed_settings['variations'] ); + } + // TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block(). wp_add_inline_script( 'wp-blocks', diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 1165ce94b5921f..dda7ec709692ed 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -342,12 +342,11 @@ function register_block_core_navigation_link_variation( $variation ) { } /** - * Register the navigation link block. + * Returns an array of variations for the navigation link block. * - * @uses render_block_core_navigation() - * @throws WP_Error An WP_Error exception parsing the block definition. + * @return array */ -function register_block_core_navigation_link() { +function build_navigation_link_block_variations() { // This will only handle post types and taxonomies registered until this point (init on priority 9). // See action hooks below for other post types and taxonomies. // See https://github.com/WordPress/gutenberg/issues/53826 for details. @@ -382,11 +381,21 @@ function register_block_core_navigation_link() { } } + return array_merge( $built_ins, $variations ); +} + +/** + * Register the navigation link block. + * + * @uses render_block_core_navigation() + * @throws WP_Error An WP_Error exception parsing the block definition. + */ +function register_block_core_navigation_link() { register_block_type_from_metadata( __DIR__ . '/navigation-link', array( 'render_callback' => 'render_block_core_navigation_link', - 'variations' => array_merge( $built_ins, $variations ), + 'variations' => 'build_navigation_link_block_variations', ) ); } diff --git a/packages/block-library/src/post-terms/index.php b/packages/block-library/src/post-terms/index.php index c97155b81e3a95..77d231fe33d662 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -59,9 +59,11 @@ function render_block_core_post_terms( $attributes, $content, $block ) { } /** - * Registers the `core/post-terms` block on the server. + * Returns the available variations for the `core/post-terms` block. + * + * @return array The available variations for the block. */ -function register_block_core_post_terms() { +function build_post_term_block_variations() { $taxonomies = get_taxonomies( array( 'publicly_queryable' => true, @@ -103,11 +105,18 @@ function register_block_core_post_terms() { } } + return array_merge( $built_ins, $custom_variations ); +} + +/** + * Registers the `core/post-terms` block on the server. + */ +function register_block_core_post_terms() { register_block_type_from_metadata( __DIR__ . '/post-terms', array( 'render_callback' => 'render_block_core_post_terms', - 'variations' => array_merge( $built_ins, $custom_variations ), + 'variations' => 'build_post_term_block_variations', ) ); } diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 26d90c51b5529c..4c9356bb137960 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -276,7 +276,7 @@ function register_block_core_template_part() { __DIR__ . '/template-part', array( 'render_callback' => 'render_block_core_template_part', - 'variations' => build_template_part_block_variations(), + 'variations' => 'build_template_part_block_variations', ) ); } From e57d8ae23f3c58d0a36631c1b1c7a4ca3f0f5301 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 15 Dec 2023 16:53:28 +0000 Subject: [PATCH 02/12] move gutenberg_add_hooked_blocks from 6.4 to 6.5 --- lib/compat/wordpress-6.4/block-hooks.php | 88 ---------------------- lib/compat/wordpress-6.5/block-hooks.php | 94 ++++++++++++++++++++++++ lib/load.php | 1 + 3 files changed, 95 insertions(+), 88 deletions(-) create mode 100644 lib/compat/wordpress-6.5/block-hooks.php diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index 536169a2621cf2..465e0eb7b9fb98 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -5,94 +5,6 @@ * @package gutenberg */ -/** - * Register hooked blocks for automatic insertion, based on their block.json metadata. - * - * @param array $settings Array of determined settings for registering a block type. - * @param array $metadata Metadata provided for registering a block type. - * @return array Updated settings array. - */ -function gutenberg_add_hooked_blocks( $settings, $metadata ) { - if ( ! isset( $metadata['blockHooks'] ) ) { - return $settings; - } - $block_hooks = $metadata['blockHooks']; - - /** - * Map the camelCased position string from block.json to the snake_cased block type position - * used in the hooked block registration function. - * - * @var array - */ - $property_mappings = array( - 'before' => 'before', - 'after' => 'after', - 'firstChild' => 'first_child', - 'lastChild' => 'last_child', - ); - - $inserted_block_name = $metadata['name']; - foreach ( $block_hooks as $anchor_block_name => $position ) { - // Avoid infinite recursion (hooking to itself). - if ( $inserted_block_name === $anchor_block_name ) { - _doing_it_wrong( - __METHOD__, - __( 'Cannot hook block to itself.', 'gutenberg' ), - '6.4.0' - ); - continue; - } - - if ( ! isset( $property_mappings[ $position ] ) ) { - continue; - } - - $mapped_position = $property_mappings[ $position ]; - - gutenberg_add_hooked_block( $inserted_block_name, $mapped_position, $anchor_block_name ); - - $settings['block_hooks'][ $anchor_block_name ] = $mapped_position; - } - - // Copied from `get_block_editor_server_block_settings()`. - $fields_to_pick = array( - 'api_version' => 'apiVersion', - 'title' => 'title', - 'description' => 'description', - 'icon' => 'icon', - 'attributes' => 'attributes', - 'provides_context' => 'providesContext', - 'uses_context' => 'usesContext', - 'selectors' => 'selectors', - 'supports' => 'supports', - 'category' => 'category', - 'styles' => 'styles', - 'textdomain' => 'textdomain', - 'parent' => 'parent', - 'ancestor' => 'ancestor', - 'keywords' => 'keywords', - 'example' => 'example', - 'variations' => 'variations', - ); - // Add `block_hooks` to the list of fields to pick. - $fields_to_pick['block_hooks'] = 'blockHooks'; - - $exposed_settings = array_intersect_key( $settings, $fields_to_pick ); - - // If the block has a variations callback, call it and add the variations to the block. - if ( isset( $exposed_settings['variations'] ) && is_callable( $exposed_settings['variations'] ) ) { - $exposed_settings['variations'] = call_user_func( $exposed_settings['variations'] ); - } - - // TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block(). - wp_add_inline_script( - 'wp-blocks', - 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( array( $inserted_block_name => $exposed_settings ) ) . ');' - ); - - return $settings; -} - /** * Register a hooked block for automatic insertion into a given block hook. * diff --git a/lib/compat/wordpress-6.5/block-hooks.php b/lib/compat/wordpress-6.5/block-hooks.php new file mode 100644 index 00000000000000..0273b05b4c1cb8 --- /dev/null +++ b/lib/compat/wordpress-6.5/block-hooks.php @@ -0,0 +1,94 @@ + 'before', + 'after' => 'after', + 'firstChild' => 'first_child', + 'lastChild' => 'last_child', + ); + + $inserted_block_name = $metadata['name']; + foreach ( $block_hooks as $anchor_block_name => $position ) { + // Avoid infinite recursion (hooking to itself). + if ( $inserted_block_name === $anchor_block_name ) { + _doing_it_wrong( + __METHOD__, + __( 'Cannot hook block to itself.', 'gutenberg' ), + '6.4.0' + ); + continue; + } + + if ( ! isset( $property_mappings[ $position ] ) ) { + continue; + } + + $mapped_position = $property_mappings[ $position ]; + + gutenberg_add_hooked_block( $inserted_block_name, $mapped_position, $anchor_block_name ); + + $settings['block_hooks'][ $anchor_block_name ] = $mapped_position; + } + + // Copied from `get_block_editor_server_block_settings()`. + $fields_to_pick = array( + 'api_version' => 'apiVersion', + 'title' => 'title', + 'description' => 'description', + 'icon' => 'icon', + 'attributes' => 'attributes', + 'provides_context' => 'providesContext', + 'uses_context' => 'usesContext', + 'selectors' => 'selectors', + 'supports' => 'supports', + 'category' => 'category', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'parent' => 'parent', + 'ancestor' => 'ancestor', + 'keywords' => 'keywords', + 'example' => 'example', + 'variations' => 'variations', + ); + // Add `block_hooks` to the list of fields to pick. + $fields_to_pick['block_hooks'] = 'blockHooks'; + + $exposed_settings = array_intersect_key( $settings, $fields_to_pick ); + + // If the block has a variations callback, call it and add the variations to the block. + if ( isset( $exposed_settings['variations'] ) && is_callable( $exposed_settings['variations'] ) ) { + $exposed_settings['variations'] = call_user_func( $exposed_settings['variations'] ); + } + + // TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block(). + wp_add_inline_script( + 'wp-blocks', + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( array( $inserted_block_name => $exposed_settings ) ) . ');' + ); + + return $settings; +} diff --git a/lib/load.php b/lib/load.php index 59fb75541ac41e..34426f8b7307e0 100644 --- a/lib/load.php +++ b/lib/load.php @@ -102,6 +102,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.4/kses.php'; // WordPress 6.5 compat. +require __DIR__ . '/compat/wordpress-6.4/block-hooks.php'; require __DIR__ . '/compat/wordpress-6.5/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.5/class-wp-navigation-block-renderer.php'; From 4e9f747f74c40fa818d13d72ed2dff26eb83c956 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 15 Dec 2023 18:00:11 +0000 Subject: [PATCH 03/12] cs fixes --- phpcs.xml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 56cd6734e4f3ee..a1ab47ddb7690b 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -155,6 +155,8 @@ + + From d4d12ea67821796633ba504b6df8a7d94c9cb097 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 15 Dec 2023 18:01:59 +0000 Subject: [PATCH 04/12] fix version number --- lib/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index 34426f8b7307e0..141bfca2eda1ff 100644 --- a/lib/load.php +++ b/lib/load.php @@ -102,7 +102,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.4/kses.php'; // WordPress 6.5 compat. -require __DIR__ . '/compat/wordpress-6.4/block-hooks.php'; +require __DIR__ . '/compat/wordpress-6.5/block-hooks.php'; require __DIR__ . '/compat/wordpress-6.5/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.5/class-wp-navigation-block-renderer.php'; From 06df7be129466e1f278c519f8b18c9dc75a1292a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 03:08:03 +0000 Subject: [PATCH 05/12] revert some code --- lib/compat/wordpress-6.4/block-hooks.php | 83 +++++++++++++++++++++ lib/compat/wordpress-6.5/block-hooks.php | 94 ------------------------ lib/load.php | 1 - 3 files changed, 83 insertions(+), 95 deletions(-) delete mode 100644 lib/compat/wordpress-6.5/block-hooks.php diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index 465e0eb7b9fb98..9cf6d2414926a5 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -5,6 +5,89 @@ * @package gutenberg */ +/** + * Register hooked blocks for automatic insertion, based on their block.json metadata. + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + * @return array Updated settings array. + */ +function gutenberg_add_hooked_blocks( $settings, $metadata ) { + if ( ! isset( $metadata['blockHooks'] ) ) { + return $settings; + } + $block_hooks = $metadata['blockHooks']; + + /** + * Map the camelCased position string from block.json to the snake_cased block type position + * used in the hooked block registration function. + * + * @var array + */ + $property_mappings = array( + 'before' => 'before', + 'after' => 'after', + 'firstChild' => 'first_child', + 'lastChild' => 'last_child', + ); + + $inserted_block_name = $metadata['name']; + foreach ( $block_hooks as $anchor_block_name => $position ) { + // Avoid infinite recursion (hooking to itself). + if ( $inserted_block_name === $anchor_block_name ) { + _doing_it_wrong( + __METHOD__, + __( 'Cannot hook block to itself.', 'gutenberg' ), + '6.4.0' + ); + continue; + } + + if ( ! isset( $property_mappings[ $position ] ) ) { + continue; + } + + $mapped_position = $property_mappings[ $position ]; + + gutenberg_add_hooked_block( $inserted_block_name, $mapped_position, $anchor_block_name ); + + $settings['block_hooks'][ $anchor_block_name ] = $mapped_position; + } + + // Copied from `get_block_editor_server_block_settings()`. + $fields_to_pick = array( + 'api_version' => 'apiVersion', + 'title' => 'title', + 'description' => 'description', + 'icon' => 'icon', + 'attributes' => 'attributes', + 'provides_context' => 'providesContext', + 'uses_context' => 'usesContext', + 'selectors' => 'selectors', + 'supports' => 'supports', + 'category' => 'category', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'parent' => 'parent', + 'ancestor' => 'ancestor', + 'keywords' => 'keywords', + 'example' => 'example', + 'variations' => 'variations', + ); + // Add `block_hooks` to the list of fields to pick. + $fields_to_pick['block_hooks'] = 'blockHooks'; + + $exposed_settings = array_intersect_key( $settings, $fields_to_pick ); + + // TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block(). + wp_add_inline_script( + 'wp-blocks', + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( array( $inserted_block_name => $exposed_settings ) ) . ');' + ); + + return $settings; +} + /** * Register a hooked block for automatic insertion into a given block hook. * diff --git a/lib/compat/wordpress-6.5/block-hooks.php b/lib/compat/wordpress-6.5/block-hooks.php deleted file mode 100644 index 0273b05b4c1cb8..00000000000000 --- a/lib/compat/wordpress-6.5/block-hooks.php +++ /dev/null @@ -1,94 +0,0 @@ - 'before', - 'after' => 'after', - 'firstChild' => 'first_child', - 'lastChild' => 'last_child', - ); - - $inserted_block_name = $metadata['name']; - foreach ( $block_hooks as $anchor_block_name => $position ) { - // Avoid infinite recursion (hooking to itself). - if ( $inserted_block_name === $anchor_block_name ) { - _doing_it_wrong( - __METHOD__, - __( 'Cannot hook block to itself.', 'gutenberg' ), - '6.4.0' - ); - continue; - } - - if ( ! isset( $property_mappings[ $position ] ) ) { - continue; - } - - $mapped_position = $property_mappings[ $position ]; - - gutenberg_add_hooked_block( $inserted_block_name, $mapped_position, $anchor_block_name ); - - $settings['block_hooks'][ $anchor_block_name ] = $mapped_position; - } - - // Copied from `get_block_editor_server_block_settings()`. - $fields_to_pick = array( - 'api_version' => 'apiVersion', - 'title' => 'title', - 'description' => 'description', - 'icon' => 'icon', - 'attributes' => 'attributes', - 'provides_context' => 'providesContext', - 'uses_context' => 'usesContext', - 'selectors' => 'selectors', - 'supports' => 'supports', - 'category' => 'category', - 'styles' => 'styles', - 'textdomain' => 'textdomain', - 'parent' => 'parent', - 'ancestor' => 'ancestor', - 'keywords' => 'keywords', - 'example' => 'example', - 'variations' => 'variations', - ); - // Add `block_hooks` to the list of fields to pick. - $fields_to_pick['block_hooks'] = 'blockHooks'; - - $exposed_settings = array_intersect_key( $settings, $fields_to_pick ); - - // If the block has a variations callback, call it and add the variations to the block. - if ( isset( $exposed_settings['variations'] ) && is_callable( $exposed_settings['variations'] ) ) { - $exposed_settings['variations'] = call_user_func( $exposed_settings['variations'] ); - } - - // TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block(). - wp_add_inline_script( - 'wp-blocks', - 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( array( $inserted_block_name => $exposed_settings ) ) . ');' - ); - - return $settings; -} diff --git a/lib/load.php b/lib/load.php index 07021fe1f7068e..1aa55f7581c272 100644 --- a/lib/load.php +++ b/lib/load.php @@ -101,7 +101,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.4/kses.php'; // WordPress 6.5 compat. -require __DIR__ . '/compat/wordpress-6.5/block-hooks.php'; require __DIR__ . '/compat/wordpress-6.5/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.5/class-wp-navigation-block-renderer.php'; require __DIR__ . '/compat/wordpress-6.5/kses.php'; From 63f8744a15e8a4f24a64b46a4706875fb9f854e2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 03:19:33 +0000 Subject: [PATCH 06/12] adapting to variation_calback --- packages/block-library/src/navigation-link/index.php | 4 ++-- packages/block-library/src/post-terms/index.php | 4 ++-- packages/block-library/src/template-part/index.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index dda7ec709692ed..69f3634a3044d0 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -394,8 +394,8 @@ function register_block_core_navigation_link() { register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', - 'variations' => 'build_navigation_link_block_variations', + 'render_callback' => 'render_block_core_navigation_link', + 'variation_callback' => 'build_navigation_link_block_variations', ) ); } diff --git a/packages/block-library/src/post-terms/index.php b/packages/block-library/src/post-terms/index.php index 77d231fe33d662..44291038383d42 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -115,8 +115,8 @@ function register_block_core_post_terms() { register_block_type_from_metadata( __DIR__ . '/post-terms', array( - 'render_callback' => 'render_block_core_post_terms', - 'variations' => 'build_post_term_block_variations', + 'render_callback' => 'render_block_core_post_terms', + 'variation_callback' => 'build_post_term_block_variations', ) ); } diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index b8bb55cd8eb1d5..86a17f33c92f39 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -281,8 +281,8 @@ function register_block_core_template_part() { register_block_type_from_metadata( __DIR__ . '/template-part', array( - 'render_callback' => 'render_block_core_template_part', - 'variations' => 'build_template_part_block_variations', + 'render_callback' => 'render_block_core_template_part', + 'variation_callback' => 'build_template_part_block_variations', ) ); } From 1ed9873b2c5b656d77a99ecf531e809498e5e8c7 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 04:25:17 +0000 Subject: [PATCH 07/12] B/C for plugin --- lib/compat/wordpress-6.4/block-hooks.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index 9cf6d2414926a5..b2903aae44abec 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -302,6 +302,20 @@ function gutenberg_register_block_hooks_rest_field() { ); } +/** + * Shim for the `variation_callback` block type argument. + * + * @param array $args The block type arguments. + * @return array The updated block type arguments. + */ +function gutenberg_register_block_type_args_shim( $args ) { + if( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { + $args['variations'] = call_user_func( $args['variation_callback'] ); + unset( $args['variation_callback'] ); + } + return $args; +} + // Install the polyfill for Block Hooks only if it isn't already handled in WordPress core. if ( ! function_exists( 'traverse_and_serialize_blocks' ) ) { add_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks', 10, 2 ); @@ -310,6 +324,10 @@ function gutenberg_register_block_hooks_rest_field() { add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' ); } +if( !method_exists( 'WP_Block_Type', 'get_variations' ) ) { + add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' ); +} + // Helper functions. // ----------------- // The sole purpose of the following two functions (`gutenberg_serialize_block` From e29e6bd067db5fd64d497b3932d5cdffb08579f3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 04:31:33 +0000 Subject: [PATCH 08/12] cs fixes --- lib/compat/wordpress-6.4/block-hooks.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index b2903aae44abec..f3f99facbeddaa 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -304,16 +304,16 @@ function gutenberg_register_block_hooks_rest_field() { /** * Shim for the `variation_callback` block type argument. - * + * * @param array $args The block type arguments. * @return array The updated block type arguments. */ function gutenberg_register_block_type_args_shim( $args ) { - if( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { - $args['variations'] = call_user_func( $args['variation_callback'] ); - unset( $args['variation_callback'] ); - } - return $args; + if( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { + $args['variations'] = call_user_func( $args['variation_callback'] ); + unset( $args['variation_callback'] ); + } + return $args; } // Install the polyfill for Block Hooks only if it isn't already handled in WordPress core. @@ -324,7 +324,7 @@ function gutenberg_register_block_type_args_shim( $args ) { add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' ); } -if( !method_exists( 'WP_Block_Type', 'get_variations' ) ) { +if( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' ); } From ca658e278bdbfc0d95355ac73114ef159b8e98f0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 04:33:58 +0000 Subject: [PATCH 09/12] cs fixes --- lib/compat/wordpress-6.4/block-hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index f3f99facbeddaa..e7262b1ba82cec 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -312,7 +312,7 @@ function gutenberg_register_block_type_args_shim( $args ) { if( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { $args['variations'] = call_user_func( $args['variation_callback'] ); unset( $args['variation_callback'] ); - } + } return $args; } From 7e7edd3352962ac0f0efc80416e14cf7597fdf14 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 04:55:51 +0000 Subject: [PATCH 10/12] cs fixes --- lib/compat/wordpress-6.4/block-hooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index e7262b1ba82cec..241e4213c6e43b 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -309,7 +309,7 @@ function gutenberg_register_block_hooks_rest_field() { * @return array The updated block type arguments. */ function gutenberg_register_block_type_args_shim( $args ) { - if( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { + if ( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { $args['variations'] = call_user_func( $args['variation_callback'] ); unset( $args['variation_callback'] ); } @@ -324,7 +324,7 @@ function gutenberg_register_block_type_args_shim( $args ) { add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' ); } -if( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { +if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' ); } From 87a5982aef78a29bff5b41c3b2c1c500992232dd Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 15 Jan 2024 10:50:31 +0000 Subject: [PATCH 11/12] shims to support block variation changes for WP < 6.5 --- lib/compat/wordpress-6.4/block-hooks.php | 18 ------------------ lib/compat/wordpress-6.5/blocks.php | 24 ++++++++++++++++++++++++ lib/load.php | 1 + 3 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 lib/compat/wordpress-6.5/blocks.php diff --git a/lib/compat/wordpress-6.4/block-hooks.php b/lib/compat/wordpress-6.4/block-hooks.php index 241e4213c6e43b..9cf6d2414926a5 100644 --- a/lib/compat/wordpress-6.4/block-hooks.php +++ b/lib/compat/wordpress-6.4/block-hooks.php @@ -302,20 +302,6 @@ function gutenberg_register_block_hooks_rest_field() { ); } -/** - * Shim for the `variation_callback` block type argument. - * - * @param array $args The block type arguments. - * @return array The updated block type arguments. - */ -function gutenberg_register_block_type_args_shim( $args ) { - if ( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) { - $args['variations'] = call_user_func( $args['variation_callback'] ); - unset( $args['variation_callback'] ); - } - return $args; -} - // Install the polyfill for Block Hooks only if it isn't already handled in WordPress core. if ( ! function_exists( 'traverse_and_serialize_blocks' ) ) { add_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks', 10, 2 ); @@ -324,10 +310,6 @@ function gutenberg_register_block_type_args_shim( $args ) { add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' ); } -if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { - add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' ); -} - // Helper functions. // ----------------- // The sole purpose of the following two functions (`gutenberg_serialize_block` diff --git a/lib/compat/wordpress-6.5/blocks.php b/lib/compat/wordpress-6.5/blocks.php new file mode 100644 index 00000000000000..7731ec2281d3eb --- /dev/null +++ b/lib/compat/wordpress-6.5/blocks.php @@ -0,0 +1,24 @@ + Date: Mon, 15 Jan 2024 11:40:42 +0000 Subject: [PATCH 12/12] EOL for PHPCS --- lib/compat/wordpress-6.5/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/blocks.php b/lib/compat/wordpress-6.5/blocks.php index 7731ec2281d3eb..b6890c14dc1f97 100644 --- a/lib/compat/wordpress-6.5/blocks.php +++ b/lib/compat/wordpress-6.5/blocks.php @@ -21,4 +21,4 @@ function gutenberg_register_block_type_args_shim( $args ) { if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' ); -} \ No newline at end of file +}