Auto-embedding:

- We already match URLs on their own line, add another regex to match URLs in their own paragraphs.
- Always exclude the `\s<>"` characters when matching.
- Add more unit tests.

Props iseulde, azaozz.
Fixes #25387.

git-svn-id: https://develop.svn.wordpress.org/trunk@37627 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2016-06-02 21:23:43 +00:00
parent f1fdf6a1e8
commit 245709dcd1
2 changed files with 81 additions and 2 deletions

View File

@ -334,8 +334,12 @@ class WP_Embed {
// Replace line breaks from all HTML elements with placeholders.
$content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
// Find URLs that are on their own line.
$content = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) {
// Find URLs on their own line.
$content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
// Find URLs in their own paragraph.
$content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content );
}
// Put the line breaks back.
return str_replace( '<!-- wp-line-break -->', "\n", $content );

View File

@ -158,6 +158,72 @@ EOF;
$this->assertEquals( $content, $result );
}
function data_autoembed() {
return array(
// Should embed
array(
'https://w.org',
'[embed]'
),
array(
'test
https://w.org
test',
'test
[embed]
test'
),
array(
'<p class="test">https://w.org</p>',
'<p class="test">[embed]</p>'
),
array(
'<p> https://w.org </p>',
'<p> [embed] </p>'
),
array(
'<p>test
https://w.org
test</p>',
'<p>test
[embed]
test</p>'
),
array(
'<p>https://w.org
</p>',
'<p>[embed]
</p>'
),
// Should NOT embed
array(
'test https://w.org</p>'
),
array(
'<span>https://w.org</a>'
),
array(
'<pre>https://w.org
</p>'
),
array(
'<a href="https://w.org">
https://w.org</a>'
),
);
}
/**
* @dataProvider data_autoembed
*/
function test_autoembed( $content, $result = null ) {
$wp_embed = new Test_Autoembed;
$this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content );
}
function test_wp_prepare_attachment_for_js() {
// Attachment without media
$id = wp_insert_attachment(array(
@ -1612,3 +1678,12 @@ EOF;
$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
}
}
/**
* Helper class for `test_autoembed`.
*/
class Test_Autoembed extends WP_Embed {
public function shortcode( $attr, $url = '' ) {
return '[embed]';
}
}