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
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
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
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
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
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
Allow passing a function callback to serialize_block(s) that is invoked on each node in the tree of parsed blocks as it is traversed for serialization.
A function argument was chosen for passing the callback function as it reflects a pattern familiar from other algorithms that apply a given callback function while traversing a data structure -- most notably PHP's own `array_map()`.
Introducing a filter was considered as an alternative but ultimately dismissed. For a fairly low-level and general-purpose function such as `serialize_block()`, using a filter to pass the callback seemed risky, as it also requires ''removing'' that filter after usage in order to prevent the callback from being accidentally applied when `serialize_block()` is called in an entirely different context.
Introducing a separate function for applying a given operation during tree traversal (i.e. independently of serialization) was also considered but dismissed, as it would unnecessarily duplicate tree traversal.
Props dlh, gziolo.
Fixes#59327. See #59313.
Built from https://develop.svn.wordpress.org/trunk@56557
git-svn-id: http://core.svn.wordpress.org/trunk@56069 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Previously, in `register_block_script_handle()` the `$script_uri` variable initialized with a `plugins_url()` call that was reported to invoke four times. In this patch the var is initialized as a blank string with a fallback to use the `plugins_url()` if the other intended conditions are not met.
Props mukesh27, daxelrod, adamsilverstein, davidbaumwald.
Fixes#59181.
Built from https://develop.svn.wordpress.org/trunk@56455
git-svn-id: http://core.svn.wordpress.org/trunk@55967 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Most block feature checks are for a single feature string, and for such cases it is not intuitive to require an array for the `$feature` parameter of the `block_has_support()` function.
This changeset brings it in line with other functions like `post_type_supports()`, allowing to pass a string for the `$feature`. An array is still supported for more complex cases where support for sub-features needs to be determined. This change furthermore includes a very minor performance tweak by avoiding calls to the `_wp_array_get()` function if a single feature string is being checked for.
Props thekt12, nihar007, mukesh27, swissspidy.
Fixes#58532.
Built from https://develop.svn.wordpress.org/trunk@56382
git-svn-id: http://core.svn.wordpress.org/trunk@55894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset fixes both `register_block_script_handle()` and `register_block_style_handle()` functions, allowing for child themes to access and utilise
blocks defined in parent themes.
Previously these functions could not handle blocks registered in Parent themes. This changeset fixes the issue by determining whether the given asset path
belongs to either of the template or stylesheet directories (Parent and Child themes respectively) as opposed to just checking using
`get_theme_file_path('')`.
Props jacknotman, Levdbas, audrasjb, guillaumeturpin, leprincenoir, whaze, isabel_brison.
Fixes#57566.
--Cette ligne, et les suivantes ci-dessous, seront ignorées--
M trunk/src/wp-includes/blocks.php
Built from https://develop.svn.wordpress.org/trunk@56183
git-svn-id: http://core.svn.wordpress.org/trunk@55695 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset adds handler for multiple cases where author data can be passed to the Query Block:
- array: array of author ids
- string: comma separated author ids
- integer: single author id
Props adi3890, costdev, mayur8991, oglekler.
Fixes#58426.
Built from https://develop.svn.wordpress.org/trunk@56109
git-svn-id: http://core.svn.wordpress.org/trunk@55621 1a063a9b-81f0-0310-95a4-ce76da25c4cd
In [52069] the function `wp_common_block_scripts_and_styles` was changed load individual theme stylesheets, if the current theme supports block styles and loading separate core block assets. To do this, the function calls many expensive file operation functions, such as `glob`, `file_exists` and `file_get_contents`. This is wasteful, as these functions are loaded on every page request, even request that do not include blocks, like REST API calls. In [56044] all core block styles are registered in a single place. In `register_core_block_style_handles` calls `glob` to get all css styles in block directories. While registering style and editor styles, also register block theme styles, under a new style handle. Example `wp-block-avatar-theme`. If the current theme supports block styles, also request the block to enqueue the theme style on the front end. As these new stylesheets have a path attribute set, the function `wp_maybe_inline_styles` will automatically inline the styles for you.
Props spacedmonkey, flixos90, oandregal, costdev, audrasjb, mukesh27.
Fixes#58560.
Built from https://develop.svn.wordpress.org/trunk@56064
git-svn-id: http://core.svn.wordpress.org/trunk@55576 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Register all core blocks in a new function called `register_core_block_style_handles`. This mirrors the function `wp_default_styles` where all core styles are registered in one place. This improves block registration performance, as it avoids expensive file lookups, like realpath in `register_block_style_handle`. The new function `register_core_block_style_handles` uses `glob` to get all css files in the blocks directory. This glob is cached in a transient to save lookups on subsequent requests. The function `register_block_style_handle` now checks to see if the style handle is already registered before trying to register it again.
Props mukesh27, westonruter, flixos90, joemcgill, spacedmonkey.
Fixes#58528.
Built from https://develop.svn.wordpress.org/trunk@56044
git-svn-id: http://core.svn.wordpress.org/trunk@55556 1a063a9b-81f0-0310-95a4-ce76da25c4cd
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [55988].
Props spacedmonkey.
See #58220.
Built from https://develop.svn.wordpress.org/trunk@56021
git-svn-id: http://core.svn.wordpress.org/trunk@55533 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset fixes a bug where WordPress core's own block editor styles (`wp-block-{$block_name}-editor`) were referencing the same CSS files as their frontend equivalents (`wp-block-{$block_name}`). This would result in incorrect frontend styles potentially being loaded in the block editor.
Tests for the related logic have been added.
Props flixos90, joemcgill, mukesh27, spacedmonkey.
Fixes#58605.
Built from https://develop.svn.wordpress.org/trunk@56005
git-svn-id: http://core.svn.wordpress.org/trunk@55517 1a063a9b-81f0-0310-95a4-ce76da25c4cd
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes#58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
In `register_block_type_from_metadata` function, skip calling `file_exists` on core blocks. Core blocks are part of the codebase and will never not exist. Not calling this function is better for performance, as the file lookup can be expensive.
Props spacedmonkey, joemcgill.
Fixes#58385.
Built from https://develop.svn.wordpress.org/trunk@55910
git-svn-id: http://core.svn.wordpress.org/trunk@55422 1a063a9b-81f0-0310-95a4-ce76da25c4cd
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes#58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Adds support for the new selectors property for block types. It adds it to the allowed metadata when registering a block type, makes the WP_Block_Type class aware of it, exposes it through the block types REST API, and the get_block_editor_server_block_settings function.
Corresponding work in the Gutenberg plugin: https://github.com/WordPress/gutenberg/pull/46496.
Fixes#57585.
Props aaronrobertshaw, hellofromTonya.
Built from https://develop.svn.wordpress.org/trunk@55673
git-svn-id: http://core.svn.wordpress.org/trunk@55185 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Per [https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#writing-include-require-statements WordPress PHP coding standards], it is ''strongly recommended'' to use `require[_once]` for unconditional includes. When using `include[_once]`, PHP will throw a warning when the file is not found but will continue execution, which will almost certainly lead to other errors/warnings/notices being thrown if your application depends on the file loaded, potentially leading to security leaks. For that reason, `require[_once]` is generally the better choice as it will throw a `Fatal Error` if the file cannot be found.
Follow-up to [1674], [1812], [1964], [6779], [8540], [10521], [11005], [11911], [16065], [16149], [25421], [25466], [25823], [37714], [42981], [45448], [47198], [54276], [55633].
Props kausaralm, SergeyBiryukov.
See #57839.
Built from https://develop.svn.wordpress.org/trunk@55641
git-svn-id: http://core.svn.wordpress.org/trunk@55153 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This resolves two WPCS errors:
{{{
String ".css" does not require double quotes; use single quotes instead
String "-rtl.css" does not require double quotes; use single quotes instead
}}}
Follow-up to [55544].
See #57903.
Built from https://develop.svn.wordpress.org/trunk@55547
git-svn-id: http://core.svn.wordpress.org/trunk@55059 1a063a9b-81f0-0310-95a4-ce76da25c4cd
If `SCRIPT_DEBUG` is disabled, `register_block_style_handle()` loads core blocks' styles with the `.min` suffix, while non-core ones never use the minified files, but the suffix was still mistakenly included in the `-rtl` file lookup.
This commit updates the logic to match the style path set earlier in the function, ensuring that RTL stylesheets are loaded properly for both core and non-core blocks, with or without `SCRIPT_DEBUG`.
Follow-up to [49982], [50836], [54330], [55486].
Props david.binda.
Fixes#57903.
Built from https://develop.svn.wordpress.org/trunk@55544
git-svn-id: http://core.svn.wordpress.org/trunk@55056 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset ensures the file path is correctly passed in the output from `_doing_it_wrong()` notice in `register_block_script_handle()`.
Props desrosj, neychok, mahbubshovan, ironprogrammer, robinwpdeveloper, hellofromTonya, simongomes02, mukesh27, costdev.
Fixes#53566.
Built from https://develop.svn.wordpress.org/trunk@55446
git-svn-id: http://core.svn.wordpress.org/trunk@54979 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This applies to:
* `$block_name` in `strip_core_block_namespace()`
* `$allowed_html` in `filter_block_content()`
* `$pagination_type` in `get_comments_pagination_arrow()`
Follow-up to [46896], [48794], [53138], [54181].
Props rakibwordpress, ironprogrammer, sabernhardt, SergeyBiryukov.
Fixes#56596.
Built from https://develop.svn.wordpress.org/trunk@54520
git-svn-id: http://core.svn.wordpress.org/trunk@54075 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This affects:
* `register_block_script_handle()`
* `register_block_style_handle()`
Both functions set a variable with this code:
{{{
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
}}}
That value never changes during page load, so we can save it to a static variable. By doing so, we can avoid ~200 calls to `realpath()` and `wp_normalize_path()`, or even more if third-party plugins register scripts or styles.
Follow-up to [52291], [52939], [54290], [54291], [54309], [54327].
Props aristath, mukesh27, SergeyBiryukov.
Fixes#56758.
Built from https://develop.svn.wordpress.org/trunk@54415
git-svn-id: http://core.svn.wordpress.org/trunk@53974 1a063a9b-81f0-0310-95a4-ce76da25c4cd
When setting an RTL language under Settings → General, some RTL stylesheets were not loaded, with LTR stylesheets being loaded instead, meaning that some blocks were not displayed correctly.
This commit ensures that all appropriate RTL stylesheets are loaded when selecting an RTL language.
Additionally, this commit improves performance by only running a `file_exists()` check for an RTL stylesheet if `is_rtl()` returns true, i.e. an RTL locale is selected.
Follow-up to [49982], [50836].
Props zoonini, sabernhardt, maahrokh, ankit-k-gupta, aristath, poena, SergeyBiryukov.
See #56325.
Built from https://develop.svn.wordpress.org/trunk@54330
git-svn-id: http://core.svn.wordpress.org/trunk@53889 1a063a9b-81f0-0310-95a4-ce76da25c4cd
The `register_block_style_handle()` function runs ~200 times on each page load. Each time it runs, we call `get_theme_file_path()` and then run it through `wp_normalize_path()`.
`get_theme_file_path()` calls a few other functions: `get_stylesheet_directory()`, `get_stylesheet()`, `get_option()`, and there's a bunch of filters that run on each iteration of that, without ever changing.
By caching the value in a static variable, we can avoid ~200 calls on many functions and filters, improving performance.
Follow-up to [53091], [54290], [54291], [54309].
Props aristath, mukesh27.
Fixes#56666.
Built from https://develop.svn.wordpress.org/trunk@54327
git-svn-id: http://core.svn.wordpress.org/trunk@53886 1a063a9b-81f0-0310-95a4-ce76da25c4cd
[54155] broke loading of style.css files, namely it was enqueuing style.css files that don't exist on the frontend, which lead to 404 HTTO errors. All these style.css files don't exist for core blocks as they should be registered style handlers without a file path.
Follow-up to [54155].
Props tobiasbg, nendeb55.
Fixes#56408, #56614.
Built from https://develop.svn.wordpress.org/trunk@54323
git-svn-id: http://core.svn.wordpress.org/trunk@53882 1a063a9b-81f0-0310-95a4-ce76da25c4cd
`realpath()` already checks if the file exists, and returns `false` on failure. The additional `file_exists()` check is not necessary and can be removed, improving the performance.
This commit simplifies the checks in two functions:
* `register_block_type_from_metadata()`
* `wp_json_file_decode()`
Note: In both of these cases, the values are passed through `wp_normalize_path()` after `realpath()`, so if the file does not exist, the `false` value gets converted to an empty string. The updated checks work both for `false` and `''` values.
Though this is a small tweak, it saves a lot of checks since one of the places we do this is when registering block styles, so it runs quite a few times on each page load.
Reference: [https://www.php.net/manual/en/function.realpath.php PHP Manual: realpath()].
Follow-up to [51599], [54132], [54290], [54291].
Props aristath.
Fixes#56654.
Built from https://develop.svn.wordpress.org/trunk@54309
git-svn-id: http://core.svn.wordpress.org/trunk@53868 1a063a9b-81f0-0310-95a4-ce76da25c4cd
The `register_block_style_handle()` function called `realpath()` when retrieving the normalized style path, and then a few lines below that, recalculated the exact same value, running `realpath()` again.
This commit removes duplicate calculations, reducing the number of `realpath()` calls in the function by half. In tests ran using Xdebug & Webgrind, the total `realpath()` invocation count goes down from 639 to 461, and total self cost (the time that the function is responsible for) goes down from 146 ms to 89 ms.
Follow-up to [48141], [52291], [53091], [54155].
Props aristath.
Fixes#56636.
Built from https://develop.svn.wordpress.org/trunk@54290
git-svn-id: http://core.svn.wordpress.org/trunk@53849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit improves PHP performance for core blocks by reading a single PHP file with block metadata, instead of reading a JSON file per-block and then decoding from JSON to PHP.
Includes:
* Adding a new Grunt task to convert `block.json` files to `block-json.php`.
* Using the new `block-json.php` file in the `register_block_type_from_metadata()` function.
Follow-up to [48141].
Props aristath, gziolo, johnbillion, presstoke, mukesh27, hellofromTonya, petitphp, adamsilverstein, costdev, desrosj, SergeyBiryukov.
Fixes#55005.
Built from https://develop.svn.wordpress.org/trunk@54276
git-svn-id: http://core.svn.wordpress.org/trunk@53835 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Add a note that the parameter is optional and defaults to the result of `wp_allowed_protocols()`.
This affects:
* `wp_kses()`
* `filter_block_content()`
* `filter_block_kses()`
* `filter_block_kses_value()`
Includes synchronizing the `$allowed_html` parameter description for consistency.
Follow-up to [649], [6630], [18826], [32603], [43016], [46896], [48478].
Props armondal, SergeyBiryukov.
Fixes#56580.
Built from https://develop.svn.wordpress.org/trunk@54181
git-svn-id: http://core.svn.wordpress.org/trunk@53740 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Follow-up #54337, [52069]. Part of https://github.com/WordPress/gutenberg/issues/41236. More details in https://github.com/WordPress/gutenberg/issues/33542.
Allow passing more than one script per block for `editorScript`, `script`, and `viewScript` fields in the `block.json` metadata file. This aligns with the previously added changes for `style` and `editorStyle` fields.
This change impacts the `WP_Block_Type` class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have `_handles` suffix.
Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter.
Fixes#56408.
Built from https://develop.svn.wordpress.org/trunk@54155
git-svn-id: http://core.svn.wordpress.org/trunk@53714 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset adss an `aria-hidden="true"` attribute to the optional arrows/chevron characters to the Query Loop and to the Comments Query Loop blocks pagination links.
These characters are text, so they're read out by assistive technologies. For better accessibility, it's best to reduce noise for screen reader users and prevent them to be rendered, using the `aria-hidden="true"` attribute.
Follow-up to [52057].
Props afercia, sabernhardt, kamig478, zieladam, audrasjb, joedolson, costdev.
Fixes#56067.
Built from https://develop.svn.wordpress.org/trunk@54138
git-svn-id: http://core.svn.wordpress.org/trunk@53697 1a063a9b-81f0-0310-95a4-ce76da25c4cd