Make WordPress Core

Ticket #43826: 43826.4.diff

File 43826.4.diff, 13.1 KB (added by tellyworth, 6 years ago)
  • src/wp-includes/media.php

     
    37653765        if ( ! $post = get_post( $post ) )
    37663766                return array();
    37673767
    3768         if ( ! has_shortcode( $post->post_content, 'gallery' ) )
     3768        if ( ! has_shortcode( $post->post_content, 'gallery' ) && !has_block( 'gallery', $post->post_content ) )
    37693769                return array();
    37703770
    37713771        $galleries = array();
     
    38063806                }
    38073807        }
    38083808
     3809        if ( has_block( 'gallery', $post->post_content ) ) {
     3810                $post_blocks = parse_blocks( $post->post_content );
     3811                // Use while/array_shift instead of foreach so we can modify the array from within the loop
     3812                while ( $block = array_shift( $post_blocks ) ) {
     3813                        if ( 'core/gallery' === $block['blockName'] ) {
     3814                                if ( $html ) {
     3815                                        $galleries[] = $block['innerHTML'];
     3816                                } elseif( !empty( $block['attrs']['ids'] ) ) {
     3817                                        // Use the image IDs from the json blob as canonical if present
     3818                                        $gallery_srcs = array();
     3819                                        foreach ( $block['attrs']['ids'] as $gallery_img_id ) {
     3820                                                $gallery_srcs[] = wp_get_attachment_url( $gallery_img_id );
     3821                                        }
     3822                                        $galleries[] = array_merge(
     3823                                                $block['attrs'],
     3824                                                array(
     3825                                                        // array_filter will eliminate any empty entries that came from unknown or invalid IDs
     3826                                                        'src' => array_values( array_filter( array_unique( $gallery_srcs ) ) ),
     3827                                                        // overwrite the 'ids' attribute with a comma-separated string, for compatibility with $shortcode_attrs as above
     3828                                                        'ids' => implode( ',', $block['attrs']['ids'] ),
     3829                                                )
     3830                                        );
     3831                                } else {
     3832                                        // Otherwise extract srcs from the innerHTML
     3833                                        $srcs = array();
     3834                                        preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
     3835                                        if ( ! empty( $src ) ) {
     3836                                                foreach ( $src as $s ) {
     3837                                                        $srcs[] = $s[2];
     3838                                                }
     3839                                        }
     3840
     3841                                        $galleries[] = array(
     3842                                                // Note that unlike shortcodes, all we are returning here is the src list
     3843                                                'src' => array_values( array_unique( $srcs ) )
     3844                                        );
     3845                                }
     3846
     3847                        } elseif ( !empty( $block['innerBlocks'] ) ) {
     3848                                // If we have nested blocks then gradually flatten it by moving those onto the end of the root array for traversal
     3849                                while ( $inner = array_pop( $block['innerBlocks'] ) ) {
     3850                                        array_push( $post_blocks, $inner );
     3851                                }
     3852                        }
     3853                }
     3854        }
     3855
    38093856        /**
    38103857         * Filters the list of all found galleries in the given post.
    38113858         *
  • tests/phpunit/tests/media.php

     
    439439         * @ticket 22960
    440440         */
    441441        function test_post_galleries_images() {
    442                 $ids1 = array();
    443                 $ids1_srcs = array();
    444                 foreach ( range( 1, 3 ) as $i ) {
     442                $ids = array();
     443                $ids_srcs = array();
     444                foreach ( range( 1, 6 ) as $i ) {
    445445                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    446446                                'post_mime_type' => 'image/jpeg',
    447447                                'post_type' => 'attachment'
     
    448448                        ) );
    449449                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    450450                        wp_update_attachment_metadata( $attachment_id, $metadata );
    451                         $ids1[] = $attachment_id;
    452                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     451                        $ids[] = $attachment_id;
     452                        $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    453453                }
    454454
    455                 $ids2 = array();
    456                 $ids2_srcs = array();
    457                 foreach ( range( 4, 6 ) as $i ) {
    458                         $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    459                                 'post_mime_type' => 'image/jpeg',
    460                                 'post_type' => 'attachment'
    461                         ) );
    462                         $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    463                         wp_update_attachment_metadata( $attachment_id, $metadata );
    464                         $ids2[] = $attachment_id;
    465                         $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    466                 }
     455                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     456                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
    467457
    468                 $ids1_joined = join( ',', $ids1 );
    469                 $ids2_joined = join( ',', $ids2 );
    470 
    471458                $blob =<<<BLOB
    472459[gallery ids="$ids1_joined"]
    473460
     
    475462BLOB;
    476463                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    477464                $srcs = get_post_galleries_images( $post_id );
    478                 $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
     465                $this->assertEquals( $srcs, array( array_slice( $ids_srcs, 0, 3 ), array_slice( $ids_srcs, 3, 3 ) ) );
    479466        }
    480467
    481468        /**
     
    565552         * @ticket 22960
    566553         */
    567554        function test_post_gallery_images() {
    568                 $ids1 = array();
    569                 $ids1_srcs = array();
    570                 foreach ( range( 1, 3 ) as $i ) {
     555                $ids = array();
     556                $ids_srcs = array();
     557                foreach ( range( 1, 6 ) as $i ) {
    571558                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    572559                                'post_mime_type' => 'image/jpeg',
    573560                                'post_type' => 'attachment'
     
    574561                        ) );
    575562                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    576563                        wp_update_attachment_metadata( $attachment_id, $metadata );
    577                         $ids1[] = $attachment_id;
    578                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     564                        $ids[] = $attachment_id;
     565                        $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    579566                }
    580567
    581                 $ids2 = array();
    582                 $ids2_srcs = array();
    583                 foreach ( range( 4, 6 ) as $i ) {
     568                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     569                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     570
     571                $blob =<<<BLOB
     572[gallery ids="$ids1_joined"]
     573
     574[gallery ids="$ids2_joined"]
     575BLOB;
     576                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     577                $srcs = get_post_gallery_images( $post_id );
     578                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     579        }
     580
     581        /**
     582         * @ticket 43826
     583         * @group blocks
     584         */
     585        function test_block_post_galleries() {
     586                // Set up an unattached image.
     587                $this->factory->attachment->create_object( array(
     588                        'file' => 'test.jpg',
     589                        'post_parent' => 0,
     590                        'post_mime_type' => 'image/jpeg',
     591                        'post_type' => 'attachment'
     592                ) );
     593
     594                $post_id = $this->factory->post->create( array(
     595                        'post_content' => '<!-- wp:gallery -->',
     596                ) );
     597
     598                $galleries = get_post_galleries( $post_id, false );
     599
     600                $this->assertTrue( is_array( $galleries ) );
     601                $this->assertEmpty( $galleries[0]['src'] );
     602        }
     603
     604        /**
     605         * @ticket 43826
     606         * @group blocks
     607         */
     608        function test_block_post_gallery_images() {
     609                // Similar to test_post_gallery_images but with blocks instead of shortcodes
     610                $ids = array();
     611                $imgs = array();
     612                $ids_srcs = array();
     613                foreach ( range( 1, 6 ) as $i ) {
    584614                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    585615                                'post_mime_type' => 'image/jpeg',
    586616                                'post_type' => 'attachment'
     
    587617                        ) );
    588618                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    589619                        wp_update_attachment_metadata( $attachment_id, $metadata );
    590                         $ids2[] = $attachment_id;
    591                         $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     620                        $ids[] = $attachment_id;
     621                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     622                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     623
    592624                }
    593625
    594                 $ids1_joined = join( ',', $ids1 );
    595                 $ids2_joined = join( ',', $ids2 );
     626                $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) );
     627                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
    596628
    597629                $blob =<<<BLOB
     630<!-- wp:gallery -->
     631$imgs1_joined
     632<!-- /wp:gallery -->
     633
     634<!-- wp:gallery -->
     635$imgs2_joined
     636<!-- /wp:gallery -->
     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_gallery_images_json() {
     648                // Similar to test_block_post_gallery_images, with IDs in the json blob
     649                $ids = array();
     650                $imgs = array();
     651                $ids_srcs = array();
     652                foreach ( range( 1, 6 ) as $i ) {
     653                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     654                                'post_mime_type' => 'image/jpeg',
     655                                'post_type' => 'attachment'
     656                        ) );
     657                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     658                        wp_update_attachment_metadata( $attachment_id, $metadata );
     659                        $ids[] = $attachment_id;
     660                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     661                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     662
     663                }
     664
     665                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     666                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     667
     668                $blob =<<<BLOB
     669<!-- wp:gallery {"ids":[$ids1_joined]} -->
     670<!-- /wp:gallery -->
     671
     672<!-- wp:gallery {"ids":[$ids2_joined]} -->
     673<!-- /wp:gallery -->
     674BLOB;
     675                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     676                $srcs = get_post_gallery_images( $post_id );
     677                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     678        }
     679
     680        /**
     681         * @ticket 43826
     682         * @group blocks
     683         */
     684        function test_mixed_post_gallery_images() {
     685                // Similar to test_post_gallery_images but with a shortcode and a block in the same post
     686                $ids = array();
     687                $imgs = array();
     688                $ids_srcs = array();
     689                foreach ( range( 1, 6 ) as $i ) {
     690                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     691                                'post_mime_type' => 'image/jpeg',
     692                                'post_type' => 'attachment'
     693                        ) );
     694                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     695                        wp_update_attachment_metadata( $attachment_id, $metadata );
     696                        $ids[] = $attachment_id;
     697                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     698                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     699
     700                }
     701
     702                $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) );
     703                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
     704
     705                $blob =<<<BLOB
    598706[gallery ids="$ids1_joined"]
    599707
    600 [gallery ids="$ids2_joined"]
     708<!-- wp:gallery -->
     709$imgs2_joined
     710<!-- /wp:gallery -->
    601711BLOB;
    602712                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    603713                $srcs = get_post_gallery_images( $post_id );
    604                 $this->assertEquals( $srcs, $ids1_srcs );
     714                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
    605715        }
    606716
     717        /**
     718         * @ticket 43826
     719         * @group blocks
     720         */
     721        function test_mixed_post_galleries() {
     722                // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries
     723                $ids = array();
     724                $imgs = array();
     725                $ids_srcs = array();
     726                foreach ( range( 1, 6 ) as $i ) {
     727                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     728                                'post_mime_type' => 'image/jpeg',
     729                                'post_type' => 'attachment'
     730                        ) );
     731                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     732                        wp_update_attachment_metadata( $attachment_id, $metadata );
     733                        $ids[] = $attachment_id;
     734                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     735                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     736
     737                }
     738
     739                $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) );
     740                $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) );
     741
     742                $blob =<<<BLOB
     743[gallery ids="$ids1_joined"]
     744
     745<!-- wp:gallery {"ids":[$ids2_joined]} -->
     746<!-- /wp:gallery -->
     747BLOB;
     748
     749                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     750
     751                $galleries = get_post_galleries( $post_id, false );
     752                $this->assertEquals( $galleries, array(
     753                        array(
     754                                'ids' => $ids1_joined,
     755                                'src' => array_slice( $ids_srcs, 0, 3 ),
     756                        ),
     757                        array(
     758                                'ids' => $ids2_joined,
     759                                'src' => array_slice( $ids_srcs, 3, 3 ),
     760                        ),
     761                ) );
     762
     763        }
     764
     765        /**
     766         * @ticket 43826
     767         * @group blocks
     768         */
     769        function test_block_inner_post_gallery_images() {
     770                // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
     771                $ids = array();
     772                $imgs = array();
     773                $ids_srcs = array();
     774                foreach ( range( 1, 3 ) as $i ) {
     775                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     776                                'post_mime_type' => 'image/jpeg',
     777                                'post_type' => 'attachment'
     778                        ) );
     779                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     780                        wp_update_attachment_metadata( $attachment_id, $metadata );
     781                        $ids[] = $attachment_id;
     782                        $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     783                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     784
     785                }
     786
     787                $imgs_joined = join( "\n", $imgs );
     788
     789                $blob =<<<BLOB
     790<!-- wp:columns -->
     791<!-- wp:column -->
     792<!-- wp:gallery -->
     793$imgs_joined
     794<!-- /wp:gallery -->
     795<!-- /wp:column -->
     796<!-- /wp:columns -->
     797BLOB;
     798                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     799                $srcs = get_post_gallery_images( $post_id );
     800                $this->assertEquals( $srcs, $ids_srcs );
     801        }
     802
    607803        function test_get_media_embedded_in_content() {
    608804                $object =<<<OBJ
    609805<object src="this" data="that">