<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52190] trunk: Media: Add support for v1 and v2 gallery block in `get_post_galleries()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52190">52190</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52190","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-16 21:23:12 +0000 (Tue, 16 Nov 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Add support for v1 and v2 gallery block in `get_post_galleries()`.

The `get_post_galleries()` function only handled galleries from the `[gallery]` shortcode. It did not process gallery blocks.

Introducing v1 and v2 gallery block support in `get_post_galleries()` including support for innerblock nesting.

There are no changes to how the function is called. It detects if the post content has one or more gallery blocks. If detected, it parses the blocks and then processes to add each gallery block's HTML to the array of galleries before being passed through the filter and returned.

Includes integration tests.

Follow-up to <a href="https://core.trac.wordpress.org/changeset/24682">[24682]</a>, <a href="https://core.trac.wordpress.org/changeset/43309">[43309]</a>, <a href="https://core.trac.wordpress.org/changeset/48262">[48262]</a>, <a href="https://core.trac.wordpress.org/changeset/52042">[52042]</a>.

Props glendaviesnz, costdev, antpb, audrasjb, birgire, celloexpressions, desrosj, hellofromTonya, jeffpaul, lynk, pento, ramonopoly, russhylov, takahashi_fumiki, tellyworth.
Fixes <a href="https://core.trac.wordpress.org/ticket/43826">#43826</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmediagetPostGalleriesphp">trunk/tests/phpunit/tests/media/getPostGalleries.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php   2021-11-16 19:59:11 UTC (rev 52189)
+++ trunk/src/wp-includes/media.php     2021-11-16 21:23:12 UTC (rev 52190)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4725,7 +4725,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return array();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return array();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4767,6 +4767,95 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( has_block( 'gallery', $post->post_content ) ) {
+               $post_blocks = parse_blocks( $post->post_content );
+
+               while ( $block = array_shift( $post_blocks ) ) {
+                       $has_inner_blocks = ! empty( $block['innerBlocks'] );
+
+                       // Skip blocks with no blockName and no innerHTML.
+                       if ( ! $block['blockName'] ) {
+                               continue;
+                       }
+
+                       // All blocks nested inside non-Gallery blocks should be in the root array.
+                       if ( $has_inner_blocks && 'core/gallery' !== $block['blockName'] ) {
+                               array_push( $post_blocks, ...$block['innerBlocks'] );
+                               continue;
+                       }
+
+                       // New Gallery block format as HTML.
+                       if ( $has_inner_blocks && $html ) {
+                               $block_html  = wp_list_pluck( $block['innerBlocks'], 'innerHTML' );
+                               $galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>';
+                               continue;
+                       }
+
+                       $srcs = array();
+
+                       // New Gallery block format as an array.
+                       if ( $has_inner_blocks ) {
+                               $attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' );
+                               $ids   = wp_list_pluck( $attrs, 'id' );
+
+                               foreach ( $ids as $id ) {
+                                       $url = wp_get_attachment_url( $id );
+
+                                       if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
+                                               $srcs[] = $url;
+                                       }
+                               }
+
+                               $galleries[] = array(
+                                       'ids' => implode( ',', $ids ),
+                                       'src' => $srcs,
+                               );
+
+                               continue;
+                       }
+
+                       // Old Gallery block format as HTML.
+                       if ( $html ) {
+                               $galleries[] = $block['innerHTML'];
+                               continue;
+                       }
+
+                       // Old Gallery block format as an array.
+                       $ids = ! empty( $block['attrs']['ids'] ) ? $block['attrs']['ids'] : array();
+
+                       // If present, use the image IDs from the JSON blob as canonical.
+                       if ( ! empty( $ids ) ) {
+                               foreach ( $ids as $id ) {
+                                       $url = wp_get_attachment_url( $id );
+
+                                       if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
+                                               $srcs[] = $url;
+                                       }
+                               }
+
+                               $galleries[] = array(
+                                       'ids' => implode( ',', $ids ),
+                                       'src' => $srcs,
+                               );
+
+                               continue;
+                       }
+
+                       // Otherwise, extract srcs from the innerHTML.
+                       preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER );
+
+                       if ( ! empty( $found_srcs[0] ) ) {
+                               foreach ( $found_srcs as $src ) {
+                                       if ( isset( $src[2] ) && ! in_array( $src[2], $srcs, true ) ) {
+                                               $srcs[] = $src[2];
+                                       }
+                               }
+                       }
+
+                       $galleries[] = array( 'src' => $srcs );
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the list of all found galleries in the given post.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunktestsphpunittestsmediagetPostGalleriesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/getPostGalleries.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/getPostGalleries.php                              (rev 0)
+++ trunk/tests/phpunit/tests/media/getPostGalleries.php        2021-11-16 21:23:12 UTC (rev 52190)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,969 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * @group media
+ *
+ * @covers ::get_post_galleries
+ */
+class Tests_Functions_getPostGalleries extends WP_UnitTestCase {
+
+       public function set_up() {
+               parent::set_up();
+               $this->img_meta = array(
+                       'width'  => 100,
+                       'height' => 100,
+                       'sizes'  => '',
+               );
+       }
+
+       /**
+        * Test that an empty array is returned for a post that does not exist.
+        *
+        * @ticket 43826
+        */
+       public function test_returns_empty_array_with_non_existent_post() {
+               $galleries = get_post_galleries( 99999, false );
+               $this->assertEmpty( $galleries );
+       }
+
+       /**
+        * Test that an empty array is returned for a post that has no gallery.
+        *
+        * @ticket 43826
+        */
+       public function test_returns_empty_array_with_post_with_no_gallery() {
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => '<p>A post with no gallery</p>',
+                       )
+               );
+
+               $galleries = get_post_galleries( $post_id, false );
+               $this->assertEmpty( $galleries );
+       }
+
+       /**
+        * Test that no srcs are returned for a shortcode gallery
+        * in a post with no attached images.
+        *
+        * @ticket 39304
+        *
+        * @group shortcode
+        */
+       public function test_returns_no_srcs_with_shortcode_in_post_with_no_attached_images() {
+               // Set up an unattached image.
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => 0,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => '[gallery]',
+                       )
+               );
+
+               $galleries = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertEmpty(
+                       $galleries[0]['src'],
+                       'The src key is not empty.'
+               );
+       }
+
+       /**
+        * Test that no srcs are returned for a gallery block
+        * in a post with no attached images.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_no_srcs_with_block_in_post_with_no_attached_images() {
+               // Set up an unattached image.
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => 0,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => '<!-- wp:gallery -->',
+                       )
+               );
+
+               $galleries = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of strings
+               // instead of an array of arrays.
+               $this->assertIsArray(
+                       $galleries[0],
+                       'The returned data does not contain an array.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertEmpty(
+                       $galleries[0]['src'],
+                       'The src key of the first gallery is not empty.'
+               );
+       }
+
+       /**
+        * Test that no srcs are returned for a gallery block v2
+        * in a post with no attached images.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_no_srcs_with_block_v2_in_post_with_no_attached_images() {
+               // Set up an unattached image.
+               $image_id = $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => 0,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
+
+               $blob = <<< BLOB
+<!-- wp:gallery {"linkTo":"none","className":"columns-2"} -->
+<figure
+       class="wp-block-gallery has-nested-images columns-default is-cropped columns-2"
+>
+       <!-- wp:image {"id":$image_id,"sizeSlug":"large","linkDestination":"none"} -->
+       <figure class="wp-block-image size-large">
+               <img
+                       src="$image_url"
+                       alt="Image gallery image"
+                       class="wp-image-$image_id"
+               />
+       </figure>
+       <!-- /wp:image -->
+</figure>
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+
+               $expected_srcs = array( $image_url );
+               $galleries     = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of strings
+               // instead of an array of arrays.
+               $this->assertIsArray(
+                       $galleries[0],
+                       'The returned data does not contain an array.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       $expected_srcs,
+                       $galleries[0]['src'],
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that HTML is returned for a shortcode gallery.
+        *
+        * @ticket 43826
+        *
+        * @group shortcode
+        */
+       public function test_returns_html_with_shortcode_gallery() {
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => 'I have no gallery',
+                       )
+               );
+
+               $post_id_two = $this->factory->post->create(
+                       array(
+                               'post_content' => "[gallery id='$post_id']",
+                       )
+               );
+
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $expected  = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"';
+               $galleries = get_post_galleries( $post_id_two );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of arrays
+               // instead of an array of strings.
+               $this->assertIsString(
+                       $galleries[0],
+                       'Did not return the data as a string.'
+               );
+
+               $this->assertStringContainsString(
+                       $expected,
+                       $galleries[0],
+                       'The returned data did not contain a src attribute with the expected image URL.'
+               );
+       }
+
+       /**
+        * Test that HTML is returned for a block gallery.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_html_with_block_gallery() {
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => 'I have no gallery.',
+                       )
+               );
+
+               // Set up an unattached image.
+               $image_id = $this->factory->attachment->create(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
+
+               $blob = <<< BLOB
+<!-- wp:gallery -->
+<figure><img src="$image_url" data-id="$image_id" /></figure>
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id_two = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+
+               $expected  = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"';
+               $galleries = get_post_galleries( $post_id_two );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of arrays
+               // instead of an array of strings.
+               $this->assertIsString(
+                       $galleries[0],
+                       'Did not return the data as a string.'
+               );
+
+               $this->assertStringContainsString(
+                       $expected,
+                       $galleries[0],
+                       'The returned data did not contain a src attribute with the expected image URL.'
+               );
+       }
+
+       /**
+        * Test that HTML is returned for a block gallery v2.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_html_with_block_gallery_v2() {
+               $image_id = $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => 0,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
+
+               $blob = <<< BLOB
+<!-- wp:gallery {"linkTo":"none","className":"columns-2"} -->
+<figure
+       class="wp-block-gallery has-nested-images columns-default is-cropped columns-2"
+>
+       <!-- wp:image {"id":$image_id,"sizeSlug":"large","linkDestination":"none"} -->
+       <figure class="wp-block-image size-large">
+               <img
+                       src="$image_url"
+                       alt="Image gallery image"
+                       class="wp-image-$image_id"
+               />
+       </figure>
+       <!-- /wp:image -->
+</figure>
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+
+               $expected  = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"';
+               $galleries = get_post_galleries( $post_id );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of arrays
+               // instead of an array of strings.
+               $this->assertIsString(
+                       $galleries[0],
+                       'Did not return the data as a string.'
+               );
+
+               $this->assertStringContainsString(
+                       $expected,
+                       $galleries[0],
+                       'The returned data did not contain a src attribute with the expected image URL.'
+               );
+       }
+
+       /**
+        * Test that the global post object does not override
+        * a provided post ID with a shortcode gallery.
+        *
+        * @ticket 39304
+        *
+        * @group shortcode
+        */
+       public function test_respects_post_id_with_shortcode_gallery() {
+               $global_post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => 'Global Post',
+                       )
+               );
+               $post_id        = $this->factory->post->create(
+                       array(
+                               'post_content' => '[gallery]',
+                       )
+               );
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+               $expected_srcs = array(
+                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
+               );
+
+               // Set the global $post context to the other post.
+               $GLOBALS['post'] = get_post( $global_post_id );
+
+               $galleries = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       $expected_srcs,
+                       $galleries[0]['src'],
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that the global post object does not override
+        * a provided post ID with a block gallery.
+        *
+        * @ticket 43826
+        *
+        * @group block
+        */
+       public function test_respects_post_id_with_block_gallery() {
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 3 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+
+               }
+
+               $ids_joined = join( ',', $ids );
+
+               $global_post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => 'Global Post',
+                       )
+               );
+
+               $blob = <<< BLOB
+<!-- wp:gallery {"ids":[$ids_joined]} -->
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+               $expected_srcs = array(
+                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
+               );
+
+               // Set the global $post context to the other post.
+               $GLOBALS['post'] = get_post( $global_post_id );
+
+               $galleries = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       array(
+                               array(
+                                       'ids' => $ids_joined,
+                                       'src' => $ids_srcs,
+                               ),
+                       ),
+                       $galleries,
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that the global post object does not override
+        * a provided post ID with a block gallery v2.
+        *
+        * @ticket 43826
+        *
+        * @group block
+        */
+       public function test_respects_post_id_with_block_gallery_v2() {
+               $attachment_id  = self::factory()->attachment->create_object(
+                       'image1.jpg',
+                       0,
+                       array(
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+               $metadata       = array_merge( array( 'file' => 'image1.jpg' ), $this->img_meta );
+               $url            = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . 'image1.jpg';
+               $global_post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => 'Global Post',
+                       )
+               );
+
+               wp_update_attachment_metadata( $attachment_id, $metadata );
+
+               $blob = <<< BLOB
+<!-- wp:gallery {"linkTo":"none","className":"columns-2"} -->
+<figure
+       class="wp-block-gallery has-nested-images columns-default is-cropped columns-2"
+>
+       <!-- wp:image {"id":$attachment_id,"sizeSlug":"large","linkDestination":"none"} -->
+       <figure class="wp-block-image size-large">
+               <img
+                       src="$url"
+                       alt="Image gallery image"
+                       class="wp-image-$attachment_id"
+               />
+       </figure>
+       <!-- /wp:image -->
+</figure>
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+               $expected_srcs = array(
+                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
+               );
+
+               // Set the global $post context to the other post.
+               $GLOBALS['post'] = get_post( $global_post_id );
+
+               $galleries = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       array(
+                               array(
+                                       'ids' => (string) $attachment_id,
+                                       'src' => array( $url ),
+                               ),
+                       ),
+                       $galleries,
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that the gallery only contains images specified in
+        * the shortcode's id attribute.
+        *
+        * @ticket 39304
+        *
+        * @group shortcode
+        */
+       public function test_respects_shortcode_id_attribute() {
+               $post_id     = $this->factory->post->create(
+                       array(
+                               'post_content' => 'No gallery defined',
+                       )
+               );
+               $post_id_two = $this->factory->post->create(
+                       array(
+                               'post_content' => "[gallery id='$post_id']",
+                       )
+               );
+               $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+               $expected_srcs = array(
+                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
+               );
+
+               $galleries = get_post_galleries( $post_id_two, false );
+
+               // Set the global $post context.
+               $GLOBALS['post']               = get_post( $post_id_two );
+               $galleries_with_global_context = get_post_galleries( $post_id_two, false );
+
+               // Check that the global post state doesn't affect the results.
+               $this->assertSameSetsWithIndex(
+                       $galleries,
+                       $galleries_with_global_context,
+                       'The global post state affected the results.'
+               );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of strings
+               // instead of an array of arrays.
+               $this->assertIsArray(
+                       $galleries[0],
+                       'The returned data does not contain an array.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       $expected_srcs,
+                       $galleries[0]['src'],
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that galleries only contain images specified in the
+        * id attribute of their respective shortcode and block.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        * @group shortcode
+        */
+       public function test_respects_shortcode_and_block_id_attributes() {
+               // Test the get_post_galleries() function in $html=false mode, with both shortcode and block galleries
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+
+               }
+
+               $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
+               $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
+
+               $blob = <<<BLOB
+[gallery ids="$ids1_joined"]
+
+<!-- wp:gallery {"ids":[$ids2_joined]} -->
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+
+               $galleries = get_post_galleries( $post_id, false );
+               $this->assertSameSetsWithIndex(
+                       array(
+                               array(
+                                       'ids' => $ids1_joined,
+                                       'src' => array_slice( $ids_srcs, 0, 3 ),
+                               ),
+                               array(
+                                       'ids' => $ids2_joined,
+                                       'src' => array_slice( $ids_srcs, 3, 3 ),
+                               ),
+                       ),
+                       $galleries
+               );
+
+       }
+
+       /**
+        * Test that galleries contain the additional attributes
+        * specified for their respective shortcode and block.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        * @group shortcode
+        */
+       public function test_respects_additional_shortcode_and_block_attributes() {
+               // Test attributes returned by get_post_galleries() function in $html=false mode, with both shortcode and block galleries
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+
+               }
+
+               $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
+               $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
+               $blob        = <<<BLOB
+[gallery ids="$ids1_joined" type="type" foo="bar"]
+
+<!-- wp:gallery {"ids":[$ids2_joined],"columns":3,"imageCrop":false,"linkTo":"media"} -->
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+
+               $galleries = get_post_galleries( $post_id, false );
+               $this->assertSameSetsWithIndex(
+                       array(
+                               array(
+                                       'ids'  => $ids1_joined,
+                                       // The shortcode code passes arbitrary attributes
+                                       'type' => 'type',
+                                       'foo'  => 'bar',
+                                       'src'  => array_slice( $ids_srcs, 0, 3 ),
+                               ),
+                               array(
+                                       'ids' => $ids2_joined,
+                                       // The block only passes ids, no other attributes
+                                       'src' => array_slice( $ids_srcs, 3, 3 ),
+                               ),
+                       ),
+                       $galleries
+               );
+
+       }
+
+       /**
+        * Test that srcs are retrieved from the HTML of a block gallery
+        * that has no JSON blob.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_srcs_from_html_with_block_with_no_json_blob() {
+               // Set up an unattached image.
+               $image_id = $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => 0,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg';
+
+               $blob = <<< BLOB
+<!-- wp:gallery -->
+<ul class="wp-block-gallery columns-2 is-cropped"><li class="blocks-gallery-item">
+<figure>
+<img src="$image_url" alt="title"/>
+</figure>
+</li>
+</ul>
+<!-- /wp:gallery -->
+BLOB;
+
+               $post_id = $this->factory->post->create(
+                       array(
+                               'post_content' => $blob,
+                       )
+               );
+
+               $expected_srcs = array( $image_url );
+               $galleries     = get_post_galleries( $post_id, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of strings
+               // instead of an array of arrays.
+               $this->assertIsArray(
+                       $galleries[0],
+                       'The returned data does not contain an array.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertSameSetsWithIndex(
+                       $expected_srcs,
+                       $galleries[0]['src'],
+                       'The expected and actual srcs are not the same.'
+               );
+       }
+
+       /**
+        * Test that srcs are returned for a block gallery nested within
+        * other blocks.
+        *
+        * @ticket 43826
+        *
+        * @group blocks
+        */
+       public function test_returns_srcs_with_nested_block_gallery() {
+               $post_id  = $this->factory->post->create(
+                       array(
+                               'post_content' => 'I have no gallery.',
+                       )
+               );
+               $image_id = $this->factory->attachment->create_object(
+                       array(
+                               'file'           => 'test.jpg',
+                               'post_parent'    => $post_id,
+                               'post_mime_type' => 'image/jpeg',
+                               'post_type'      => 'attachment',
+                       )
+               );
+
+               $blob = <<<BLOB
+<!-- wp:columns -->
+<!-- wp:column -->
+<!-- wp:gallery {"ids":[$image_id]} -->
+<!-- /wp:gallery -->
+<!-- /wp:column -->
+<!-- /wp:columns -->
+BLOB;
+
+               $post_id_two = $this->factory->post->create( array( 'post_content' => $blob ) );
+
+               $galleries = get_post_galleries( $post_id_two, false );
+
+               // The method can return an empty array.
+               $this->assertNotEmpty(
+                       $galleries,
+                       'The galleries array is empty.'
+               );
+
+               // The method can return an array of strings
+               // instead of an array of arrays.
+               $this->assertIsArray(
+                       $galleries[0],
+                       'The returned data does not contain an array.'
+               );
+
+               // This prevents future changes from causing
+               // backwards compatibility breaks.
+               $this->assertArrayHasKey(
+                       'src',
+                       $galleries[0],
+                       'A src key does not exist.'
+               );
+
+               $this->assertNotEmpty(
+                       $galleries[0]['src'],
+                       'The src key of the first gallery is empty.'
+               );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/getPostGalleries.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php       2021-11-16 19:59:11 UTC (rev 52189)
+++ trunk/tests/phpunit/tests/media.php 2021-11-16 21:23:12 UTC (rev 52190)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -512,6 +512,57 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_post_galleries_images() {
</span><span class="cx" style="display: block; padding: 0 10px">                $ids1      = array();
</span><span class="cx" style="display: block; padding: 0 10px">                $ids1_srcs = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids1[]      = $attachment_id;
+                       $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+               }
+
+               $ids2      = array();
+               $ids2_srcs = array();
+               foreach ( range( 4, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids2[]      = $attachment_id;
+                       $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+               }
+
+               $ids1_joined = join( ',', array_slice( $ids1, 0, 3 ) );
+               $ids2_joined = join( ',', array_slice( $ids2, 3, 3 ) );
+
+               $blob    = <<<BLOB
+[gallery ids="$ids1_joined"]
+
+[gallery ids="$ids2_joined"]
+BLOB;
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+               $srcs    = get_post_galleries_images( $post_id );
+               $this->assertSameSetsWithIndex( $srcs, array( array_slice( $ids1_srcs, 0, 3 ), array_slice( $ids2_srcs, 3, 3 ) ) );
+       }
+
+       /**
+        * @ticket 22960
+        */
+       public function test_post_gallery_images() {
+               $ids1      = array();
+               $ids1_srcs = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( range( 1, 3 ) as $i ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $attachment_id = self::factory()->attachment->create_object(
</span><span class="cx" style="display: block; padding: 0 10px">                                "image$i.jpg",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -553,115 +604,138 @@
</span><span class="cx" style="display: block; padding: 0 10px"> [gallery ids="$ids2_joined"]
</span><span class="cx" style="display: block; padding: 0 10px"> BLOB;
</span><span class="cx" style="display: block; padding: 0 10px">                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $srcs    = get_post_galleries_images( $post_id );
-               $this->assertSame( $srcs, array( $ids1_srcs, $ids2_srcs ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $srcs    = get_post_gallery_images( $post_id );
+               $this->assertSame( $srcs, $ids1_srcs );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 39304
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 43826
+        * @group blocks
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_post_galleries_images_without_global_post() {
-               // Set up an unattached image.
-               $this->factory->attachment->create_object(
-                       array(
-                               'file'           => 'test.jpg',
-                               'post_parent'    => 0,
-                               'post_mime_type' => 'image/jpeg',
-                               'post_type'      => 'attachment',
-                       )
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_block_post_gallery_images() {
+               // Similar to test_post_gallery_images but with blocks instead of shortcodes
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $post_id = $this->factory->post->create(
-                       array(
-                               'post_content' => '[gallery]',
-                       )
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) );
+               $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $galleries = get_post_galleries( $post_id, false );
-
-               $this->assertEmpty( $galleries[0]['src'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $blob    = <<<BLOB
+<!-- wp:gallery -->
+$imgs1_joined
+<!-- /wp:gallery -->
+<!-- wp:gallery -->
+$imgs2_joined
+<!-- /wp:gallery -->
+BLOB;
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+               $srcs    = get_post_gallery_images( $post_id );
+               $this->assertSameSetsWithIndex( array_slice( $ids_srcs, 0, 3 ), $srcs );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 39304
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 43826
+        * @group blocks
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_post_galleries_ignores_global_post() {
-               $global_post_id = $this->factory->post->create(
-                       array(
-                               'post_content' => 'Global Post',
-                       )
-               );
-               $post_id        = $this->factory->post->create(
-                       array(
-                               'post_content' => '[gallery]',
-                       )
-               );
-               $this->factory->attachment->create_object(
-                       array(
-                               'file'           => 'test.jpg',
-                               'post_parent'    => $post_id,
-                               'post_mime_type' => 'image/jpeg',
-                               'post_type'      => 'attachment',
-                       )
-               );
-               $expected_srcs = array(
-                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_block_post_gallery_images_json() {
+               // Similar to test_block_post_gallery_images, with IDs in the json blob
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Set the global $post context to the other post.
-               $GLOBALS['post'] = get_post( $global_post_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $galleries = get_post_galleries( $post_id, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) );
+               $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertNotEmpty( $galleries[0]['src'] );
-               $this->assertSame( $galleries[0]['src'], $expected_srcs );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $blob    = <<<BLOB
+<!-- wp:gallery {"ids":[$ids1_joined]} -->
+<!-- /wp:gallery -->
+
+<!-- wp:gallery {"ids":[$ids2_joined]} -->
+<!-- /wp:gallery -->
+BLOB;
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+               $srcs    = get_post_gallery_images( $post_id );
+               $this->assertSameSetsWithIndex( array_slice( $ids_srcs, 0, 3 ), $srcs );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 39304
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 43826
+        * @group blocks
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_post_galleries_respects_id_attrs() {
-               $post_id     = $this->factory->post->create(
-                       array(
-                               'post_content' => 'No gallery defined',
-                       )
-               );
-               $post_id_two = $this->factory->post->create(
-                       array(
-                               'post_content' => "[gallery id='$post_id']",
-                       )
-               );
-               $this->factory->attachment->create_object(
-                       array(
-                               'file'           => 'test.jpg',
-                               'post_parent'    => $post_id,
-                               'post_mime_type' => 'image/jpeg',
-                               'post_type'      => 'attachment',
-                       )
-               );
-               $expected_srcs = array(
-                       'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_mixed_post_gallery_images() {
+               // Similar to test_post_gallery_images but with a shortcode and a block in the same post
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 6 ) as $i ) {
+                       $attachment_id = self::factory()->attachment->create_object(
+                               "image$i.jpg",
+                               0,
+                               array(
+                                       'post_mime_type' => 'image/jpeg',
+                                       'post_type'      => 'attachment',
+                               )
+                       );
+                       $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
+                       wp_update_attachment_metadata( $attachment_id, $metadata );
+                       $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $galleries = get_post_galleries( $post_id_two, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $ids1_joined  = join( "\n", array_slice( $ids, 0, 3 ) );
+               $ids2_joined  = join( "\n", array_slice( $ids, 3, 3 ) );
+               $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Set the global $post context.
-               $GLOBALS['post']               = get_post( $post_id_two );
-               $galleries_with_global_context = get_post_galleries( $post_id_two, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $blob    = <<<BLOB
+[gallery ids="$ids1_joined"]
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Check that the global post state doesn't affect the results.
-               $this->assertSame( $galleries, $galleries_with_global_context );
-
-               $this->assertNotEmpty( $galleries[0]['src'] );
-               $this->assertSame( $galleries[0]['src'], $expected_srcs );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+[gallery ids="$ids2_joined"]
+<!-- wp:gallery -->
+$imgs2_joined
+<!-- /wp:gallery -->
+BLOB;
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+               $srcs    = get_post_gallery_images( $post_id );
+               $this->assertSameSetsWithIndex( array_slice( $ids_srcs, 0, 3 ), $srcs );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 22960
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 43826
+        * @group blocks
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_post_gallery_images() {
-               $ids1      = array();
-               $ids1_srcs = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_block_inner_post_gallery_images() {
+               // Make sure get_post_gallery_images() works with gallery blocks that are nested inside something else
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( range( 1, 3 ) as $i ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $attachment_id = self::factory()->attachment->create_object(
</span><span class="cx" style="display: block; padding: 0 10px">                                "image$i.jpg",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -673,13 +747,39 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_update_attachment_metadata( $attachment_id, $metadata );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $ids1[]      = $attachment_id;
-                       $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $ids2      = array();
-               $ids2_srcs = array();
-               foreach ( range( 4, 6 ) as $i ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $imgs_joined = join( "\n", $imgs );
+
+               $blob    = <<<BLOB
+<!-- wp:columns -->
+<!-- wp:column -->
+<!-- wp:gallery -->
+$imgs_joined
+<!-- /wp:gallery -->
+<!-- /wp:column -->
+<!-- /wp:columns -->
+BLOB;
+               $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
+               $srcs    = get_post_gallery_images( $post_id );
+               $this->assertSameSetsWithIndex( $ids_srcs, $srcs );
+       }
+
+       /**
+        * @ticket 43826
+        * @group blocks
+        */
+       public function test_block_post_gallery_innerblock_images() {
+               // Make sure get_post_gallery_images() works with new version of gallery block with nested image blocks.
+               $ids      = array();
+               $imgs     = array();
+               $ids_srcs = array();
+               foreach ( range( 1, 3 ) as $i ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $attachment_id = self::factory()->attachment->create_object(
</span><span class="cx" style="display: block; padding: 0 10px">                                "image$i.jpg",
</span><span class="cx" style="display: block; padding: 0 10px">                                0,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -690,21 +790,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                        $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_update_attachment_metadata( $attachment_id, $metadata );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $ids2[]      = $attachment_id;
-                       $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $ids[]      = $attachment_id;
+                       $url        = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
+                       $ids_srcs[] = $url;
+                       $imgs[]     = '<!-- wp:image {"id":' . $attachment_id . ',"sizeSlug":"large","linkDestination":"none"} --><figure class="wp-block-image size-large"><img src="' . $url . '" /></figure><!-- /wp:image -->';
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $ids1_joined = implode( ',', $ids1 );
-               $ids2_joined = implode( ',', $ids2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $imgs_joined = join( "\n", $imgs );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $blob    = <<<BLOB
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-[gallery ids="$ids1_joined"]
-
-[gallery ids="$ids2_joined"]
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<!-- wp:gallery -->
+<figure class="wp-block-gallery has-nested-images columns-default is-cropped">
+$imgs_joined
+</figure>
+<!-- /wp:gallery -->
</ins><span class="cx" style="display: block; padding: 0 10px"> BLOB;
</span><span class="cx" style="display: block; padding: 0 10px">                $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $srcs    = get_post_gallery_images( $post_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( $srcs, $ids1_srcs );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSameSetsWithIndex( $ids_srcs, $srcs );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_get_media_embedded_in_content() {
</span></span></pre>
</div>
</div>

</body>
</html>