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 API: Add block bindings PHP registration mechanisms and "Post meta" source under the experimental flag #57249

Merged
merged 28 commits into from
Jan 11, 2024

Conversation

SantosGuillamot
Copy link
Contributor

@SantosGuillamot SantosGuillamot commented Dec 20, 2023

What?

Related issues:

#54536
#53300
#56867

I'm splitting the block bindings API prototype into smaller PRs so they are more manageable and discussions can be kept separately. You can find more information in the original PR.

This pull request covers:

  • Change the "Test connections" experiment name to "Test block bindings & custom fields" and modified the code depending on that.
  • Remove the old UI to add connections.
  • Add the PHP mechanisms needed for the block bindings API:
    • A new PHP function register_block_bindings_source that allow users to create new sources, passing a name, a label and a callback.
    • Logic to replace the proper HTML depending on the block attribute source. Right now it is hardcoded for the allowed blocks (paragraph, heading, button, and image), but we could use the set_inner_html function from the HTML API once that is ready.
  • Register the post_meta source, that binds block attributes and custom fields.
  • Register the pattern_attributes source, that is needed for partially synced patterns.
  • Adapt the partially synced patterns experiment to use the new system.

This pull request doesn't add a UI to create the bindings, so the attribute has to be added manually.

Why?

This pull request is the basis for the block bindings API which will enable the connection between block attributes and other sources like custom fields.

As mentioned, I am splitting the original prototype into smaller PRs.

Testing Instructions

For all the testing we have to go to Gutenberg-Experiments and enable the Test Block Bindings option.

Test that adding the bindings attribute works

  1. Register a new custom field however you prefer. You can use a snippet similar to this:
register_meta(
	'post',
	'text_custom_field',
	array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'default'	   => 'This is the content of the text custom field',
	)
);
register_meta(
	'post',
	'url_custom_field',
	array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'default'	   => 'https://wpmovies.dev/wp-content/uploads/2023/04/goncharov-poster-original-1-682x1024.jpeg',
	)
);
  1. Create a new post/page and insert a paragraph.
  2. Open the code editor view and modify the attributes of the paragraph to create the binding:
{"metadata":{"bindings":{"content":{"source":{"name":"post_meta","attributes":{"value":"text_custom_field"}}}}}}
  1. Go to the frontend and check that, independently of the content of the paragraph in the editor, it is changed by the custom field value in the server.

Test that multiple attributes values can be connected

Repeat the same process but using a button (or an image) to bind the content and the URL.

  1. Add bindings for both attributes with something like this:
{"metadata":{"bindings":{"text":{"source":{"name":"post_meta","attributes":{"value":"text_custom_field"}}}, "url":{"source":{"name":"post_meta","attributes":{"value":"url_custom_field"}}}}}}
  1. Go to the frontend and check that both the text and the URL point to the custom field.

Check that the old UI is not there

In the existing experiment, the UI used doesn't make sense anymore.

  1. Go to any paragraph.
  2. Check that in the block panel in the right sidebar the Connections section doesn't exist anymore.

Test that new sources can be registered

  1. Using a PHP snippet, or any method preferred, register a new source using the register_block_bindings_source function. In this case, I'm adding a "Shortcode" source and create a custom one to test it:
// // Register the shortcode source
if (function_exists('register_block_bindings_source')) {
    $shortcode_source_callback = function ( $source_attrs, $block_content, $block, $block_instance ) {
		return do_shortcode( $source_attrs['value'] );
	};
	register_block_bindings_source( 'shortcode', $shortcode_source_callback );
	register_block_bindings_source(
		'shortcode',
		array(
			'label' => __( 'Shortcode' ),
			'apply' => $shortcode_source_callback,
		)
	);
}

// Add a shortcode for testing
function my_custom_shortcode_callback() {
    return "This is my custom shortcode content.";
}
add_shortcode('my_custom_shortcode', 'my_custom_shortcode_callback');
  1. In any paragraph, add a new attribute pointing to the new source you created:
{"metadata":{"bindings":{"content":{"source":{"name":"shortcode","attributes":{"value":"[my_custom_shortcode]"}}}}}}
  1. Check in the frontend that the value of your source is shown.

Test that partially synced patterns keep working as expected
Partially synced patterns experiment should keep working as it does before this PR.

  1. Go to the Site Editor -> Patterns -> Create a new synced pattern.
  2. Add a heading and a paragraph.
  3. In the block settings of the paragraph -> Advanced -> Toggle Synced Attribute "content" to enable editing the paragraph.
  4. Insert that pattern into a post/page multiple times and define different values for the paragraphs.
  5. Check in the frontend that the value persists.
  6. Modify something in the synced pattern (layout for example) and check that is applied to the post/page you created.

