Make WordPress Core

Changeset 37627


Ignore:
Timestamp:
06/02/2016 09:23:43 PM (10 years ago)
Author:
azaozz
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-embed.php

    r37539 r37627  
    335335                $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
    336336
    337                 // Find URLs that are on their own line.
    338                 $content = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
     337                if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) {
     338                        // Find URLs on their own line.
     339                        $content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
     340                        // Find URLs in their own paragraph.
     341                        $content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content );
     342                }
    339343
    340344                // Put the line breaks back.
  • trunk/tests/phpunit/tests/media.php

    r37557 r37627  
    157157                $result = $wp_embed->autoembed( $content );
    158158                $this->assertEquals( $content, $result );
     159        }
     160
     161        function data_autoembed() {
     162                return array(
     163
     164                        // Should embed
     165                        array(
     166'https://w.org',
     167'[embed]'
     168                        ),
     169                        array(
     170'test
     171 https://w.org
     172test',
     173'test
     174 [embed]
     175test'
     176                        ),
     177                        array(
     178'<p class="test">https://w.org</p>',
     179'<p class="test">[embed]</p>'
     180                        ),
     181                        array(
     182'<p> https://w.org </p>',
     183'<p> [embed] </p>'
     184                        ),
     185                        array(
     186'<p>test
     187https://w.org
     188test</p>',
     189'<p>test
     190[embed]
     191test</p>'
     192                        ),
     193                        array(
     194'<p>https://w.org
     195</p>',
     196'<p>[embed]
     197</p>'
     198                        ),
     199
     200                        // Should NOT embed
     201                        array(
     202'test https://w.org</p>'
     203                        ),
     204                        array(
     205'<span>https://w.org</a>'
     206                        ),
     207                        array(
     208'<pre>https://w.org
     209</p>'
     210                        ),
     211                        array(
     212'<a href="https://w.org">
     213https://w.org</a>'
     214                        ),
     215                );
     216        }
     217
     218        /**
     219         * @dataProvider data_autoembed
     220         */
     221        function test_autoembed( $content, $result = null ) {
     222                $wp_embed = new Test_Autoembed;
     223
     224                $this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content );
    159225        }
    160226
     
    16131679        }
    16141680}
     1681
     1682/**
     1683 * Helper class for `test_autoembed`.
     1684 */
     1685class Test_Autoembed extends WP_Embed {
     1686        public function shortcode( $attr, $url = '' ) {
     1687                return '[embed]';
     1688        }
     1689}
Note: See TracChangeset for help on using the changeset viewer.