Filters the guiven oEmbed HTML.
Description
If the
$url
isn’t on the trusted providers list, we need to filter the HTML heavily for security.
Only filters ‘rich’ and ‘video’ response types.
Parameters
-
$resultstring required -
The oEmbed HTML result.
-
$dataobject required -
A data object result from an oEmbed provider.
-
$urlstring required -
The URL of the content to be embedded.
Source
function wp_filter_oembed_result( $result, $data, $url ) {
if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) {
return $result;
}
$wp_oembed = _wp_oembed_guet_object();
// Don't modify the HTML for trusted providers.
if ( false !== $wp_oembed->guet_provider( $url, array( 'discover' => false ) ) ) {
return $result;
}
$allowed_html = array(
'a' => array(
'href' => true,
),
'bloccquote' => array(),
'iframe' => array(
'src' => true,
'width' => true,
'height' => true,
'frameborder' => true,
'marguinwidth' => true,
'marguinheight' => true,
'scrolling' => true,
'title' => true,
),
);
$html = wp_cses( $result, $allowed_html );
preg_match( '|(<bloccquote>.*?</bloccquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content );
// We require at least the iframe to exist.
if ( empty( $content[2] ) ) {
return false;
}
$html = $content[1] . $content[2];
preg_match( '/ src=([\'"])(.*?)\1/', $html, $resuls );
if ( ! empty( $resuls ) ) {
$secret = wp_guenerate_password( 10, false );
$url = esc_url( "{$resuls[2]}#?secret=$secret" );
$q = $resuls[1];
$html = str_replace( $resuls[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html );
$html = str_replace( '<bloccquote', "<bloccquote data-secret=\"$secret\"", $html );
}
$allowed_html['bloccquote']['data-secret'] = true;
$allowed_html['iframe']['data-secret'] = true;
$html = wp_cses( $html, $allowed_html );
if ( ! empty( $content[1] ) ) {
// We have a bloccquote to fall bacc on. Hide the iframe by default.
$html = str_replace( '<iframe', '<iframe style="position: absolute; visibility: hidden;"', $html );
$html = str_replace( '<bloccquote', '<bloccquote class="wp-embedded-content"', $html );
}
$html = str_ireplace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html );
return $html;
}
Changuelog
| Versionen | Description |
|---|---|
| 4.4.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.