-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Blocks: Re-register core blocks via build copy prefixing #13521
Conversation
5b5c05c
to
9c42ec1
Compare
9c42ec1
to
68a592b
Compare
In 68a592b, I considered a discovery-based implementation, rather than explicitly listing each of the blocks (doing so after personally neglecting to include the second of two entries for the new "Search" block in a rebase). I'm not totally convinced about it yet, partly because of the magical implications and the possible "trust" implications of discovery-based file loading (though it's something we've done previously in #2014, and later removed in #10147). |
lib/load.php
Outdated
$registry->unregister( $block_name ); | ||
} | ||
|
||
require $blocks_dir . $file; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this function is called on the init
hook and the files it requires also trigger functions on the init
hook. Isn't it too late for the init
hooks added in those files to trigger properly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this function is called on the
init
hook and the files it requires also trigger functions on theinit
hook. Isn't it too late for theinit
hooks added in those files to trigger properly?
Mmm, I'd have sworn I'd seen somewhere we'd used a similar implementation, but in mind of the following, I think it'd be expected to not work. If we handle init
here at an earlier priority, it may not be an issue though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured out that my comment is (#13521 (comment)) related to what @youknowriad raised. When I change the code to execute the block's registration immediately then all blocks load properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it too late for the init hooks added in those files to trigger properly?
That should work ever since WP_Hook
was introduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should work ever since
WP_Hook
was introduced.
Can you clarify what you mean @swissspidy ? The current issue is that we want to load the blocks implementations at the same priority as the core registration, which based on the aforementioned test cases does not appear to be a supported use-case for hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/load.php
Outdated
} | ||
|
||
// Derive the assumed block name by file path. | ||
$block_name = 'core/' . $parts['filename']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not always the same, for example:
https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/classic/index.js#L13
Once JSON file is there, it should fix the issue though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not always the same, for example:
https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/classic/index.js#L13Once JSON file is there, it should fix the issue though.
I'm not sure I follow what's meant by "Once JSON file is there", but I think the fact that we can't really rely on the file paths to match the block name could serve as pretty good justification to avoid the newer "discovery"-based approach in favor of an explicit list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to #13693 and RFC for server side block registration.
It might be better to hardcode it for now.
lib/load.php
Outdated
// Derive the assumed block name by file path. | ||
$block_name = 'core/' . $parts['filename']; | ||
|
||
if ( $registry->is_registered( $block_name ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering why it isn't using remove_action
against add_action( 'init', 'register_block_core_latest_comments' );
and others? Wouldn't be that the most expected behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering why it isn't using
remove_action
againstadd_action( 'init', 'register_block_core_latest_comments' );
and others? Wouldn't be that the most expected behavior?
Maybe, but there are a few complications:
- In the current stable release of WordPress, some blocks are registered immediately, not as part of an
init
hook (example, addressed as part of the changes of this pull request) - As noted in your prior review note, trying to compose these names (i.e.
register_
) dynamically is prone to being fragile - As noted by @youknowriad 's comment, removing a filter would need to occur at an earlier priority than the default for it to work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine as is. I was mostly interested to see whether if this is technically possible without too much hassle.
68a592b
to
dfc8732
Compare
Rebased to resolve conflicts with the introduction of calendar block (@jorgefilipecosta). Restored a hard-coded blocks list (a single one at least, this time) in dfc8732. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will test it again and get back soon :)
lib/load.php
Outdated
$registry = WP_Block_Type_Registry::get_instance(); | ||
|
||
foreach ( $block_names as $file => $block_name ) { | ||
if ( ! file_exists( $blocks_dir . $file ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't mind if this would also print some error using some WordPress magic.
lib/load.php
Outdated
// Derive the assumed block name by file path. | ||
$block_name = 'core/' . $parts['filename']; | ||
|
||
if ( $registry->is_registered( $block_name ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine as is. I was mostly interested to see whether if this is technically possible without too much hassle.
I'm still seeing 404 errors when trying to use those blocks which use https://github.com/WordPress/wordpress-develop/blob/d763cdf82c2b1c1493674d348e02f18cc266672a/src/wp-includes/rest-api.php#L268-L269 |
dfc8732
to
e69baad
Compare
I have no idea if this e69baad is the right approach but it fixes the issue I raised. Let's see what Travis thinks about my hack. |
It will resolve the issue of the blocks not being defined, but per #13521 (comment) will be problematic in that the default core blocks will attempt to register themselves at the normal priority and fail (with a warning notice). Travis doesn't catch it because I haven't yet finished #13452 😄 |
I stumbled upon the same issue when working on #14238. As a quick workaround, I renamed all methods to be prefixed with |
cb194f1
to
5bad351
Compare
I rebased this PR with the latest changes in I will give it another round of testing before adding ✅. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is finally sorted out 🎉
I tested it extensively and it works properly, Travis tends to agree with me
Might need one more change, with the block PHP now being considered as part of gutenberg/bin/build-plugin-zip.sh Line 110 in c34d6b7
Related: #14289 (comment) Should be an easy fix. I'll plan to make it, and give the ZIP a try. |
I was able to reproduce it being an issue, but I had a bit more trouble fixing it than I'd anticipated. Just expanding the (Part of me also wonders whether we ought to even both whitelisting specific file extensions, especially when the |
It blocks #14533 from landing. I will have a look at the issue myself to unblock my other PR. |
38537a6 should do the trick: |
@gziolo That seems to work well in my testing 👍 Also confirmed that with some local revisions to a block, they were respected when packaging the plugin and uploading it to a test site. Generally I'm wondering why |
Thinking out loud: I was double-checking whether this would have any impact on the core tooling for bringing block implementations into WordPress. As I see it, the primary purpose for this pull request to exist is the fact that we explicitly do not want to change the process, at least as far as implementations of blocks PHP within |
This pull request seeks to try an approach for re-registering the core blocks as a Gutenberg-specific variant, while still allowing core to copy those distributed through the
@wordpress/block-library
module. The technique is rather primitive: usingCopyWebpackPlugin
, it copies PHP files from theblock-library
package, transforming them by looking for function definitions (by pattern) to prefix withgutenberg_
. Gutenberg deregisters the core-registered blocks, and registers these instead. Since it's built-in to the Webpack pipeline, it also respects rebuilds on changes duringnpm run dev
.Testing instructions:
npm run dev