@SantosGuillamot SantosGuillamot added [Feature] Block API API that allows to express the block paradigm. [Type] New API New API to be used by plugin developers or package users. labels Dec 20, 2023
Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/block-bindings/html-processing.php
❔ lib/experimental/block-bindings/index.php
❔ lib/experimental/block-bindings/sources/index.php
❔ lib/experimental/block-bindings/sources/pattern.php
❔ lib/experimental/block-bindings/sources/post-meta.php
❔ lib/block-supports/pattern.php
❔ lib/experimental/blocks.php
❔ lib/experimental/editor-settings.php
❔ lib/experiments-page.php

Copy link

github-actions bot commented Dec 20, 2023

Size Change: +35 B (0%)

Total Size: 1.69 MB

Filename Size Change
build/block-editor/index.min.js 247 kB -78 B (0%)
build/block-library/blocks/button/style-rtl.css 632 B -1 B (0%)
build/block-library/blocks/button/style.css 631 B -1 B (0%)
build/block-library/blocks/table/editor-rtl.css 395 B -4 B (-1%)
build/block-library/blocks/table/editor.css 395 B -4 B (-1%)
build/block-library/editor-rtl.css 12.3 kB -4 B (0%)
build/block-library/editor.css 12.3 kB -4 B (0%)
build/block-library/index.min.js 215 kB -28 B (0%)
build/block-library/style-rtl.css 14.7 kB +3 B (0%)
build/components/index.min.js 235 kB +13 B (0%)
build/edit-post/style-rtl.css 6.29 kB -10 B (0%)
build/edit-post/style.css 6.27 kB -16 B (0%)
build/edit-site/index.min.js 195 kB +186 B (0%)
build/edit-site/style-rtl.css 15 kB -14 B (0%)
build/edit-site/style.css 15 kB -12 B (0%)
build/editor/index.min.js 59.1 kB +1 B (0%)
build/patterns/index.min.js 5.37 kB +8 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 964 B
build/annotations/index.min.js 2.71 kB
build/api-fetch/index.min.js 2.29 kB
build/autop/index.min.js 2.11 kB
build/blob/index.min.js 590 B
build/block-directory/index.min.js 7.25 kB
build/block-directory/style-rtl.css 1.04 kB
build/block-directory/style.css 1.04 kB
build/block-editor/content-rtl.css 4.31 kB
build/block-editor/content.css 4.31 kB
build/block-editor/default-editor-styles-rtl.css 403 B
build/block-editor/default-editor-styles.css 403 B
build/block-editor/style-rtl.css 15.3 kB
build/block-editor/style.css 15.3 kB
build/block-library/blocks/archives/editor-rtl.css 61 B
build/block-library/blocks/archives/editor.css 60 B
build/block-library/blocks/archives/style-rtl.css 90 B
build/block-library/blocks/archives/style.css 90 B
build/block-library/blocks/audio/editor-rtl.css 150 B
build/block-library/blocks/audio/editor.css 150 B
build/block-library/blocks/audio/style-rtl.css 122 B
build/block-library/blocks/audio/style.css 122 B
build/block-library/blocks/audio/theme-rtl.css 138 B
build/block-library/blocks/audio/theme.css 138 B
build/block-library/blocks/avatar/editor-rtl.css 116 B
build/block-library/blocks/avatar/editor.css 116 B
build/block-library/blocks/avatar/style-rtl.css 104 B
build/block-library/blocks/avatar/style.css 104 B
build/block-library/blocks/block/editor-rtl.css 305 B
build/block-library/blocks/block/editor.css 305 B
build/block-library/blocks/button/editor-rtl.css 419 B
build/block-library/blocks/button/editor.css 417 B
build/block-library/blocks/buttons/editor-rtl.css 337 B
build/block-library/blocks/buttons/editor.css 337 B
build/block-library/blocks/buttons/style-rtl.css 332 B
build/block-library/blocks/buttons/style.css 332 B
build/block-library/blocks/calendar/style-rtl.css 239 B
build/block-library/blocks/calendar/style.css 239 B
build/block-library/blocks/categories/editor-rtl.css 113 B
build/block-library/blocks/categories/editor.css 112 B
build/block-library/blocks/categories/style-rtl.css 124 B
build/block-library/blocks/categories/style.css 124 B
build/block-library/blocks/code/editor-rtl.css 53 B
build/block-library/blocks/code/editor.css 53 B
build/block-library/blocks/code/style-rtl.css 121 B
build/block-library/blocks/code/style.css 121 B
build/block-library/blocks/code/theme-rtl.css 124 B
build/block-library/blocks/code/theme.css 124 B
build/block-library/blocks/columns/editor-rtl.css 108 B
build/block-library/blocks/columns/editor.css 108 B
build/block-library/blocks/columns/style-rtl.css 421 B
build/block-library/blocks/columns/style.css 421 B
build/block-library/blocks/comment-author-avatar/editor-rtl.css 125 B
build/block-library/blocks/comment-author-avatar/editor.css 125 B
build/block-library/blocks/comment-content/style-rtl.css 92 B
build/block-library/blocks/comment-content/style.css 92 B
build/block-library/blocks/comment-template/style-rtl.css 199 B
build/block-library/blocks/comment-template/style.css 198 B
build/block-library/blocks/comments-pagination-numbers/editor-rtl.css 123 B
build/block-library/blocks/comments-pagination-numbers/editor.css 121 B
build/block-library/blocks/comments-pagination/editor-rtl.css 222 B
build/block-library/blocks/comments-pagination/editor.css 209 B
build/block-library/blocks/comments-pagination/style-rtl.css 235 B
build/block-library/blocks/comments-pagination/style.css 231 B
build/block-library/blocks/comments-title/editor-rtl.css 75 B
build/block-library/blocks/comments-title/editor.css 75 B
build/block-library/blocks/comments/editor-rtl.css 840 B
build/block-library/blocks/comments/editor.css 839 B
build/block-library/blocks/comments/style-rtl.css 637 B
build/block-library/blocks/comments/style.css 636 B
build/block-library/blocks/cover/editor-rtl.css 647 B
build/block-library/blocks/cover/editor.css 650 B
build/block-library/blocks/cover/style-rtl.css 1.7 kB
build/block-library/blocks/cover/style.css 1.69 kB
build/block-library/blocks/details/editor-rtl.css 65 B
build/block-library/blocks/details/editor.css 65 B
build/block-library/blocks/details/style-rtl.css 98 B
build/block-library/blocks/details/style.css 98 B
build/block-library/blocks/embed/editor-rtl.css 293 B
build/block-library/blocks/embed/editor.css 293 B
build/block-library/blocks/embed/style-rtl.css 410 B
build/block-library/blocks/embed/style.css 410 B
build/block-library/blocks/embed/theme-rtl.css 138 B
build/block-library/blocks/embed/theme.css 138 B
build/block-library/blocks/file/editor-rtl.css 316 B
build/block-library/blocks/file/editor.css 316 B
build/block-library/blocks/file/style-rtl.css 280 B
build/block-library/blocks/file/style.css 281 B
build/block-library/blocks/file/view.min.js 322 B
build/block-library/blocks/footnotes/style-rtl.css 201 B
build/block-library/blocks/footnotes/style.css 199 B
build/block-library/blocks/form-input/editor-rtl.css 229 B
build/block-library/blocks/form-input/editor.css 228 B
build/block-library/blocks/form-input/style-rtl.css 343 B
build/block-library/blocks/form-input/style.css 343 B
build/block-library/blocks/form-submission-notification/editor-rtl.css 343 B
build/block-library/blocks/form-submission-notification/editor.css 342 B
build/block-library/blocks/form-submit-button/style-rtl.css 69 B
build/block-library/blocks/form-submit-button/style.css 69 B
build/block-library/blocks/form/view.min.js 452 B
build/block-library/blocks/freeform/editor-rtl.css 2.61 kB
build/block-library/blocks/freeform/editor.css 2.61 kB
build/block-library/blocks/gallery/editor-rtl.css 957 B
build/block-library/blocks/gallery/editor.css 962 B
build/block-library/blocks/gallery/style-rtl.css 1.75 kB
build/block-library/blocks/gallery/style.css 1.75 kB
build/block-library/blocks/gallery/theme-rtl.css 122 B
build/block-library/blocks/gallery/theme.css 122 B
build/block-library/blocks/group/editor-rtl.css 654 B
build/block-library/blocks/group/editor.css 654 B
build/block-library/blocks/group/style-rtl.css 57 B
build/block-library/blocks/group/style.css 57 B
build/block-library/blocks/group/theme-rtl.css 78 B
build/block-library/blocks/group/theme.css 78 B
build/block-library/blocks/heading/style-rtl.css 189 B
build/block-library/blocks/heading/style.css 189 B
build/block-library/blocks/html/editor-rtl.css 340 B
build/block-library/blocks/html/editor.css 341 B
build/block-library/blocks/image/editor-rtl.css 834 B
build/block-library/blocks/image/editor.css 833 B
build/block-library/blocks/image/style-rtl.css 1.61 kB
build/block-library/blocks/image/style.css 1.6 kB
build/block-library/blocks/image/theme-rtl.css 137 B
build/block-library/blocks/image/theme.css 137 B
build/block-library/blocks/image/view.min.js 2.02 kB
build/block-library/blocks/latest-comments/style-rtl.css 357 B
build/block-library/blocks/latest-comments/style.css 357 B
build/block-library/blocks/latest-posts/editor-rtl.css 213 B
build/block-library/blocks/latest-posts/editor.css 212 B
build/block-library/blocks/latest-posts/style-rtl.css 478 B
build/block-library/blocks/latest-posts/style.css 478 B
build/block-library/blocks/list/style-rtl.css 88 B
build/block-library/blocks/list/style.css 88 B
build/block-library/blocks/media-text/editor-rtl.css 266 B
build/block-library/blocks/media-text/editor.css 263 B
build/block-library/blocks/media-text/style-rtl.css 505 B
build/block-library/blocks/media-text/style.css 503 B
build/block-library/blocks/more/editor-rtl.css 431 B
build/block-library/blocks/more/editor.css 431 B
build/block-library/blocks/navigation-link/editor-rtl.css 671 B
build/block-library/blocks/navigation-link/editor.css 672 B
build/block-library/blocks/navigation-link/style-rtl.css 103 B
build/block-library/blocks/navigation-link/style.css 103 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 299 B
build/block-library/blocks/navigation-submenu/editor.css 299 B
build/block-library/blocks/navigation/editor-rtl.css 2.26 kB
build/block-library/blocks/navigation/editor.css 2.26 kB
build/block-library/blocks/navigation/style-rtl.css 2.25 kB
build/block-library/blocks/navigation/style.css 2.23 kB
build/block-library/blocks/navigation/view.min.js 1.1 kB
build/block-library/blocks/nextpage/editor-rtl.css 395 B
build/block-library/blocks/nextpage/editor.css 395 B
build/block-library/blocks/page-list/editor-rtl.css 401 B
build/block-library/blocks/page-list/editor.css 401 B
build/block-library/blocks/page-list/style-rtl.css 175 B
build/block-library/blocks/page-list/style.css 175 B
build/block-library/blocks/paragraph/editor-rtl.css 235 B
build/block-library/blocks/paragraph/editor.css 235 B
build/block-library/blocks/paragraph/style-rtl.css 335 B
build/block-library/blocks/paragraph/style.css 335 B
build/block-library/blocks/post-author/style-rtl.css 175 B
build/block-library/blocks/post-author/style.css 176 B
build/block-library/blocks/post-comments-form/editor-rtl.css 96 B
build/block-library/blocks/post-comments-form/editor.css 96 B
build/block-library/blocks/post-comments-form/style-rtl.css 508 B
build/block-library/blocks/post-comments-form/style.css 508 B
build/block-library/blocks/post-date/style-rtl.css 61 B
build/block-library/blocks/post-date/style.css 61 B
build/block-library/blocks/post-excerpt/editor-rtl.css 71 B
build/block-library/blocks/post-excerpt/editor.css 71 B
build/block-library/blocks/post-excerpt/style-rtl.css 141 B
build/block-library/blocks/post-excerpt/style.css 141 B
build/block-library/blocks/post-featured-image/editor-rtl.css 666 B
build/block-library/blocks/post-featured-image/editor.css 662 B
build/block-library/blocks/post-featured-image/style-rtl.css 345 B
build/block-library/blocks/post-featured-image/style.css 345 B
build/block-library/blocks/post-navigation-link/style-rtl.css 215 B
build/block-library/blocks/post-navigation-link/style.css 214 B
build/block-library/blocks/post-template/editor-rtl.css 99 B
build/block-library/blocks/post-template/editor.css 98 B
build/block-library/blocks/post-template/style-rtl.css 409 B
build/block-library/blocks/post-template/style.css 408 B
build/block-library/blocks/post-terms/style-rtl.css 96 B
build/block-library/blocks/post-terms/style.css 96 B
build/block-library/blocks/post-time-to-read/style-rtl.css 69 B
build/block-library/blocks/post-time-to-read/style.css 69 B
build/block-library/blocks/post-title/style-rtl.css 100 B
build/block-library/blocks/post-title/style.css 100 B
build/block-library/blocks/preformatted/style-rtl.css 125 B
build/block-library/blocks/preformatted/style.css 125 B
build/block-library/blocks/pullquote/editor-rtl.css 135 B
build/block-library/blocks/pullquote/editor.css 135 B
build/block-library/blocks/pullquote/style-rtl.css 335 B
build/block-library/blocks/pullquote/style.css 335 B
build/block-library/blocks/pullquote/theme-rtl.css 168 B
build/block-library/blocks/pullquote/theme.css 168 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B
build/block-library/blocks/query-pagination/editor-rtl.css 221 B
build/block-library/blocks/query-pagination/editor.css 211 B
build/block-library/blocks/query-pagination/style-rtl.css 288 B
build/block-library/blocks/query-pagination/style.css 284 B
build/block-library/blocks/query-title/style-rtl.css 63 B
build/block-library/blocks/query-title/style.css 63 B
build/block-library/blocks/query/editor-rtl.css 486 B
build/block-library/blocks/query/editor.css 486 B
build/block-library/blocks/query/style-rtl.css 312 B
build/block-library/blocks/query/style.css 308 B
build/block-library/blocks/query/view.min.js 647 B
build/block-library/blocks/quote/style-rtl.css 237 B
build/block-library/blocks/quote/style.css 237 B
build/block-library/blocks/quote/theme-rtl.css 223 B
build/block-library/blocks/quote/theme.css 226 B
build/block-library/blocks/read-more/style-rtl.css 140 B
build/block-library/blocks/read-more/style.css 140 B
build/block-library/blocks/rss/editor-rtl.css 149 B
build/block-library/blocks/rss/editor.css 149 B
build/block-library/blocks/rss/style-rtl.css 289 B
build/block-library/blocks/rss/style.css 288 B
build/block-library/blocks/search/editor-rtl.css 184 B
build/block-library/blocks/search/editor.css 184 B
build/block-library/blocks/search/style-rtl.css 602 B
build/block-library/blocks/search/style.css 602 B
build/block-library/blocks/search/theme-rtl.css 114 B
build/block-library/blocks/search/theme.css 114 B
build/block-library/blocks/search/view.min.js 475 B
build/block-library/blocks/separator/editor-rtl.css 146 B
build/block-library/blocks/separator/editor.css 146 B
build/block-library/blocks/separator/style-rtl.css 234 B
build/block-library/blocks/separator/style.css 234 B
build/block-library/blocks/separator/theme-rtl.css 194 B
build/block-library/blocks/separator/theme.css 194 B
build/block-library/blocks/shortcode/editor-rtl.css 329 B
build/block-library/blocks/shortcode/editor.css 329 B
build/block-library/blocks/site-logo/editor-rtl.css 760 B
build/block-library/blocks/site-logo/editor.css 760 B
build/block-library/blocks/site-logo/style-rtl.css 204 B
build/block-library/blocks/site-logo/style.css 204 B
build/block-library/blocks/site-tagline/editor-rtl.css 86 B
build/block-library/blocks/site-tagline/editor.css 86 B
build/block-library/blocks/site-title/editor-rtl.css 116 B
build/block-library/blocks/site-title/editor.css 116 B
build/block-library/blocks/site-title/style-rtl.css 57 B
build/block-library/blocks/site-title/style.css 57 B
build/block-library/blocks/social-link/editor-rtl.css 184 B
build/block-library/blocks/social-link/editor.css 184 B
build/block-library/blocks/social-links/editor-rtl.css 682 B
build/block-library/blocks/social-links/editor.css 681 B
build/block-library/blocks/social-links/style-rtl.css 1.49 kB
build/block-library/blocks/social-links/style.css 1.49 kB
build/block-library/blocks/spacer/editor-rtl.css 359 B
build/block-library/blocks/spacer/editor.css 359 B
build/block-library/blocks/spacer/style-rtl.css 48 B
build/block-library/blocks/spacer/style.css 48 B
build/block-library/blocks/table/style-rtl.css 646 B
build/block-library/blocks/table/style.css 645 B
build/block-library/blocks/table/theme-rtl.css 157 B
build/block-library/blocks/table/theme.css 157 B
build/block-library/blocks/tag-cloud/style-rtl.css 251 B
build/block-library/blocks/tag-cloud/style.css 253 B
build/block-library/blocks/template-part/editor-rtl.css 403 B
build/block-library/blocks/template-part/editor.css 403 B
build/block-library/blocks/template-part/theme-rtl.css 101 B
build/block-library/blocks/template-part/theme.css 101 B
build/block-library/blocks/term-description/style-rtl.css 111 B
build/block-library/blocks/term-description/style.css 111 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B
build/block-library/blocks/text-columns/editor.css 95 B
build/block-library/blocks/text-columns/style-rtl.css 166 B
build/block-library/blocks/text-columns/style.css 166 B
build/block-library/blocks/verse/style-rtl.css 99 B
build/block-library/blocks/verse/style.css 99 B
build/block-library/blocks/video/editor-rtl.css 552 B
build/block-library/blocks/video/editor.css 555 B
build/block-library/blocks/video/style-rtl.css 191 B
build/block-library/blocks/video/style.css 191 B
build/block-library/blocks/video/theme-rtl.css 139 B
build/block-library/blocks/video/theme.css 139 B
build/block-library/classic-rtl.css 179 B
build/block-library/classic.css 179 B
build/block-library/common-rtl.css 1.11 kB
build/block-library/common.css 1.11 kB
build/block-library/editor-elements-rtl.css 75 B
build/block-library/editor-elements.css 75 B
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/reset-rtl.css 472 B
build/block-library/reset.css 472 B
build/block-library/style.css 14.7 kB
build/block-library/theme-rtl.css 700 B
build/block-library/theme.css 705 B
build/block-serialization-default-parser/index.min.js 1.13 kB
build/block-serialization-spec-parser/index.min.js 2.87 kB
build/blocks/index.min.js 51.6 kB
build/commands/index.min.js 15.5 kB
build/commands/style-rtl.css 947 B
build/commands/style.css 942 B
build/components/style-rtl.css 12.1 kB
build/components/style.css 12.1 kB
build/compose/index.min.js 12.8 kB
build/core-commands/index.min.js 2.73 kB
build/core-data/index.min.js 72.7 kB
build/customize-widgets/index.min.js 12.1 kB
build/customize-widgets/style-rtl.css 1.36 kB
build/customize-widgets/style.css 1.36 kB
build/data-controls/index.min.js 651 B
build/data/index.min.js 8.94 kB
build/date/index.min.js 17.9 kB
build/deprecated/index.min.js 462 B
build/dom-ready/index.min.js 336 B
build/dom/index.min.js 4.68 kB
build/edit-post/classic-rtl.css 571 B
build/edit-post/classic.css 571 B
build/edit-post/index.min.js 27.9 kB
build/edit-widgets/index.min.js 17.3 kB
build/edit-widgets/style-rtl.css 4.73 kB
build/edit-widgets/style.css 4.72 kB
build/editor/style-rtl.css 5.21 kB
build/editor/style.css 5.21 kB
build/element/index.min.js 4.87 kB
build/escape-html/index.min.js 548 B
build/format-library/index.min.js 7.98 kB
build/format-library/style-rtl.css 500 B
build/format-library/style.css 500 B
build/hooks/index.min.js 1.57 kB
build/html-entities/index.min.js 454 B
build/i18n/index.min.js 3.61 kB
build/interactivity/file.min.js 442 B
build/interactivity/image.min.js 2.15 kB
build/interactivity/index.min.js 12.5 kB
build/interactivity/navigation.min.js 1.23 kB
build/interactivity/query.min.js 791 B
build/interactivity/search.min.js 610 B
build/is-shallow-equal/index.min.js 535 B
build/keyboard-shortcuts/index.min.js 1.76 kB
build/keycodes/index.min.js 1.49 kB
build/list-reusable-blocks/index.min.js 2.11 kB
build/list-reusable-blocks/style-rtl.css 865 B
build/list-reusable-blocks/style.css 865 B
build/media-utils/index.min.js 2.92 kB
build/modules/importmap-polyfill.min.js 12.2 kB
build/notices/index.min.js 964 B
build/nux/index.min.js 2.01 kB
build/nux/style-rtl.css 775 B
build/nux/style.css 771 B
build/patterns/style-rtl.css 564 B
build/patterns/style.css 564 B
build/plugins/index.min.js 1.81 kB
build/preferences-persistence/index.min.js 2.07 kB
build/preferences/index.min.js 1.26 kB
build/primitives/index.min.js 994 B
build/priority-queue/index.min.js 1.52 kB
build/private-apis/index.min.js 1 kB
build/react-i18n/index.min.js 631 B
build/react-refresh-entry/index.min.js 9.46 kB
build/react-refresh-runtime/index.min.js 6.78 kB
build/redux-routine/index.min.js 2.71 kB
build/reusable-blocks/index.min.js 2.74 kB
build/reusable-blocks/style-rtl.css 265 B
build/reusable-blocks/style.css 265 B
build/rich-text/index.min.js 10.4 kB
build/router/index.min.js 1.79 kB
build/server-side-render/index.min.js 1.96 kB
build/shortcode/index.min.js 1.4 kB
build/style-engine/index.min.js 2.06 kB
build/token-list/index.min.js 587 B
build/url/index.min.js 3.83 kB
build/vendors/inert-polyfill.min.js 2.48 kB
build/vendors/react-dom.min.js 41.8 kB
build/vendors/react.min.js 4.02 kB
build/viewport/index.min.js 967 B
build/warning/index.min.js 259 B
build/widgets/index.min.js 7.22 kB
build/widgets/style-rtl.css 1.18 kB
build/widgets/style.css 1.18 kB
build/wordcount/index.min.js 1.03 kB

