Skip to content

Commit 5f269d3

Browse files
committed
Editor: Sanitize Template Part HTML tag on save.
Props xknown, peterwilsoncc, jorbin, bernhard-reiter, azaozz. git-svn-id: https://develop.svn.wordpress.org/trunk@58471 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8ee7211 commit 5f269d3

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/wp-includes/blocks.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ function _filter_block_content_callback( $matches ) {
17251725
* @return array The filtered and sanitized block object result.
17261726
*/
17271727
function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) {
1728-
$block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols );
1728+
$block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols, $block );
17291729

17301730
if ( is_array( $block['innerBlocks'] ) ) {
17311731
foreach ( $block['innerBlocks'] as $i => $inner_block ) {
@@ -1741,21 +1741,26 @@ function filter_block_kses( $block, $allowed_html, $allowed_protocols = array()
17411741
* non-allowable HTML.
17421742
*
17431743
* @since 5.3.1
1744+
* @since 6.5.5 Added the `$block_context` parameter.
17441745
*
17451746
* @param string[]|string $value The attribute value to filter.
17461747
* @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
17471748
* or a context name such as 'post'. See wp_kses_allowed_html()
17481749
* for the list of accepted context names.
17491750
* @param string[] $allowed_protocols Optional. Array of allowed URL protocols.
17501751
* Defaults to the result of wp_allowed_protocols().
1752+
* @param array $block_context Optional. The block the attribute belongs to, in parsed block array format.
17511753
* @return string[]|string The filtered and sanitized result.
17521754
*/
1753-
function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array() ) {
1755+
function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) {
17541756
if ( is_array( $value ) ) {
17551757
foreach ( $value as $key => $inner_value ) {
1756-
$filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols );
1757-
$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols );
1758+
$filtered_key = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context );
1759+
$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context );
17581760

1761+
if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) {
1762+
$filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html );
1763+
}
17591764
if ( $filtered_key !== $key ) {
17601765
unset( $value[ $key ] );
17611766
}
@@ -1769,6 +1774,29 @@ function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = ar
17691774
return $value;
17701775
}
17711776

1777+
1778+
/**
1779+
* Sanitizes the value of the Template Part block's `tagName` attribute.
1780+
*
1781+
* @since 6.5.5
1782+
*
1783+
* @param string $attribute_value The attribute value to filter.
1784+
* @param string $attribute_name The attribute name.
1785+
* @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
1786+
* or a context name such as 'post'. See wp_kses_allowed_html()
1787+
* for the list of accepted context names.
1788+
* @return string The sanitized attribute value.
1789+
*/
1790+
function filter_block_core_template_part_attributes( $attribute_value, $attribute_name, $allowed_html ) {
1791+
if ( empty( $attribute_value ) || 'tagName' !== $attribute_name ) {
1792+
return $attribute_value;
1793+
}
1794+
if ( ! is_array( $allowed_html ) ) {
1795+
$allowed_html = wp_kses_allowed_html( $allowed_html );
1796+
}
1797+
return isset( $allowed_html[ $attribute_value ] ) ? $attribute_value : '';
1798+
}
1799+
17721800
/**
17731801
* Parses blocks out of a content string, and renders those appropriate for the excerpt.
17741802
*

src/wp-includes/formatting.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4790,12 +4790,13 @@ static function ( $matches ) {
47904790
* Escapes an HTML tag name.
47914791
*
47924792
* @since 2.5.0
4793+
* @since 6.5.5 Allow hyphens in tag names (i.e. custom elements).
47934794
*
47944795
* @param string $tag_name
47954796
* @return string
47964797
*/
47974798
function tag_escape( $tag_name ) {
4798-
$safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9_:]/', '', $tag_name ) );
4799+
$safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9-_:]/', '', $tag_name ) );
47994800
/**
48004801
* Filters a string cleaned and escaped for output as an HTML tag.
48014802
*

0 commit comments

Comments
 (0)