diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index f04ae19164..cb2f25f98d 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3220,16 +3220,22 @@ function get_attached_media( $type, $post = 0 ) { */ function get_media_embedded_in_content( $content, $types = null ) { $html = array(); - $allowed_media_types = array( 'audio', 'video', 'object', 'embed', 'iframe' ); + + $allowed_media_types = apply_filters( 'get_media_embedded_in_content_allowed', array( 'audio', 'video', 'object', 'embed', 'iframe' ) ); + if ( ! empty( $types ) ) { - if ( ! is_array( $types ) ) + if ( ! is_array( $types ) ) { $types = array( $types ); + } + $allowed_media_types = array_intersect( $allowed_media_types, $types ); } - foreach ( $allowed_media_types as $tag ) { - if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { - $html[] = $matches[0]; + $tags = implode( '|', $allowed_media_types ); + + if ( preg_match_all( '#<(?P' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) { + foreach ( $matches[0] as $match ) { + $html[] = $match; } } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 9f0a131503..877bf601f2 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -378,7 +378,7 @@ $video This is a comment CONTENT; - $types = array( 'audio', 'video', 'object', 'embed', 'iframe' ); + $types = array( 'object', 'embed', 'iframe', 'audio', 'video' ); $contents = array_values( compact( $types ) ); $matches = get_media_embedded_in_content( $content, 'audio' ); @@ -400,6 +400,27 @@ CONTENT; $this->assertEquals( $contents, $matches ); } + function test_get_media_embedded_in_content_order() { + $audio =<< +AUDIO; + $video =<< +VIDEO; + $content = $audio . $video; + + $matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) ); + $this->assertEquals( array( $audio, $video ), $matches1 ); + + $reversed = $video . $audio; + $matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) ); + $this->assertEquals( array( $video, $audio ), $matches2 ); + } + /** * Test [video] shortcode processing *