Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeds: Resolve conflicts and add oEmbed filter tests #8300

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions src/wp-includes/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ function wp_filter_oembed_result( $result, $data, $url ) {
'a' => array(
'href' => true,
),
'p' => array(),
'blockquote' => array(),
'iframe' => array(
'src' => true,
Expand All @@ -947,36 +948,36 @@ function wp_filter_oembed_result( $result, $data, $url ) {
$html = wp_kses( $result, $allowed_html );

preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content );

// We require at least the iframe to exist.
if ( empty( $content[2] ) ) {
return false;
}
$html = $content[1] . $content[2];
if ( ! empty( $content[2] ) ) {
$html = $content[1] . $content[2];

preg_match( '/ src=([\'"])(.*?)\1/', $html, $results );
preg_match( '/ src=([\'"])(.*?)\1/', $html, $results );

if ( ! empty( $results ) ) {
$secret = wp_generate_password( 10, false );
if ( ! empty( $results ) ) {
$secret = wp_generate_password( 10, false );

$url = esc_url( "{$results[2]}#?secret=$secret" );
$q = $results[1];
$url = esc_url( "{$results[2]}#?secret=$secret" );
$q = $results[1];

$html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html );
$html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
}
$html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html );
$html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
}

$allowed_html['blockquote']['data-secret'] = true;
$allowed_html['iframe']['data-secret'] = true;
$allowed_html['blockquote']['data-secret'] = true;
$allowed_html['iframe']['data-secret'] = true;

$html = wp_kses( $html, $allowed_html );
$html = wp_kses( $html, $allowed_html );

if ( ! empty( $content[1] ) ) {
// We have a blockquote to fall back on. Hide the iframe by default.
$html = str_replace( '<iframe', '<iframe style="position: absolute; visibility: hidden;"', $html );
$html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html );
}
if ( ! empty( $content[1] ) ) {
// We have a blockquote to fall back on. Hide the iframe by default.
$html = str_replace( '<iframe', '<iframe style="position: absolute; visibility: hidden;"', $html );
$html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html );
}

$html = str_ireplace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html );
$html = str_ireplace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html );
}

return $html;
}
Expand Down
36 changes: 33 additions & 3 deletions tests/phpunit/tests/oembed/filterResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
$html = '<span>Hello</span><p>World</p>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );

$this->assertFalse( $actual );
$this->assertSame( 'Hello<p>World</p>' ,$actual );

Check failure on line 51 in tests/phpunit/tests/oembed/filterResult.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Space found before comma in argument list

Check failure on line 51 in tests/phpunit/tests/oembed/filterResult.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

No space found after comma in argument list

$html = '<div><p></p></div><script></script>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );

$this->assertFalse( $actual );
$this->assertSame( '<p></p>' , $actual );

Check failure on line 56 in tests/phpunit/tests/oembed/filterResult.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Space found before comma in argument list
}

public function test_filter_oembed_result_secret_param_available() {
Expand All @@ -76,7 +76,7 @@

public function test_filter_oembed_result_invalid_result() {
$this->assertFalse( wp_filter_oembed_result( false, (object) array( 'type' => 'rich' ), '' ) );
$this->assertFalse( wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) );
$this->assertSame( '', wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) );
}

public function test_filter_oembed_result_blockquote_adds_style_to_iframe() {
Expand Down Expand Up @@ -136,4 +136,34 @@

$this->assertSame( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe>', $actual );
}

/**
* Test standalone blockquote with paragraph tag is allowed.
*/
public function test_filter_oembed_result_standalone_blockquote_paragraph() {
$html = '<blockquote><p>Test content</p></blockquote>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );

$this->assertSame( '<blockquote><p>Test content</p></blockquote>', $actual );
}

/**
* Test multiple paragraph tags within blockquote are preserved.
*/
public function test_filter_oembed_result_multiple_paragraphs() {
$html = '<blockquote><p>First paragraph</p><p>Second paragraph</p></blockquote>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );

$this->assertSame( '<blockquote><p>First paragraph</p><p>Second paragraph</p></blockquote>', $actual );
}

/**
* Test blockquote with paragraph and link tags.
*/
public function test_filter_oembed_result_paragraph_with_link() {
$html = '<blockquote><p>Text with <a href="https://example.com">link</a></p></blockquote>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );

$this->assertSame( '<blockquote><p>Text with <a href="https://example.com">link</a></p></blockquote>', $actual );
}
}
Loading