Index: /branches/5.0/src/wp-includes/kses.php
===================================================================
--- /branches/5.0/src/wp-includes/kses.php (revision 43726)
+++ /branches/5.0/src/wp-includes/kses.php (revision 43727)
@@ -855,4 +855,5 @@
*
* @since 4.2.3
+ * @since 5.0.0 Add support for `data-*` wildcard attributes.
*
* @param string $name The attribute name. Returns empty string when not allowed.
@@ -865,10 +866,29 @@
*/
function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
- $allowed_attr = $allowed_html[strtolower( $element )];
+ $allowed_attr = $allowed_html[ strtolower( $element ) ];
$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;
+ }
}
@@ -885,5 +905,5 @@
}
- if ( is_array( $allowed_attr[$name_low] ) ) {
+ if ( is_array( $allowed_attr[ $name_low ] ) ) {
// there are some checks
foreach ( $allowed_attr[$name_low] as $currkey => $currval ) {
@@ -1809,4 +1829,5 @@
*
* @since 3.5.0
+ * @since 5.0.0 Add support for `data-*` wildcard attributes.
* @access private
*
@@ -1821,4 +1842,5 @@
'title' => true,
'role' => true,
+ 'data-*' => true,
);
Index: /branches/5.0/tests/phpunit/tests/kses.php
===================================================================
--- /branches/5.0/tests/phpunit/tests/kses.php (revision 43726)
+++ /branches/5.0/tests/phpunit/tests/kses.php (revision 43727)
@@ -719,3 +719,98 @@
$this->assertEquals( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => false ), array() ) );
}
+
+ /**
+ * Data attributes are globally accepted.
+ *
+ * @ticket 33121
+ */
+ function test_wp_kses_attr_data_attribute_is_allowed() {
+ $test = '
Pens and pencils
';
+ $expected = 'Pens and pencils
';
+
+ $this->assertEquals( $expected, wp_kses_post( $test ) );
+ }
+
+ /**
+ * Ensure wildcard attributes block unprefixed wildcard uses.
+ *
+ * @ticket 33121
+ */
+ function test_wildcard_requires_hyphen_after_prefix() {
+ $allowed_html = array(
+ 'div' => array(
+ 'data-*' => true,
+ 'on-*' => true,
+ ),
+ );
+
+ $string = 'Malformed attributes
';
+ $expected = 'Malformed attributes
';
+
+ $actual = wp_kses( $string, $allowed_html );
+
+ $this->assertSame( $expected, $actual );
+ }
+
+ /**
+ * Ensure wildcard allows two hyphen.
+ *
+ * @ticket 33121
+ */
+ function test_wildcard_allows_two_hyphens() {
+ $allowed_html = array(
+ 'div' => array(
+ 'data-*' => true,
+ ),
+ );
+
+ $string = 'Well formed attribute
';
+ $expected = 'Well formed attribute
';
+
+ $actual = wp_kses( $string, $allowed_html );
+
+ $this->assertSame( $expected, $actual );
+ }
+
+ /**
+ * Ensure wildcard attributes only support valid prefixes.
+ *
+ * @dataProvider data_wildcard_attribute_prefixes
+ *
+ * @ticket 33121
+ */
+ function test_wildcard_attribute_prefixes( $wildcard_attribute, $expected ) {
+ $allowed_html = array(
+ 'div' => array(
+ $wildcard_attribute => true,
+ ),
+ );
+
+ $name = str_replace( '*', strtolower( __FUNCTION__ ), $wildcard_attribute );
+ $value = __FUNCTION__;
+ $whole = "{$name}=\"{$value}\"";
+
+ $actual = wp_kses_attr_check( $name, $value, $whole, 'n', 'div', $allowed_html );
+
+ $this->assertSame( $expected, $actual );
+ }
+
+ /**
+ * @return array Array of arguments for wildcard testing
+ * [0] The prefix being tested.
+ * [1] The outcome of `wp_kses_attr_check` for the prefix.
+ */
+ function data_wildcard_attribute_prefixes() {
+ return array(
+ // Ends correctly
+ array( 'data-*', true ),
+
+ // Does not end with trialing `-`.
+ array( 'data*', false ),
+
+ // Multiple wildcards.
+ array( 'd*ta-*', false ),
+ array( 'data**', false ),
+ );
+ }
}
Index: /branches/5.0/tests/phpunit/tests/rest-api/rest-attachments-controller.php
===================================================================
--- /branches/5.0/tests/phpunit/tests/rest-api/rest-attachments-controller.php (revision 43726)
+++ /branches/5.0/tests/phpunit/tests/rest-api/rest-attachments-controller.php (revision 43727)
@@ -929,7 +929,7 @@
// Raw values.
array(
- 'title' => 'link',
- 'description' => 'link',
- 'caption' => 'link',
+ 'title' => 'link',
+ 'description' => 'link',
+ 'caption' => 'link',
),
// Expected returned values.
Index: /branches/5.0/tests/phpunit/tests/rest-api/rest-posts-controller.php
===================================================================
--- /branches/5.0/tests/phpunit/tests/rest-api/rest-posts-controller.php (revision 43726)
+++ /branches/5.0/tests/phpunit/tests/rest-api/rest-posts-controller.php (revision 43727)
@@ -2914,7 +2914,7 @@
// Raw values.
array(
- 'title' => 'link',
- 'content' => 'link',
- 'excerpt' => 'link',
+ 'title' => 'link',
+ 'content' => 'link',
+ 'excerpt' => 'link',
),
// Expected returned values.