Make WordPress Core

Ticket #43826: 43826.6.diff

File 43826.6.diff, 15.3 KB (added by audrasjb, 6 years ago)

patch refreshed

  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index a8bf034..04c607a 100644
    a b function get_post_galleries( $post, $html = true ) { 
    39103910                return array();
    39113911        }
    39123912
    3913         if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
     3913        if ( ! has_shortcode( $post->post_content, 'gallery' ) && !has_block( 'gallery', $post->post_content ) ) {
    39143914                return array();
    39153915        }
    39163916
    function get_post_galleries( $post, $html = true ) { 
    39513951                        }
    39523952                }
    39533953        }
     3954       
     3955        if ( has_block( 'gallery', $post->post_content ) ) {
     3956                $post_blocks = parse_blocks( $post->post_content );
     3957                // Use while/array_shift instead of foreach so we can modify the array from within the loop
     3958                while ( $block = array_shift( $post_blocks ) ) {
     3959                        if ( 'core/gallery' === $block['blockName'] ) {
     3960                                if ( $html ) {
     3961                                        $galleries[] = $block['innerHTML'];
     3962                                } elseif( !empty( $block['attrs']['ids'] ) ) {
     3963                                        // Use the image IDs from the json blob as canonical if present
     3964                                        $gallery_srcs = array();
     3965                                        foreach ( $block['attrs']['ids'] as $gallery_img_id ) {
     3966                                                $gallery_srcs[] = wp_get_attachment_url( $gallery_img_id );
     3967                                        }
     3968                                        $galleries[] = array(
     3969                                                // array_filter will eliminate any empty entries that came from unknown or invalid IDs
     3970                                                'src' => array_values( array_filter( array_unique( $gallery_srcs ) ) ),
     3971                                                // Only explicitly include the ids attribute. In future this could be changed to include all attributes, similar to $shortcode_attrs above.
     3972                                                'ids' => implode( ',', $block['attrs']['ids'] ),
     3973                                        );
     3974                                } else {
     3975                                        // Otherwise extract srcs from the innerHTML
     3976                                        $srcs = array();
     3977                                        preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
     3978                                        if ( ! empty( $src ) ) {
     3979                                                foreach ( $src as $s ) {
     3980                                                        $srcs[] = $s[2];
     3981                                                }
     3982                                        }
     3983
     3984                                        $galleries[] = array(
     3985                                                // Note that unlike shortcodes, all we are returning here is the src list
     3986                                                'src' => array_values( array_unique( $srcs ) )
     3987                                        );
     3988                                }
     3989
     3990                        } elseif ( !empty( $block['innerBlocks'] ) ) {
     3991                                // If we have nested blocks then gradually flatten it by moving those onto the end of the root array for traversal
     3992                                while ( $inner = array_pop( $block['innerBlocks'] ) ) {
     3993                                        array_push( $post_blocks, $inner );
     3994                                }
     3995                        }
     3996                }
     3997        }
    39543998
    39553999        /**
    39564000         * Filters the list of all found galleries in the given post.
  • tests/phpunit/tests/media.php

    diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
    index eea6f24..7564c22 100644
    a b https://w.org</a>', 
    477477         * @ticket 22960
    478478         */
    479479        function test_post_galleries_images() {
    480                 $ids1      = array();
    481                 $ids1_srcs = array();
    482                 foreach ( range( 1, 3 ) as $i ) {
     480                $ids = array();
     481                $ids_srcs = array();
     482                foreach ( range( 1, 6 ) as $i ) {
    483483                        $attachment_id = self::factory()->attachment->create_object(
    484484                                "image$i.jpg",
    485485                                0,
    https://w.org</a>', 
    490490                        );
    491491                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
    492492                        wp_update_attachment_metadata( $attachment_id, $metadata );
    493                         $ids1[]      = $attachment_id;
    494                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     493                        $ids[] = $attachment_id;
     494                        $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    495495                }
    496496
    497                 $ids2      = array();
    498                 $ids2_srcs = array();
    499                 foreach ( range( 4, 6 ) as $i ) {
    500                         $attachment_id = self::factory()->attachment->create_object(
    501                                 "image$i.jpg",
    502                                 0,
    503                                 array(
    504                                         'post_mime_type' => 'image/jpeg',
    505                                         'post_type'      => 'attachment',
    506                                 )
    507                         );
    508                         $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
    509                         wp_update_attachment_metadata( $attachment_id, $metadata );
    510                         $ids2[]      = $attachment_id;
    511                         $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    512                 }
    513 
    514                 $ids1_joined = join( ',', $ids1 );
    515                 $ids2_joined = join( ',', $ids2 );
     497                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     498                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
    516499
    517500                $blob    = <<<BLOB
    518501[gallery ids="$ids1_joined"]
    https://w.org</a>', 
    521504BLOB;
    522505                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    523506                $srcs    = get_post_galleries_images( $post_id );
    524                 $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
     507                $this->assertEquals( $srcs, array( array_slice( $ids_srcs, 0, 3 ), array_slice( $ids_srcs, 3, 3 ) ) );
    525508        }
    526509
    527510        /**
    BLOB; 
    627610         * @ticket 22960
    628611         */
    629612        function test_post_gallery_images() {
    630                 $ids1      = array();
    631                 $ids1_srcs = array();
    632                 foreach ( range( 1, 3 ) as $i ) {
     613                $ids = array();
     614                $ids_srcs = array();
     615                foreach ( range( 1, 6 ) as $i ) {
    633616                        $attachment_id = self::factory()->attachment->create_object(
    634617                                "image$i.jpg",
    635618                                0,
    BLOB; 
    640623                        );
    641624                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
    642625                        wp_update_attachment_metadata( $attachment_id, $metadata );
    643                         $ids1[]      = $attachment_id;
    644                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     626                        $ids[] = $attachment_id;
     627                        $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    645628                }
    646629
    647                 $ids2      = array();
    648                 $ids2_srcs = array();
    649                 foreach ( range( 4, 6 ) as $i ) {
    650                         $attachment_id = self::factory()->attachment->create_object(
    651                                 "image$i.jpg",
    652                                 0,
    653                                 array(
    654                                         'post_mime_type' => 'image/jpeg',
    655                                         'post_type'      => 'attachment',
     630                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     631                $ids2_joined = join( ',', $ids2 );
     632
     633                $blob    = <<<BLOB
     634[gallery ids="$ids1_joined"]
     635
     636[gallery ids="$ids2_joined"]
     637BLOB;
     638                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     639                $srcs = get_post_gallery_images( $post_id );
     640                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     641        }
     642
     643        /**
     644         * @ticket 43826
     645         * @group blocks
     646         */
     647        function test_block_post_galleries() {
     648                // Set up an unattached image.
     649                $this->factory->attachment->create_object( array(
     650                        'file' => 'test.jpg',
     651                        'post_parent' => 0,
     652                        'post_mime_type' => 'image/jpeg',
     653                        'post_type' => 'attachment'
     654                ) );
     655
     656                $post_id = $this->factory->post->create( array(
     657                        'post_content' => '<!-- wp:gallery -->',
     658                ) );
     659
     660                $galleries = get_post_galleries( $post_id, false );
     661
     662                $this->assertTrue( is_array( $galleries ) );
     663                $this->assertEmpty( $galleries[0]['src'] );
     664        }
     665
     666        /**
     667         * @ticket 43826
     668         * @group blocks
     669         */
     670        function test_block_post_gallery_images() {
     671                // Similar to test_post_gallery_images but with blocks instead of shortcodes
     672                $ids = array();
     673                $imgs = array();
     674                $ids_srcs = array();
     675                foreach ( range( 1, 6 ) as $i ) {
     676                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     677                                'post_mime_type' => 'image/jpeg',
     678                                'post_type' => 'attachment'
    656679                                )
    657680                        );
    658                         $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     681                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    659682                        wp_update_attachment_metadata( $attachment_id, $metadata );
    660                         $ids2[]      = $attachment_id;
    661                         $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     683                        $ids[] = $attachment_id;
     684                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     685                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
    662686                }
     687               
     688                $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) );
     689                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
     690               
     691                $blob =<<<BLOB
     692<!-- wp:gallery -->
     693$imgs1_joined
     694<!-- /wp:gallery -->
     695
     696<!-- wp:gallery -->
     697$imgs2_joined
     698<!-- /wp:gallery -->
     699BLOB;
     700                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     701                $srcs = get_post_gallery_images( $post_id );
     702                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     703        }
    663704
    664                 $ids1_joined = join( ',', $ids1 );
    665                 $ids2_joined = join( ',', $ids2 );
     705        /**
     706         * @ticket 43826
     707         * @group blocks
     708         */
     709        function test_block_post_gallery_images_json() {
     710                // Similar to test_block_post_gallery_images, with IDs in the json blob
     711                $ids = array();
     712                $imgs = array();
     713                $ids_srcs = array();
     714                foreach ( range( 1, 6 ) as $i ) {
     715                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     716                                'post_mime_type' => 'image/jpeg',
     717                                'post_type' => 'attachment'
     718                        ) );
     719                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     720                        wp_update_attachment_metadata( $attachment_id, $metadata );
     721                        $ids[] = $attachment_id;
     722                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     723                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     724                }
    666725
    667                 $blob    = <<<BLOB
     726                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     727                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     728
     729                $blob =<<<BLOB
     730<!-- wp:gallery {"ids":[$ids1_joined]} -->
     731<!-- /wp:gallery -->
     732
     733<!-- wp:gallery {"ids":[$ids2_joined]} -->
     734<!-- /wp:gallery -->
     735BLOB;
     736                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     737                $srcs = get_post_gallery_images( $post_id );
     738                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     739        }
     740
     741        /**
     742         * @ticket 43826
     743         * @group blocks
     744         */
     745        function test_mixed_post_gallery_images() {
     746                // Similar to test_post_gallery_images but with a shortcode and a block in the same post
     747                $ids = array();
     748                $imgs = array();
     749                $ids_srcs = array();
     750                foreach ( range( 1, 6 ) as $i ) {
     751                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     752                                'post_mime_type' => 'image/jpeg',
     753                                'post_type' => 'attachment'
     754                        ) );
     755                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     756                        wp_update_attachment_metadata( $attachment_id, $metadata );
     757                        $ids[] = $attachment_id;
     758                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     759                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     760                }
     761
     762                $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) );
     763                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
     764
     765                $blob =<<<BLOB
     766<!-- wp:gallery -->
     767$imgs2_joined
     768<!-- /wp:gallery -->
     769BLOB;
     770                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     771                $srcs = get_post_gallery_images( $post_id );
     772                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     773               
     774        /**
     775         * @ticket 43826
     776         * @group blocks
     777         */
     778        function test_mixed_post_galleries() {
     779                // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries
     780                $ids = array();
     781                $imgs = array();
     782                $ids_srcs = array();
     783                foreach ( range( 1, 6 ) as $i ) {
     784                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     785                                'post_mime_type' => 'image/jpeg',
     786                                'post_type' => 'attachment'
     787                        ) );
     788                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     789                        wp_update_attachment_metadata( $attachment_id, $metadata );
     790                        $ids[] = $attachment_id;
     791                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     792                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     793                }
     794                $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) );
     795                $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) );
     796
     797                $blob =<<<BLOB
    668798[gallery ids="$ids1_joined"]
    669799
    670 [gallery ids="$ids2_joined"]
     800<!-- wp:gallery {"ids":[$ids2_joined]} -->
     801<!-- /wp:gallery -->
     802BLOB;
     803
     804                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     805
     806                $galleries = get_post_galleries( $post_id, false );
     807                $this->assertEquals( $galleries, array(
     808                        array(
     809                                'ids' => $ids1_joined,
     810                                'src' => array_slice( $ids_srcs, 0, 3 ),
     811                        ),
     812                        array(
     813                                'ids' => $ids2_joined,
     814                                'src' => array_slice( $ids_srcs, 3, 3 ),
     815                        ),
     816                ) );
     817
     818        }
     819
     820        /**
     821         * @ticket 43826
     822         * @group blocks
     823         */
     824        function test_mixed_post_galleries_data() {
     825                // Test attributes returned by get_post_galleries() function in $html=false mode, with both shortcode and block galleries
     826                $ids = array();
     827                $imgs = array();
     828                $ids_srcs = array();
     829                foreach ( range( 1, 6 ) as $i ) {
     830                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     831                                'post_mime_type' => 'image/jpeg',
     832                                'post_type' => 'attachment'
     833                        ) );
     834                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     835                        wp_update_attachment_metadata( $attachment_id, $metadata );
     836                        $ids[] = $attachment_id;
     837                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     838                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     839               
     840                }
     841               
     842                $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) );
     843                $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) );
     844
     845                $blob =<<<BLOB
     846[gallery ids="$ids1_joined" type="type" foo="bar"]
     847
     848<!-- wp:gallery {"ids":[$ids2_joined],"columns":3,"imageCrop":false,"linkTo":"media"} -->
     849<!-- /wp:gallery -->
     850BLOB;
     851
     852                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     853
     854                $galleries = get_post_galleries( $post_id, false );
     855                $this->assertEquals( $galleries, array(
     856                        array(
     857                                        'ids' => $ids1_joined,
     858                                        'src' => array_slice( $ids_srcs, 0, 3 ),
     859                                        'type' => 'type',
     860                                        'foo' => 'bar', // The shortcode code passes arbitrary attributes
     861                                ),
     862                                array(
     863                                        'ids' => $ids2_joined,
     864                                        'src' => array_slice( $ids_srcs, 3, 3 ),
     865                                        // The block only passes ids, no other attributes
     866                                ),
     867                        ) );
     868       
     869                }
     870       
     871        /**
     872         * @ticket 43826
     873         * @group blocks
     874         */
     875        function test_block_inner_post_gallery_images() {
     876                // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
     877                $ids = array();
     878                $imgs = array();
     879                $ids_srcs = array();
     880                foreach ( range( 1, 3 ) as $i ) {
     881                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     882                                'post_mime_type' => 'image/jpeg',
     883                                'post_type' => 'attachment'
     884                        ) );
     885                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     886                        wp_update_attachment_metadata( $attachment_id, $metadata );
     887                        $ids[] = $attachment_id;
     888                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     889                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     890                }
     891               
     892                $imgs_joined = join( "\n", $imgs );
     893               
     894                $blob =<<<BLOB
     895<!-- wp:columns -->
     896<!-- wp:column -->
     897<!-- wp:gallery -->
     898$imgs_joined
     899<!-- /wp:gallery -->
     900<!-- /wp:column -->
     901<!-- /wp:columns -->
    671902BLOB;
     903
    672904                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    673                 $srcs    = get_post_gallery_images( $post_id );
    674                 $this->assertEquals( $srcs, $ids1_srcs );
     905                $srcs = get_post_gallery_images( $post_id );
     906                $this->assertEquals( $srcs, $ids_srcs );
    675907        }
    676908
    677909        function test_get_media_embedded_in_content() {