Commit Graph

209 Commits

Author SHA1 Message Date
gziolo
7889a84379 Editor: Add fallback for block version when registering block script module
It aligns implementation with how regular scripts are handled.

Follow-up [57437].
See #60233.



Built from https://develop.svn.wordpress.org/trunk@57677


git-svn-id: http://core.svn.wordpress.org/trunk@57178 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-21 11:20:10 +00:00
audrasjb
46e8b6fc71 Editor: Allow symlinks in directories provided to get_block_asset_url().
This changeset allows using symbolic links for `editorScript` and `editorStyle` in `register_block_type()`. This adds `realpath` to template and stylesheet in `get_block_asset_url()` to ensure returning canonicalized absolute pathnames.

Follow-up to [56683].

Props antonlukin, spacedmonkey, maxpertici.
Fixes #59175.
See #58525.




Built from https://develop.svn.wordpress.org/trunk@57675


git-svn-id: http://core.svn.wordpress.org/trunk@57176 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-21 09:38:12 +00:00
Bernhard Reiter
52170ea9d8 Block Hooks: Allow hooked_block filters to return null.
Allow returning null from the `hooked_block` and `hooked_block_{$hooked_block_type}` filters to suppress a hooked block from being inserted. This is required to allow extenders conditionally inserting a hooked block based on e.g. the value of an attribute of the anchor block.

Props swissspidy, gziolo, joshuatf, tomjcafferkey.
Fixes 60580.
Built from https://develop.svn.wordpress.org/trunk@57668


git-svn-id: http://core.svn.wordpress.org/trunk@57169 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-20 15:22:08 +00:00
Pascal Birchler
b440a4f84f Block Hooks: Introduce a new hooked_block filter.
This is a counterpart to the dynamic `hooked_block_{$block_type}` filter introduced in [57354],
which makes it easier to modify all hooked blocks prior to insertion.

Also adds the hooked block type as an additional argument to both filters for consistency.

Props bernhard-reiter, swissspidy.
Fixes #60574.
Built from https://develop.svn.wordpress.org/trunk@57660


git-svn-id: http://core.svn.wordpress.org/trunk@57161 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-20 09:27:06 +00:00
John Blackbourn
eadb61542a Docs: Various improvements and corrections to inline documentation.
See #59651

Built from https://develop.svn.wordpress.org/trunk@57644


git-svn-id: http://core.svn.wordpress.org/trunk@57145 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-16 21:47:12 +00:00
Bernhard Reiter
d1f4f405a8 Block Hooks: Set ignoredHookedBlocks metadata upon saving.
Decouple hooked blocks insertion from setting the `metadata.ignoredHookedBlocks` attribute on anchor blocks, and perform the latter step upon saving a template or template part to the database. This ensures that anchor blocks that have been newly added to a template (or part) on the client side will also get `ignoredHookedBlocks` metadata set correctly, thus preserving editor/front-end parity. Hooked block insertion, on the other hand, will continue to happen upon ''reading'' a template (or part).

Props gziolo, tomjcafferkey.
Fixes #60506.
Built from https://develop.svn.wordpress.org/trunk@57627


