diff --git a/wp-includes/kses.php b/wp-includes/kses.php index 20790d1c44..3da5bf2624 100644 --- a/wp-includes/kses.php +++ b/wp-includes/kses.php @@ -1076,6 +1076,7 @@ function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) { * Determines whether an attribute is allowed. * * @since 4.2.3 + * @since 5.0.0 Add support for `data-*` wildcard attributes. * * @param string $name The attribute name. Passed by reference. Returns empty string when not allowed. * @param string $value The attribute value. Passed by reference. Returns a filtered value. @@ -1090,8 +1091,26 @@ function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowe $name_low = strtolower( $name ); if ( ! isset( $allowed_attr[ $name_low ] ) || '' == $allowed_attr[ $name_low ] ) { - $name = $value = $whole = ''; - return false; + /* + * Allow `data-*` attributes. + * + * When specifying `$allowed_html`, the attribute name should be set as + * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see + * https://www.w3.org/TR/html40/struct/objects.html#adef-data). + * + * Note: the attribute name should only contain `A-Za-z0-9_-` chars, + * double hyphens `--` are not accepted by WordPress. + */ + if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] ) && preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match ) ) { + /* + * Add the whole attribute name to the allowed attributes and set any restrictions + * for the `data-*` attribute values for the current element. + */ + $allowed_attr[ $match[0] ] = $allowed_attr['data-*']; + } else { + $name = $value = $whole = ''; + return false; + } } if ( 'style' == $name_low ) { @@ -2091,6 +2110,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) { * Helper function to add global attributes to a tag in the allowed html list. * * @since 3.5.0 + * @since 5.0.0 Add support for `data-*` wildcard attributes. * @access private * @ignore * @@ -2099,11 +2119,12 @@ function safecss_filter_attr( $css, $deprecated = '' ) { */ function _wp_add_global_attributes( $value ) { $global_attributes = array( - 'class' => true, - 'id' => true, - 'style' => true, - 'title' => true, - 'role' => true, + 'class' => true, + 'id' => true, + 'style' => true, + 'title' => true, + 'role' => true, + 'data-*' => true, ); if ( true === $value ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 2b02a833c7..6f385b30fa 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-43980'; +$wp_version = '5.1-alpha-43981'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.