Edit File by line
/home/barbar84/www/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
* Registers a block type.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.0.0
[12] Fix | Delete
*
[13] Fix | Delete
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively
[14] Fix | Delete
* a complete WP_Block_Type instance. In case a WP_Block_Type
[15] Fix | Delete
* is provided, the $args parameter will be ignored.
[16] Fix | Delete
* @param array $args Optional. Array of block type arguments. Accepts any public property
[17] Fix | Delete
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information
[18] Fix | Delete
* on accepted arguments. Default empty array.
[19] Fix | Delete
* @return WP_Block_Type|false The registered block type on success, or false on failure.
[20] Fix | Delete
*/
[21] Fix | Delete
function register_block_type( $name, $args = array() ) {
[22] Fix | Delete
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Unregisters a block type.
[27] Fix | Delete
*
[28] Fix | Delete
* @since 5.0.0
[29] Fix | Delete
*
[30] Fix | Delete
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively
[31] Fix | Delete
* a complete WP_Block_Type instance.
[32] Fix | Delete
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
[33] Fix | Delete
*/
[34] Fix | Delete
function unregister_block_type( $name ) {
[35] Fix | Delete
return WP_Block_Type_Registry::get_instance()->unregister( $name );
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Removes the block asset's path prefix if provided.
[40] Fix | Delete
*
[41] Fix | Delete
* @since 5.5.0
[42] Fix | Delete
*
[43] Fix | Delete
* @param string $asset_handle_or_path Asset handle or prefixed path.
[44] Fix | Delete
* @return string Path without the prefix or the original value.
[45] Fix | Delete
*/
[46] Fix | Delete
function remove_block_asset_path_prefix( $asset_handle_or_path ) {
[47] Fix | Delete
$path_prefix = 'file:';
[48] Fix | Delete
if ( 0 !== strpos( $asset_handle_or_path, $path_prefix ) ) {
[49] Fix | Delete
return $asset_handle_or_path;
[50] Fix | Delete
}
[51] Fix | Delete
return substr(
[52] Fix | Delete
$asset_handle_or_path,
[53] Fix | Delete
strlen( $path_prefix )
[54] Fix | Delete
);
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Generates the name for an asset based on the name of the block
[59] Fix | Delete
* and the field name provided.
[60] Fix | Delete
*
[61] Fix | Delete
* @since 5.5.0
[62] Fix | Delete
*
[63] Fix | Delete
* @param string $block_name Name of the block.
[64] Fix | Delete
* @param string $field_name Name of the metadata field.
[65] Fix | Delete
* @return string Generated asset name for the block's field.
[66] Fix | Delete
*/
[67] Fix | Delete
function generate_block_asset_handle( $block_name, $field_name ) {
[68] Fix | Delete
if ( 0 === strpos( $block_name, 'core/' ) ) {
[69] Fix | Delete
$asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
[70] Fix | Delete
if ( 0 === strpos( $field_name, 'editor' ) ) {
[71] Fix | Delete
$asset_handle .= '-editor';
[72] Fix | Delete
}
[73] Fix | Delete
return $asset_handle;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
$field_mappings = array(
[77] Fix | Delete
'editorScript' => 'editor-script',
[78] Fix | Delete
'script' => 'script',
[79] Fix | Delete
'editorStyle' => 'editor-style',
[80] Fix | Delete
'style' => 'style',
[81] Fix | Delete
);
[82] Fix | Delete
return str_replace( '/', '-', $block_name ) .
[83] Fix | Delete
'-' . $field_mappings[ $field_name ];
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Finds a script handle for the selected block metadata field. It detects
[88] Fix | Delete
* when a path to file was provided and finds a corresponding asset file
[89] Fix | Delete
* with details necessary to register the script under automatically
[90] Fix | Delete
* generated handle name. It returns unprocessed script handle otherwise.
[91] Fix | Delete
*
[92] Fix | Delete
* @since 5.5.0
[93] Fix | Delete
*
[94] Fix | Delete
* @param array $metadata Block metadata.
[95] Fix | Delete
* @param string $field_name Field name to pick from metadata.
[96] Fix | Delete
* @return string|false Script handle provided directly or created through
[97] Fix | Delete
* script's registration, or false on failure.
[98] Fix | Delete
*/
[99] Fix | Delete
function register_block_script_handle( $metadata, $field_name ) {
[100] Fix | Delete
if ( empty( $metadata[ $field_name ] ) ) {
[101] Fix | Delete
return false;
[102] Fix | Delete
}
[103] Fix | Delete
$script_handle = $metadata[ $field_name ];
[104] Fix | Delete
$script_path = remove_block_asset_path_prefix( $metadata[ $field_name ] );
[105] Fix | Delete
if ( $script_handle === $script_path ) {
[106] Fix | Delete
return $script_handle;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$script_handle = generate_block_asset_handle( $metadata['name'], $field_name );
[110] Fix | Delete
$script_asset_path = realpath(
[111] Fix | Delete
dirname( $metadata['file'] ) . '/' .
[112] Fix | Delete
substr_replace( $script_path, '.asset.php', - strlen( '.js' ) )
[113] Fix | Delete
);
[114] Fix | Delete
if ( ! file_exists( $script_asset_path ) ) {
[115] Fix | Delete
$message = sprintf(
[116] Fix | Delete
/* translators: %1: field name. %2: block name */
[117] Fix | Delete
__( 'The asset file for the "%1$s" defined in "%2$s" block definition is missing.', 'default' ),
[118] Fix | Delete
$field_name,
[119] Fix | Delete
$metadata['name']
[120] Fix | Delete
);
[121] Fix | Delete
_doing_it_wrong( __FUNCTION__, $message, '5.5.0' );
[122] Fix | Delete
return false;
[123] Fix | Delete
}
[124] Fix | Delete
$script_asset = require $script_asset_path;
[125] Fix | Delete
$result = wp_register_script(
[126] Fix | Delete
$script_handle,
[127] Fix | Delete
plugins_url( $script_path, $metadata['file'] ),
[128] Fix | Delete
$script_asset['dependencies'],
[129] Fix | Delete
$script_asset['version']
[130] Fix | Delete
);
[131] Fix | Delete
if ( ! $result ) {
[132] Fix | Delete
return false;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
if ( ! empty( $metadata['textdomain'] ) ) {
[136] Fix | Delete
wp_set_script_translations( $script_handle, $metadata['textdomain'] );
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
return $script_handle;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Finds a style handle for the block metadata field. It detects when a path
[144] Fix | Delete
* to file was provided and registers the style under automatically
[145] Fix | Delete
* generated handle name. It returns unprocessed style handle otherwise.
[146] Fix | Delete
*
[147] Fix | Delete
* @since 5.5.0
[148] Fix | Delete
*
[149] Fix | Delete
* @param array $metadata Block metadata.
[150] Fix | Delete
* @param string $field_name Field name to pick from metadata.
[151] Fix | Delete
* @return string|false Style handle provided directly or created through
[152] Fix | Delete
* style's registration, or false on failure.
[153] Fix | Delete
*/
[154] Fix | Delete
function register_block_style_handle( $metadata, $field_name ) {
[155] Fix | Delete
if ( empty( $metadata[ $field_name ] ) ) {
[156] Fix | Delete
return false;
[157] Fix | Delete
}
[158] Fix | Delete
$style_handle = $metadata[ $field_name ];
[159] Fix | Delete
$style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] );
[160] Fix | Delete
if ( $style_handle === $style_path ) {
[161] Fix | Delete
return $style_handle;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name );
[165] Fix | Delete
$block_dir = dirname( $metadata['file'] );
[166] Fix | Delete
$style_file = realpath( "$block_dir/$style_path" );
[167] Fix | Delete
$result = wp_register_style(
[168] Fix | Delete
$style_handle,
[169] Fix | Delete
plugins_url( $style_path, $metadata['file'] ),
[170] Fix | Delete
array(),
[171] Fix | Delete
filemtime( $style_file )
[172] Fix | Delete
);
[173] Fix | Delete
if ( file_exists( str_replace( '.css', '-rtl.css', $style_file ) ) ) {
[174] Fix | Delete
wp_style_add_data( $style_handle, 'rtl', 'replace' );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
return $result ? $style_handle : false;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/**
[181] Fix | Delete
* Registers a block type from metadata stored in the `block.json` file.
[182] Fix | Delete
*
[183] Fix | Delete
* @since 5.5.0
[184] Fix | Delete
*
[185] Fix | Delete
* @param string $file_or_folder Path to the JSON file with metadata definition for
[186] Fix | Delete
* the block or path to the folder where the `block.json` file is located.
[187] Fix | Delete
* @param array $args Optional. Array of block type arguments. Accepts any public property
[188] Fix | Delete
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information
[189] Fix | Delete
* on accepted arguments. Default empty array.
[190] Fix | Delete
* @return WP_Block_Type|false The registered block type on success, or false on failure.
[191] Fix | Delete
*/
[192] Fix | Delete
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
[193] Fix | Delete
$filename = 'block.json';
[194] Fix | Delete
$metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
[195] Fix | Delete
trailingslashit( $file_or_folder ) . $filename :
[196] Fix | Delete
$file_or_folder;
[197] Fix | Delete
if ( ! file_exists( $metadata_file ) ) {
[198] Fix | Delete
return false;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$metadata = json_decode( file_get_contents( $metadata_file ), true );
[202] Fix | Delete
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
[203] Fix | Delete
return false;
[204] Fix | Delete
}
[205] Fix | Delete
$metadata['file'] = $metadata_file;
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Filters the metadata provided for registering a block type.
[209] Fix | Delete
*
[210] Fix | Delete
* @since 5.7.0
[211] Fix | Delete
*
[212] Fix | Delete
* @param array $metadata Metadata for registering a block type.
[213] Fix | Delete
*/
[214] Fix | Delete
$metadata = apply_filters( 'block_type_metadata', $metadata );
[215] Fix | Delete
[216] Fix | Delete
$settings = array();
[217] Fix | Delete
$property_mappings = array(
[218] Fix | Delete
'title' => 'title',
[219] Fix | Delete
'category' => 'category',
[220] Fix | Delete
'parent' => 'parent',
[221] Fix | Delete
'icon' => 'icon',
[222] Fix | Delete
'description' => 'description',
[223] Fix | Delete
'keywords' => 'keywords',
[224] Fix | Delete
'attributes' => 'attributes',
[225] Fix | Delete
'providesContext' => 'provides_context',
[226] Fix | Delete
'usesContext' => 'uses_context',
[227] Fix | Delete
'supports' => 'supports',
[228] Fix | Delete
'styles' => 'styles',
[229] Fix | Delete
'example' => 'example',
[230] Fix | Delete
'apiVersion' => 'api_version',
[231] Fix | Delete
);
[232] Fix | Delete
[233] Fix | Delete
foreach ( $property_mappings as $key => $mapped_key ) {
[234] Fix | Delete
if ( isset( $metadata[ $key ] ) ) {
[235] Fix | Delete
$value = $metadata[ $key ];
[236] Fix | Delete
if ( empty( $metadata['textdomain'] ) ) {
[237] Fix | Delete
$settings[ $mapped_key ] = $value;
[238] Fix | Delete
continue;
[239] Fix | Delete
}
[240] Fix | Delete
$textdomain = $metadata['textdomain'];
[241] Fix | Delete
switch ( $key ) {
[242] Fix | Delete
case 'title':
[243] Fix | Delete
case 'description':
[244] Fix | Delete
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain
[245] Fix | Delete
$settings[ $mapped_key ] = translate_with_gettext_context( $value, sprintf( 'block %s', $key ), $textdomain );
[246] Fix | Delete
break;
[247] Fix | Delete
case 'keywords':
[248] Fix | Delete
$settings[ $mapped_key ] = array();
[249] Fix | Delete
if ( ! is_array( $value ) ) {
[250] Fix | Delete
continue 2;
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
foreach ( $value as $keyword ) {
[254] Fix | Delete
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
[255] Fix | Delete
$settings[ $mapped_key ][] = translate_with_gettext_context( $keyword, 'block keyword', $textdomain );
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
break;
[259] Fix | Delete
case 'styles':
[260] Fix | Delete
$settings[ $mapped_key ] = array();
[261] Fix | Delete
if ( ! is_array( $value ) ) {
[262] Fix | Delete
continue 2;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
foreach ( $value as $style ) {
[266] Fix | Delete
if ( ! empty( $style['label'] ) ) {
[267] Fix | Delete
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
[268] Fix | Delete
$style['label'] = translate_with_gettext_context( $style['label'], 'block style label', $textdomain );
[269] Fix | Delete
}
[270] Fix | Delete
$settings[ $mapped_key ][] = $style;
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
break;
[274] Fix | Delete
default:
[275] Fix | Delete
$settings[ $mapped_key ] = $value;
[276] Fix | Delete
}
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
if ( ! empty( $metadata['editorScript'] ) ) {
[281] Fix | Delete
$settings['editor_script'] = register_block_script_handle(
[282] Fix | Delete
$metadata,
[283] Fix | Delete
'editorScript'
[284] Fix | Delete
);
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
if ( ! empty( $metadata['script'] ) ) {
[288] Fix | Delete
$settings['script'] = register_block_script_handle(
[289] Fix | Delete
$metadata,
[290] Fix | Delete
'script'
[291] Fix | Delete
);
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
if ( ! empty( $metadata['editorStyle'] ) ) {
[295] Fix | Delete
$settings['editor_style'] = register_block_style_handle(
[296] Fix | Delete
$metadata,
[297] Fix | Delete
'editorStyle'
[298] Fix | Delete
);
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
if ( ! empty( $metadata['style'] ) ) {
[302] Fix | Delete
$settings['style'] = register_block_style_handle(
[303] Fix | Delete
$metadata,
[304] Fix | Delete
'style'
[305] Fix | Delete
);
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* Filters the settings determined from the block type metadata.
[310] Fix | Delete
*
[311] Fix | Delete
* @since 5.7.0
[312] Fix | Delete
*
[313] Fix | Delete
* @param array $settings Array of determined settings for registering a block type.
[314] Fix | Delete
* @param array $metadata Metadata provided for registering a block type.
[315] Fix | Delete
*/
[316] Fix | Delete
$settings = apply_filters(
[317] Fix | Delete
'block_type_metadata_settings',
[318] Fix | Delete
array_merge(
[319] Fix | Delete
$settings,
[320] Fix | Delete
$args
[321] Fix | Delete
),
[322] Fix | Delete
$metadata
[323] Fix | Delete
);
[324] Fix | Delete
[325] Fix | Delete
return register_block_type(
[326] Fix | Delete
$metadata['name'],
[327] Fix | Delete
$settings
[328] Fix | Delete
);
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
/**
[332] Fix | Delete
* Determine whether a post or content string has blocks.
[333] Fix | Delete
*
[334] Fix | Delete
* This test optimizes for performance rather than strict accuracy, detecting
[335] Fix | Delete
* the pattern of a block but not validating its structure. For strict accuracy,
[336] Fix | Delete
* you should use the block parser on post content.
[337] Fix | Delete
*
[338] Fix | Delete
* @since 5.0.0
[339] Fix | Delete
*
[340] Fix | Delete
* @see parse_blocks()
[341] Fix | Delete
*
[342] Fix | Delete
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
[343] Fix | Delete
* @return bool Whether the post has blocks.
[344] Fix | Delete
*/
[345] Fix | Delete
function has_blocks( $post = null ) {
[346] Fix | Delete
if ( ! is_string( $post ) ) {
[347] Fix | Delete
$wp_post = get_post( $post );
[348] Fix | Delete
if ( $wp_post instanceof WP_Post ) {
[349] Fix | Delete
$post = $wp_post->post_content;
[350] Fix | Delete
}
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
return false !== strpos( (string) $post, '<!-- wp:' );
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
/**
[357] Fix | Delete
* Determine whether a $post or a string contains a specific block type.
[358] Fix | Delete
*
[359] Fix | Delete
* This test optimizes for performance rather than strict accuracy, detecting
[360] Fix | Delete
* the block type exists but not validating its structure. For strict accuracy,
[361] Fix | Delete
* you should use the block parser on post content.
[362] Fix | Delete
*
[363] Fix | Delete
* @since 5.0.0
[364] Fix | Delete
*
[365] Fix | Delete
* @see parse_blocks()
[366] Fix | Delete
*
[367] Fix | Delete
* @param string $block_name Full Block type to look for.
[368] Fix | Delete
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
[369] Fix | Delete
* @return bool Whether the post content contains the specified block.
[370] Fix | Delete
*/
[371] Fix | Delete
function has_block( $block_name, $post = null ) {
[372] Fix | Delete
if ( ! has_blocks( $post ) ) {
[373] Fix | Delete
return false;
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
if ( ! is_string( $post ) ) {
[377] Fix | Delete
$wp_post = get_post( $post );
[378] Fix | Delete
if ( $wp_post instanceof WP_Post ) {
[379] Fix | Delete
$post = $wp_post->post_content;
[380] Fix | Delete
}
[381] Fix | Delete
}
[382] Fix | Delete
[383] Fix | Delete
/*
[384] Fix | Delete
* Normalize block name to include namespace, if provided as non-namespaced.
[385] Fix | Delete
* This matches behavior for WordPress 5.0.0 - 5.3.0 in matching blocks by
[386] Fix | Delete
* their serialized names.
[387] Fix | Delete
*/
[388] Fix | Delete
if ( false === strpos( $block_name, '/' ) ) {
[389] Fix | Delete
$block_name = 'core/' . $block_name;
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
// Test for existence of block by its fully qualified name.
[393] Fix | Delete
$has_block = false !== strpos( $post, '<!-- wp:' . $block_name . ' ' );
[394] Fix | Delete
[395] Fix | Delete
if ( ! $has_block ) {
[396] Fix | Delete
/*
[397] Fix | Delete
* If the given block name would serialize to a different name, test for
[398] Fix | Delete
* existence by the serialized form.
[399] Fix | Delete
*/
[400] Fix | Delete
$serialized_block_name = strip_core_block_namespace( $block_name );
[401] Fix | Delete
if ( $serialized_block_name !== $block_name ) {
[402] Fix | Delete
$has_block = false !== strpos( $post, '<!-- wp:' . $serialized_block_name . ' ' );
[403] Fix | Delete
}
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
return $has_block;
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
/**
[410] Fix | Delete
* Returns an array of the names of all registered dynamic block types.
[411] Fix | Delete
*
[412] Fix | Delete
* @since 5.0.0
[413] Fix | Delete
*
[414] Fix | Delete
* @return string[] Array of dynamic block names.
[415] Fix | Delete
*/
[416] Fix | Delete
function get_dynamic_block_names() {
[417] Fix | Delete
$dynamic_block_names = array();
[418] Fix | Delete
[419] Fix | Delete
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
[420] Fix | Delete
foreach ( $block_types as $block_type ) {
[421] Fix | Delete
if ( $block_type->is_dynamic() ) {
[422] Fix | Delete
$dynamic_block_names[] = $block_type->name;
[423] Fix | Delete
}
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
return $dynamic_block_names;
[427] Fix | Delete
}
[428] Fix | Delete
[429] Fix | Delete
/**
[430] Fix | Delete
* Given an array of attributes, returns a string in the serialized attributes
[431] Fix | Delete
* format prepared for post content.
[432] Fix | Delete
*
[433] Fix | Delete
* The serialized result is a JSON-encoded string, with unicode escape sequence
[434] Fix | Delete
* substitution for characters which might otherwise interfere with embedding
[435] Fix | Delete
* the result in an HTML comment.
[436] Fix | Delete
*
[437] Fix | Delete
* @since 5.3.1
[438] Fix | Delete
*
[439] Fix | Delete
* @param array $block_attributes Attributes object.
[440] Fix | Delete
* @return string Serialized attributes.
[441] Fix | Delete
*/
[442] Fix | Delete
function serialize_block_attributes( $block_attributes ) {
[443] Fix | Delete
$encoded_attributes = json_encode( $block_attributes );
[444] Fix | Delete
$encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes );
[445] Fix | Delete
$encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes );
[446] Fix | Delete
$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes );
[447] Fix | Delete
$encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes );
[448] Fix | Delete
// Regex: /\\"/
[449] Fix | Delete
$encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes );
[450] Fix | Delete
[451] Fix | Delete
return $encoded_attributes;
[452] Fix | Delete
}
[453] Fix | Delete
[454] Fix | Delete
/**
[455] Fix | Delete
* Returns the block name to use for serialization. This will remove the default
[456] Fix | Delete
* "core/" namespace from a block name.
[457] Fix | Delete
*
[458] Fix | Delete
* @since 5.3.1
[459] Fix | Delete
*
[460] Fix | Delete
* @param string $block_name Original block name.
[461] Fix | Delete
* @return string Block name to use for serialization.
[462] Fix | Delete
*/
[463] Fix | Delete
function strip_core_block_namespace( $block_name = null ) {
[464] Fix | Delete
if ( is_string( $block_name ) && 0 === strpos( $block_name, 'core/' ) ) {
[465] Fix | Delete
return substr( $block_name, 5 );
[466] Fix | Delete
}
[467] Fix | Delete
[468] Fix | Delete
return $block_name;
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
/**
[472] Fix | Delete
* Returns the content of a block, including comment delimiters.
[473] Fix | Delete
*
[474] Fix | Delete
* @since 5.3.1
[475] Fix | Delete
*
[476] Fix | Delete
* @param string|null $block_name Block name. Null if the block name is unknown,
[477] Fix | Delete
* e.g. Classic blocks have their name set to null.
[478] Fix | Delete
* @param array $block_attributes Block attributes.
[479] Fix | Delete
* @param string $block_content Block save content.
[480] Fix | Delete
* @return string Comment-delimited block content.
[481] Fix | Delete
*/
[482] Fix | Delete
function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) {
[483] Fix | Delete
if ( is_null( $block_name ) ) {
[484] Fix | Delete
return $block_content;
[485] Fix | Delete
}
[486] Fix | Delete
[487] Fix | Delete
$serialized_block_name = strip_core_block_namespace( $block_name );
[488] Fix | Delete
$serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' ';
[489] Fix | Delete
[490] Fix | Delete
if ( empty( $block_content ) ) {
[491] Fix | Delete
return sprintf( '<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes );
[492] Fix | Delete
}
[493] Fix | Delete
[494] Fix | Delete
return sprintf(
[495] Fix | Delete
'<!-- wp:%s %s-->%s<!-- /wp:%s -->',
[496] Fix | Delete
$serialized_block_name,
[497] Fix | Delete
$serialized_attributes,
[498] Fix | Delete
$block_content,
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function