Make WordPress Core

Ticket #43826: 43826.diff

File 43826.diff, 16.3 KB (added by desrosj, 6 years ago)
  • src/wp-includes/media.php

     
    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
     
    39503950                                }
    39513951                        }
    39523952                }
     3953        }
     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                        } elseif ( ! empty( $block['innerBlocks'] ) ) {
     3990                                // If we have nested blocks then gradually flatten it by moving those onto the end of the root array for traversal
     3991                                while ( $inner = array_pop( $block['innerBlocks'] ) ) {
     3992                                        array_push( $post_blocks, $inner );
     3993                                }
     3994                        }
     3995                }
    39533996        }
    39543997
    39553998        /**
  • tests/phpunit/tests/media.php

     
    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,
     
    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"]
     
    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        /**
     
    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 ) {
     616                        $attachment_id = self::factory()->attachment->create_object(
     617                                "image$i.jpg",
     618                                0,
     619                                array(
     620                                        'post_mime_type' => 'image/jpeg',
     621                                        'post_type'      => 'attachment',
     622                                )
     623                        );
     624                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     625                        wp_update_attachment_metadata( $attachment_id, $metadata );
     626                        $ids[]      = $attachment_id;
     627                        $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     628                }
     629
     630                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     631                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     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(
     650                        array(
     651                                'file'           => 'test.jpg',
     652                                'post_parent'    => 0,
     653                                'post_mime_type' => 'image/jpeg',
     654                                'post_type'      => 'attachment',
     655                        )
     656                );
     657
     658                $post_id = $this->factory->post->create(
     659                        array(
     660                                'post_content' => '<!-- wp:gallery -->',
     661                        )
     662                );
     663
     664                $galleries = get_post_galleries( $post_id, false );
     665
     666                $this->assertTrue( is_array( $galleries ) );
     667                $this->assertEmpty( $galleries[0]['src'] );
     668        }
     669
     670        /**
     671         * @ticket 43826
     672         * @group blocks
     673         */
     674        function test_block_post_gallery_images() {
     675                // Similar to test_post_gallery_images but with blocks instead of shortcodes
     676                $ids      = array();
     677                $imgs     = array();
     678                $ids_srcs = array();
     679                foreach ( range( 1, 6 ) as $i ) {
    633680                        $attachment_id = self::factory()->attachment->create_object(
    634681                                "image$i.jpg",
    635682                                0,
     
    640687                        );
    641688                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
    642689                        wp_update_attachment_metadata( $attachment_id, $metadata );
    643                         $ids1[]      = $attachment_id;
    644                         $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     690                        $ids[]  = $attachment_id;
     691                        $url    = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     692                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     693
    645694                }
    646695
    647                 $ids2      = array();
    648                 $ids2_srcs = array();
    649                 foreach ( range( 4, 6 ) as $i ) {
     696                $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) );
     697                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
     698
     699                $blob    = <<<BLOB
     700<!-- wp:gallery -->
     701$imgs1_joined
     702<!-- /wp:gallery -->
     703
     704<!-- wp:gallery -->
     705$imgs2_joined
     706<!-- /wp:gallery -->
     707BLOB;
     708                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     709                $srcs    = get_post_gallery_images( $post_id );
     710                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     711        }
     712
     713        /**
     714         * @ticket 43826
     715         * @group blocks
     716         */
     717        function test_block_post_gallery_images_json() {
     718                // Similar to test_block_post_gallery_images, with IDs in the json blob
     719                $ids      = array();
     720                $imgs     = array();
     721                $ids_srcs = array();
     722                foreach ( range( 1, 6 ) as $i ) {
    650723                        $attachment_id = self::factory()->attachment->create_object(
    651724                                "image$i.jpg",
    652725                                0,
     
    657730                        );
    658731                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
    659732                        wp_update_attachment_metadata( $attachment_id, $metadata );
    660                         $ids2[]      = $attachment_id;
    661                         $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     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
    662737                }
    663738
    664                 $ids1_joined = join( ',', $ids1 );
    665                 $ids2_joined = join( ',', $ids2 );
     739                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     740                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     741
     742                $blob    = <<<BLOB
     743<!-- wp:gallery {"ids":[$ids1_joined]} -->
     744<!-- /wp:gallery -->
     745
     746<!-- wp:gallery {"ids":[$ids2_joined]} -->
     747<!-- /wp:gallery -->
     748BLOB;
     749                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     750                $srcs    = get_post_gallery_images( $post_id );
     751                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     752        }
     753
     754        /**
     755         * @ticket 43826
     756         * @group blocks
     757         */
     758        function test_mixed_post_gallery_images() {
     759                // Similar to test_post_gallery_images but with a shortcode and a block in the same post
     760                $ids      = array();
     761                $imgs     = array();
     762                $ids_srcs = array();
     763                foreach ( range( 1, 6 ) as $i ) {
     764                        $attachment_id = self::factory()->attachment->create_object(
     765                                "image$i.jpg",
     766                                0,
     767                                array(
     768                                        'post_mime_type' => 'image/jpeg',
     769                                        'post_type'      => 'attachment',
     770                                )
     771                        );
     772                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     773                        wp_update_attachment_metadata( $attachment_id, $metadata );
     774                        $ids[]  = $attachment_id;
     775                        $url    = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     776                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     777
     778                }
     779
     780                $ids1_joined  = join( "\n", array_slice( $ids, 0, 3 ) );
     781                $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
    666782
    667783                $blob    = <<<BLOB
    668784[gallery ids="$ids1_joined"]
    669785
    670 [gallery ids="$ids2_joined"]
     786<!-- wp:gallery -->
     787$imgs2_joined
     788<!-- /wp:gallery -->
     789BLOB;
     790                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     791                $srcs    = get_post_gallery_images( $post_id );
     792                $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) );
     793        }
     794
     795        /**
     796         * @ticket 43826
     797         * @group blocks
     798         */
     799        function test_mixed_post_galleries() {
     800                // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries
     801                $ids      = array();
     802                $imgs     = array();
     803                $ids_srcs = array();
     804                foreach ( range( 1, 6 ) as $i ) {
     805                        $attachment_id = self::factory()->attachment->create_object(
     806                                "image$i.jpg",
     807                                0,
     808                                array(
     809                                        'post_mime_type' => 'image/jpeg',
     810                                        'post_type'      => 'attachment',
     811                                )
     812                        );
     813                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     814                        wp_update_attachment_metadata( $attachment_id, $metadata );
     815                        $ids[]  = $attachment_id;
     816                        $url    = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     817                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     818
     819                }
     820
     821                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     822                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     823
     824                $blob = <<<BLOB
     825[gallery ids="$ids1_joined"]
     826
     827<!-- wp:gallery {"ids":[$ids2_joined]} -->
     828<!-- /wp:gallery -->
     829BLOB;
     830
     831                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     832
     833                $galleries = get_post_galleries( $post_id, false );
     834                $this->assertEquals(
     835                        $galleries,
     836                        array(
     837                                array(
     838                                        'ids' => $ids1_joined,
     839                                        'src' => array_slice( $ids_srcs, 0, 3 ),
     840                                ),
     841                                array(
     842                                        'ids' => $ids2_joined,
     843                                        'src' => array_slice( $ids_srcs, 3, 3 ),
     844                                ),
     845                        )
     846                );
     847
     848        }
     849
     850        /**
     851         * @ticket 43826
     852         * @group blocks
     853         */
     854        function test_mixed_post_galleries_data() {
     855                // Test attributes returned by get_post_galleries() function in $html=false mode, with both shortcode and block galleries
     856                $ids      = array();
     857                $imgs     = array();
     858                $ids_srcs = array();
     859                foreach ( range( 1, 6 ) as $i ) {
     860                        $attachment_id = self::factory()->attachment->create_object(
     861                                "image$i.jpg",
     862                                0,
     863                                array(
     864                                        'post_mime_type' => 'image/jpeg',
     865                                        'post_type'      => 'attachment',
     866                                )
     867                        );
     868                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     869                        wp_update_attachment_metadata( $attachment_id, $metadata );
     870                        $ids[]  = $attachment_id;
     871                        $url    = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     872                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     873
     874                }
     875
     876                $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
     877                $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
     878
     879                $blob = <<<BLOB
     880[gallery ids="$ids1_joined" type="type" foo="bar"]
     881
     882<!-- wp:gallery {"ids":[$ids2_joined],"columns":3,"imageCrop":false,"linkTo":"media"} -->
     883<!-- /wp:gallery -->
     884BLOB;
     885
     886                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
     887
     888                $galleries = get_post_galleries( $post_id, false );
     889                $this->assertEquals(
     890                        $galleries,
     891                        array(
     892                                array(
     893                                        'ids'  => $ids1_joined,
     894                                        'src'  => array_slice( $ids_srcs, 0, 3 ),
     895                                        'type' => 'type',
     896                                        'foo'  => 'bar', // The shortcode code passes arbitrary attributes
     897                                ),
     898                                array(
     899                                        'ids' => $ids2_joined,
     900                                        'src' => array_slice( $ids_srcs, 3, 3 ),
     901                                        // The block only passes ids, no other attributes
     902                                ),
     903                        )
     904                );
     905
     906        }
     907
     908        /**
     909         * @ticket 43826
     910         * @group blocks
     911         */
     912        function test_block_inner_post_gallery_images() {
     913                // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
     914                $ids      = array();
     915                $imgs     = array();
     916                $ids_srcs = array();
     917                foreach ( range( 1, 3 ) as $i ) {
     918                        $attachment_id = self::factory()->attachment->create_object(
     919                                "image$i.jpg",
     920                                0,
     921                                array(
     922                                        'post_mime_type' => 'image/jpeg',
     923                                        'post_type'      => 'attachment',
     924                                )
     925                        );
     926                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     927                        wp_update_attachment_metadata( $attachment_id, $metadata );
     928                        $ids[]  = $attachment_id;
     929                        $url    = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     930                        $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
     931
     932                }
     933
     934                $imgs_joined = join( "\n", $imgs );
     935
     936                $blob    = <<<BLOB
     937<!-- wp:columns -->
     938<!-- wp:column -->
     939<!-- wp:gallery -->
     940$imgs_joined
     941<!-- /wp:gallery -->
     942<!-- /wp:column -->
     943<!-- /wp:columns -->
    671944BLOB;
    672945                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
    673946                $srcs    = get_post_gallery_images( $post_id );
    674                 $this->assertEquals( $srcs, $ids1_srcs );
     947                $this->assertEquals( $srcs, $ids_srcs );
    675948        }
    676949
    677950        function test_get_media_embedded_in_content() {
  • tests/qunit/fixtures/wp-api-generated.js

     
    57765776        "taxonomy": "category",
    57775777        "parent": 0,
    57785778        "meta": {
    5779             "test_single": "",
    5780             "test_multi": [],
    5781             "meta_key": "",
    5782             "test_cat_single": "",
    5783             "test_cat_multi": []
     5779            "meta_key": ""
    57845780        },
    57855781        "_links": {
    57865782            "self": [
     
    58245820    "taxonomy": "category",
    58255821    "parent": 0,
    58265822    "meta": {
    5827         "test_single": "",
    5828         "test_multi": [],
    5829         "meta_key": "",
    5830         "test_cat_single": "",
    5831         "test_cat_multi": []
     5823        "meta_key": ""
    58325824    }
    58335825};
    58345826
     
    58425834        "slug": "restapi-client-fixture-tag",
    58435835        "taxonomy": "post_tag",
    58445836        "meta": {
    5845             "test_single": "",
    5846             "test_multi": [],
    5847             "meta_key": "meta_value",
    5848             "test_tag_meta": ""
     5837            "meta_key": "meta_value"
    58495838        },
    58505839        "_links": {
    58515840            "self": [
     
    58885877    "slug": "restapi-client-fixture-tag",
    58895878    "taxonomy": "post_tag",
    58905879    "meta": {
    5891         "test_single": "",
    5892         "test_multi": [],
    5893         "meta_key": "meta_value",
    5894         "test_tag_meta": ""
     5880        "meta_key": "meta_value"
    58955881    }
    58965882};
    58975883