Skip to content

Commit

Permalink
Block Bindings: Remove the experimental flag (#58089)
Browse files Browse the repository at this point in the history
* Remove the experimental flag for Block Bindings

* Move block-bindings to `compat/wordpress-6.5` folder.

* Remove the need for the experiment flag in the pattern overrides e2e tests

* `require` the files in load.php instead

* Fix the order of the requires

* Update comment fromat in blocks.php

Co-authored-by: Carlos Bravo <37012961+c4rl0sbr4v0@users.noreply.github.com>

* Remove changes that sneaked in during the rebase

* Remove other changes that sneaked in during rebase

* One more fix for the bad rebase

* Add 'linkTarget' property to core/button block in $allowed_blocks

---------

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
Co-authored-by: Carlos Bravo <37012961+c4rl0sbr4v0@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 24, 2024
1 parent 0227f40 commit 015d8b5
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 123 deletions.
79 changes: 79 additions & 0 deletions lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,82 @@ function gutenberg_register_metadata_attribute( $args ) {
return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );


if ( ! function_exists( 'gutenberg_process_block_bindings' ) ) {
/**
* Process the block bindings attribute.
*
* @param string $block_content Block Content.
* @param array $block Block attributes.
* @param WP_Block $block_instance The block instance.
*/
function gutenberg_process_block_bindings( $block_content, $block, $block_instance ) {

// Allowed blocks that support block bindings.
// TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes?
$allowed_blocks = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text', 'linkTarget' ),
);

// If the block doesn't have the bindings property or isn't one of the allowed block types, return.
if ( ! isset( $block['attrs']['metadata']['bindings'] ) || ! isset( $allowed_blocks[ $block_instance->name ] ) ) {
return $block_content;
}

/*
* Assuming the following format for the bindings property of the "metadata" attribute:
*
* "bindings": {
* "title": {
* "source": {
* "name": "post_meta",
* "attributes": { "value": "text_custom_field" }
* }
* },
* "url": {
* "source": {
* "name": "post_meta",
* "attributes": { "value": "text_custom_field" }
* }
* }
* }
*/

$block_bindings_sources = wp_block_bindings_get_sources();
$modified_block_content = $block_content;
foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) {

// If the attribute is not in the list, process next attribute.
if ( ! in_array( $binding_attribute, $allowed_blocks[ $block_instance->name ], true ) ) {
continue;
}
// If no source is provided, or that source is not registered, process next attribute.
if ( ! isset( $binding_source['source'] ) || ! isset( $binding_source['source']['name'] ) || ! isset( $block_bindings_sources[ $binding_source['source']['name'] ] ) ) {
continue;
}

$source_callback = $block_bindings_sources[ $binding_source['source']['name'] ]['apply'];
// Get the value based on the source.
if ( ! isset( $binding_source['source']['attributes'] ) ) {
$source_args = array();
} else {
$source_args = $binding_source['source']['attributes'];
}
$source_value = $source_callback( $source_args, $block_instance, $binding_attribute );
// If the value is null, process next attribute.
if ( is_null( $source_value ) ) {
continue;
}

// Process the HTML based on the block and the attribute.
$modified_block_content = wp_block_bindings_replace_html( $modified_block_content, $block_instance->name, $binding_attribute, $source_value );
}
return $modified_block_content;
}
}

add_filter( 'render_block', 'gutenberg_process_block_bindings', 20, 3 );
17 changes: 0 additions & 17 deletions lib/experimental/block-bindings/index.php

This file was deleted.

85 changes: 0 additions & 85 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,88 +77,3 @@ function wp_enqueue_block_view_script( $block_name, $args ) {
add_filter( 'render_block', $callback, 10, 2 );
}
}

