WordPress.org

Make WordPress Core

Changeset 31574


Ignore:
Timestamp:
02/27/2015 04:11:00 PM (5 years ago)
Author:
wonderboymusic
Message:

Improve get_media_embedded_in_content() so that it returns the media it finds in the same order that it appears in the content.

Adds unit test, updates another.

Props kopepasah.
See #26675.

Location:
trunk
Files:
2 edited

Legend:

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

    r31530 r31574  
    32213221function get_media_embedded_in_content( $content, $types = null ) {
    32223222    $html = array();
    3223     $allowed_media_types = array( 'audio', 'video', 'object', 'embed', 'iframe' );
     3223
     3224    $allowed_media_types = apply_filters( 'get_media_embedded_in_content_allowed', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
     3225
    32243226    if ( ! empty( $types ) ) {
    3225         if ( ! is_array( $types ) )
     3227        if ( ! is_array( $types ) ) {
    32263228            $types = array( $types );
     3229        }
     3230
    32273231        $allowed_media_types = array_intersect( $allowed_media_types, $types );
    32283232    }
    32293233
    3230     foreach ( $allowed_media_types as $tag ) {
    3231         if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
    3232             $html[] = $matches[0];
     3234    $tags = implode( '|', $allowed_media_types );
     3235
     3236    if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) {
     3237        foreach ( $matches[0] as $match ) {
     3238            $html[] = $match;
    32333239        }
    32343240    }
  • trunk/tests/phpunit/tests/media.php

    r31239 r31574  
    379379CONTENT;
    380380
    381         $types = array( 'audio', 'video', 'object', 'embed', 'iframe' );
     381        $types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
    382382        $contents = array_values( compact( $types ) );
    383383
     
    399399        $matches = get_media_embedded_in_content( $content, $types );
    400400        $this->assertEquals( $contents, $matches );
     401    }
     402
     403    function test_get_media_embedded_in_content_order() {
     404        $audio =<<<AUDIO
     405<audio preload="none">
     406    <source />
     407</audio>
     408AUDIO;
     409        $video =<<<VIDEO
     410<video preload="none">
     411    <source />
     412</video>
     413VIDEO;
     414        $content = $audio . $video;
     415
     416        $matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
     417        $this->assertEquals( array( $audio, $video ), $matches1 );
     418
     419        $reversed = $video . $audio;
     420        $matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
     421        $this->assertEquals( array( $video, $audio ), $matches2 );
    401422    }
    402423
Note: See TracChangeset for help on using the changeset viewer.