Skip to content

Commit

Permalink
Merge branch 'trunk' into try/interactivity-api-router-module
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jan 24, 2024
2 parents e1bf002 + 3c3e0e9 commit 19185a3
Show file tree
Hide file tree
Showing 310 changed files with 13,923 additions and 10,551 deletions.
13 changes: 13 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ _Returns_

- `WPBlock[]`: Block objects.

### getBlocksByName

Returns all blocks that match a blockName. Results include nested blocks.

_Parameters_

- _state_ `Object`: Global application state.
- _blockName_ `?string`: Optional block name, if not specified, returns an empty array.

_Returns_

- `Array`: Array of clientIds of blocks with name equal to blockName.

### getBlockSelectionEnd

Returns the current block selection end. This value may be null, and it may represent either a singular block selection or multi-selection end. A selection is singular if its start and end match.
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core-notices.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export const ExampleComponent = () => {
const notices = useSelect( ( select ) =>
select( noticesStore ).getNotices()
);
const { removeNotices } = useDispatch( noticesStore );
const { removeAllNotices } = useDispatch( noticesStore );
return (
<>
<ul>
Expand Down
5 changes: 2 additions & 3 deletions lib/block-supports/shadow.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ function gutenberg_apply_shadow_support( $block_type, $block_attributes ) {

$shadow_block_styles = array();

$preset_shadow = array_key_exists( 'shadow', $block_attributes ) ? "var:preset|shadow|{$block_attributes['shadow']}" : null;
$custom_shadow = isset( $block_attributes['style']['shadow'] ) ? $block_attributes['style']['shadow'] : null;
$shadow_block_styles['shadow'] = $preset_shadow ? $preset_shadow : $custom_shadow;
$custom_shadow = $block_attributes['style']['shadow'] ?? null;
$shadow_block_styles['shadow'] = $custom_shadow;

$attributes = array();
$styles = gutenberg_style_engine_get_styles( $shadow_block_styles );
Expand Down
21 changes: 0 additions & 21 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,27 +440,6 @@ function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $si
add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );


/**
* Registers the metadata block attribute for all block types.
*
* @param array $args Array of arguments for registering a block type.
* @return array $args
*/
function gutenberg_register_metadata_attribute( $args ) {
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
$args['attributes']['metadata'] = array(
'type' => 'object',
);
}

return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );

/**
* Strips all HTML from the content of footnotes, and sanitizes the ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function replace_html( $block_content, $block_name, $block_attr, $source_
foreach ( $selector_attribute_names as $name ) {
$selector_attrs[ $name ] = $block_reader->get_attribute( $name );
}
$selector_markup = "<$selector>" . esc_html( $source_value ) . "</$selector>";
$selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>";
$amended_content = new WP_HTML_Tag_Processor( $selector_markup );
$amended_content->next_tag();
foreach ( $selector_attrs as $attribute_key => $attribute_value ) {
Expand Down
35 changes: 35 additions & 0 deletions lib/compat/wordpress-6.5/block-bindings/sources/pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Add the metadata source to the block bindings API.
*
* @package gutenberg
*/
if ( function_exists( 'wp_block_bindings_register_source' ) ) {
$pattern_source_callback = function ( $source_attrs, $block_instance, $attribute_name ) {
if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) {
return null;
}
$block_id = $block_instance->attributes['metadata']['id'];
$attribute_override = _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
if ( null === $attribute_override ) {
return null;
}
switch ( $attribute_override[0] ) {
case 0: // remove
/**
* TODO: This currently doesn't remove the attribute, but only set it to an empty string.
* It's a temporary solution until the block binding API supports different operations.
*/
return '';
case 1: // replace
return $attribute_override[1];
default:
return null;
}
};
wp_block_bindings_register_source(
'pattern_attributes',
__( 'Pattern Attributes', 'gutenberg' ),
$pattern_source_callback
);
}
102 changes: 102 additions & 0 deletions lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,105 @@ 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' );
}


/**
* Registers the metadata block attribute for all block types.
*
* @param array $args Array of arguments for registering a block type.
* @return array $args
*/
function gutenberg_register_metadata_attribute( $args ) {
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
$args['attributes']['metadata'] = array(
'type' => 'object',
);
}

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 );
Loading

0 comments on commit 19185a3

Please sign in to comment.