mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Accessibility: Ensure embed iframes have a title attribute.
Screen reader users rely on the iframe title attribute to describe the contents of iframes. A meaningful title attribute allows to quickly identify the iframe content, so users can determine which iframe to enter and explore in detail or skip if desired. Note: this is the only case where a title attribute is required for compliance with the W3C Web Content Accessibility Guidelines (WCAG). - checks for oEmbed response of type `video` or `rich` - checks if they use an iframe - fetches the title (if any) from the oEmbed response - adds the title to the embed iframe Props bamadesigner, TomHarrigan, swissspidy, jrf, afercia. Fixes #40245. git-svn-id: https://develop.svn.wordpress.org/trunk@44942 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -780,6 +780,55 @@ function _oembed_create_xml( $data, $node = null ) {
|
||||
return $node->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the given oEmbed HTML to make sure iframes have a title attribute.
|
||||
*
|
||||
* @since 5.2.0
|
||||
*
|
||||
* @param string $result The oEmbed HTML result.
|
||||
* @param object $data A data object result from an oEmbed provider.
|
||||
* @param string $url The URL of the content to be embedded.
|
||||
* @return string The filtered oEmbed result.
|
||||
*/
|
||||
function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) {
|
||||
if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ) ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$title = ! empty( $data->title ) ? $data->title : '';
|
||||
|
||||
$pattern = '`<iframe[^>]*?title=(\\\\\'|\\\\"|[\'"])([^>]*?)\1`i';
|
||||
$has_title_attr = preg_match( $pattern, $result, $matches );
|
||||
|
||||
if ( $has_title_attr && ! empty( $matches[2] ) ) {
|
||||
$title = $matches[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the title attribute of the given oEmbed HTML iframe.
|
||||
*
|
||||
* @since 5.2.0
|
||||
*
|
||||
* @param string $title The title attribute.
|
||||
* @param string $result The oEmbed HTML result.
|
||||
* @param object $data A data object result from an oEmbed provider.
|
||||
* @param string $url The URL of the content to be embedded.
|
||||
*/
|
||||
$title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url );
|
||||
|
||||
if ( '' === $title ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( $has_title_attr ) {
|
||||
// Remove the old title, $matches[1]: quote, $matches[2]: title attribute value.
|
||||
$result = str_replace( ' title=' . $matches[1] . $matches[2] . $matches[1], '', $result );
|
||||
}
|
||||
|
||||
return str_ireplace( '<iframe ', sprintf( '<iframe title="%s" ', esc_attr( $title ) ), $result );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filters the given oEmbed HTML.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user