Edit File by line
/home/barbar84/www/wp-inclu...
File: blocks.php
$serialized_block_name
[500] Fix | Delete
);
[501] Fix | Delete
}
[502] Fix | Delete
[503] Fix | Delete
/**
[504] Fix | Delete
* Returns the content of a block, including comment delimiters, serializing all
[505] Fix | Delete
* attributes from the given parsed block.
[506] Fix | Delete
*
[507] Fix | Delete
* This should be used when preparing a block to be saved to post content.
[508] Fix | Delete
* Prefer `render_block` when preparing a block for display. Unlike
[509] Fix | Delete
* `render_block`, this does not evaluate a block's `render_callback`, and will
[510] Fix | Delete
* instead preserve the markup as parsed.
[511] Fix | Delete
*
[512] Fix | Delete
* @since 5.3.1
[513] Fix | Delete
*
[514] Fix | Delete
* @param WP_Block_Parser_Block $block A single parsed block object.
[515] Fix | Delete
* @return string String of rendered HTML.
[516] Fix | Delete
*/
[517] Fix | Delete
function serialize_block( $block ) {
[518] Fix | Delete
$block_content = '';
[519] Fix | Delete
[520] Fix | Delete
$index = 0;
[521] Fix | Delete
foreach ( $block['innerContent'] as $chunk ) {
[522] Fix | Delete
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
[523] Fix | Delete
}
[524] Fix | Delete
[525] Fix | Delete
if ( ! is_array( $block['attrs'] ) ) {
[526] Fix | Delete
$block['attrs'] = array();
[527] Fix | Delete
}
[528] Fix | Delete
[529] Fix | Delete
return get_comment_delimited_block_content(
[530] Fix | Delete
$block['blockName'],
[531] Fix | Delete
$block['attrs'],
[532] Fix | Delete
$block_content
[533] Fix | Delete
);
[534] Fix | Delete
}
[535] Fix | Delete
[536] Fix | Delete
/**
[537] Fix | Delete
* Returns a joined string of the aggregate serialization of the given parsed
[538] Fix | Delete
* blocks.
[539] Fix | Delete
*
[540] Fix | Delete
* @since 5.3.1
[541] Fix | Delete
*
[542] Fix | Delete
* @param WP_Block_Parser_Block[] $blocks Parsed block objects.
[543] Fix | Delete
* @return string String of rendered HTML.
[544] Fix | Delete
*/
[545] Fix | Delete
function serialize_blocks( $blocks ) {
[546] Fix | Delete
return implode( '', array_map( 'serialize_block', $blocks ) );
[547] Fix | Delete
}
[548] Fix | Delete
[549] Fix | Delete
/**
[550] Fix | Delete
* Filters and sanitizes block content to remove non-allowable HTML from
[551] Fix | Delete
* parsed block attribute values.
[552] Fix | Delete
*
[553] Fix | Delete
* @since 5.3.1
[554] Fix | Delete
*
[555] Fix | Delete
* @param string $text Text that may contain block content.
[556] Fix | Delete
* @param array[]|string $allowed_html An array of allowed HTML elements
[557] Fix | Delete
* and attributes, or a context name
[558] Fix | Delete
* such as 'post'.
[559] Fix | Delete
* @param string[] $allowed_protocols Array of allowed URL protocols.
[560] Fix | Delete
* @return string The filtered and sanitized content result.
[561] Fix | Delete
*/
[562] Fix | Delete
function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) {
[563] Fix | Delete
$result = '';
[564] Fix | Delete
[565] Fix | Delete
if ( false !== strpos( $text, '<!--' ) && false !== strpos( $text, '--->' ) ) {
[566] Fix | Delete
$text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
[567] Fix | Delete
}
[568] Fix | Delete
[569] Fix | Delete
$blocks = parse_blocks( $text );
[570] Fix | Delete
foreach ( $blocks as $block ) {
[571] Fix | Delete
$block = filter_block_kses( $block, $allowed_html, $allowed_protocols );
[572] Fix | Delete
$result .= serialize_block( $block );
[573] Fix | Delete
}
[574] Fix | Delete
[575] Fix | Delete
return $result;
[576] Fix | Delete
}
[577] Fix | Delete
[578] Fix | Delete
/**
[579] Fix | Delete
* Callback used for regular expression replacement in filter_block_content().
[580] Fix | Delete
*
[581] Fix | Delete
* @private
[582] Fix | Delete
* @since 6.2.1
[583] Fix | Delete
*
[584] Fix | Delete
* @param array $matches Array of preg_replace_callback matches.
[585] Fix | Delete
* @return string Replacement string.
[586] Fix | Delete
*/
[587] Fix | Delete
function _filter_block_content_callback( $matches ) {
[588] Fix | Delete
return '<!--' . rtrim( $matches[1], '-' ) . '-->';
[589] Fix | Delete
}
[590] Fix | Delete
[591] Fix | Delete
/**
[592] Fix | Delete
* Filters and sanitizes a parsed block to remove non-allowable HTML from block
[593] Fix | Delete
* attribute values.
[594] Fix | Delete
*
[595] Fix | Delete
* @since 5.3.1
[596] Fix | Delete
*
[597] Fix | Delete
* @param WP_Block_Parser_Block $block The parsed block object.
[598] Fix | Delete
* @param array[]|string $allowed_html An array of allowed HTML
[599] Fix | Delete
* elements and attributes, or a
[600] Fix | Delete
* context name such as 'post'.
[601] Fix | Delete
* @param string[] $allowed_protocols Allowed URL protocols.
[602] Fix | Delete
* @return array The filtered and sanitized block object result.
[603] Fix | Delete
*/
[604] Fix | Delete
function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) {
[605] Fix | Delete
$block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols );
[606] Fix | Delete
[607] Fix | Delete
if ( is_array( $block['innerBlocks'] ) ) {
[608] Fix | Delete
foreach ( $block['innerBlocks'] as $i => $inner_block ) {
[609] Fix | Delete
$block['innerBlocks'][ $i ] = filter_block_kses( $inner_block, $allowed_html, $allowed_protocols );
[610] Fix | Delete
}
[611] Fix | Delete
}
[612] Fix | Delete
[613] Fix | Delete
return $block;
[614] Fix | Delete
}
[615] Fix | Delete
[616] Fix | Delete
/**
[617] Fix | Delete
* Filters and sanitizes a parsed block attribute value to remove non-allowable
[618] Fix | Delete
* HTML.
[619] Fix | Delete
*
[620] Fix | Delete
* @since 5.3.1
[621] Fix | Delete
*
[622] Fix | Delete
* @param string[]|string $value The attribute value to filter.
[623] Fix | Delete
* @param array[]|string $allowed_html An array of allowed HTML elements
[624] Fix | Delete
* and attributes, or a context name
[625] Fix | Delete
* such as 'post'.
[626] Fix | Delete
* @param string[] $allowed_protocols Array of allowed URL protocols.
[627] Fix | Delete
* @return string[]|string The filtered and sanitized result.
[628] Fix | Delete
*/
[629] Fix | Delete
function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array() ) {
[630] Fix | Delete
if ( is_array( $value ) ) {
[631] Fix | Delete
foreach ( $value as $key => $inner_value ) {
[632] Fix | Delete
$filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols );
[633] Fix | Delete
$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols );
[634] Fix | Delete
[635] Fix | Delete
if ( $filtered_key !== $key ) {
[636] Fix | Delete
unset( $value[ $key ] );
[637] Fix | Delete
}
[638] Fix | Delete
[639] Fix | Delete
$value[ $filtered_key ] = $filtered_value;
[640] Fix | Delete
}
[641] Fix | Delete
} elseif ( is_string( $value ) ) {
[642] Fix | Delete
return wp_kses( $value, $allowed_html, $allowed_protocols );
[643] Fix | Delete
}
[644] Fix | Delete
[645] Fix | Delete
return $value;
[646] Fix | Delete
}
[647] Fix | Delete
[648] Fix | Delete
/**
[649] Fix | Delete
* Parses blocks out of a content string, and renders those appropriate for the excerpt.
[650] Fix | Delete
*
[651] Fix | Delete
* As the excerpt should be a small string of text relevant to the full post content,
[652] Fix | Delete
* this function renders the blocks that are most likely to contain such text.
[653] Fix | Delete
*
[654] Fix | Delete
* @since 5.0.0
[655] Fix | Delete
*
[656] Fix | Delete
* @param string $content The content to parse.
[657] Fix | Delete
* @return string The parsed and filtered content.
[658] Fix | Delete
*/
[659] Fix | Delete
function excerpt_remove_blocks( $content ) {
[660] Fix | Delete
$allowed_inner_blocks = array(
[661] Fix | Delete
// Classic blocks have their blockName set to null.
[662] Fix | Delete
null,
[663] Fix | Delete
'core/freeform',
[664] Fix | Delete
'core/heading',
[665] Fix | Delete
'core/html',
[666] Fix | Delete
'core/list',
[667] Fix | Delete
'core/media-text',
[668] Fix | Delete
'core/paragraph',
[669] Fix | Delete
'core/preformatted',
[670] Fix | Delete
'core/pullquote',
[671] Fix | Delete
'core/quote',
[672] Fix | Delete
'core/table',
[673] Fix | Delete
'core/verse',
[674] Fix | Delete
);
[675] Fix | Delete
[676] Fix | Delete
$allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) );
[677] Fix | Delete
[678] Fix | Delete
/**
[679] Fix | Delete
* Filters the list of blocks that can contribute to the excerpt.
[680] Fix | Delete
*
[681] Fix | Delete
* If a dynamic block is added to this list, it must not generate another
[682] Fix | Delete
* excerpt, as this will cause an infinite loop to occur.
[683] Fix | Delete
*
[684] Fix | Delete
* @since 5.0.0
[685] Fix | Delete
*
[686] Fix | Delete
* @param array $allowed_blocks The list of allowed blocks.
[687] Fix | Delete
*/
[688] Fix | Delete
$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
[689] Fix | Delete
$blocks = parse_blocks( $content );
[690] Fix | Delete
$output = '';
[691] Fix | Delete
[692] Fix | Delete
foreach ( $blocks as $block ) {
[693] Fix | Delete
if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
[694] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[695] Fix | Delete
if ( 'core/columns' === $block['blockName'] ) {
[696] Fix | Delete
$output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks );
[697] Fix | Delete
continue;
[698] Fix | Delete
}
[699] Fix | Delete
[700] Fix | Delete
// Skip the block if it has disallowed or nested inner blocks.
[701] Fix | Delete
foreach ( $block['innerBlocks'] as $inner_block ) {
[702] Fix | Delete
if (
[703] Fix | Delete
! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
[704] Fix | Delete
! empty( $inner_block['innerBlocks'] )
[705] Fix | Delete
) {
[706] Fix | Delete
continue 2;
[707] Fix | Delete
}
[708] Fix | Delete
}
[709] Fix | Delete
}
[710] Fix | Delete
[711] Fix | Delete
$output .= render_block( $block );
[712] Fix | Delete
}
[713] Fix | Delete
}
[714] Fix | Delete
[715] Fix | Delete
return $output;
[716] Fix | Delete
}
[717] Fix | Delete
[718] Fix | Delete
/**
[719] Fix | Delete
* Render inner blocks from the `core/columns` block for generating an excerpt.
[720] Fix | Delete
*
[721] Fix | Delete
* @since 5.2.0
[722] Fix | Delete
* @access private
[723] Fix | Delete
*
[724] Fix | Delete
* @param array $columns The parsed columns block.
[725] Fix | Delete
* @param array $allowed_blocks The list of allowed inner blocks.
[726] Fix | Delete
* @return string The rendered inner blocks.
[727] Fix | Delete
*/
[728] Fix | Delete
function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
[729] Fix | Delete
$output = '';
[730] Fix | Delete
[731] Fix | Delete
foreach ( $columns['innerBlocks'] as $column ) {
[732] Fix | Delete
foreach ( $column['innerBlocks'] as $inner_block ) {
[733] Fix | Delete
if ( in_array( $inner_block['blockName'], $allowed_blocks, true ) && empty( $inner_block['innerBlocks'] ) ) {
[734] Fix | Delete
$output .= render_block( $inner_block );
[735] Fix | Delete
}
[736] Fix | Delete
}
[737] Fix | Delete
}
[738] Fix | Delete
[739] Fix | Delete
return $output;
[740] Fix | Delete
}
[741] Fix | Delete
[742] Fix | Delete
/**
[743] Fix | Delete
* Renders a single block into a HTML string.
[744] Fix | Delete
*
[745] Fix | Delete
* @since 5.0.0
[746] Fix | Delete
*
[747] Fix | Delete
* @global WP_Post $post The post to edit.
[748] Fix | Delete
* @global WP_Query $wp_query WordPress Query object.
[749] Fix | Delete
*
[750] Fix | Delete
* @param array $parsed_block A single parsed block object.
[751] Fix | Delete
* @return string String of rendered HTML.
[752] Fix | Delete
*/
[753] Fix | Delete
function render_block( $parsed_block ) {
[754] Fix | Delete
global $post, $wp_query;
[755] Fix | Delete
[756] Fix | Delete
/**
[757] Fix | Delete
* Allows render_block() to be short-circuited, by returning a non-null value.
[758] Fix | Delete
*
[759] Fix | Delete
* @since 5.1.0
[760] Fix | Delete
*
[761] Fix | Delete
* @param string|null $pre_render The pre-rendered content. Default null.
[762] Fix | Delete
* @param array $parsed_block The block being rendered.
[763] Fix | Delete
*/
[764] Fix | Delete
$pre_render = apply_filters( 'pre_render_block', null, $parsed_block );
[765] Fix | Delete
if ( ! is_null( $pre_render ) ) {
[766] Fix | Delete
return $pre_render;
[767] Fix | Delete
}
[768] Fix | Delete
[769] Fix | Delete
$source_block = $parsed_block;
[770] Fix | Delete
[771] Fix | Delete
/**
[772] Fix | Delete
* Filters the block being rendered in render_block(), before it's processed.
[773] Fix | Delete
*
[774] Fix | Delete
* @since 5.1.0
[775] Fix | Delete
*
[776] Fix | Delete
* @param array $parsed_block The block being rendered.
[777] Fix | Delete
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
[778] Fix | Delete
*/
[779] Fix | Delete
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
[780] Fix | Delete
[781] Fix | Delete
$context = array();
[782] Fix | Delete
[783] Fix | Delete
if ( $post instanceof WP_Post ) {
[784] Fix | Delete
$context['postId'] = $post->ID;
[785] Fix | Delete
[786] Fix | Delete
/*
[787] Fix | Delete
* The `postType` context is largely unnecessary server-side, since the ID
[788] Fix | Delete
* is usually sufficient on its own. That being said, since a block's
[789] Fix | Delete
* manifest is expected to be shared between the server and the client,
[790] Fix | Delete
* it should be included to consistently fulfill the expectation.
[791] Fix | Delete
*/
[792] Fix | Delete
$context['postType'] = $post->post_type;
[793] Fix | Delete
}
[794] Fix | Delete
[795] Fix | Delete
if ( $wp_query instanceof WP_Query && isset( $wp_query->tax_query->queried_terms['category'] ) ) {
[796] Fix | Delete
$context['query'] = array( 'categoryIds' => array() );
[797] Fix | Delete
foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) {
[798] Fix | Delete
$context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id;
[799] Fix | Delete
}
[800] Fix | Delete
}
[801] Fix | Delete
[802] Fix | Delete
/**
[803] Fix | Delete
* Filters the default context provided to a rendered block.
[804] Fix | Delete
*
[805] Fix | Delete
* @since 5.5.0
[806] Fix | Delete
*
[807] Fix | Delete
* @param array $context Default context.
[808] Fix | Delete
* @param array $parsed_block Block being rendered, filtered by `render_block_data`.
[809] Fix | Delete
*/
[810] Fix | Delete
$context = apply_filters( 'render_block_context', $context, $parsed_block );
[811] Fix | Delete
[812] Fix | Delete
$block = new WP_Block( $parsed_block, $context );
[813] Fix | Delete
[814] Fix | Delete
return $block->render();
[815] Fix | Delete
}
[816] Fix | Delete
[817] Fix | Delete
/**
[818] Fix | Delete
* Parses blocks out of a content string.
[819] Fix | Delete
*
[820] Fix | Delete
* @since 5.0.0
[821] Fix | Delete
*
[822] Fix | Delete
* @param string $content Post content.
[823] Fix | Delete
* @return array[] Array of parsed block objects.
[824] Fix | Delete
*/
[825] Fix | Delete
function parse_blocks( $content ) {
[826] Fix | Delete
/**
[827] Fix | Delete
* Filter to allow plugins to replace the server-side block parser
[828] Fix | Delete
*
[829] Fix | Delete
* @since 5.0.0
[830] Fix | Delete
*
[831] Fix | Delete
* @param string $parser_class Name of block parser class.
[832] Fix | Delete
*/
[833] Fix | Delete
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
[834] Fix | Delete
[835] Fix | Delete
$parser = new $parser_class();
[836] Fix | Delete
return $parser->parse( $content );
[837] Fix | Delete
}
[838] Fix | Delete
[839] Fix | Delete
/**
[840] Fix | Delete
* Parses dynamic blocks out of `post_content` and re-renders them.
[841] Fix | Delete
*
[842] Fix | Delete
* @since 5.0.0
[843] Fix | Delete
*
[844] Fix | Delete
* @param string $content Post content.
[845] Fix | Delete
* @return string Updated post content.
[846] Fix | Delete
*/
[847] Fix | Delete
function do_blocks( $content ) {
[848] Fix | Delete
$blocks = parse_blocks( $content );
[849] Fix | Delete
$output = '';
[850] Fix | Delete
[851] Fix | Delete
foreach ( $blocks as $block ) {
[852] Fix | Delete
$output .= render_block( $block );
[853] Fix | Delete
}
[854] Fix | Delete
[855] Fix | Delete
// If there are blocks in this content, we shouldn't run wpautop() on it later.
[856] Fix | Delete
$priority = has_filter( 'the_content', 'wpautop' );
[857] Fix | Delete
if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
[858] Fix | Delete
remove_filter( 'the_content', 'wpautop', $priority );
[859] Fix | Delete
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
[860] Fix | Delete
}
[861] Fix | Delete
[862] Fix | Delete
return $output;
[863] Fix | Delete
}
[864] Fix | Delete
[865] Fix | Delete
/**
[866] Fix | Delete
* If do_blocks() needs to remove wpautop() from the `the_content` filter, this re-adds it afterwards,
[867] Fix | Delete
* for subsequent `the_content` usage.
[868] Fix | Delete
*
[869] Fix | Delete
* @access private
[870] Fix | Delete
*
[871] Fix | Delete
* @since 5.0.0
[872] Fix | Delete
*
[873] Fix | Delete
* @param string $content The post content running through this filter.
[874] Fix | Delete
* @return string The unmodified content.
[875] Fix | Delete
*/
[876] Fix | Delete
function _restore_wpautop_hook( $content ) {
[877] Fix | Delete
$current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );
[878] Fix | Delete
[879] Fix | Delete
add_filter( 'the_content', 'wpautop', $current_priority - 1 );
[880] Fix | Delete
remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
[881] Fix | Delete
[882] Fix | Delete
return $content;
[883] Fix | Delete
}
[884] Fix | Delete
[885] Fix | Delete
/**
[886] Fix | Delete
* Returns the current version of the block format that the content string is using.
[887] Fix | Delete
*
[888] Fix | Delete
* If the string doesn't contain blocks, it returns 0.
[889] Fix | Delete
*
[890] Fix | Delete
* @since 5.0.0
[891] Fix | Delete
*
[892] Fix | Delete
* @param string $content Content to test.
[893] Fix | Delete
* @return int The block format version is 1 if the content contains one or more blocks, 0 otherwise.
[894] Fix | Delete
*/
[895] Fix | Delete
function block_version( $content ) {
[896] Fix | Delete
return has_blocks( $content ) ? 1 : 0;
[897] Fix | Delete
}
[898] Fix | Delete
[899] Fix | Delete
/**
[900] Fix | Delete
* Registers a new block style.
[901] Fix | Delete
*
[902] Fix | Delete
* @since 5.3.0
[903] Fix | Delete
*
[904] Fix | Delete
* @param string $block_name Block type name including namespace.
[905] Fix | Delete
* @param array $style_properties Array containing the properties of the style name,
[906] Fix | Delete
* label, style (name of the stylesheet to be enqueued),
[907] Fix | Delete
* inline_style (string containing the CSS to be added).
[908] Fix | Delete
* @return bool True if the block style was registered with success and false otherwise.
[909] Fix | Delete
*/
[910] Fix | Delete
function register_block_style( $block_name, $style_properties ) {
[911] Fix | Delete
return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
[912] Fix | Delete
}
[913] Fix | Delete
[914] Fix | Delete
/**
[915] Fix | Delete
* Unregisters a block style.
[916] Fix | Delete
*
[917] Fix | Delete
* @since 5.3.0
[918] Fix | Delete
*
[919] Fix | Delete
* @param string $block_name Block type name including namespace.
[920] Fix | Delete
* @param array $block_style_name Block style name.
[921] Fix | Delete
* @return bool True if the block style was unregistered with success and false otherwise.
[922] Fix | Delete
*/
[923] Fix | Delete
function unregister_block_style( $block_name, $block_style_name ) {
[924] Fix | Delete
return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
[925] Fix | Delete
}
[926] Fix | Delete
[927] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function