Skip to content

Commit 29fe42e

Browse files
committed
Docs: Improve block asset registration docblocks.
Per the inline documentation standards, a docblock's summary belongs on its own line separated from the description, and the description should not open with "It". This is applied to `register_block_script_module_id()`, `register_block_script_handle()`, and `register_block_style_handle()`, together with some missing articles in the same descriptions. Two of those descriptions no longer matched the code. `register_block_script_handle()` said the script is registered under an automatically generated handle, but since 6.5.0 the handle is taken from the asset file whenever one provides it, and generation is only the fallback. `register_block_style_handle()` said it returns the unprocessed style handle otherwise, which does not hold for the first style of a core block: that one is registered from the block's own stylesheet when separate core block assets are loaded, and skipped entirely when they are not. The same functions gain `@phpstan-` annotations describing the shape of the `$metadata` they accept and the narrower strings they return. The shapes follow the `block.json` schema, which constrains only `name`, so the remaining fields stay plain strings; `file` is nullable and `name` optional because `register_block_type_from_metadata()` can reach all three functions with neither present. Developed in WordPress#11851. Follow-up to r48141, r55447, r57559, r57565. Props deepakrohilla, westonruter, sabernhardt, wildworks, audrasjb. See #64898. Fixes #65259. git-svn-id: https://develop.svn.wordpress.org/trunk@62966 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 47a5084 commit 29fe42e

1 file changed

Lines changed: 73 additions & 13 deletions

File tree

