Skip to content

Commit ddbe5a1

Browse files
committed
Galleries: tests and fixes.
1 parent 78e4e48 commit ddbe5a1

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/wp-includes/media.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5268,6 +5268,10 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
52685268
$galleries = array();
52695269
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
52705270
foreach ( $matches as $shortcode ) {
5271+
if ( count( $galleries ) >= $max_galleries ) {
5272+
return $galleries;
5273+
}
5274+
52715275
if ( 'gallery' === $shortcode[2] ) {
52725276
$sources = array();
52735277

@@ -5300,6 +5304,10 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
53005304
}
53015305
}
53025306

5307+
if ( count( $galleries ) > $max_galleries ) {
5308+
return array_slice( $galleries, 0, $max_galleries );
5309+
}
5310+
53035311
$processor = new WP_Block_Processor( $post->post_content );
53045312
while ( count( $galleries ) < $max_galleries && $processor->next_block( 'gallery' ) ) {
53055313
/*

tests/phpunit/tests/media/getPostGalleries.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ public function test_returns_no_srcs_with_block_in_post_with_no_attached_images(
221221
);
222222
}
223223

224+
/**
225+
* Ensures that the function only returns up to the requested count of galleries.
226+
*
227+
* @dataProvider data_unique_gallery_type_content
228+
*
229+
* @param $gallery
230+
*/
231+
public function tests_returns_requested_max_number_of_galleries( $gallery ) {
232+
// @todo Why is this necessary?
233+
// add_shortcode( 'gallery', 'gallery_shortcode' );
234+
235+
$post_id = self::factory()->post->create(
236+
array( 'post_content' => str_repeat( "{$gallery}\n", 5 ) )
237+
);
238+
239+
// Test negative counts, the zero count, and a max count above the total contained galleries.
240+
foreach ( range( -5, 10 ) as $max_count ) {
241+
$this->assertCount(
242+
max( 0, min( 5, $max_count ) ),
243+
get_post_galleries( $post_id, false, $max_count ),
244+
'Failed to fetch up to the max requested number of galleries.'
245+
);
246+
}
247+
}
248+
249+
/**
250+
* Data provider.
251+
*
252+
* @return array[]
253+
*/
254+
public static function data_unique_gallery_type_content() {
255+
return array(
256+
'Shortcode with ids' => array( '[gallery ids="11,12,13"]' ),
257+
'Shortcode without ids' => array( '[gallery]<figure><img src="image-15.jpg" data-id="15"></figure>[/gallery]' ),
258+
'Block with ids' => array( '<!-- wp:gallery {"ids":[14, 15, 16]} /-->' ),
259+
'Block with inner blocks' => array( '<!-- wp:gallery --><!-- wp:image {"id":11} --><img src="image-11.jpg" data-id="11"><!-- /wp:image --><!-- /wp:gallery -->' ),
260+
);
261+
}
262+
224263
/**
225264
* Tests that no srcs are returned for a gallery block v2
226265
* in a post with no attached images.

0 commit comments

Comments
 (0)