Make WordPress Core

Ticket #43826: 43826.3.diff

File 43826.3.diff, 9.5 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();
     
    38043804                                }
    38053805                        }
    38063806                }
     3807        }
     3808
     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                                } else {
     3817                                        $srcs = array();
     3818                                        preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
     3819                                        if ( ! empty( $src ) ) {
     3820                                                foreach ( $src as $s ) {
     3821                                                        $srcs[] = $s[2];
     3822                                                }
     3823                                        }
     3824
     3825                                        $galleries[] = array(
     3826                                                // Note that unlike shortcodes, all we are returning here is the src list
     3827                                                'src' => array_values( array_unique( $srcs ) )
     3828                                        );
     3829                                }
     3830
     3831                        } elseif ( !empty( $block['innerBlocks'] ) ) {
     3832                                // If we have nested blocks then gradually flatten it by moving those onto the end of the root array for traversal
     3833                                while ( $inner = array_pop( $block['innerBlocks'] ) ) {
     3834                                        array_push( $post_blocks, $inner );
     3835                                }
     3836                        }
     3837                }
    38073838        }
    38083839
    38093840        /**
  • 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 ) {
    445                         $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    446                                 'post_mime_type' => 'image/jpeg',
    447                                 'post_type' => 'attachment'
    448                         ) );
    449                         $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    450                         wp_update_attachment_metadata( $attachment_id, $metadata );
    451                         $ids1[] = $attachment_id;
    452                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
    453                 }
    454 
    455                 $ids2 = array();
    456                 $ids2_srcs = array();
    457                 foreach ( range( 4, 6 ) as $i ) {
     442                $ids = array();
     443                $ids_srcs = array();
     444                foreach ( range( 1, 6 ) as $i ) {
    458445                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
    459446                                'post_mime_type' => 'image/jpeg',
    460447                                'post_type' => 'attachment'
    461448                        ) );
    462449                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
    463450                        wp_update_attachment_metadata( $attachment_id, $metadata );
    464                         $ids2[] = $attachment_id;
    465                         $ids2_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";
    466453                }
    467454
    468                 $ids1_joined = join( ',', $ids1 );
    469                 $ids2_joined = join( ',', $ids2 );
     455                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     456                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
    470457
    471458                $blob =<<<BLOB
    472459[gallery ids="$ids1_joined"]
     
    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
     624                }
     625
     626                $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) );
     627                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
     628
     629                $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_mixed_post_gallery_images() {
     648                // Similar to test_post_gallery_images but with a shortcode and a block in the same post
     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
    592663                }
    593664
    594                 $ids1_joined = join( ',', $ids1 );
    595                 $ids2_joined = join( ',', $ids2 );
     665                $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) );
     666                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
    596667
    597668                $blob =<<<BLOB
    598669[gallery ids="$ids1_joined"]
    599670
    600 [gallery ids="$ids2_joined"]
     671<!-- wp:gallery -->
     672$imgs2_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_block_inner_post_gallery_images() {
     685                // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
     686                $ids = array();
     687                $imgs = array();
     688                $ids_srcs = array();
     689                foreach ( range( 1, 3 ) 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                $imgs_joined = join( "\n", $imgs );
     703
     704                $blob =<<<BLOB
     705<!-- wp:columns -->
     706<!-- wp:column -->
     707<!-- wp:gallery -->
     708$imgs_joined
     709<!-- /wp:gallery -->
     710<!-- /wp:column -->
     711<!-- /wp:columns -->
    601712BLOB;
    602713                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    603714                $srcs = get_post_gallery_images( $post_id );
    604                 $this->assertEquals( $srcs, $ids1_srcs );
     715                $this->assertEquals( $srcs, $ids_srcs );
    605716        }
    606717
    607718        function test_get_media_embedded_in_content() {