Skip to content

Commit 1775941

Browse files
committed
KSES: Make the URI attributes DRY.
This commit introduces the `wp_kses_uri_attributes` function and filter. The function centralizes the list of attributes, in order to prevent inconsistency, and the filter provides a way for plugins to customize the attributes. Merges [44014] and [44017] to the `4.9` branch. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@44020 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4a807b3 commit 1775941

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/wp-includes/kses.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
536536
* @return string Filtered attribute.
537537
*/
538538
function wp_kses_one_attr( $string, $element ) {
539-
$uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
539+
$uris = wp_kses_uri_attributes();
540540
$allowed_html = wp_kses_allowed_html( 'post' );
541541
$allowed_protocols = wp_allowed_protocols();
542542
$string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
@@ -733,6 +733,56 @@ function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
733733
return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
734734
}
735735

736+
/**
737+
* Helper function listing HTML attributes containing a URL.
738+
*
739+
* This function returns a list of all HTML attributes that must contain
740+
* a URL according to the HTML specification.
741+
*
742+
* This list includes URI attributes both allowed and disallowed by KSES.
743+
*
744+
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
745+
*
746+
* @since 5.0.1
747+
*
748+
* @return array HTML attributes that must include a URL.
749+
*/
750+
function wp_kses_uri_attributes() {
751+
$uri_attributes = array(
752+
'action',
753+
'archive',
754+
'background',
755+
'cite',
756+
'classid',
757+
'codebase',
758+
'data',
759+
'formaction',
760+
'href',
761+
'icon',
762+
'longdesc',
763+
'manifest',
764+
'poster',
765+
'profile',
766+
'src',
767+
'usemap',
768+
'xmlns',
769+
);
770+
771+
/**
772+
* Filters the list of attributes that are required to contain a URL.
773+
*
774+
* Use this filter to add any `data-` attributes that are required to be
775+
* validated as a URL.
776+
*
777+
* @since 5.0.1
778+
*
779+
* @param array $uri_attributes HTML attributes requiring validation as a URL.
780+
*/
781+
$uri_attributes = apply_filters( 'wp_kses_uri_attributes', $uri_attributes );
782+
783+
return $uri_attributes;
784+
}
785+
736786
/**
737787
* Callback for wp_kses_split.
738788
*
@@ -930,7 +980,7 @@ function wp_kses_hair($attr, $allowed_protocols) {
930980
$attrarr = array();
931981
$mode = 0;
932982
$attrname = '';
933-
$uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
983+
$uris = wp_kses_uri_attributes();
934984

935985
// Loop through the whole attribute list
936986

0 commit comments

Comments
 (0)