Skip to content

Pullquote: Try adding Block Bindings support #70975

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

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/compat/wordpress-6.9/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Temporary compatibility shims for block APIs present in Gutenberg.
*
* @package gutenberg
*/

/**

Check warning on line 8 in lib/compat/wordpress-6.9/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.
* Process the block bindings attribute.
*
* @param string $block_content Block Content.
* @param array $parsed_block The full block, including name and attributes.
* @param WP_Block $block_instance The block instance.
* @return string Block content with the bind applied.
*/
function gutenberg_process_pullquote_bindings( $block_content, $parsed_block, $block_instance ) {
if (
empty( $parsed_block['attrs']['metadata']['bindings'] )
) {
return $block_content;
}

Check failure on line 22 in lib/compat/wordpress-6.9/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Functions must not contain multiple empty lines in a row; found 2 empty lines

$bindings = $parsed_block['attrs']['metadata']['bindings'];
$block_reader = new WP_HTML_Tag_Processor( $block_content );
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_instance->name );

Check failure on line 26 in lib/compat/wordpress-6.9/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

while ( $block_reader->next_tag() ) {
$current_tag = $block_reader->get_tag();
if ( 'P' === $current_tag && ! empty( $bindings['value'] ) ) {
$value_binding_source = get_block_bindings_source( $bindings['value']['source'] );
$value_args = ! empty( $bindings['value']['args'] ) && is_array( $bindings['value']['args'] ) ? $bindings['value']['args'] : array();
$block_type->uses_context = array_merge( $block_type->uses_context, $value_binding_source->uses_context );
$block_instance->refresh_context_dependents();
$value = $value_binding_source->get_value( $value_args, $block_instance, 'value' );
$block_reader->set_attribute( 'data-value', $value );
Copy link
Member Author

Choose a reason for hiding this comment

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

The final solution should use set_inner_html once it is implemented.

} else if ( 'CITE' === $current_tag && ! empty( $bindings['citation'] ) ) {

Check warning on line 37 in lib/compat/wordpress-6.9/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Usage of ELSE IF is discouraged; use ELSEIF instead
$citation_binding_source = get_block_bindings_source( $bindings['citation']['source'] );
$citation_args = ! empty( $bindings['citation']['args'] ) && is_array( $bindings['citation']['args'] ) ? $bindings['citation']['args'] : array();
$citation = $citation_binding_source->get_value( $citation_args, $block_instance, 'citation' );
$block_type->uses_context = array_merge( $block_type->uses_context, $value_binding_source->uses_context );
$block_instance->refresh_context_dependents();
$block_reader->set_attribute( 'data-citation', $citation );
}
}

return $block_reader->get_updated_html();
}


add_filter( 'render_block_core/pullquote', 'gutenberg_process_pullquote_bindings', 20, 3 );
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.8/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.8/site-preview.php';

// WordPress 6.9 compat.
require __DIR__ . '/compat/wordpress-6.9/blocks.php';

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/blocks.php';
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/utils/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const BLOCK_BINDINGS_ALLOWED_BLOCKS = {
'core/image': [ 'id', 'url', 'title', 'alt' ],
'core/button': [ 'url', 'text', 'linkTarget', 'rel' ],
'core/post-date': [ 'datetime' ],
'core/pullquote': [ 'citation', 'value' ],
};

/**
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/pullquote/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import clsx from 'clsx';
import { RichText, useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { textAlign, citation, value } = attributes;
const shouldShowCitation = ! RichText.isEmpty( citation );
const { textAlign, citation, value, metadata } = attributes;
const hasBoundCitation = !! metadata?.bindings?.citation;
const shouldShowCitation =
hasBoundCitation || ! RichText.isEmpty( citation );
Comment on lines +13 to +15
Copy link
Member Author

Choose a reason for hiding this comment

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

@cbravobernal, circling back to our chat earlier this week, this check allows us not to render the cite tag when the binding is not set, making the save function fully backward compatible.


return (
<figure
Expand All @@ -23,7 +25,11 @@ export default function save( { attributes } ) {
<blockquote>
<RichText.Content tagName="p" value={ value } />
{ shouldShowCitation && (
<RichText.Content tagName="cite" value={ citation } />
<RichText.Content
tagName="cite"
data-wp-maybe-remove={ hasBoundCitation || undefined }
value={ citation }
/>
) }
</blockquote>
</figure>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function getTransformedMetadata(
'core/heading',
'core/image',
'core/button',
'core/pullquote',
];
// The metadata properties that should be preserved after the transform.
const transformSupportedProps = [];
Expand Down
1 change: 1 addition & 0 deletions packages/patterns/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const PARTIAL_SYNCING_SUPPORTED_BLOCKS = {
'core/heading': [ 'content' ],
'core/button': [ 'text', 'url', 'linkTarget', 'rel' ],
'core/image': [ 'id', 'url', 'title', 'alt' ],
'core/pullquote': [ 'citation', 'value' ],
};

export const PATTERN_OVERRIDES_BINDING_SOURCE = 'core/pattern-overrides';
Loading