| 455 | | $ids2 = array(); |
| 456 | | $ids2_srcs = array(); |
| 457 | | foreach ( range( 4, 6 ) as $i ) { |
| 458 | | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| 459 | | 'post_mime_type' => 'image/jpeg', |
| 460 | | 'post_type' => 'attachment' |
| 461 | | ) ); |
| 462 | | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| 463 | | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| 464 | | $ids2[] = $attachment_id; |
| 465 | | $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| 466 | | } |
| | 455 | $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) ); |
| | 456 | $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) ); |
| 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"] |
| | 575 | BLOB; |
| | 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 ) { |
| | 630 | <!-- wp:gallery --> |
| | 631 | $imgs1_joined |
| | 632 | <!-- /wp:gallery --> |
| | 633 | |
| | 634 | <!-- wp:gallery --> |
| | 635 | $imgs2_joined |
| | 636 | <!-- /wp:gallery --> |
| | 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_gallery_images_json() { |
| | 648 | // Similar to test_block_post_gallery_images, with IDs in the json blob |
| | 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 | |
| | 663 | } |
| | 664 | |
| | 665 | $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) ); |
| | 666 | $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) ); |
| | 667 | |
| | 668 | $blob =<<<BLOB |
| | 669 | <!-- wp:gallery {"ids":[$ids1_joined]} --> |
| | 670 | <!-- /wp:gallery --> |
| | 671 | |
| | 672 | <!-- wp:gallery {"ids":[$ids2_joined]} --> |
| | 673 | <!-- /wp:gallery --> |
| | 674 | BLOB; |
| | 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_mixed_post_gallery_images() { |
| | 685 | // Similar to test_post_gallery_images but with a shortcode and a block in the same post |
| | 686 | $ids = array(); |
| | 687 | $imgs = array(); |
| | 688 | $ids_srcs = array(); |
| | 689 | foreach ( range( 1, 6 ) 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 | $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) ); |
| | 703 | $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) ); |
| | 704 | |
| | 705 | $blob =<<<BLOB |
| | 717 | /** |
| | 718 | * @ticket 43826 |
| | 719 | * @group blocks |
| | 720 | */ |
| | 721 | function test_mixed_post_galleries() { |
| | 722 | // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries |
| | 723 | $ids = array(); |
| | 724 | $imgs = array(); |
| | 725 | $ids_srcs = array(); |
| | 726 | foreach ( range( 1, 6 ) as $i ) { |
| | 727 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 728 | 'post_mime_type' => 'image/jpeg', |
| | 729 | 'post_type' => 'attachment' |
| | 730 | ) ); |
| | 731 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 732 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 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 | |
| | 737 | } |
| | 738 | |
| | 739 | $ids1_joined = join( ",", array_slice( $ids, 0, 3 ) ); |
| | 740 | $ids2_joined = join( ",", array_slice( $ids, 3, 3 ) ); |
| | 741 | |
| | 742 | $blob =<<<BLOB |
| | 743 | [gallery ids="$ids1_joined"] |
| | 744 | |
| | 745 | <!-- wp:gallery {"ids":[$ids2_joined]} --> |
| | 746 | <!-- /wp:gallery --> |
| | 747 | BLOB; |
| | 748 | |
| | 749 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 750 | |
| | 751 | $galleries = get_post_galleries( $post_id, false ); |
| | 752 | $this->assertEquals( $galleries, array( |
| | 753 | array( |
| | 754 | 'ids' => $ids1_joined, |
| | 755 | 'src' => array_slice( $ids_srcs, 0, 3 ), |
| | 756 | ), |
| | 757 | array( |
| | 758 | 'ids' => $ids2_joined, |
| | 759 | 'src' => array_slice( $ids_srcs, 3, 3 ), |
| | 760 | ), |
| | 761 | ) ); |
| | 762 | |
| | 763 | } |
| | 764 | |
| | 765 | /** |
| | 766 | * @ticket 43826 |
| | 767 | * @group blocks |
| | 768 | */ |
| | 769 | function test_block_inner_post_gallery_images() { |
| | 770 | // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else |
| | 771 | $ids = array(); |
| | 772 | $imgs = array(); |
| | 773 | $ids_srcs = array(); |
| | 774 | foreach ( range( 1, 3 ) as $i ) { |
| | 775 | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
| | 776 | 'post_mime_type' => 'image/jpeg', |
| | 777 | 'post_type' => 'attachment' |
| | 778 | ) ); |
| | 779 | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
| | 780 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| | 781 | $ids[] = $attachment_id; |
| | 782 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| | 783 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| | 784 | |
| | 785 | } |
| | 786 | |
| | 787 | $imgs_joined = join( "\n", $imgs ); |
| | 788 | |
| | 789 | $blob =<<<BLOB |
| | 790 | <!-- wp:columns --> |
| | 791 | <!-- wp:column --> |
| | 792 | <!-- wp:gallery --> |
| | 793 | $imgs_joined |
| | 794 | <!-- /wp:gallery --> |
| | 795 | <!-- /wp:column --> |
| | 796 | <!-- /wp:columns --> |
| | 797 | BLOB; |
| | 798 | $post_id = self::factory()->post->create( array( 'post_content' => $blob ) ); |
| | 799 | $srcs = get_post_gallery_images( $post_id ); |
| | 800 | $this->assertEquals( $srcs, $ids_srcs ); |
| | 801 | } |
| | 802 | |