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 ) ); |
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"] |
| 637 | BLOB; |
| 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 ) { |
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 --> |
| 748 | BLOB; |
| 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 ) ); |
670 | | [gallery ids="$ids2_joined"] |
| 786 | <!-- wp:gallery --> |
| 787 | $imgs2_joined |
| 788 | <!-- /wp:gallery --> |
| 789 | BLOB; |
| 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 --> |
| 829 | BLOB; |
| 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 --> |
| 884 | BLOB; |
| 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 --> |