compressed-size-action

@michalczaplinski michalczaplinski changed the title Block Bindigns API: Add block bindings PHP registration mechanisms and "Post meta" source under the experimental flag Block Bindings API: Add block bindings PHP registration mechanisms and "Post meta" source under the experimental flag Dec 20, 2023
@SantosGuillamot SantosGuillamot added the [Feature] Custom Fields Anything related to the custom fields project - connecting block attributes and dynamic values label Dec 22, 2023
Copy link
Contributor

@artemiomorales artemiomorales left a comment

Choose a reason for hiding this comment

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

Fantastic job with this and the related PRs 🙌 I’ve followed the testing instructions and everything works as expected. While I don’t have enough experience with Gutenberg to advise on best practices there, I have a few general thoughts.

At a high level, as per our conversation @SantosGuillamot, I think a minimal version like this without any visible UI could be good to merge into core for 6.5 (though perhaps a bit early for the partially synced patterns). Regardless, I think we can just focus on merging the prototype to Gutenberg and collecting feedback rather than worrying too much about core for the moment.

A heads up that both @c4rl0sbr4v0 and I found the customizable attributes in the partially synced patterns to have a confusing label, since these attributes are in fact the ones that will not be synchronized. Would we be able to rename this heading from Synced Attributes to Customizable Attributes or Decoupled Attributes or another more apt name?

