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

Block Bindings: Remove the experimental flag #58089

Merged
merged 10 commits into from
Jan 24, 2024
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
Loading