` β `
`, `href='x'` β
* `href="x"`, `text-align: center;` β `text-align: center`: same page, nothing lost. A tag, an
* attribute, visible text or a comment that changes is a real change.
*/
final class Html {
/** Elements that run code, embed other documents or take input: never added through the bridge. */
const ACTIVE_TAGS = 'script|iframe|frame|frameset|object|embed|applet|form|input|button|textarea|select|style|link|meta|base|svg|math|template|portal';
/**
* Markup that $new adds compared with $old which the bridge's own allow-list filter would remove
* β whatever the connected user's role. WordPress lets Editors and Administrators save any HTML
* (unfiltered_html); through the bridge nobody adds scripts, embeds, forms, event handlers or
* script links, so a stolen token can't plant code. Markup already in the field (a map iframe)
* may stay exactly as it is.
* There is no switch that turns this off: not a setting, not a filter.
*
* Allow-list, not a pattern list: each value goes through wp_kses() with the post tags minus
* every active element, and only http, https, mailto and tel links. What that filter removes is
* read with the browser-compatible tag processor (tags()); the new value may only lose what the
* current value already loses, each item no more often.
*
* @param mixed $new A string, or an array of strings (ACF repeaters, links).
* @param mixed $old The field's current value, same shape.
* @return string[] What the filter would remove from $new that isn't already in $old.
*/
public static function unsafe_additions( $new, $old ) {
if ( $new === $old ) {
return array();
}
$left = array();
foreach ( self::strings( $old ) as $string ) {
$left = array_merge( $left, self::filtered_out( $string ) );
}
$added = array();
foreach ( self::strings( $new ) as $string ) {
foreach ( self::filtered_out( $string ) as $item ) {
$i = array_search( $item, $left, true );
if ( false === $i ) {
$added[] = $item;
} else {
unset( $left[ $i ] ); // already there, unchanged: may stay (each copy once)
}
}
}
return array_values( array_unique( $added ) );
}
/** The bridge's allow-list filter: post markup minus anything active; http(s), mailto, tel links only. */
public static function bridge_kses( $html ) {
return wp_kses( (string) $html, self::allowed_html(), array( 'http', 'https', 'mailto', 'tel' ) );
}
private static function allowed_html() {
static $allowed = null;
if ( null === $allowed ) {
$allowed = wp_kses_allowed_html( 'post' );
foreach ( explode( '|', self::ACTIVE_TAGS ) as $tag ) {
unset( $allowed[ $tag ] );
}
foreach ( $allowed as $tag => $attrs ) {
foreach ( array_keys( is_array( $attrs ) ? $attrs : array() ) as $attr ) {
if ( 0 === strpos( $attr, 'on' ) || in_array( $attr, array( 'formaction', 'srcdoc' ), true ) ) {
unset( $allowed[ $tag ][ $attr ] );
}
}
}
// Harmless markup pasted from live pages (responsive images).
$allowed['img'] = array_merge( isset( $allowed['img'] ) ? $allowed['img'] : array(), array_fill_keys( array( 'decoding', 'srcset', 'sizes', 'fetchpriority', 'loading' ), true ) );
$allowed['picture'] = array();
$allowed['source'] = array_fill_keys( array( 'srcset', 'sizes', 'type', 'media', 'width', 'height' ), true );
}
return $allowed;
}
/** Elements whose content is raw text to the browser: their content is part of what must stay unchanged. */
const RAW_TEXT = 'script|style|iframe|textarea|title|xmp|noscript|noembed|noframes|plaintext|template';
/** Per-request memo: md5 of a value => what the filter takes out of it. */
private static $filtered = array();
/**
* What bridge_kses() would take out of $html, as comparable items:
* - tags (with attributes, as a browser reads them) the filter changes or drops;
* - raw-text elements (script, style, iframeβ¦) with their whole content β swapping the code inside
* an existing