Screenshot 2023-12-22 at 3 28 43 PM

Test that multiple attributes values can be connected
Repeat the same process but using a button (or an image) to bind the content and the URL.

To help folks who are testing and may not have a chance to look in-depth at the code, we should note that currently 1.) for the button, only the url and text attributes work, and 2.) for the image, only the url and title attributes work. On that note, how about we also expose the alt attribute for the image? I know we’re mostly in a phase of getting feedback at the moment, but that small change would make this more useable in the wild.

Lastly, I saw in the previous PR's description that you were still undecided on the format of the bindings property. Has that discussion now been settled? Note that I don’t have a strong opinion in any direction — I’m just curious to hear if it's been resolved or requires further input.

Will continue looking at the code and offer additional thoughts as they arise. Thanks 🙏

@SantosGuillamot
Copy link
Contributor Author

Thanks for sharing your thoughts, Artemio 🙂

Would we be able to rename this heading from Synced Attributes to Customizable Attributes or Decoupled Attributes or another more apt name?

Would you mind sharing this feedback in the Synced Patterns issue: link? This PR just focuses on the block bindings API, that is used by synced patterns. But their UI is independent of this.

On that note, how about we also expose the alt attribute for the image?

alt attribute should work exactly like the title so there should be no problem adding that. I just added a few attributes to understand the system better, but we can definitely add alt as well.

