Edit File by line
/home/zeestwma/ceyloniy.../wp-inclu...
File: blocks.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Functions related to registering and parsing blocks.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Blocks
[5] Fix | Delete
* @since 5.0.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Removes the block asset's path prefix if provided.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.5.0
[12] Fix | Delete
*
[13] Fix | Delete
* @param string $asset_handle_or_path Asset handle or prefixed path.
[14] Fix | Delete
* @return string Path without the prefix or the original value.
[15] Fix | Delete
*/
[16] Fix | Delete
function remove_block_asset_path_prefix( $asset_handle_or_path ) {
[17] Fix | Delete
$path_prefix = 'file:';
[18] Fix | Delete
if ( ! str_starts_with( $asset_handle_or_path, $path_prefix ) ) {
[19] Fix | Delete
return $asset_handle_or_path;
[20] Fix | Delete
}
[21] Fix | Delete
$path = substr(
[22] Fix | Delete
$asset_handle_or_path,
[23] Fix | Delete
strlen( $path_prefix )
[24] Fix | Delete
);
[25] Fix | Delete
if ( str_starts_with( $path, './' ) ) {
[26] Fix | Delete
$path = substr( $path, 2 );
[27] Fix | Delete
}
[28] Fix | Delete
return $path;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Generates the name for an asset based on the name of the block
[33] Fix | Delete
* and the field name provided.
[34] Fix | Delete
*
[35] Fix | Delete
* @since 5.5.0
[36] Fix | Delete
* @since 6.1.0 Added `$index` parameter.
[37] Fix | Delete
* @since 6.5.0 Added support for `viewScriptModule` field.
[38] Fix | Delete
*
[39] Fix | Delete
* @param string $block_name Name of the block.
[40] Fix | Delete
* @param string $field_name Name of the metadata field.
[41] Fix | Delete
* @param int $index Optional. Index of the asset when multiple items passed.
[42] Fix | Delete
* Default 0.
[43] Fix | Delete
* @return string Generated asset name for the block's field.
[44] Fix | Delete
*/
[45] Fix | Delete
function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
[46] Fix | Delete
if ( str_starts_with( $block_name, 'core/' ) ) {
[47] Fix | Delete
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
[48] Fix | Delete
if ( str_starts_with( $field_name, 'editor' ) ) {
[49] Fix | Delete
$asset_handle .= '-editor';
[50] Fix | Delete
}
[51] Fix | Delete
if ( str_starts_with( $field_name, 'view' ) ) {
[52] Fix | Delete
$asset_handle .= '-view';
[53] Fix | Delete
}
[54] Fix | Delete
if ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
[55] Fix | Delete
$asset_handle .= '-script-module';
[56] Fix | Delete
}
[57] Fix | Delete
if ( $index > 0 ) {
[58] Fix | Delete
$asset_handle .= '-' . ( $index + 1 );
[59] Fix | Delete
}
[60] Fix | Delete
return $asset_handle;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
$field_mappings = array(
[64] Fix | Delete
'editorScript' => 'editor-script',
[65] Fix | Delete
'editorStyle' => 'editor-style',
[66] Fix | Delete
'script' => 'script',
[67] Fix | Delete
'style' => 'style',
[68] Fix | Delete
'viewScript' => 'view-script',
[69] Fix | Delete
'viewScriptModule' => 'view-script-module',
[70] Fix | Delete
'viewStyle' => 'view-style',
[71] Fix | Delete
);
[72] Fix | Delete
$asset_handle = str_replace( '/', '-', $block_name ) .
[73] Fix | Delete
'-' . $field_mappings[ $field_name ];
[74] Fix | Delete
if ( $index > 0 ) {
[75] Fix | Delete
$asset_handle .= '-' . ( $index + 1 );
[76] Fix | Delete
}
[77] Fix | Delete
return $asset_handle;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Gets the URL to a block asset.
[82] Fix | Delete
*
[83] Fix | Delete
* @since 6.4.0
[84] Fix | Delete
*
[85] Fix | Delete
* @param string $path A normalized path to a block asset.
[86] Fix | Delete
* @return string|false The URL to the block asset or false on failure.
[87] Fix | Delete
*/
[88] Fix | Delete
function get_block_asset_url( $path ) {
[89] Fix | Delete
if ( empty( $path ) ) {
[90] Fix | Delete
return false;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
// Path needs to be normalized to work in Windows env.
[94] Fix | Delete
static $wpinc_path_norm = '';
[95] Fix | Delete
if ( ! $wpinc_path_norm ) {
[96] Fix | Delete
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( str_starts_with( $path, $wpinc_path_norm ) ) {
[100] Fix | Delete
return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
static $template_paths_norm = array();
[104] Fix | Delete
[105] Fix | Delete
$template = get_template();
[106] Fix | Delete
if ( ! isset( $template_paths_norm[ $template ] ) ) {
[107] Fix | Delete
$template_paths_norm[ $template ] = wp_normalize_path( realpath( get_template_directory() ) );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
[111] Fix | Delete
return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if ( is_child_theme() ) {
[115] Fix | Delete
$stylesheet = get_stylesheet();
[116] Fix | Delete
if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
[117] Fix | Delete
$template_paths_norm[ $stylesheet ] = wp_normalize_path( realpath( get_stylesheet_directory() ) );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
[121] Fix | Delete
return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
[122] Fix | Delete
}
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
return plugins_url( basename( $path ), $path );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Finds a script module ID for the selected block metadata field. It detects
[130] Fix | Delete
* when a path to file was provided and optionally finds a corresponding asset
[131] Fix | Delete
* file with details necessary to register the script module under with an
[132] Fix | Delete
* automatically generated module ID. It returns unprocessed script module
[133] Fix | Delete
* ID otherwise.
[134] Fix | Delete
*
[135] Fix | Delete
* @since 6.5.0
[136] Fix | Delete
*
[137] Fix | Delete
* @param array $metadata Block metadata.
[138] Fix | Delete
* @param string $field_name Field name to pick from metadata.
[139] Fix | Delete
* @param int $index Optional. Index of the script module ID to register when multiple
[140] Fix | Delete
* items passed. Default 0.
[141] Fix | Delete
* @return string|false Script module ID or false on failure.
[142] Fix | Delete
*/
[143] Fix | Delete
function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
[144] Fix | Delete
if ( empty( $metadata[ $field_name ] ) ) {
[145] Fix | Delete
return false;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
$module_id = $metadata[ $field_name ];
[149] Fix | Delete
if ( is_array( $module_id ) ) {
[150] Fix | Delete
if ( empty( $module_id[ $index ] ) ) {
[151] Fix | Delete
return false;
[152] Fix | Delete
}
[153] Fix | Delete
$module_id = $module_id[ $index ];
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
$module_path = remove_block_asset_path_prefix( $module_id );
[157] Fix | Delete
if ( $module_id === $module_path ) {
[158] Fix | Delete
return $module_id;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
$path = dirname( $metadata['file'] );
[162] Fix | Delete
$module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
[163] Fix | Delete
$module_id = generate_block_asset_handle( $metadata['name'], $field_name, $index );
[164] Fix | Delete
$module_asset_path = wp_normalize_path(
[165] Fix | Delete
realpath( $module_asset_raw_path )
[166] Fix | Delete
);
[167] Fix | Delete
[168] Fix | Delete
$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
[169] Fix | Delete
$module_uri = get_block_asset_url( $module_path_norm );
[170] Fix | Delete
[171] Fix | Delete
$module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
[172] Fix | Delete
$module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
[173] Fix | Delete
$block_version = isset( $metadata['version'] ) ? $metadata['version'] : false;
[174] Fix | Delete
$module_version = isset( $module_asset['version'] ) ? $module_asset['version'] : $block_version;
[175] Fix | Delete
[176] Fix | Delete
$supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity'];
[177] Fix | Delete
$is_interactive = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] );
[178] Fix | Delete
$supports_client_navigation = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['clientNavigation'] ) && true === $metadata['supports']['interactivity']['clientNavigation'] );
[179] Fix | Delete
[180] Fix | Delete
$args = array();
[181] Fix | Delete
[182] Fix | Delete
// Blocks using the Interactivity API are server-side rendered, so they are
[183] Fix | Delete
// by design not in the critical rendering path and should be deprioritized.
[184] Fix | Delete
if ( $is_interactive ) {
[185] Fix | Delete
$args['fetchpriority'] = 'low';
[186] Fix | Delete
$args['in_footer'] = true;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
// Blocks using the Interactivity API that support client-side navigation
[190] Fix | Delete
// must be marked as such in their script modules.
[191] Fix | Delete
if ( $is_interactive && $supports_client_navigation ) {
[192] Fix | Delete
wp_interactivity()->add_client_navigation_support_to_script_module( $module_id );
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
wp_register_script_module(
[196] Fix | Delete
$module_id,
[197] Fix | Delete
$module_uri,
[198] Fix | Delete
$module_dependencies,
[199] Fix | Delete
$module_version,
[200] Fix | Delete
$args
[201] Fix | Delete
);
[202] Fix | Delete
[203] Fix | Delete
return $module_id;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* Finds a script handle for the selected block metadata field. It detects
[208] Fix | Delete
* when a path to file was provided and optionally finds a corresponding asset
[209] Fix | Delete
* file with details necessary to register the script under automatically
[210] Fix | Delete
* generated handle name. It returns unprocessed script handle otherwise.
[211] Fix | Delete
*
[212] Fix | Delete
* @since 5.5.0
[213] Fix | Delete
* @since 6.1.0 Added `$index` parameter.
[214] Fix | Delete
* @since 6.5.0 The asset file is optional. Added script handle support in the asset file.
[215] Fix | Delete
*
[216] Fix | Delete
* @param array $metadata Block metadata.
[217] Fix | Delete
* @param string $field_name Field name to pick from metadata.
[218] Fix | Delete
* @param int $index Optional. Index of the script to register when multiple items passed.
[219] Fix | Delete
* Default 0.
[220] Fix | Delete
* @return string|false Script handle provided directly or created through
[221] Fix | Delete
* script's registration, or false on failure.
[222] Fix | Delete
*/
[223] Fix | Delete
function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
[224] Fix | Delete
if ( empty( $metadata[ $field_name ] ) ) {
[225] Fix | Delete
return false;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
$script_handle_or_path = $metadata[ $field_name ];
[229] Fix | Delete
if ( is_array( $script_handle_or_path ) ) {
[230] Fix | Delete
if ( empty( $script_handle_or_path[ $index ] ) ) {
[231] Fix | Delete
return false;
[232] Fix | Delete
}
[233] Fix | Delete
$script_handle_or_path = $script_handle_or_path[ $index ];
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
$script_path = remove_block_asset_path_prefix( $script_handle_or_path );
[237] Fix | Delete
if ( $script_handle_or_path === $script_path ) {
[238] Fix | Delete
return $script_handle_or_path;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
$path = dirname( $metadata['file'] );
[242] Fix | Delete
$script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
[243] Fix | Delete
$script_asset_path = wp_normalize_path(
[244] Fix | Delete
realpath( $script_asset_raw_path )
[245] Fix | Delete
);
[246] Fix | Delete
[247] Fix | Delete
// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
[248] Fix | Delete
$script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array();
[249] Fix | Delete
$script_handle = isset( $script_asset['handle'] ) ?
[250] Fix | Delete
$script_asset['handle'] :
[251] Fix | Delete
generate_block_asset_handle( $metadata['name'], $field_name, $index );
[252] Fix | Delete
if ( wp_script_is( $script_handle, 'registered' ) ) {
[253] Fix | Delete
return $script_handle;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
$script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
[257] Fix | Delete
$script_uri = get_block_asset_url( $script_path_norm );
[258] Fix | Delete
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
[259] Fix | Delete
$block_version = isset( $metadata['version'] ) ? $metadata['version'] : false;
[260] Fix | Delete
$script_version = isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version;
[261] Fix | Delete
$script_args = array();
[262] Fix | Delete
if ( 'viewScript' === $field_name && $script_uri ) {
[263] Fix | Delete
$script_args['strategy'] = 'defer';
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
$result = wp_register_script(
[267] Fix | Delete
$script_handle,
[268] Fix | Delete
$script_uri,
[269] Fix | Delete
$script_dependencies,
[270] Fix | Delete
$script_version,
[271] Fix | Delete
$script_args
[272] Fix | Delete
);
[273] Fix | Delete
if ( ! $result ) {
[274] Fix | Delete
return false;
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) {
[278] Fix | Delete
wp_set_script_translations( $script_handle, $metadata['textdomain'] );
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
return $script_handle;
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
/**
[285] Fix | Delete
* Finds a style handle for the block metadata field. It detects when a path
[286] Fix | Delete
* to file was provided and registers the style under automatically
[287] Fix | Delete
* generated handle name. It returns unprocessed style handle otherwise.
[288] Fix | Delete
*
[289] Fix | Delete
* @since 5.5.0
[290] Fix | Delete
* @since 6.1.0 Added `$index` parameter.
[291] Fix | Delete
*
[292] Fix | Delete
* @param array $metadata Block metadata.
[293] Fix | Delete
* @param string $field_name Field name to pick from metadata.
[294] Fix | Delete
* @param int $index Optional. Index of the style to register when multiple items passed.
[295] Fix | Delete
* Default 0.
[296] Fix | Delete
* @return string|false Style handle provided directly or created through
[297] Fix | Delete
* style's registration, or false on failure.
[298] Fix | Delete
*/
[299] Fix | Delete
function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
[300] Fix | Delete
if ( empty( $metadata[ $field_name ] ) ) {
[301] Fix | Delete
return false;
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
$style_handle = $metadata[ $field_name ];
[305] Fix | Delete
if ( is_array( $style_handle ) ) {
[306] Fix | Delete
if ( empty( $style_handle[ $index ] ) ) {
[307] Fix | Delete
return false;
[308] Fix | Delete
}
[309] Fix | Delete
$style_handle = $style_handle[ $index ];
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
[313] Fix | Delete
// If the style handle is already registered, skip re-registering.
[314] Fix | Delete
if ( wp_style_is( $style_handle_name, 'registered' ) ) {
[315] Fix | Delete
return $style_handle_name;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
static $wpinc_path_norm = '';
[319] Fix | Delete
if ( ! $wpinc_path_norm ) {
[320] Fix | Delete
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
$is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
[324] Fix | Delete
// Skip registering individual styles for each core block when a bundled version provided.
[325] Fix | Delete
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
[326] Fix | Delete
return false;
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
$style_path = remove_block_asset_path_prefix( $style_handle );
[330] Fix | Delete
$is_style_handle = $style_handle === $style_path;
[331] Fix | Delete
// Allow only passing style handles for core blocks.
[332] Fix | Delete
if ( $is_core_block && ! $is_style_handle ) {
[333] Fix | Delete
return false;
[334] Fix | Delete
}
[335] Fix | Delete
// Return the style handle unless it's the first item for every core block that requires special treatment.
[336] Fix | Delete
if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
[337] Fix | Delete
return $style_handle;
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
// Check whether styles should have a ".min" suffix or not.
[341] Fix | Delete
$suffix = SCRIPT_DEBUG ? '' : '.min';
[342] Fix | Delete
if ( $is_core_block ) {
[343] Fix | Delete
$style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
[347] Fix | Delete
$style_uri = get_block_asset_url( $style_path_norm );
[348] Fix | Delete
[349] Fix | Delete
$block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
[350] Fix | Delete
$version = $style_path_norm && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( $style_path_norm ) : $block_version;
[351] Fix | Delete
$result = wp_register_style(
[352] Fix | Delete
$style_handle_name,
[353] Fix | Delete
$style_uri,
[354] Fix | Delete
array(),
[355] Fix | Delete
$version
[356] Fix | Delete
);
[357] Fix | Delete
if ( ! $result ) {
[358] Fix | Delete
return false;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
if ( $style_uri ) {
[362] Fix | Delete
wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
[363] Fix | Delete
[364] Fix | Delete
if ( $is_core_block ) {
[365] Fix | Delete
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
[366] Fix | Delete
} else {
[367] Fix | Delete
$rtl_file = str_replace( '.css', '-rtl.css', $style_path_norm );
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
if ( is_rtl() && file_exists( $rtl_file ) ) {
[371] Fix | Delete
wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
[372] Fix | Delete
wp_style_add_data( $style_handle_name, 'suffix', $suffix );
[373] Fix | Delete
wp_style_add_data( $style_handle_name, 'path', $rtl_file );
[374] Fix | Delete
}
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
return $style_handle_name;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Gets i18n schema for block's metadata read from `block.json` file.
[382] Fix | Delete
*
[383] Fix | Delete
* @since 5.9.0
[384] Fix | Delete
*
[385] Fix | Delete
* @return object The schema for block's metadata.
[386] Fix | Delete
*/
[387] Fix | Delete
function get_block_metadata_i18n_schema() {
[388] Fix | Delete
static $i18n_block_schema;
[389] Fix | Delete
[390] Fix | Delete
if ( ! isset( $i18n_block_schema ) ) {
[391] Fix | Delete
$i18n_block_schema = wp_json_file_decode( __DIR__ . '/block-i18n.json' );
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
return $i18n_block_schema;
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
/**
[398] Fix | Delete
* Registers all block types from a block metadata collection.
[399] Fix | Delete
*
[400] Fix | Delete
* This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided,
[401] Fix | Delete
* register the metadata collection directly within the same function call.
[402] Fix | Delete
*
[403] Fix | Delete
* @since 6.8.0
[404] Fix | Delete
* @see wp_register_block_metadata_collection()
[405] Fix | Delete
* @see register_block_type_from_metadata()
[406] Fix | Delete
*
[407] Fix | Delete
* @param string $path The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
[408] Fix | Delete
* @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in
[409] Fix | Delete
* order to register the collection. If this parameter is not provided, the `$path` parameter
[410] Fix | Delete
* must reference a previously registered block metadata collection.
[411] Fix | Delete
*/
[412] Fix | Delete
function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
[413] Fix | Delete
if ( $manifest ) {
[414] Fix | Delete
wp_register_block_metadata_collection( $path, $manifest );
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
$block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
[418] Fix | Delete
foreach ( $block_metadata_files as $block_metadata_file ) {
[419] Fix | Delete
register_block_type_from_metadata( $block_metadata_file );
[420] Fix | Delete
}
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
/**
[424] Fix | Delete
* Registers a block metadata collection.
[425] Fix | Delete
*
[426] Fix | Delete
* This function allows core and third-party plugins to register their block metadata
[427] Fix | Delete
* collections in a centralized location. Registering collections can improve performance
[428] Fix | Delete
* by avoiding multiple reads from the filesystem and parsing JSON.
[429] Fix | Delete
*
[430] Fix | Delete
* @since 6.7.0
[431] Fix | Delete
*
[432] Fix | Delete
* @param string $path The base path in which block files for the collection reside.
[433] Fix | Delete
* @param string $manifest The path to the manifest file for the collection.
[434] Fix | Delete
*/
[435] Fix | Delete
function wp_register_block_metadata_collection( $path, $manifest ) {
[436] Fix | Delete
WP_Block_Metadata_Registry::register_collection( $path, $manifest );
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
/**
[440] Fix | Delete
* Registers a block type from the metadata stored in the `block.json` file.
[441] Fix | Delete
*
[442] Fix | Delete
* @since 5.5.0
[443] Fix | Delete
* @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
[444] Fix | Delete
* @since 5.9.0 Added support for `variations` and `viewScript` fields.
[445] Fix | Delete
* @since 6.1.0 Added support for `render` field.
[446] Fix | Delete
* @since 6.3.0 Added `selectors` field.
[447] Fix | Delete
* @since 6.4.0 Added support for `blockHooks` field.
[448] Fix | Delete
* @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields.
[449] Fix | Delete
* @since 6.7.0 Allow PHP filename as `variations` argument.
[450] Fix | Delete
*
[451] Fix | Delete
* @param string $file_or_folder Path to the JSON file with metadata definition for
[452] Fix | Delete
* the block or path to the folder where the `block.json` file is located.
[453] Fix | Delete
* If providing the path to a JSON file, the filename must end with `block.json`.
[454] Fix | Delete
* @param array $args Optional. Array of block type arguments. Accepts any public property
[455] Fix | Delete
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information
[456] Fix | Delete
* on accepted arguments. Default empty array.
[457] Fix | Delete
* @return WP_Block_Type|false The registered block type on success, or false on failure.
[458] Fix | Delete
*/
[459] Fix | Delete
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
[460] Fix | Delete
/*
[461] Fix | Delete
* Get an array of metadata from a PHP file.
[462] Fix | Delete
* This improves performance for core blocks as it's only necessary to read a single PHP file
[463] Fix | Delete
* instead of reading a JSON file per-block, and then decoding from JSON to PHP.
[464] Fix | Delete
* Using a static variable ensures that the metadata is only read once per request.
[465] Fix | Delete
*/
[466] Fix | Delete
[467] Fix | Delete
$file_or_folder = wp_normalize_path( $file_or_folder );
[468] Fix | Delete
[469] Fix | Delete
$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
[470] Fix | Delete
trailingslashit( $file_or_folder ) . 'block.json' :
[471] Fix | Delete
$file_or_folder;
[472] Fix | Delete
[473] Fix | Delete
$is_core_block = str_starts_with( $file_or_folder, wp_normalize_path( ABSPATH . WPINC ) );
[474] Fix | Delete
$metadata_file_exists = $is_core_block || file_exists( $metadata_file );
[475] Fix | Delete
$registry_metadata = WP_Block_Metadata_Registry::get_metadata( $file_or_folder );
[476] Fix | Delete
[477] Fix | Delete
if ( $registry_metadata ) {
[478] Fix | Delete
$metadata = $registry_metadata;
[479] Fix | Delete
} elseif ( $metadata_file_exists ) {
[480] Fix | Delete
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
[481] Fix | Delete
} else {
[482] Fix | Delete
$metadata = array();
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) {
[486] Fix | Delete
return false;
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
$metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null;
[490] Fix | Delete
[491] Fix | Delete
/**
[492] Fix | Delete
* Filters the metadata provided for registering a block type.
[493] Fix | Delete
*
[494] Fix | Delete
* @since 5.7.0
[495] Fix | Delete
*
[496] Fix | Delete
* @param array $metadata Metadata for registering a block type.
[497] Fix | Delete
*/
[498] Fix | Delete
$metadata = apply_filters( 'block_type_metadata', $metadata );
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function