_wp_kses_directive_binds_value_restricted_attribute()
Determines whether a directive binds an attribute whose value KSES restricts.
This only applies to elements for which KSES restricts the value of at least one attribute. On those elements, a data-wp-bind directive is reported unless it binds an attribute which KSES allows there with any value. KSES calls this to keep such a directive out of content saved by an author who cannot post unfiltered HTML.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
bool. Whether the directive binds an attribute whose value KSES restricts.
Usage
_wp_kses_directive_binds_value_restricted_attribute( $directive_name, $allowed_attributes ): bool;
- $directive_name(string) (required)
- The directive attribute name, in lowercase.
- $allowed_attributes(array|true|false) (required)
- The attributes KSES allows on the element.
Changelog
| Since 7.1.4 | Introduced. |
_wp_kses_directive_binds_value_restricted_attribute() wp kses directive binds value restricted attribute code WP 7.1.4
function _wp_kses_directive_binds_value_restricted_attribute( string $directive_name, $allowed_attributes ): bool {
if ( ! str_starts_with( $directive_name, 'data-wp-bind--' ) ) {
return false;
}
if ( ! _wp_kses_restricts_attribute_values( $allowed_attributes ) ) {
return false;
}
/*
* Both the whole remainder of the name and the part before a unique ID are checked, so
* that the result does not depend on how the unique ID is parsed. A binding with a unique
* ID is not applied at all, so removing one changes nothing.
*/
$bound_attribute = substr( $directive_name, strlen( 'data-wp-bind--' ) );
$bound_attributes = array( $bound_attribute );
$unique_id_index = strpos( $bound_attribute, '---' );
if ( false !== $unique_id_index ) {
$bound_attributes[] = substr( $bound_attribute, 0, $unique_id_index );
}
foreach ( $bound_attributes as $attribute_name ) {
if ( isset( $allowed_attributes[ $attribute_name ] ) ) {
$rules = $allowed_attributes[ $attribute_name ];
} elseif ( 1 === preg_match( '/^data-[a-z0-9_-]+$/', $attribute_name ) && isset( $allowed_attributes['data-*'] ) ) {
$rules = $allowed_attributes['data-*'];
} else {
return true;
}
if ( ! $rules || is_array( $rules ) ) {
return true;
}
}
return false;
}