Lastly, I saw in the #56867 that you were still undecided on the format of the bindings property. Has that discussion now been settled?

I discussed it with some folks, and we agreed on an initial syntax, trying to leave as few remaining decisions as possible. But if you, or anyone else, believes another syntax is better, we can still modify it.

@artemiomorales
Copy link
Contributor

Would you mind sharing this feedback in the Synced Patterns issue: #53705? This PR just focuses on the block bindings API, that is used by synced patterns. But their UI is independent of this.

I just checked out trunk and it appears the wording was changed to Pattern Overrides: Allow instance overrides in the following PR, so all good on this front: #57009

Screenshot 2024-01-02 at 2 30 48 PM

I discussed it with some folks, and we agreed on an initial syntax, trying to leave as few remaining decisions as possible. But if you, or anyone else, believes another syntax is better, we can still modify it.

Ok great. I think the initial syntax we have is good 👍

@@ -18,51 +18,45 @@ import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
function PartialSyncingControls( { name, attributes, setAttributes } ) {
const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this PR would be the best place to do this, but I think we should rename this to editableAttributes or something similar for clarity, as these are the attributes that users will be able to override.

@artemiomorales
Copy link
Contributor

I renamed some variables for clarity and cleaned up some code around the button markup in 176da0a, though feel free to change it back or continue modifying.

As far as I can tell, everything in the PR still seems to work.

@kevin940726
Copy link
Member

A heads up that both @c4rl0sbr4v0 and I found the customizable attributes in the partially synced patterns to have a confusing label, since these attributes are in fact the ones that will not be synchronized. Would we be able to rename this heading from Synced Attributes to Customizable Attributes or Decoupled Attributes or another more apt name?

We recently modified the control to be a single checkbox in #57009. We just need a rebase here 🙂. I'm happy to help too!

@artemiomorales
Copy link
Contributor

artemiomorales commented Jan 4, 2024

We recently modified the control to be a single checkbox in #57009. We just need a rebase here 🙂. I'm happy to help too!

@kevin940726 Great, thanks! I've opened a throwaway PR in my fork to illustrate how I'm thinking to approach the rebase. I believe it works as expected, but would you be able to take a look?

Note that I added a commit to attempt to actually use the multiple overridden attributes in the pattern, but it did not work for the image — only one attribute was read and it was erroneously used for both the title and alt.

I see here however that support for multiple attributes does not yet seem to be added in the editor code for the partially synced pattern. Am I following this correctly?

I just want to check in before I get too deep. Any guidance appreciated!

@kevin940726
Copy link
Member

Note that I added a commit to attempt to actually use the multiple overridden attributes in the pattern, but it did not work for the image — only one attribute was read and it was erroneously used for both the title and alt.

I see here however that support for multiple attributes does not yet seem to be added in the editor code for the partially synced pattern. Am I following this correctly?

Yes, that's correct! We don't currently support them at the time, but we're planning to add support soon.

The rebase PR looks good! It's mostly the same as what I did 😆. I think the next step for us would be adding support for core/heading and core/button. Then, we'll consider adding support for core/image (and multiple attributes).

@artemiomorales
Copy link
Contributor

Noting here that I've come across a bug — when enabling pattern overrides, the original content disappears when the pattern is used in an instance.

I won't have time to look at this until next week, but I'm documenting it here in case anyone is able to look.

pattern-override-bug.mp4

Copy link
Contributor

@artemiomorales artemiomorales left a comment

Choose a reason for hiding this comment

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

I'm approving with the caveat that there's still a lot to improve in terms of the API as well as removing the old block supports code and other improvements.

There's already a lot happening here though so I think it makes sense to begin addressing these in follow up PRs, as the partially synced patterns cleanup especially seems to be moving beyond the scope of what's being proposed here.

Outstanding Items

  • Finish removing block supports
  • Reformulate registration mechanisms
  • Begin implementing singleton pattern via a Block Bindings class
  • I noticed on trunk today that the block UI has gone missing from pattern instances when the partially synced patterns experiment is enabled, and while not caused by this PR, it prevents the functionality here from working and also needs to be fixed. Edit: It turns out this is now expected behavior.
Screenshot 2024-01-09 at 4 59 42 PM

@@ -13,7 +13,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_pattern_support( $block_type ) {
$pattern_support = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( '__experimentalConnections' ), false ) : false;
$pattern_support = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( '__experimentalBlockBindings' ), false ) : false;

