Make WordPress Core

Ticket #43826: 43826.2.diff

File 43826.2.diff, 5.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();
     
    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                                } 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                }
     3838        }
     3839
    38093840        /**
    38103841         * Filters the list of all found galleries in the given post.
    38113842         *
  • tests/phpunit/tests/media.php

     
    604604                $this->assertEquals( $srcs, $ids1_srcs );
    605605        }
    606606
     607        /**
     608         * @ticket 43826
     609         */
     610        function test_block_post_galleries() {
     611                // Set up an unattached image.
     612                $this->factory->attachment->create_object( array(
     613                        'file' => 'test.jpg',
     614                        'post_parent' => 0,
     615                        'post_mime_type' => 'image/jpeg',
     616                        'post_type' => 'attachment'
     617                ) );
     618
     619                $post_id = $this->factory->post->create( array(
     620                        'post_content' => '<!-- wp:gallery -->',
     621                ) );
     622
     623                $galleries = get_post_galleries( $post_id, false );
     624
     625                $this->assertTrue( is_array( $galleries ) );
     626                $this->assertEmpty( $galleries[0]['src'] );
     627        }
     628
     629        /**
     630         * @ticket 43826
     631         */
     632        function test_block_post_gallery_images() {
     633                // Similar to test_post_gallery_images but with blocks instead of shortcodes
     634                $ids1 = array();
     635                $imgs1 = array();
     636                $ids1_srcs = array();
     637                foreach ( range( 1, 3 ) as $i ) {
     638                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     639                                'post_mime_type' => 'image/jpeg',
     640                                'post_type' => 'attachment'
     641                        ) );
     642                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     643                        wp_update_attachment_metadata( $attachment_id, $metadata );
     644                        $ids1[] = $attachment_id;
     645                        $url = $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     646                        $imgs1[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     647
     648                }
     649
     650                $ids2 = array();
     651                $imgs2 = array();
     652                $ids2_srcs = array();
     653                foreach ( range( 4, 6 ) as $i ) {
     654                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     655                                'post_mime_type' => 'image/jpeg',
     656                                'post_type' => 'attachment'
     657                        ) );
     658                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     659                        wp_update_attachment_metadata( $attachment_id, $metadata );
     660                        $ids2[] = $attachment_id;
     661                        $url = $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     662                        $imgs2[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     663                }
     664
     665                $imgs1_joined = join( "\n", $imgs1 );
     666                $imgs2_joined = join( "\n", $imgs2 );
     667
     668                $blob =<<<BLOB
     669<!-- wp:gallery -->
     670$imgs1_joined
     671<!-- /wp:gallery -->
     672
     673<!-- wp:gallery -->
     674$imgs2_joined
     675<!-- /wp:gallery -->
     676BLOB;
     677                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     678                $srcs = get_post_gallery_images( $post_id );
     679                $this->assertEquals( $srcs, $ids1_srcs );
     680        }
     681
     682        /**
     683         * @ticket 43826
     684         */
     685        function test_block_inner_post_gallery_images() {
     686                // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
     687                $ids1 = array();
     688                $imgs1 = array();
     689                $ids1_srcs = array();
     690                foreach ( range( 1, 3 ) as $i ) {
     691                        $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
     692                                'post_mime_type' => 'image/jpeg',
     693                                'post_type' => 'attachment'
     694                        ) );
     695                        $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
     696                        wp_update_attachment_metadata( $attachment_id, $metadata );
     697                        $ids1[] = $attachment_id;
     698                        $url = $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     699                        $imgs1[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     700
     701                }
     702
     703                $imgs1_joined = join( "\n", $imgs1 );
     704
     705                $blob =<<<BLOB
     706<!-- wp:columns -->
     707<!-- wp:column -->
     708<!-- wp:gallery -->
     709$imgs1_joined
     710<!-- /wp:gallery -->
     711<!-- /wp:column -->
     712<!-- /wp:columns -->
     713BLOB;
     714                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     715                $srcs = get_post_gallery_images( $post_id );
     716                $this->assertEquals( $srcs, $ids1_srcs );
     717        }
     718
    607719        function test_get_media_embedded_in_content() {
    608720                $object =<<<OBJ
    609721<object src="this" data="that">