git-svn-id: http://core.svn.wordpress.org/trunk@57128 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-13 15:12:09 +00:00
gziolo
48da229fa5 Blocks: Allow reading the script handle from asset files
In the [documentation for WPDefinedAsset definition](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#wpdefinedasset) for block metadata there is a note about the handle. That was missing in WordPress core.

Props gziolo, jsnajdr, youknowriad.
Fixes #60485.



Built from https://develop.svn.wordpress.org/trunk@57590


git-svn-id: http://core.svn.wordpress.org/trunk@57091 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-12 11:42:11 +00:00
gziolo
f31040bcdb Editor: Add viewScriptModule handling to block.json metadata
Syncing changes from the Gutenberg plugin: https://github.com/WordPress/gutenberg/pull/57437.

Scripts and styles can be registered for blocks via `block.json` metadata. There is now a Modules API, but was no way to register or associate module assets with blocks via `block.json`.

Fixes #60233.
Props jonsurrell, gziolo, cbravobernal, luisherranz, youknowriad.


Built from https://develop.svn.wordpress.org/trunk@57565


git-svn-id: http://core.svn.wordpress.org/trunk@57066 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-08 10:41:13 +00:00
gziolo
3791772c2d Editor: Make asset file optional for block scripts
It is no longer a hard requirement that a *.asset.php file is present to register a script for block. 

Fixes #57234.
Props joefusco, gziolo, spacedmonkey, colorful-tones. 


Built from https://develop.svn.wordpress.org/trunk@57559


git-svn-id: http://core.svn.wordpress.org/trunk@57060 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-08 08:18:10 +00:00
youknowriad
f82169f0bb Editor: Update PHPDoc for block bindings's context arg.
The context argument passed to the hooked_block_types filter can also be a Post object
in the case of the navigation block.
This adapts the PHPDoc accordingly.

Props bernhard-reiter, gziolo.
See #59743.
Built from https://develop.svn.wordpress.org/trunk@57550


git-svn-id: http://core.svn.wordpress.org/trunk@57051 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-07 10:56:08 +00:00
gziolo
86121f4f25 Editor: Add allowed_blocks field to block registration and REST API
There is a new block.json field called allowedBlocks, added in Gutenberg in https://github.com/WordPress/gutenberg/pull/58262. This adds support for this new field also on the server. 

Props: gziolo, jsnajdr.
Fixes #60403.



Built from https://develop.svn.wordpress.org/trunk@57521


git-svn-id: http://core.svn.wordpress.org/trunk@57022 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-02 13:01:21 +00:00
gziolo
9eea02e31b Editor: Add viewStyle property to block.json for frontend-only block styles
Related issue in Gutenberg: https://github.com/WordPress/gutenberg/issues/54491.

For block scripts there was already `script`, `viewScript` and `editorScript`. For block styles there was only `style` and `editorStyle`. This brings the parity.

Props gaambo.
Fixes #59673. 


Built from https://develop.svn.wordpress.org/trunk@57493


git-svn-id: http://core.svn.wordpress.org/trunk@56994 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-31 09:11:17 +00:00
Sergey Biryukov
0ef6e192cb Coding Standards: Use strict type check for in_array() in get_hooked_block_markup().
This aims to prevent type juggling causing incorrect results.

Follow-up to [57157].

Props jrf.
See #60279.
Built from https://develop.svn.wordpress.org/trunk@57372


git-svn-id: http://core.svn.wordpress.org/trunk@56878 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-28 21:34:17 +00:00
Bernhard Reiter
b22f506554 Block Hooks: Amend PHPDoc for hooked_block_{$hooked_block_type} filter.
Add missing explanation of the dynamic part of the hook name.

Follow-up [57354].
Props swissspidy.
See #59572, #60126.
Built from https://develop.svn.wordpress.org/trunk@57355


git-svn-id: http://core.svn.wordpress.org/trunk@56861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-25 14:20:17 +00:00
Bernhard Reiter
b4938efb13 Block Hooks: Introduce a new hooked_block_{$block_type} filter.
Add a new `hooked_block_{$block_type}` filter that allows modifying a hooked block (in parsed block format) prior to insertion, while providing read access to its anchor block (in the same format).

This allows block authors to e.g. set a hooked block's attributes, or its inner blocks; the filter can peruse information about the anchor block when doing so. As such, this filter provides a solution to both #59572 and #60126.

The new filter is designed to strike a good balance and separation of concerns with regard to the existing [https://developer.wordpress.org/reference/hooks/hooked_block_types/ `hooked_block_types` filter], which allows addition or removal of a block to the list of hooked blocks for a given anchor block -- all of which are identified only by their block ''types''. This new filter, on the other hand, only applies to ''one'' hooked block at a time, and allows modifying the entire (parsed) hooked block; it also gives (read) access to the parsed anchor block.

Props gziolo, tomjcafferkey, andrewserong, isabel_brison, timbroddin, yansern.
Fixes #59572, #60126.
Built from https://develop.svn.wordpress.org/trunk@57354


git-svn-id: http://core.svn.wordpress.org/trunk@56860 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-25 13:48:18 +00:00
Bernhard Reiter
5e795b65ef Block Hooks: Store ignored hooked blocks metadata in anchor block.
The biggest tradeoff that was made in the implementation of Block Hooks was that they were limited to layouts (i.e. templates, template parts, and patterns) that ''didn't have any user modifications'' (see #59313 for the reason). This changeset is a preparatory step to remove this limitation, so they’ll eventually also work with user-modified layouts.

The crucial problem to solve is how to acknowledge that a user has opted to remove or persist a hooked block, so that the auto-insertion mechanism won't run again and inject an extraneous hooked block on the frontend when none is solicited.

This is achieved by storing all known blocks hooked to a given anchor block in the `metadata` attribute on that anchor block; specifically in a field called `ignoredHookedBlocks` inside of the `metadata`. Hooked blocks are only rendered on the frontend if they're absent from that field; OTOH, they're injected into that field (via the REST API) when first loaded in the editor.

This simple logic guarantees that once a user modifies a given layout, those changes are respected on the frontend; yet if a plugin that includes a hooked block is activated after those modifications have taken place, the hooked block will be rendered on the frontend. This new technique supplants the one previously used (i.e. rendering hooked blocks on the frontend only if a layout doesn't have any modifications) in a rather direct way.

Note that this changeset only introduces the new metadata field and relevant logic; it does not yet enable hooked block insertion into modified layouts. That will be done in a subsequent step (see #59646).

Props gziolo.
Closes #60008.
Built from https://develop.svn.wordpress.org/trunk@57157


git-svn-id: http://core.svn.wordpress.org/trunk@56668 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-12-04 20:26:22 +00:00
Sergey Biryukov
d7a1816bf1 Docs: Improve some DocBlocks and inline comments per the documentation standards.
Follow-up to [56834], [56836], [56837], [56838].

Props kebbet, costdev, mukesh27, SergeyBiryukov.
See #59651.
Built from https://develop.svn.wordpress.org/trunk@57120


git-svn-id: http://core.svn.wordpress.org/trunk@56631 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-17 13:29:26 +00:00
hellofromTonya
faa928f218 Blocks: Document Block Hooks functions as private.
Documents the 4 new 6.4 Block Hooks global functions as private and for Core-only internal usage:

* `make_before_block_visitor()`
* `make_after_block_visitor()`
* `traverse_and_serialize_block()`
* `traverse_and_serialize_blocks()`

This is being done as the architectural design of these new functions may change in the next cycle. Further denoting them as private / Core only can help to avoid extender churn if any of these functions are deprecated.

Follow up to [56649], [56620].

Props azaozz, hellofromTonya, bernhard-reiter, gziolo, mikeschroder.
Fixes #59783.
See #59313.
Built from https://develop.svn.wordpress.org/trunk@57066


git-svn-id: http://core.svn.wordpress.org/trunk@56577 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-06 13:37:24 +00:00
Sergey Biryukov
747b2c9f11 Docs: Replace incorrect @private tags.
The `@private` tag does not exist and is not supported. To indicate that something is private when in the global namespace, `@access private` should be used instead.

Reference: [https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#phpdoc-tags PHP Documentation Standards: PHPDoc tags].

Props jrf.
See #59651.
Built from https://develop.svn.wordpress.org/trunk@57065


git-svn-id: http://core.svn.wordpress.org/trunk@56576 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-06 09:27:23 +00:00
Sergey Biryukov
b330629c53 Coding Standards: Correct equals sign alignment in various files.
This resolves a few WPCS warnings:
{{{
Equals sign not aligned with surrounding statements
}}}
so that the output of `composer format` is clean.

Follow-up to [56796], [56803], [56838], [56839], [56985].

See #59650.
Built from https://develop.svn.wordpress.org/trunk@57053


git-svn-id: http://core.svn.wordpress.org/trunk@56564 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-02 00:04:24 +00:00
Bernhard Reiter
798991a0ff Block Hooks: Allow traversal callbacks to modify parent block.
The callbacks returned by `make_before_block_visitor` and `make_after_block_visitor`, respectively, (which are passed as arguments to `traverse_and_serialize_block(s)`) currently accept three arguments, all of which are block arrays (i.e. with properties `blockName`, `attrs`, etc.):
- A ''reference'' to the block they're currently visiting, `&$block`;
- the block's `$parent_block`; and
- the `$prev`ious block (for `make_before_block_visitor`), or the `$next` block (for `make_after_block_visitor`), respectively.

Those arguments are passed to the "block visitor" callbacks by `traverse_and_serialize_block(s)` during traversal. The block that the callback is currently visiting is passed ''by reference'' to allow modifying it, which is e.g. used to inject the `theme` attribute into Template Part blocks.

One major limitation of Block Hooks is that they currently only work with templates, parts, and patterns that ''don't have any user modifications'' (i.e. that come straight from the corresponding theme files, rather than from the database). For WordPress 6.5, it is planned to change that to make Block Hooks work for templates, parts, and patterns that ''do'' have user modifications: #59646.

This will be implemented by storing an attribute on the "anchor" block. While working on that feature, it was found that the aforementioned callbacks will need to modify not only the currently visited `$block`, but also the `$parent_block` -- i.e. that the latter argument needs to be passed by reference as well. This is consistent with the requirement of adding an attribute to an anchor block, as it's not only the currently visited block that can serve as an anchor block (in the case of `before` or `after` sibling insertion), but also its parent (for `first_child` and `last_child` insertion).

If the `$parent_block` argument were to be changed to become a reference in a later WordPress version, this could be considered a backwards-compatibility breaking change. For this reason, this change is instead proposed for 6.4 already, which is the cycle during which the relevant functions were first introduced. This should have no impact on existing code, since nothing currently relies on `$parent_block` remaining unmodified by the respective callback, nor is anything currently modifying that argument.

Props hellofromTonya.
Fixes #59776.
Built from https://develop.svn.wordpress.org/trunk@57038


git-svn-id: http://core.svn.wordpress.org/trunk@56549 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-31 19:25:19 +00:00
Sergey Biryukov
ac2b1a56d2 Docs: Update some reusable block references to synced patterns.
In WordPress 6.3, [https://wordpress.org/documentation/article/reusable-blocks/ Reusable Blocks were renamed to Patterns]. A synced pattern will behave in exactly the same way as a reusable block.

This commit updates some references in DocBlocks and inline comments to use the new name.

Follow-up to [56030].

Props benjaminknox, oglekler, hellofromTonya, marybaum, nicolefurlan.
Fixes #59388.
Built from https://develop.svn.wordpress.org/trunk@57032


git-svn-id: http://core.svn.wordpress.org/trunk@56543 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-31 12:58:23 +00:00
Sergey Biryukov
ac10fc6896 Blocks: Parse the arguments earlier in register_block_type_from_metadata().
This makes it possible to register a block by passing an array of arguments, without the presence of a `block.json` file.

Follow-up to [48141], [49948].

Props aristath, spacedmonkey, mukesh27, costdev, audrasjb, oglekler, felipeelia, hellofromTonya.
Fixes #56865.
Built from https://develop.svn.wordpress.org/trunk@57026


git-svn-id: http://core.svn.wordpress.org/trunk@56537 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-28 01:02:26 +00:00
Bernhard Reiter
b0da2934b9 Blocks: During traversal, allow post callback to modify block.
Both the `$pre_callback` and `$post_callback` functions that are given as arguments to `traverse_and_serialize_block(s)` receive a reference to the current block as their first argument. However, while any changes that the "pre" callback makes to the block are reflected by the serialized markup, the same wasn't true for the "post" callback: Any changes that it made were only applied ''after'' the block had already been serialized.

This commit changes the behavior such that `$post_callback`'s changes to the current block are also reflected in the serialized markup.

See #59646.
Props gziolo.
Fixes #59669.
Built from https://develop.svn.wordpress.org/trunk@56970


git-svn-id: http://core.svn.wordpress.org/trunk@56481 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-18 19:32:23 +00:00
Bernhard Reiter
c054eef096 Patterns, Templates: Inject theme attr into Template Part blocks.
It was found that Template Part blocks were broken in the Site Editor, showing the `Template part has been deleted or is unavailable` message, due to a missing `theme` attribute.

This bug seems to have been introduced by [56896], whose goal was to only inject that attribute into the markup returned by the templates and patterns REST API endpoints but not on the frontend, in order to improve performance. It has been demonstrated locally that reverting that changeset fixes the bug.

Reverts [56896].
Props mmcalister, swisspidy, thelovelist, hellofromTonya, pbiron, Pauthake015, richtabor, nicolefurlan, huzaifaalmesbah, annezazu, kafleg, aegkr, sunitarai, shresthaaman, andraganescu, onemaggie, gziolo.
Fixes #59629.
Built from https://develop.svn.wordpress.org/trunk@56960


git-svn-id: http://core.svn.wordpress.org/trunk@56471 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-17 15:48:23 +00:00
Bernhard Reiter
8a7d30c7f8 Patterns: Don't inject theme attribute on frontend.
Having the patterns registry inject the `theme` attribute into all Template Part blocks inside every pattern was found to negatively impact performance. It turns out that it's only required for the editor (i.e. in the REST API) but not on the frontend; there, it's instead possible to fall back to the currently active theme.

The latter change was made to the Pattern and Template Part blocks in https://github.com/WordPress/gutenberg/pull/55217, which was sync'ed to Core in [56849]. Consequently, this changeset removes `theme` attribute insertion from the frontend.

Props flixos90, gziolo, dmsnell, hellofromtonya.
Fixes #59583.
Built from https://develop.svn.wordpress.org/trunk@56896


git-svn-id: http://core.svn.wordpress.org/trunk@56407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-12 18:44:45 +00:00
desrosj
5a306cc56d Editor: Move footnotes block hooks to default-filters.php
Follow up to [56839].
Built from https://develop.svn.wordpress.org/trunk@56845


git-svn-id: http://core.svn.wordpress.org/trunk@56357 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-12 13:28:23 +00:00
Aaron Jorbin
9f7edd0413 Editor: Harden the display of footnotes.
Props: jorgefilipecosta, peterwilsoncc, costdev, xknown, jorbin.

Built from https://develop.svn.wordpress.org/trunk@56839


git-svn-id: http://core.svn.wordpress.org/trunk@56351 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-12 12:58:23 +00:00
Bernhard Reiter
fc229aa436 Blocks: Call get_hooked_blocks only once per template/part/pattern.
Prior to this changeset, `get_hooked_blocks` was called four times ''for every parsed block'' in each template, template part, and pattern. With this changeset applied, `get_hooked_blocks` is called only once per template, template part, or pattern.

Additionally, `get_hooked_blocks` is called only once when returning the list of all registered patterns. (The latter modification brings the implementation closer to its state prior to Block Hooks.)

Finally, when there are no registered hooked blocks or `hooked_block_types` filters, parsing, hooked block insertion, and re-serializing is skipped altogether.

Props gziolo, flixos90, joemcgill, dmsnell, spacedmonkey, hellofromtonya.
Fixes #59383.
Built from https://develop.svn.wordpress.org/trunk@56805


git-svn-id: http://core.svn.wordpress.org/trunk@56317 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-09 16:40:24 +00:00
Sergey Biryukov
ffd72aac28 Coding Standards: Remove redundant ignore annotations, take 5.
The `VariableAnalysis` standard is not used by WP Core.

Follow-up to [50958], [51003], [52049], [52051], [52069], [53072], [54132], [55132], [56363], [56738], [56743], [56751], [56752].

Props jrf.
See #59161.
Built from https://develop.svn.wordpress.org/trunk@56753


git-svn-id: http://core.svn.wordpress.org/trunk@56265 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-02 11:27:24 +00:00
spacedmonkey
d922a9e96a Editor: Avoid Deferring Loading for Empty Block Script URI
In the context of register_block_script_handle, the get_block_asset_url function may return false when an empty string is provided as the input. This behavior is intended to prevent the generation of invalid URLs. However, when the script loading strategy is set to "defer" while passing false, it triggers a "doing it wrong" message.

This situation becomes problematic, especially for scenarios where the scripts haven't been built yet. In such cases, the realpath call returns an empty string because the file doesn't exist. To address this issue, we now perform a simple check to ensure that the script URI is not empty before applying the "defer" loading strategy. This adjustment prevents unnecessary deferral of loading for scripts with empty URIs.

Follow on from [56683] and [56033].

Props kebbet, mukesh27, swissspidy, westonruter, spacedmonkey.
Fixes #59475
Built from https://develop.svn.wordpress.org/trunk@56744


git-svn-id: http://core.svn.wordpress.org/trunk@56256 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-29 13:15:25 +00:00
Sergey Biryukov
9bf6faff7c Editor: Reduce the use of the _wp_array_get() function to improve performance.
`_wp_array_get()` is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.

This commit aims to further optimize its usage:
* In many cases, `_wp_array_get()` can be replaced with a much simpler and faster `isset()` check.
* The `isset()` function is capable of checking nested arrays, so `isset( $foo['a']['b']['c'] )` will return false even if `$foo['a']` is unset, without throwing any errors or warnings.
* When `_wp_array_get()` cannot be directly replaced with `isset()`, it would be good practice to wrap it in an `isset()` function so that `_wp_array_get()` only runs when it needs to.

Original PR from Gutenberg repository:
* [https://github.com/WordPress/gutenberg/pull/51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function]

Follow-up to [55851], [56382].

Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.
Built from https://develop.svn.wordpress.org/trunk@56709


git-svn-id: http://core.svn.wordpress.org/trunk@56221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 13:47:20 +00:00
Bernhard Reiter
eeefd48208 Blocks: Have get_hooked_blocks() return blocks grouped by position.
All existing calls of `get_hooked_blocks()` in non-test code are currently wrapped in an extra `array_keys()` call. This changeset absorbs that logic into the function and changes the structure of the return value accordingly.

Furthermore, this allows us to remove the extra `$relative_position` argument (introduced in [56673]) from the function again, as the same data can now be simply fetched via array access.

Props gziolo, spacedmonkey, mukesh27.
See #59383.
Built from https://develop.svn.wordpress.org/trunk@56704


git-svn-id: http://core.svn.wordpress.org/trunk@56216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 11:49:21 +00:00
spacedmonkey
66c782f6cd Editor: Introduce get_block_asset_url Utility Function.
This commit introduces a valuable utility function, get_block_asset_url, designed to simplify the retrieval of block asset URLs, such as those for CSS and JavaScript files. This utility eliminates redundancy in both register_block_script_handle and register_block_style_handle. Additionally, `get_block_asset_url` incorporates an early exit mechanism to optimize performance.

This update includes comprehensive unit tests, covering various scenarios, including asset registration from core (wp-includes), themes, child themes, plugins, and mu-plugins.

Props spacedmonkey, joemcgill, flixos90, gziolo.
Fixes #58525.
Built from https://develop.svn.wordpress.org/trunk@56683


git-svn-id: http://core.svn.wordpress.org/trunk@56195 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 17:49:19 +00:00
Sergey Biryukov
28af9eafb0 Coding Standards: Fix a few newly introduced WPCS issues.
Follow-up to [56570], [56573], [56589], [56604], [56612], [56620], [56629], [56631], [56638], [56642], [56644], [56649].

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56680


git-svn-id: http://core.svn.wordpress.org/trunk@56192 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 15:36:18 +00:00
spacedmonkey
ffad402ba3 Themes: Use instanceof in block_has_support.
In [50761], the block_has_support function was introduced. However, using `property_exists` within this function negatively impacted its performance. This commit replaces the `property_exists` function call with `instanceof WP_Block_Type`, resulting in improved performance.

Props mukesh27, gziolo, spacedmonkey.
Fixes #59441.
Built from https://develop.svn.wordpress.org/trunk@56678


git-svn-id: http://core.svn.wordpress.org/trunk@56190 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 12:17:23 +00:00
spacedmonkey
73c8507a84 Themes: Use instanceof in get_hooked_blocks.
In [56610], the get_hooked_blocks function was introduced. However, using property_exists within this function negatively impacted its performance. This commit replaces the property_exists function call with instanceof WP_Block_Type, resulting in improved performance.

Props gziolo, spacedmonkey.
See #59383.
Built from https://develop.svn.wordpress.org/trunk@56677


git-svn-id: http://core.svn.wordpress.org/trunk@56189 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 11:48:17 +00:00
Bernhard Reiter
9fd898a7c7 Blocks: Remove inject_hooked_block_markup filter.
Per discussion in #59424, there's agreement that the new `hooked_block_types` filter  (introduced in [56673]) covers conditional addition and removal of hooked blocks better and at a higher level than the `inject_hooked_block_markup` filter that was originally added in [56649] for that same purpose.
Consequently, this changeset removes the latter filter.

Props gziolo.
Fixes #59439.
Built from https://develop.svn.wordpress.org/trunk@56674


git-svn-id: http://core.svn.wordpress.org/trunk@56186 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 09:14:17 +00:00
Bernhard Reiter
ffb8b43072 Blocks: Introduce filter to allow easy addition of hooked blocks.
Introduce a `hooked_block_types` filter that allows easier conditional addition (or removal) of hooked blocks for a given anchor block and relative position.

{{{#!php
function insert_shopping_cart_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) {
	if ( 'after' === $position && 'core/navigation' === $anchor_block && /** $context is header template part **/ ) {
		$hooked_blocks[] = 'mycommerce/shopping-cart';
	}
	return $hooked_blocks;
}
add_filter( 'hooked_block_types', 'insert_shopping_cart_hooked_block', 10, 4 );
}}}

Props gziolo, nerrad, dmsnell, ndiego.
Fixes #59424.
Built from https://develop.svn.wordpress.org/trunk@56673


git-svn-id: http://core.svn.wordpress.org/trunk@56185 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 08:44:22 +00:00
Bernhard Reiter
6bf6244dc7 Blocks: Implement automatic block insertion into Block Hooks.
Block Hooks allow a third-party block to specify a position relative to a given block into which it will then be automatically inserted (e.g. a "Like" button block can ask to be inserted after the Post Content block, or an eCommerce shopping cart block can ask to be inserted after the Navigation block).

The underlying idea is to provide an extensibility mechanism for Block Themes, in analogy to WordPress' [https://developer.wordpress.org/plugins/hooks/ Hooks] concept that has allowed extending Classic Themes through filters and actions.

The two core tenets for Block Hooks are:

1. Insertion into the frontend should happen right after a plugin containing a hooked block is activated (i.e. the user isn't required to insert the block manually in the editor first); similarly, disabling the plugin should remove the hooked block from the frontend.
2. The user has the ultimate power to customize that automatic insertion: The hooked block is also visible in the editor, and the user's decision to persist, dismiss (i.e. remove), customize, or move it will be respected (and reflected on the frontend).

To account for both tenets, the **tradeoff** was made to limit automatic block insertion to unmodified templates (and template parts, respectively). The reason for this is that the simplest way of storing the information whether a block has been persisted to (or dismissed from) a given template (or part) is right in the template markup.

To accommodate for that tradeoff, [https://github.com/WordPress/gutenberg/pull/52969 UI controls (toggles)] are being added to increase visibility of hooked blocks, and to allow for their later insertion into templates (or parts) that already have been modified by the user.

For hooked blocks to appear both in the frontend and in the editor (see tenet number 2), they need to be inserted into both the frontend markup and the REST API (templates and patterns endpoints) equally. As a consequence, this means that automatic insertion couldn't (only) be implemented at block ''render'' stage, as for the editor, the ''serialized'' (but ''unrendered'') markup needs to be modified.

Furthermore, hooked blocks also have to be inserted into block patterns. Since practically no filters exist for the patterns registry, this has to be done in the registry's `get_registered` and `get_all_registered` methods.

Props gziolo.
Fixes #59313.
Built from https://develop.svn.wordpress.org/trunk@56649


git-svn-id: http://core.svn.wordpress.org/trunk@56161 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-21 16:18:17 +00:00
Bernhard Reiter
f6307ff5fc Blocks: Change traverse_and_serialize_block(s)'s callback signature.
During work on #59399, it was discovered that ''sibling'' block insertion wasn't likely going to work the way it was planned, which required devising an alternative solution. This new solution requires some changes to `traverse_and_serialize_block(s)`:

- Change the signature of the existing callback such that:
  - the return value is a string that will be prepended to the result of the inner block traversal and serialization;
  - the function arguments are: a ''reference'' to the current block (so it can be modified inline, which is important e.g. for `theme` attribute insertion), the parent block, and the previous block (instead of the block index and chunk index).
- Add a second callback argument to `traverse_and_serialize_block(s)`, which is called ''after'' the block is traversed and serialized.
  - Its function arguments are a reference to the current block, the parent block, and the next block.

Props gziolo.
Fixes #59412. See #59313.
Built from https://develop.svn.wordpress.org/trunk@56644


git-svn-id: http://core.svn.wordpress.org/trunk@56156 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-21 08:34:18 +00:00
Bernhard Reiter
68be796e1d Blocks: Revert implementation of block insertion functions.
In [56618], three functions (`insert_inner_block`, `prepend_inner_block`, and `append_inner_block`) were introduced. They were meant to be used for insertion of hooked blocks; however, it was discovered that the original idea wouldn't work for sibling insertion. Instead, a different approach will be taken (see #59412), and these functions are no longer needed and can thus be removed.

Reverts [56618].
See #59412, #59385, #59313.
Built from https://develop.svn.wordpress.org/trunk@56634


git-svn-id: http://core.svn.wordpress.org/trunk@56146 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-20 16:49:18 +00:00
gziolo
d3a8869891 Blocks: Introduce a variation of serialize blocks helper with traversing
Introduces two new functions `traverse_and_serialize_blocks` and `traverse_and_serialize_block` with the additional `$callback` argument. It is possible to pass parent block, block index, chunk index to the callback argument.

Reverts changes applied to `serialize_blocks` and `serialize_block` in #59327 with [56557].

Props ockham, mukesh27.
See #59313.



Built from https://develop.svn.wordpress.org/trunk@56620


git-svn-id: http://core.svn.wordpress.org/trunk@56132 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-19 12:50:19 +00:00
Bernhard Reiter
43b728aec5 Blocks: Implement block insertion functions.
For #59313, we need to implement functions to insert a given parsed block into another parsed block's inner blocks, and to prepend and append to that array, respectively.

We will use those functions in combination with `traverse_and_serialize_blocks` (see #59327) to implement automatic insertion of hooked blocks into block templates and patterns.

Props gziolo.
Fixes #59385.
Built from https://develop.svn.wordpress.org/trunk@56618


git-svn-id: http://core.svn.wordpress.org/trunk@56130 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-19 12:36:20 +00:00
gziolo
7520c3fb46 Blocks: Introduce helper function to retrieve hooked blocks
In order to implement Block Hooks (see #59313), we added block_hooks field to the WP_Block_Type class, as well as to block registration related functions. In this follow-up, new helper function gets introduced that is going to compute the list of hooked blocks by other registered blocks for a given block type.

Extracted from https://github.com/WordPress/wordpress-develop/pull/5158 and covered with unit tests.

Props ockham.
Fixes #59383.


Built from https://develop.svn.wordpress.org/trunk@56610


git-svn-id: http://core.svn.wordpress.org/trunk@56122 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-18 12:43:13 +00:00
Sergey Biryukov
3546f04e16 Coding Standards: Fix a few newly introduced WPCS issues.
Follow-up to [56515], [56557], [56560].

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56598


git-svn-id: http://core.svn.wordpress.org/trunk@56110 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-17 02:10:17 +00:00
gziolo
ad261fbf4e Blocks: Remove the gutenberg textdomain accidently added to translation
Props soean.
See #59346.
Follow-up [56587].


Built from https://develop.svn.wordpress.org/trunk@56592


git-svn-id: http://core.svn.wordpress.org/trunk@56104 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-15 07:51:19 +00:00
Bernhard Reiter
0b79362805 General: Add block_hooks field to block type registration, REST API.
In order to implement Block Hooks, we need to add a new `block_hooks` field to the `WP_Block_Type` class, as well as to block registration related functions, the block types REST API controller, etc.

Props gziolo.
Fixes #59346. See #59313.
Built from https://develop.svn.wordpress.org/trunk@56587


git-svn-id: http://core.svn.wordpress.org/trunk@56099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-14 13:25:18 +00:00
Felix Arntz
4bfba35169 Posts, Post Types: Avoid unnecessarily parsing blocks twice in wp_trim_excerpt().
All blocks relevant for the excerpt are already being parsed in `excerpt_remove_blocks()`. Therefore running `do_blocks()` on the post content only to create the excerpt is unnecessary and wasteful from a performance perspective.

Props thekt12, spacedmonkey, mukesh27, joemcgill.
Fixes #58682.

Built from https://develop.svn.wordpress.org/trunk@56560


git-svn-id: http://core.svn.wordpress.org/trunk@56072 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-12 19:20:21 +00:00
Sergey Biryukov
e5490118af Coding Standards: Include one space after function keyword for closures.
Note: This is enforced by WPCS 3.0.0.

Reference: [https://github.com/WordPress/WordPress-Coding-Standards/pull/2328 WPCS: PR #2328 Core: properly check formatting of function declaration statements].

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56559


git-svn-id: http://core.svn.wordpress.org/trunk@56071 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-12 15:23:18 +00:00