src/wp-includes/blocks.php

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
4343
* @param int $index Optional. Index of the asset when multiple items passed.
4444
* Default 0.
4545
* @return string Generated asset name for the block's field.
46+
*
47+
* @phpstan-param non-falsy-string $block_name
48+
* @phpstan-param 'editorScript'|'editorStyle'|'script'|'style'|'viewScript'|'viewScriptModule'|'viewStyle' $field_name
49+
* @phpstan-param int<0, max> $index
50+
* @phpstan-return non-falsy-string
4651
*/
4752
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
4853
if ( str_starts_with( $block_name, 'core/' ) ) {
@@ -86,6 +91,8 @@ function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
8691
*
8792
* @param string $path A normalized path to a block asset.
8893
* @return string|false The URL to the block asset or false on failure.
94+
*
95+
* @phpstan-return non-falsy-string|false
8996
*/
9097
function get_block_asset_url( $path ) {
9198
if ( empty( $path ) ) {
@@ -102,6 +109,7 @@ function get_block_asset_url( $path ) {
102109
return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
103110
}
104111

112+
/** @var array<string, string> $template_paths_norm */
105113
static $template_paths_norm = array();
106114

107115
$template = get_template();
@@ -128,11 +136,12 @@ function get_block_asset_url( $path ) {
128136
}
129137

130138
/**
131-
* Finds a script module ID for the selected block metadata field. It detects
132-
* when a path to file was provided and optionally finds a corresponding asset
133-
* file with details necessary to register the script module under with an
134-
* automatically generated module ID. It returns unprocessed script module
135-
* ID otherwise.
139+
* Finds a script module ID for the selected block metadata field.
140+
*
141+
* Detects when a path to a file was provided and optionally finds a
142+
* corresponding asset file with details necessary to register the script
143+
* module with an automatically generated module ID. It returns the
144+
* unprocessed script module ID otherwise.
136145
*
137146
* @since 6.5.0
138147
*
@@ -141,6 +150,21 @@ function get_block_asset_url( $path ) {
141150
* @param int $index Optional. Index of the script module ID to register when multiple
142151
* items passed. Default 0.
143152
* @return string|false Script module ID or false on failure.
153+
*
154+
* @phpstan-param array{
155+
* name?: non-falsy-string,
156+
* file: non-falsy-string|null,
157+
* version?: string,
158+
* supports?: array{
159+
* interactivity?: bool|array{interactive?: bool, clientNavigation?: bool, ...},
160+
* ...
161+
* },
162+
* viewScriptModule?: string|list<string>,
163+
* ...
164+
* } $metadata
165+
* @phpstan-param 'viewScriptModule' $field_name
166+
* @phpstan-param int<0, max> $index
167+
* @phpstan-return non-falsy-string|false
144168
*/
145169
function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
146170
if ( empty( $metadata[ $field_name ] ) ) {
@@ -170,6 +194,7 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
170194
$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
171195
$module_uri = get_block_asset_url( $module_path_norm );
172196

197+
/** @var array{ dependencies?: list<non-falsy-string|array{id: non-falsy-string, import?: 'static'|'dynamic'}>, version?: string|false|null, ... } $module_asset */
173198
$module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
174199
$module_dependencies = $module_asset['dependencies'] ?? array();
175200
$block_version = $metadata['version'] ?? false;
@@ -206,10 +231,13 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
206231
}
207232

208233
/**
209-
* Finds a script handle for the selected block metadata field. It detects
210-
* when a path to file was provided and optionally finds a corresponding asset
211-
* file with details necessary to register the script under automatically
212-
* generated handle name. It returns unprocessed script handle otherwise.
234+
* Finds a script handle for the selected block metadata field.
235+
*
236+
* Detects when a path to a file was provided and optionally finds a
237+
* corresponding asset file with details necessary to register the script. The
238+
* handle is taken from the asset file when it provides one, and is otherwise
239+
* generated automatically. It returns the unprocessed script handle when a
240+
* handle rather than a path was given.
213241
*
214242
* @since 5.5.0
215243
* @since 6.1.0 Added `$index` parameter.
@@ -221,6 +249,20 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
221249
* Default 0.
222250
* @return string|false Script handle provided directly or created through
223251
* script's registration, or false on failure.
252+
*
253+
* @phpstan-param array{
254+
* name?: non-falsy-string,
255+
* file: non-falsy-string|null,
256+
* version?: string,
257+
* textdomain?: string,
258+
* editorScript?: string|list<string>,
259+
* script?: string|list<string>,
260+
* viewScript?: string|list<string>,
261+
* ...
262+
* } $metadata
263+
* @phpstan-param 'editorScript'|'script'|'viewScript' $field_name
264+
* @phpstan-param int<0, max> $index
265+
* @phpstan-return non-falsy-string|false
224266
*/
225267
function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
226268
if ( empty( $metadata[ $field_name ] ) ) {
@@ -247,6 +289,7 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
247289
);
248290

249291
// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
292+
/** @var array{ handle?: non-falsy-string, dependencies?: list<non-falsy-string>, version?: string|false|null, ... } $script_asset */
250293
$script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array();
251294
$script_handle = $script_asset['handle'] ??
252295
generate_block_asset_handle( $metadata['name'], $field_name, $index );
@@ -283,9 +326,13 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
283326
}
284327

285328
/**
286-
* Finds a style handle for the block metadata field. It detects when a path
287-
* to file was provided and registers the style under automatically
288-
* generated handle name. It returns unprocessed style handle otherwise.
329+
* Finds a style handle for the block metadata field.
330+
*
331+
* Detects when a path to a file was provided and registers the style under an
332+
* automatically generated handle name. It returns the unprocessed style handle
333+
* otherwise, except for the first style of a core block, which is instead
334+
* registered from the block's own stylesheet when separate core block assets
335+
* are loaded. Core blocks accept only handles, not paths.
289336
*
290337
* @since 5.5.0
291338
* @since 6.1.0 Added `$index` parameter.
@@ -296,6 +343,19 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
296343
* Default 0.
297344
* @return string|false Style handle provided directly or created through
298345
* style's registration, or false on failure.
346+
*
347+
* @phpstan-param array{
348+
* name?: non-falsy-string,
349+
* file: non-falsy-string|null,
350+
* version?: string,
351+
* editorStyle?: string|list<string>,
352+
* style?: string|list<string>,
353+
* viewStyle?: string|list<string>,
354+
* ...
355+
* } $metadata
356+
* @phpstan-param 'editorStyle'|'style'|'viewStyle' $field_name
357+
* @phpstan-param int<0, max> $index
358+
* @phpstan-return non-falsy-string|false
299359
*/
300360
function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
301361
if ( empty( $metadata[ $field_name ] ) ) {
@@ -2778,7 +2838,7 @@ function build_query_vars_from_query_block( $block, $page ) {
27782838
if ( 'only' === $block->context['query']['sticky'] ) {
27792839
/*
27802840
* Passing an empty array to post__in will return have_posts() as true (and all posts will be returned).
2781-
* Logic should be used before hand to determine if WP_Query should be used in the event that the array
2841+
* Logic should be used beforehand to determine if WP_Query should be used in the event that the array
27822842
* being passed to post__in is empty.
27832843
*
27842844
* @see https://core.trac.wordpress.org/ticket/28099

0 commit comments

Comments
 (0)