` or a ``.
* @param bool $print Print it rather than return it.
*/
public static function script( string $element, bool $print = false ): string {
if ( 1 !== preg_match( '#^($#is', $element, $m ) ) {
return '';
}
$tag = new \WP_HTML_Tag_Processor( $m[1] . '' );
if ( ! $tag->next_tag( 'script' ) || null !== $tag->get_attribute( 'src' ) ) {
return '';
}
$attributes = array();
foreach ( (array) $tag->get_attribute_names_with_prefix( '' ) as $name ) {
$value = $tag->get_attribute( $name );
if ( in_array( $name, array( 'id', 'type', 'nonce' ), true ) || str_starts_with( $name, 'data-' ) ) {
$attributes[ $name ] = true === $value ? true : (string) $value;
}
}
$script = rtrim( wp_get_inline_script_tag( $m[2], $attributes ), "\n" );
if ( $print ) {
echo $script; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core's inline-script tag (wp_get_inline_script_tag()), its attributes filtered above.
return '';
}
return $script;
}
/**
* The list: WordPress's post list, the blocks' tags, the global attributes on every tag.
*
* @return array>
*/
private static function build(): array {
$set = static fn( array $names ): array => array_fill_keys( $names, true );
$draw = array( 'fill', 'fill-opacity', 'fill-rule', 'clip-rule', 'clip-path', 'mask', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity', 'opacity', 'transform', 'vector-effect', 'color', 'display', 'visibility' );
$tags = array(
// Form controls.
'form' => $set( array( 'action', 'method', 'enctype', 'accept-charset', 'novalidate', 'target', 'name', 'autocomplete', 'rel' ) ),
'input' => $set( array( 'type', 'name', 'value', 'placeholder', 'checked', 'disabled', 'readonly', 'required', 'min', 'max', 'step', 'minlength', 'maxlength', 'pattern', 'size', 'multiple', 'accept', 'autocomplete', 'autofocus', 'inputmode', 'list', 'form', 'alt', 'src', 'width', 'height', 'enterkeyhint', 'spellcheck', 'dirname' ) ),
'select' => $set( array( 'name', 'multiple', 'required', 'disabled', 'size', 'autocomplete', 'form' ) ),
'option' => $set( array( 'value', 'selected', 'disabled', 'label' ) ),
'optgroup' => $set( array( 'label', 'disabled' ) ),
'textarea' => $set( array( 'name', 'rows', 'cols', 'placeholder', 'required', 'disabled', 'readonly', 'maxlength', 'minlength', 'wrap', 'autocomplete', 'form', 'spellcheck' ) ),
'button' => $set( array( 'type', 'name', 'value', 'disabled', 'form', 'formaction', 'formmethod', 'formnovalidate', 'formtarget', 'popovertarget', 'popovertargetaction' ) ),
'label' => $set( array( 'for', 'form' ) ),
'fieldset' => $set( array( 'disabled', 'name', 'form' ) ),
'legend' => array(),
'datalist' => array(),
'output' => $set( array( 'for', 'name', 'form' ) ),
'progress' => $set( array( 'value', 'max' ) ),
'meter' => $set( array( 'value', 'min', 'max', 'low', 'high', 'optimum' ) ),
// SVG (kses compares names in lowercase: viewbox, preserveaspectratio).
'svg' => $set( array_merge( array( 'xmlns', 'xmlns:xlink', 'viewbox', 'width', 'height', 'x', 'y', 'focusable', 'preserveaspectratio', 'version', 'overflow' ), $draw ) ),
'g' => $set( $draw ),
'path' => $set( array_merge( array( 'd', 'pathlength' ), $draw ) ),
'circle' => $set( array_merge( array( 'cx', 'cy', 'r', 'pathlength' ), $draw ) ),
'ellipse' => $set( array_merge( array( 'cx', 'cy', 'rx', 'ry', 'pathlength' ), $draw ) ),
'rect' => $set( array_merge( array( 'x', 'y', 'width', 'height', 'rx', 'ry', 'pathlength' ), $draw ) ),
'line' => $set( array_merge( array( 'x1', 'y1', 'x2', 'y2', 'pathlength' ), $draw ) ),
'polyline' => $set( array_merge( array( 'points', 'pathlength' ), $draw ) ),
'polygon' => $set( array_merge( array( 'points', 'pathlength' ), $draw ) ),
'use' => $set( array_merge( array( 'href', 'xlink:href', 'x', 'y', 'width', 'height' ), $draw ) ),
'symbol' => $set( array( 'viewbox', 'preserveaspectratio', 'width', 'height', 'x', 'y' ) ),
'defs' => array(),
'title' => array(),
// Responsive and lazy media; a no-script fallback (Rooster LMS's account page).
'img' => $set( array( 'srcset', 'sizes', 'loading', 'decoding', 'fetchpriority' ) ),
'picture' => array(),
'source' => $set( array( 'src', 'srcset', 'sizes', 'type', 'media', 'width', 'height' ) ),
'noscript' => array(),
// The Interactivity API's lists (data-wp-each) render from a template.
'template' => array(),
// WooCommerce's price wraps its amount in , which isolates its direction on a right-to-left page (1.0.1).
'bdi' => array(),
);
$global = $set( array( 'class', 'id', 'style', 'title', 'lang', 'dir', 'translate', 'role', 'hidden', 'inert', 'tabindex', 'data-*' ) );
foreach ( self::ARIA as $aria ) {
$global[ 'aria-' . $aria ] = true;
}
$out = wp_kses_allowed_html( 'post' );
foreach ( $tags as $tag => $attributes ) {
$out[ $tag ] = array_merge( is_array( $out[ $tag ] ?? null ) ? $out[ $tag ] : array(), $attributes );
}
foreach ( $out as $tag => $attributes ) {
$out[ $tag ] = array_merge( is_array( $attributes ) ? $attributes : array(), $global );
}
return $out;
}
}