Make WordPress Core

Changeset 52797


Ignore:
Timestamp:
02/25/2022 12:42:10 PM (15 months ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r52725 r52797  
    47814781            }
    47824782
    4783             // All blocks nested inside non-Gallery blocks should be in the root array.
    4784             if ( $has_inner_blocks && 'core/gallery' !== $block['blockName'] ) {
    4785                 array_push( $post_blocks, ...$block['innerBlocks'] );
     4783            // Skip non-Gallery blocks.
     4784            if ( 'core/gallery' !== $block['blockName'] ) {
     4785                // Move inner blocks into the root array before skipping.
     4786                if ( $has_inner_blocks ) {
     4787                    array_push( $post_blocks, ...$block['innerBlocks'] );
     4788                }
    47864789                continue;
    47874790            }
  • trunk/tests/phpunit/tests/media/getPostGalleries.php

    r52190 r52797  
    4040        $galleries = get_post_galleries( $post_id, false );
    4141        $this->assertEmpty( $galleries );
     42    }
     43
     44    /**
     45     * Test that only galleries are returned.
     46     *
     47     * @dataProvider data_returns_only_galleries
     48     *
     49     * @ticket 55203
     50     *
     51     * @param string $content The content of the post.
     52     * @param string $needle  The content of a non-gallery block.
     53     */
     54    public function test_returns_only_galleries( $content, $needle ) {
     55        $image_id = $this->factory->attachment->create_object(
     56            array(
     57                'file'           => 'test.jpg',
     58                'post_parent'    => 0,
     59                'post_mime_type' => 'image/jpeg',
     60                'post_type'      => 'attachment',
     61            )
     62        );
     63
     64        $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
     65
     66        $content = str_replace(
     67            array( 'IMAGE_ID', 'IMAGE_URL' ),
     68            array( $image_id, $image_url ),
     69            $content
     70        );
     71
     72        $post_id = $this->factory->post->create(
     73            array(
     74                'post_content' => $content,
     75            )
     76        );
     77
     78        $galleries = get_post_galleries( $post_id );
     79        $actual    = implode( '', $galleries );
     80
     81        $this->assertStringNotContainsString( $needle, $actual );
     82    }
     83
     84    /**
     85     * Data provider.
     86     *
     87     * @return array
     88     */
     89    public function data_returns_only_galleries() {
     90        $gallery = '
     91        <!-- wp:gallery {"linkTo":"none","className":"columns-2"} -->
     92        <figure
     93        class="wp-block-gallery has-nested-images columns-default is-cropped columns-2"
     94        >
     95        <!-- wp:image {"id":IMAGE_ID,"sizeSlug":"large","linkDestination":"none"} -->
     96        <figure class="wp-block-image size-large">
     97        <img
     98        src="IMAGE_URL"
     99        alt="Image gallery image"
     100        class="wp-image-IMAGE_ID"
     101        />
     102        </figure>
     103        <!-- /wp:image -->
     104        </figure>
     105        <!-- /wp:gallery -->
     106        ';
     107
     108        return array(
     109            'a paragraph before a gallery' => array(
     110                'content' => '<!-- wp:paragraph --><p>A paragraph before a gallery.</p><!-- /wp:paragraph -->' . $gallery,
     111                'needle'  => 'A paragraph before a gallery.',
     112            ),
     113            'a paragraph after a gallery'  => array(
     114                'content' => $gallery . '<!-- wp:paragraph --><p>A paragraph after a gallery.</p><!-- /wp:paragraph -->',
     115                'needle'  => 'A paragraph after a gallery.',
     116            ),
     117        );
    42118    }
    43119
Note: See TracChangeset for help on using the changeset viewer.