Media: Make get_post_galleries() only return galleries.

This change makes sure only gallery content is returned by `get_post_galleries()`. It fixes an issue where non gallery block content was also returned by the function.

Props BinaryMoon, costdev, glendaviesnz.
Fixes #55203.


git-svn-id: https://develop.svn.wordpress.org/trunk@52797 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2022-02-25 12:42:10 +00:00
parent f4addf3ff5
commit ee201606fb
2 changed files with 82 additions and 3 deletions

View File

@ -4780,9 +4780,12 @@ function get_post_galleries( $post, $html = true ) {
continue;
}
// All blocks nested inside non-Gallery blocks should be in the root array.
if ( $has_inner_blocks && 'core/gallery' !== $block['blockName'] ) {
array_push( $post_blocks, ...$block['innerBlocks'] );
// Skip non-Gallery blocks.
if ( 'core/gallery' !== $block['blockName'] ) {
// Move inner blocks into the root array before skipping.
if ( $has_inner_blocks ) {
array_push( $post_blocks, ...$block['innerBlocks'] );
}
continue;
}

View File

@ -41,6 +41,82 @@ class Tests_Functions_getPostGalleries extends WP_UnitTestCase {
$this->assertEmpty( $galleries );
}
/**
* Test that only galleries are returned.
*
* @dataProvider data_returns_only_galleries
*
* @ticket 55203
*
* @param string $content The content of the post.
* @param string $needle The content of a non-gallery block.
*/
public function test_returns_only_galleries( $content, $needle ) {
$image_id = $this->factory->attachment->create_object(
array(
'file' => 'test.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
$content = str_replace(
array( 'IMAGE_ID', 'IMAGE_URL' ),
array( $image_id, $image_url ),
$content
);
$post_id = $this->factory->post->create(
array(
'post_content' => $content,
)
);
$galleries = get_post_galleries( $post_id );
$actual = implode( '', $galleries );
$this->assertStringNotContainsString( $needle, $actual );
}
/**
* Data provider.
*
* @return array
*/
public function data_returns_only_galleries() {
$gallery = '
<!-- wp:gallery {"linkTo":"none","className":"columns-2"} -->
<figure
class="wp-block-gallery has-nested-images columns-default is-cropped columns-2"
>
<!-- wp:image {"id":IMAGE_ID,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large">
<img
src="IMAGE_URL"
alt="Image gallery image"
class="wp-image-IMAGE_ID"
/>
</figure>
<!-- /wp:image -->
</figure>
<!-- /wp:gallery -->
';
return array(
'a paragraph before a gallery' => array(
'content' => '<!-- wp:paragraph --><p>A paragraph before a gallery.</p><!-- /wp:paragraph -->' . $gallery,
'needle' => 'A paragraph before a gallery.',
),
'a paragraph after a gallery' => array(
'content' => $gallery . '<!-- wp:paragraph --><p>A paragraph after a gallery.</p><!-- /wp:paragraph -->',
'needle' => 'A paragraph after a gallery.',
),
);
}
/**
* Test that no srcs are returned for a shortcode gallery
* in a post with no attached images.