if ( $pattern_support ) {
if ( ! $block_type->uses_context ) {
Copy link
Contributor

@artemiomorales artemiomorales Jan 9, 2024

Choose a reason for hiding this comment

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

If we're removing block supports. we should probably remove this entire file, which exists inside the block-supports/ directory, and put its logic elsewhere. EDIT: Since we may be adding block supports in the future, perhaps removing this file isn't necessary.

packages/block-library/src/block/edit.js Outdated Show resolved Hide resolved
@michalczaplinski michalczaplinski self-requested a review January 10, 2024 13:49
Copy link
Contributor

@michalczaplinski michalczaplinski left a comment

Choose a reason for hiding this comment

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

Agree with Artemio's review above.

I would only add more context to this:

Reformulate registration mechanisms

I think that there are 2 aspects to this:

  1. Reconsidering what parameters should be passed to the apply as already mentioned by Dan in Block Bindings API: Add block bindings PHP registration mechanisms and "Post meta" source under the experimental flag #57249 (comment)
  2. Possibly simplifying the signature of register_block_bindings_source.
    I believe that instead of register_block_bindings_source( $source_name, $source_args ) where `$source_args is an associative array, the function could have 3 parameters:
function register_block_bindings_source( $source_name, $label, $apply) {
  global $block_bindings_sources;
  $block_bindings_sources[ $source_name ] =  array( $label, $apply );
}

This way, the PHPDoc is much simpler and the signature is simplified as well.

I think that this and removing the block supports are the only 2 items we should address before merging.

@artemiomorales
Copy link
Contributor

Ok @michalczaplinski I've removed all of the block supports and hardcoded support for just the paragraph block, as that's all that's currently enabled for pattern overrides.

I've also updated the signature of the register function.

Let me know what you think!

Copy link
Member

@kevin940726 kevin940726 left a comment

Choose a reason for hiding this comment

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

I think this is good to go as a first step. As per Pattern Overrides we can add a follow-up to support both core/heading and core/button. Then we can also work on adding support for core/image if possible!

@michalczaplinski michalczaplinski self-requested a review January 11, 2024 14:01
Copy link
Contributor

@michalczaplinski michalczaplinski left a comment

Choose a reason for hiding this comment

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

Looks good to me!

I've only done a minor adjustment of the PHPDoc string.

We can improve on it in the follow ups!

@michalczaplinski michalczaplinski enabled auto-merge (squash) January 11, 2024 14:04
Copy link

Flaky tests detected in 23629de.
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/7489673430
📝 Reported issues:

@michalczaplinski michalczaplinski merged commit cd548af into trunk Jan 11, 2024
57 checks passed
@michalczaplinski michalczaplinski deleted the add/block-bindings-php-registration-mechanisms branch January 11, 2024 14:17
@github-actions github-actions bot added this to the Gutenberg 17.6 milestone Jan 11, 2024
@gziolo
Copy link
Member

gziolo commented Jan 11, 2024

@kevin940726, does it mean that Pattern Overrides work only with the Paragraph block at the moment? Who is working on the follow-up PR?

@kevin940726
Copy link
Member

@kevin940726, does it mean that Pattern Overrides work only with the Paragraph block at the moment? Who is working on the follow-up PR?

Yes. Because both core/heading and core/button rely on this PR. It should be trivial to implement now that this is merged though!

}
$source_value = $source_callback( $source_args, $block_instance, $binding_attribute );
// If the value is null, process next attribute.
if ( is_null( $source_value ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

I was just reminded that if the attribute accepts null or undefined as valid values for different meanings, then we should still be able to apply overrides here. Do you think we need a special value here to represent none? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

@kevin940726 can you elaborate?
What do you think @SantosGuillamot @michalczaplinski?

I wonder how we would handle a null or undefined value as valid here or here. It seems to me that what makes sense intuitively is for null or undefined to mean that there is no valid value to apply, but I also don't know enough about how developers would use this in the wild to have a strong opinion either way.

Copy link
Member

Choose a reason for hiding this comment

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

For context, I was trying to add support for the linkTarget attribute of the button block. It accepts a string (like _blank) or undefined for no target. Let's say we have a source pattern with a button block and its linkTarget is _blank. Now we want to override the source pattern in an instance to remove the linkTarget. We have two problems here:

  1. On the editor side, we need a way to represent undefined because it's not JSON serializable.
  2. On the backend, we need a way to distinguish NULL from "skipping the attribute" because sometimes NULL is a valid value for some attributes.

I opened two draft PRs exploring different solutions for this problem and I'd love all your feedback!

Note that the latter changes the overrides attribute format so it'd be better if we decide the format sooner than later to save us from back-compat pain 😅.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Custom Fields Anything related to the custom fields project - connecting block attributes and dynamic values [Type] New API New API to be used by plugin developers or package users.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants