Formatting: new filter safecss_filter_attr_allow_css on css parts.

Enables developers to determine whether a section of CSS should be allowed or discarded. By default, the value will be false if the part contains \ ( & } = or comments. Returning true allows the CSS part to be included in the output.

Replaces the `safe_style_disallowed_chars` filter introduced in r47891.

Props azaozz.
Fixes #37134.



git-svn-id: https://develop.svn.wordpress.org/trunk@48086 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2020-06-18 20:59:43 +00:00
parent 901d9c69f9
commit a1fd329682
2 changed files with 24 additions and 25 deletions

View File

@@ -2357,23 +2357,29 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
}
if ( $found ) {
// Check for any CSS containing \ ( & } = or comments, except for url() usage checked above.
$allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string );
/**
* Filters the regex limiting the list of characters not allowed in CSS rules.
* Filters the check for unsafe CSS in `safecss_filter_attr`.
*
* Default behaviour is to remove any CSS containing \ ( & } = or comments,
* except for url() usage.
* Enables developers to determine whether a section of CSS should be allowed or discarded.
* By default, the value will be false if the part contains \ ( & } = or comments.
* Return true to allow the CSS part to be included in the output.
*
* @since 5.5.0
*
* @param string $regex Regex pattern of disallowed characters in CSS rules.
* Default is '%[\\\(&=}]|/\*%'.
* @param string $css_test_string CSS value to test.
* @param bool $allow_css Whether the CSS in the test string is considered safe.
* @param string $css_test_string The css string to test.
*/
$disallowed_chars = apply_filters( 'safe_style_disallowed_chars', '%[\\\(&=}]|/\*%', $css_test_string );
if ( ! preg_match( $disallowed_chars, $css_test_string ) ) {
$allow_css = apply_filters( 'safecss_filter_attr_allow_css', $allow_css, $css_test_string );
// Only add the css part if it passes the regex check.
if ( $allow_css ) {
if ( '' !== $css ) {
$css .= ';';
}
$css .= $css_item;
}
}