| 1 |
<?php |
| 2 |
/** |
| 3 |
* Compatibility shims for KSES (content filtering) for WordPress 7.1. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Adds SVG presentation attributes to the list of safe CSS properties. |
| 10 |
* |
| 11 |
* The {@see safecss_filter_attr()} function only keeps allowlisted CSS properties, so SVG |
| 12 |
* presentation attributes such as `fill` are stripped by default. This allows |
| 13 |
* the SVG-specific ones. |
| 14 |
* |
| 15 |
* @param string[] $attr Array of allowed CSS attributes. |
| 16 |
* @return string[] Modified array of allowed CSS attributes. |
| 17 |
*/ |
| 18 |
function gutenberg_add_svg_to_safe_style_css( array $attr ): array { |
| 19 |
$svg_properties = array( |
| 20 |
// Fill. |
| 21 |
'fill', |
| 22 |
'fill-opacity', |
| 23 |
'fill-rule', |
| 24 |
|
| 25 |
// Stroke. |
| 26 |
'stroke', |
| 27 |
'stroke-dasharray', |
| 28 |
'stroke-dashoffset', |
| 29 |
'stroke-linecap', |
| 30 |
'stroke-linejoin', |
| 31 |
'stroke-miterlimit', |
| 32 |
'stroke-opacity', |
| 33 |
'stroke-width', |
| 34 |
|
| 35 |
// Paint. |
| 36 |
'color-interpolation', |
| 37 |
'color-interpolation-filters', |
| 38 |
'paint-order', |
| 39 |
'stop-color', |
| 40 |
'stop-opacity', |
| 41 |
'flood-color', |
| 42 |
'flood-opacity', |
| 43 |
'lighting-color', |
| 44 |
|
| 45 |
// Markers. |
| 46 |
'marker', |
| 47 |
'marker-end', |
| 48 |
'marker-mid', |
| 49 |
'marker-start', |
| 50 |
|
| 51 |
// Clipping and masking. |
| 52 |
'clip-path', |
| 53 |
'clip-rule', |
| 54 |
'mask', |
| 55 |
'mask-type', |
| 56 |
|
| 57 |
// Geometry. |
| 58 |
'cx', |
| 59 |
'cy', |
| 60 |
'r', |
| 61 |
'rx', |
| 62 |
'ry', |
| 63 |
'x', |
| 64 |
'y', |
| 65 |
'd', |
| 66 |
|
| 67 |
// Text. |
| 68 |
'alignment-baseline', |
| 69 |
'baseline-shift', |
| 70 |
'dominant-baseline', |
| 71 |
'glyph-orientation-horizontal', |
| 72 |
'glyph-orientation-vertical', |
| 73 |
'text-anchor', |
| 74 |
'unicode-bidi', |
| 75 |
'word-spacing', |
| 76 |
|
| 77 |
// Font. |
| 78 |
'font-size-adjust', |
| 79 |
'font-stretch', |
| 80 |
|
| 81 |
// Rendering. |
| 82 |
'color-rendering', |
| 83 |
'image-rendering', |
| 84 |
'shape-rendering', |
| 85 |
'text-rendering', |
| 86 |
'vector-effect', |
| 87 |
|
| 88 |
// Transforms. |
| 89 |
'transform', |
| 90 |
'transform-origin', |
| 91 |
|
| 92 |
// Interactivity and visibility. |
| 93 |
'pointer-events', |
| 94 |
'visibility', |
| 95 |
); |
| 96 |
|
| 97 |
return array_unique( array_merge( $attr, $svg_properties ) ); |
| 98 |
} |
| 99 |
add_filter( 'safe_style_css', 'gutenberg_add_svg_to_safe_style_css' ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Allow gradient background-image values, including gradients combined with a |
| 103 |
* url() image, in inline styles. |
| 104 |
* |
| 105 |
* Without this, {@see safecss_filter_attr()} strips gradients that use functions |
| 106 |
* beyond rgb()/rgba(), or that are combined with a url() image. This removes each |
| 107 |
* gradient from the test string and re-checks the remainder, so those values |
| 108 |
* survive sanitization. |
| 109 |
* |
| 110 |
* @param bool $allow_css Whether the CSS is allowed. |
| 111 |
* @param string $css_test_string The CSS declaration to test. |
| 112 |
* @return bool Whether the CSS is allowed. |
| 113 |
*/ |
| 114 |
function gutenberg_allow_extended_gradient_backgrounds( $allow_css, $css_test_string ) { |
| 115 |
if ( $allow_css ) { |
| 116 |
return $allow_css; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! preg_match( '/^background-image\s*:/', $css_test_string ) ) { |
| 120 |
return $allow_css; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* Remove each gradient (allowing one level of nested functions such as |
| 125 |
* rgb(), hsl(), or calc()) and re-test. Any url() has already been removed |
| 126 |
* and protocol-checked by safecss_filter_attr() before this filter runs. |
| 127 |
*/ |
| 128 |
$stripped = preg_replace( '/(?:repeating-)?(?:linear|radial|conic)-gradient\((?:[^()]|\([^()]*\))*\)/', '', $css_test_string ); |
| 129 |
|
| 130 |
if ( ! preg_match( '%[\\\(&=}]|/\*%', $stripped ) ) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
return $allow_css; |
| 135 |
} |
| 136 |
|
| 137 |
add_filter( 'safecss_filter_attr_allow_css', 'gutenberg_allow_extended_gradient_backgrounds', 10, 2 ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Allows the `tabindex` attribute on elements that support global attributes. |
| 141 |
* |
| 142 |
* `tabindex` is a global HTML attribute, but KSES strips it from post content. |
| 143 |
* The Tab Panel block saves it to keep the panel focusable. |
| 144 |
* |
| 145 |
* @param array[] $tags Array of allowed HTML tags and their allowed attributes. |
| 146 |
* @return array[] Modified array of allowed HTML tags. |
| 147 |
*/ |
| 148 |
function gutenberg_add_tabindex_to_kses_allowed_html( $tags ) { |
| 149 |
if ( ! is_array( $tags ) ) { |
| 150 |
return $tags; |
| 151 |
} |
| 152 |
|
| 153 |
foreach ( $tags as $tag => $attributes ) { |
| 154 |
if ( is_array( $attributes ) ) { |
| 155 |
$tags[ $tag ]['tabindex'] = true; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return $tags; |
| 160 |
} |
| 161 |
add_filter( 'wp_kses_allowed_html', 'gutenberg_add_tabindex_to_kses_allowed_html' ); |
| 162 |
|