$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && (
array_key_exists( 'gutenberg-block-bindings', $gutenberg_experiments )
) ) {

require_once __DIR__ . '/block-bindings/index.php';

if ( ! function_exists( 'gutenberg_process_block_bindings' ) ) {
/**
* Process the block bindings attribute.
*
* @param string $block_content Block Content.
* @param array $block Block attributes.
* @param WP_Block $block_instance The block instance.
*/
function gutenberg_process_block_bindings( $block_content, $block, $block_instance ) {

// Allowed blocks that support block bindings.
// TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes?
$allowed_blocks = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text', 'linkTarget' ),
);

// If the block doesn't have the bindings property or isn't one of the allowed block types, return.
if ( ! isset( $block['attrs']['metadata']['bindings'] ) || ! isset( $allowed_blocks[ $block_instance->name ] ) ) {
return $block_content;
}

// Assuming the following format for the bindings property of the "metadata" attribute:
//
// "bindings": {
// "title": {
// "source": {
// "name": "post_meta",
// "attributes": { "value": "text_custom_field" }
// }
// },
// "url": {
// "source": {
// "name": "post_meta",
// "attributes": { "value": "text_custom_field" }
// }
// }
// }
//

$block_bindings_sources = wp_block_bindings_get_sources();
$modified_block_content = $block_content;
foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) {

// If the attribute is not in the list, process next attribute.
if ( ! in_array( $binding_attribute, $allowed_blocks[ $block_instance->name ], true ) ) {
continue;
}
// If no source is provided, or that source is not registered, process next attribute.
if ( ! isset( $binding_source['source'] ) || ! isset( $binding_source['source']['name'] ) || ! isset( $block_bindings_sources[ $binding_source['source']['name'] ] ) ) {
continue;
}

$source_callback = $block_bindings_sources[ $binding_source['source']['name'] ]['apply'];
// Get the value based on the source.
if ( ! isset( $binding_source['source']['attributes'] ) ) {
$source_args = array();
} else {
$source_args = $binding_source['source']['attributes'];
}
$source_value = $source_callback( $source_args, $block_instance, $binding_attribute );
// If the value is null, process next attribute.
if ( is_null( $source_value ) ) {
continue;
}

// Process the HTML based on the block and the attribute.
$modified_block_content = wp_block_bindings_replace_html( $modified_block_content, $block_instance->name, $binding_attribute, $source_value );
}
return $modified_block_content;
}
}

add_filter( 'render_block', 'gutenberg_process_block_bindings', 20, 3 );
}
5 changes: 0 additions & 5 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-group-grid-variation', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGroupGridVariation = true', 'before' );
}

if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-bindings', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalBlockBindings = true', 'before' );
}

if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) {
wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' );
}
Expand Down
12 changes: 0 additions & 12 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,6 @@ function gutenberg_initialize_experiments_settings() {
)
);

add_settings_field(
'gutenberg-custom-fields',
__( 'Block Bindings & Custom Fields', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Test connecting block attributes to different sources like custom fields', 'gutenberg' ),
'id' => 'gutenberg-block-bindings',
)
);

register_setting(
'gutenberg-experiments',
'gutenberg-experiments'
Expand Down
5 changes: 5 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.5/kses.php';
require __DIR__ . '/compat/wordpress-6.5/class-wp-script-modules.php';
require __DIR__ . '/compat/wordpress-6.5/scripts-modules.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/class-wp-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/block-bindings.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/sources/post-meta.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/sources/pattern.php';


// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
Expand Down
4 changes: 0 additions & 4 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ test.describe( 'Pattern Overrides', () => {
test.beforeAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.activateTheme( 'emptytheme' ),
requestUtils.setGutenbergExperiments( [
'gutenberg-block-bindings',
] ),
requestUtils.deleteAllBlocks(),
] );
} );
Expand All @@ -20,7 +17,6 @@ test.describe( 'Pattern Overrides', () => {

test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
requestUtils.setGutenbergExperiments( [] ),
requestUtils.activateTheme( 'twentytwentyone' ),
] );
} );
Expand Down

1 comment on commit 015d8b5

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 015d8b5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7643499080
📝 Reported issues:

Please sign in to comment.