442 | | $ids1 = array(); |
443 | | $ids1_srcs = array(); |
444 | | foreach ( range( 1, 3 ) as $i ) { |
445 | | $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( |
446 | | 'post_mime_type' => 'image/jpeg', |
447 | | 'post_type' => 'attachment' |
448 | | ) ); |
449 | | $metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta ); |
450 | | wp_update_attachment_metadata( $attachment_id, $metadata ); |
451 | | $ids1[] = $attachment_id; |
452 | | $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
453 | | } |
454 | | |
455 | | $ids2 = array(); |
456 | | $ids2_srcs = array(); |
457 | | foreach ( range( 4, 6 ) as $i ) { |
| 442 | $ids = array(); |
| 443 | $ids_srcs = array(); |
| 444 | foreach ( range( 1, 6 ) as $i ) { |
468 | | $ids1_joined = join( ',', $ids1 ); |
469 | | $ids2_joined = join( ',', $ids2 ); |
| 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 ) { |
590 | | $ids2[] = $attachment_id; |
591 | | $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| 620 | $ids[] = $attachment_id; |
| 621 | $url = $ids_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; |
| 622 | $imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>'; |
| 623 | |
| 624 | } |
| 625 | |
| 626 | $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) ); |
| 627 | $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) ); |
| 628 | |
| 629 | $blob =<<<BLOB |
| 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_mixed_post_gallery_images() { |
| 648 | // Similar to test_post_gallery_images but with a shortcode and a block in the same post |
| 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 | |
600 | | [gallery ids="$ids2_joined"] |
| 671 | <!-- wp:gallery --> |
| 672 | $imgs2_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_block_inner_post_gallery_images() { |
| 685 | // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else |
| 686 | $ids = array(); |
| 687 | $imgs = array(); |
| 688 | $ids_srcs = array(); |
| 689 | foreach ( range( 1, 3 ) 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 | $imgs_joined = join( "\n", $imgs ); |
| 703 | |
| 704 | $blob =<<<BLOB |
| 705 | <!-- wp:columns --> |
| 706 | <!-- wp:column --> |
| 707 | <!-- wp:gallery --> |
| 708 | $imgs_joined |
| 709 | <!-- /wp:gallery --> |
| 710 | <!-- /wp:column --> |
| 711 | <!-- /wp:columns --> |