| 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 ) ); |
| 647 | | $ids2 = array(); |
| 648 | | $ids2_srcs = array(); |
| 649 | | foreach ( range( 4, 6 ) as $i ) { |
| 650 | | $attachment_id = self::factory()->attachment->create_object( |
| 651 | | "image$i.jpg", |
| 652 | | 0, |
| 653 | | array( |
| 654 | | 'post_mime_type' => 'image/jpeg', |
| 655 | | 'post_type' => 'attachment', |
| | 630 | $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) ); |
| | 631 | $ids2_joined = join( ',', $ids2 ); |
| | 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( array( |
| | 650 | 'file' => 'test.jpg', |
| | 651 | 'post_parent' => 0, |
| | 652 | 'post_mime_type' => 'image/jpeg', |
| | 653 | 'post_type' => 'attachment' |
| | 654 | ) ); |
| | 655 | |
| | 656 | $post_id = $this->factory->post->create( array( |
| | 657 | 'post_content' => '<!-- wp:gallery -->', |
| | 658 | ) ); |
| | 659 | |
| | 660 | $galleries = get_post_galleries( $post_id, false ); |
| | 661 | |
| | 662 | $this->assertTrue( is_array( $galleries ) ); |
| | 663 | $this->assertEmpty( $galleries[0]['src'] ); |
| | 664 | } |
| | 665 | |
| | 666 | /** |
| | 667 | * @ticket 43826 |
| | 668 | * @group blocks |
| | 669 | */ |
| | 670 | function test_block_post_gallery_images() { |
| | 671 | // Similar to test_post_gallery_images but with blocks instead of shortcodes |
| | 672 | $ids = array(); |
| | 673 | $imgs = array(); |
| | 674 | $ids_srcs = array(); |
| | 675 | foreach ( range( 1, 6 ) as $i ) { |
| | 676 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 677 | 'post_mime_type' => 'image/jpeg', |
| | 678 | 'post_type' => 'attachment' |
| 664 | | $ids1_joined = join( ',', $ids1 ); |
| 665 | | $ids2_joined = join( ',', $ids2 ); |
| | 705 | /** |
| | 706 | * @ticket 43826 |
| | 707 | * @group blocks |
| | 708 | */ |
| | 709 | function test_block_post_gallery_images_json() { |
| | 710 | // Similar to test_block_post_gallery_images, with IDs in the json blob |
| | 711 | $ids = array(); |
| | 712 | $imgs = array(); |
| | 713 | $ids_srcs = array(); |
| | 714 | foreach ( range( 1, 6 ) as $i ) { |
| | 715 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 716 | 'post_mime_type' => 'image/jpeg', |
| | 717 | 'post_type' => 'attachment' |
| | 718 | ) ); |
| | 719 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 720 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 721 | $ids[] = $attachment_id; |
| | 722 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 723 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 724 | } |
| 667 | | $blob = <<<BLOB |
| | 726 | $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) ); |
| | 727 | $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) ); |
| | 728 | |
| | 729 | $blob =<<<BLOB |
| | 730 | <!-- wp:gallery {"ids":[$ids1_joined]} --> |
| | 731 | <!-- /wp:gallery --> |
| | 732 | |
| | 733 | <!-- wp:gallery {"ids":[$ids2_joined]} --> |
| | 734 | <!-- /wp:gallery --> |
| | 735 | BLOB; |
| | 736 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 737 | $srcs = get_post_gallery_images( $post_id ); |
| | 738 | $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) ); |
| | 739 | } |
| | 740 | |
| | 741 | /** |
| | 742 | * @ticket 43826 |
| | 743 | * @group blocks |
| | 744 | */ |
| | 745 | function test_mixed_post_gallery_images() { |
| | 746 | // Similar to test_post_gallery_images but with a shortcode and a block in the same post |
| | 747 | $ids = array(); |
| | 748 | $imgs = array(); |
| | 749 | $ids_srcs = array(); |
| | 750 | foreach ( range( 1, 6 ) as $i ) { |
| | 751 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 752 | 'post_mime_type' => 'image/jpeg', |
| | 753 | 'post_type' => 'attachment' |
| | 754 | ) ); |
| | 755 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 756 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 757 | $ids[] = $attachment_id; |
| | 758 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 759 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 760 | } |
| | 761 | |
| | 762 | $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) ); |
| | 763 | $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) ); |
| | 764 | |
| | 765 | $blob =<<<BLOB |
| | 766 | <!-- wp:gallery --> |
| | 767 | $imgs2_joined |
| | 768 | <!-- /wp:gallery --> |
| | 769 | BLOB; |
| | 770 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 771 | $srcs = get_post_gallery_images( $post_id ); |
| | 772 | $this->assertEquals( $srcs, array_slice( $ids_srcs, 0, 3 ) ); |
| | 773 | |
| | 774 | /** |
| | 775 | * @ticket 43826 |
| | 776 | * @group blocks |
| | 777 | */ |
| | 778 | function test_mixed_post_galleries() { |
| | 779 | // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries |
| | 780 | $ids = array(); |
| | 781 | $imgs = array(); |
| | 782 | $ids_srcs = array(); |
| | 783 | foreach ( range( 1, 6 ) as $i ) { |
| | 784 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 785 | 'post_mime_type' => 'image/jpeg', |
| | 786 | 'post_type' => 'attachment' |
| | 787 | ) ); |
| | 788 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 789 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 790 | $ids[] = $attachment_id; |
| | 791 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 792 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 793 | } |
| | 794 | $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) ); |
| | 795 | $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) ); |
| | 796 | |
| | 797 | $blob =<<<BLOB |
| 670 | | [gallery ids="$ids2_joined"] |
| | 800 | <!-- wp:gallery {"ids":[$ids2_joined]} --> |
| | 801 | <!-- /wp:gallery --> |
| | 802 | BLOB; |
| | 803 | |
| | 804 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 805 | |
| | 806 | $galleries = get_post_galleries( $post_id, false ); |
| | 807 | $this->assertEquals( $galleries, array( |
| | 808 | array( |
| | 809 | 'ids' => $ids1_joined, |
| | 810 | 'src' => array_slice( $ids_srcs, 0, 3 ), |
| | 811 | ), |
| | 812 | array( |
| | 813 | 'ids' => $ids2_joined, |
| | 814 | 'src' => array_slice( $ids_srcs, 3, 3 ), |
| | 815 | ), |
| | 816 | ) ); |
| | 817 | |
| | 818 | } |
| | 819 | |
| | 820 | /** |
| | 821 | * @ticket 43826 |
| | 822 | * @group blocks |
| | 823 | */ |
| | 824 | function test_mixed_post_galleries_data() { |
| | 825 | // Test attributes returned by get_post_galleries() function in $html=false mode, with both shortcode and block galleries |
| | 826 | $ids = array(); |
| | 827 | $imgs = array(); |
| | 828 | $ids_srcs = array(); |
| | 829 | foreach ( range( 1, 6 ) as $i ) { |
| | 830 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 831 | 'post_mime_type' => 'image/jpeg', |
| | 832 | 'post_type' => 'attachment' |
| | 833 | ) ); |
| | 834 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 835 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 836 | $ids[] = $attachment_id; |
| | 837 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 838 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 839 | |
| | 840 | } |
| | 841 | |
| | 842 | $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) ); |
| | 843 | $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) ); |
| | 844 | |
| | 845 | $blob =<<<BLOB |
| | 846 | [gallery ids="$ids1_joined" type="type" foo="bar"] |
| | 847 | |
| | 848 | <!-- wp:gallery {"ids":[$ids2_joined],"columns":3,"imageCrop":false,"linkTo":"media"} --> |
| | 849 | <!-- /wp:gallery --> |
| | 850 | BLOB; |
| | 851 | |
| | 852 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 853 | |
| | 854 | $galleries = get_post_galleries( $post_id, false ); |
| | 855 | $this->assertEquals( $galleries, array( |
| | 856 | array( |
| | 857 | 'ids' => $ids1_joined, |
| | 858 | 'src' => array_slice( $ids_srcs, 0, 3 ), |
| | 859 | 'type' => 'type', |
| | 860 | 'foo' => 'bar', // The shortcode code passes arbitrary attributes |
| | 861 | ), |
| | 862 | array( |
| | 863 | 'ids' => $ids2_joined, |
| | 864 | 'src' => array_slice( $ids_srcs, 3, 3 ), |
| | 865 | // The block only passes ids, no other attributes |
| | 866 | ), |
| | 867 | ) ); |
| | 868 | |
| | 869 | } |
| | 870 | |
| | 871 | /** |
| | 872 | * @ticket 43826 |
| | 873 | * @group blocks |
| | 874 | */ |
| | 875 | function test_block_inner_post_gallery_images() { |
| | 876 | // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else |
| | 877 | $ids = array(); |
| | 878 | $imgs = array(); |
| | 879 | $ids_srcs = array(); |
| | 880 | foreach ( range( 1, 3 ) as $i ) { |
| | 881 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 882 | 'post_mime_type' => 'image/jpeg', |
| | 883 | 'post_type' => 'attachment' |
| | 884 | ) ); |
| | 885 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 886 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 887 | $ids[] = $attachment_id; |
| | 888 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 889 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 890 | } |
| | 891 | |
| | 892 | $imgs_joined = join( "\n", $imgs ); |
| | 893 | |
| | 894 | $blob =<<<BLOB |
| | 895 | <!-- wp:columns --> |
| | 896 | <!-- wp:column --> |
| | 897 | <!-- wp:gallery --> |
| | 898 | $imgs_joined |
| | 899 | <!-- /wp:gallery --> |
| | 900 | <!-- /wp:column --> |
| | 901 | <!-- /wp:columns --> |