Make WordPress Core

Changeset 53006


Ignore:
Timestamp:
03/28/2022 08:51:00 PM (4 years 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.
Merges [52797] to the 5.9 branch.
Fixes #55203.

Location:
branches/5.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.9

  • branches/5.9/src/wp-includes/media.php

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

    r52190 r53006  
    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.