Skip to content

Commit

Permalink
Don't use filter to add editor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 3, 2024
1 parent 59720e6 commit 6f0a931
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/wp-includes/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,3 @@ function get_all_registered_block_bindings_sources() {
function get_block_bindings_source( string $source_name ) {
return WP_Block_Bindings_Registry::get_instance()->get_registered( $source_name );
}

// Add the `uses_context` to the block editor settings to ensure it can be consumed in the client.
add_filter( 'block_editor_settings_all', array( 'WP_Block_Bindings_Registry', 'add_uses_context_to_editor_settings' ), 10 );
17 changes: 17 additions & 0 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,23 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
$editor_settings['postContentAttributes'] = $post_content_block_attributes;
}

// Expose block bindings sources in the editor settings.
$registered_block_bindings_sources = get_all_registered_block_bindings_sources();
if ( ! empty( $registered_block_bindings_sources ) ) {
// Initialize array.
$editor_settings['blockBindings'] = array();
foreach ( $registered_block_bindings_sources as $source_name => $source_properties ) {
// Add source with the label to editor settings.
$editor_settings['blockBindings'][ $source_name ] = array(
'label' => $source_properties->label,
);
// Add `usesContext` property if exists.
if ( ! empty( $source_properties->uses_context ) ) {
$editor_settings['blockBindings'][ $source_name ]['usesContext'] = $source_properties->uses_context;
}
}
}

/**
* Filters the settings to pass to the block editor for all editor type.
*
Expand Down
20 changes: 0 additions & 20 deletions src/wp-includes/class-wp-block-bindings-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,4 @@ public static function get_instance() {

return self::$instance;
}

/**
* Adds the `uses_context` to the editor settings.
*
* This allows to reuse the `uses_context` definition in the editor.
*
* @since 6.6.0
*
* @param array $settings The block editor settings from the `block_editor_settings_all` filter.
* @return array The editor settings with extended block bindings `uses_context`.
*/
public static function add_uses_context_to_editor_settings( $settings ) {
$registered_block_bindings_sources = get_all_registered_block_bindings_sources();
foreach ( $registered_block_bindings_sources as $source ) {
if ( ! empty( $source->uses_context ) ) {
$settings['__experimentalBlockBindings'][ $source->name ]['usesContext'] = $source->uses_context;
}
}
return $settings;
}
}

0 comments on commit 6f0a931

Please sign in to comment.