Skip to content

Commit 7a3b593

Browse files
committed
Media: Add limit attribute to gallery shortcode
1 parent b03a05e commit 7a3b593

2 files changed

Lines changed: 188 additions & 2 deletions

File tree

src/wp-includes/media.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,8 @@ function img_caption_shortcode( $attr, $content = '' ) {
27202720
* @type int $columns Number of columns of images to display. Default 3.
27212721
* @type string|int[] $size Size of the images to display. Accepts any registered image size name, or an array
27222722
* of width and height values in pixels (in that order). Default 'thumbnail'.
2723+
* @type int $limit Maximum number of images to display. Values less than 1 display all images.
2724+
* Default -1.
27232725
* @type string $ids A comma-separated list of IDs of attachments to display. Default empty.
27242726
* @type string $include A comma-separated list of IDs of attachments to include. Default empty.
27252727
* @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty.
@@ -2774,6 +2776,7 @@ function gallery_shortcode( $attr ) {
27742776
'captiontag' => $html5 ? 'figcaption' : 'dd',
27752777
'columns' => 3,
27762778
'size' => 'thumbnail',
2779+
'limit' => -1,
27772780
'include' => '',
27782781
'exclude' => '',
27792782
'link' => '',
@@ -2782,7 +2785,11 @@ function gallery_shortcode( $attr ) {
27822785
'gallery'
27832786
);
27842787

2785-
$id = (int) $atts['id'];
2788+
$id = (int) $atts['id'];
2789+
$limit = (int) $atts['limit'];
2790+
if ( $limit < 1 ) {
2791+
$limit = -1;
2792+
}
27862793

27872794
if ( ! empty( $atts['include'] ) ) {
27882795
$_attachments = get_posts(
@@ -2800,6 +2807,12 @@ function gallery_shortcode( $attr ) {
28002807
foreach ( $_attachments as $key => $val ) {
28012808
$attachments[ $val->ID ] = $_attachments[ $key ];
28022809
}
2810+
2811+
// Limit is applied via array_slice because get_posts() ignores
2812+
// the numberposts parameter when include is specified.
2813+
if ( $limit > 0 ) {
2814+
$attachments = array_slice( $attachments, 0, $limit, true );
2815+
}
28032816
} elseif ( ! empty( $atts['exclude'] ) ) {
28042817
$post_parent_id = $id;
28052818
$attachments = get_children(
@@ -2811,6 +2824,7 @@ function gallery_shortcode( $attr ) {
28112824
'post_mime_type' => 'image',
28122825
'order' => $atts['order'],
28132826
'orderby' => $atts['orderby'],
2827+
'numberposts' => $limit,
28142828
)
28152829
);
28162830
} else {
@@ -2823,6 +2837,7 @@ function gallery_shortcode( $attr ) {
28232837
'post_mime_type' => 'image',
28242838
'order' => $atts['order'],
28252839
'orderby' => $atts['orderby'],
2840+
'numberposts' => $limit,
28262841
)
28272842
);
28282843
}
@@ -6446,4 +6461,3 @@ function wp_get_image_editor_output_format( $filename, $mime_type ) {
64466461
*/
64476462
return apply_filters( 'image_editor_output_format', $output_format, $filename, $mime_type );
64486463
}
6449-

tests/phpunit/tests/media.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,178 @@ public function test_gallery_shortcode_when_is_feed_true() {
38083808
$this->assertStringNotContainsString( '<a ', $actual );
38093809
}
38103810

3811+
/**
3812+
* @ticket 12799
3813+
*
3814+
* @dataProvider data_gallery_shortcode_limit
3815+
*/
3816+
public function test_gallery_shortcode_limit( $limit, $expected_count ) {
3817+
$post_id = self::factory()->post->create(
3818+
array(
3819+
'post_status' => 'publish',
3820+
)
3821+
);
3822+
3823+
$attachment_ids = array();
3824+
for ( $i = 0; $i < 5; $i++ ) {
3825+
$attachment_ids[] = self::factory()->attachment->create_object(
3826+
array(
3827+
'post_parent' => $post_id,
3828+
'post_status' => 'inherit',
3829+
'post_type' => 'attachment',
3830+
'post_mime_type' => 'image/jpeg',
3831+
)
3832+
);
3833+
}
3834+
3835+
$actual = gallery_shortcode(
3836+
array(
3837+
'id' => $post_id,
3838+
'limit' => $limit,
3839+
)
3840+
);
3841+
3842+
$count = substr_count( $actual, '<figure' ) + substr_count( $actual, '<dl' );
3843+
$this->assertSame( $expected_count, $count );
3844+
}
3845+
3846+
/**
3847+
* @ticket 12799
3848+
*/
3849+
public function test_gallery_shortcode_limit_with_ids() {
3850+
$attachment_ids = array();
3851+
for ( $i = 0; $i < 5; $i++ ) {
3852+
$attachment_ids[] = self::factory()->attachment->create_object(
3853+
array(
3854+
'post_status' => 'inherit',
3855+
'post_type' => 'attachment',
3856+
'post_mime_type' => 'image/jpeg',
3857+
)
3858+
);
3859+
}
3860+
3861+
$actual = gallery_shortcode(
3862+
array(
3863+
'ids' => implode( ',', $attachment_ids ),
3864+
'limit' => 2,
3865+
)
3866+
);
3867+
3868+
$count = substr_count( $actual, '<figure' ) + substr_count( $actual, '<dl' );
3869+
$this->assertSame( 2, $count );
3870+
}
3871+
3872+
/**
3873+
* @ticket 12799
3874+
*/
3875+
public function test_gallery_shortcode_limit_with_include() {
3876+
$post_id = self::factory()->post->create(
3877+
array(
3878+
'post_status' => 'publish',
3879+
)
3880+
);
3881+
3882+
$attachment_ids = array();
3883+
for ( $i = 0; $i < 5; $i++ ) {
3884+
$attachment_ids[] = self::factory()->attachment->create_object(
3885+
array(
3886+
'post_parent' => $post_id,
3887+
'post_status' => 'inherit',
3888+
'post_type' => 'attachment',
3889+
'post_mime_type' => 'image/jpeg',
3890+
)
3891+
);
3892+
}
3893+
3894+
$actual = gallery_shortcode(
3895+
array(
3896+
'id' => $post_id,
3897+
'include' => implode( ',', $attachment_ids ),
3898+
'limit' => 3,
3899+
)
3900+
);
3901+
3902+
$count = substr_count( $actual, '<figure' ) + substr_count( $actual, '<dl' );
3903+
$this->assertSame( 3, $count );
3904+
}
3905+
3906+
/**
3907+
* @ticket 12799
3908+
*/
3909+
public function test_gallery_shortcode_limit_with_exclude() {
3910+
$post_id = self::factory()->post->create(
3911+
array(
3912+
'post_status' => 'publish',
3913+
)
3914+
);
3915+
3916+
$attachment_ids = array();
3917+
for ( $i = 0; $i < 5; $i++ ) {
3918+
$attachment_ids[] = self::factory()->attachment->create_object(
3919+
array(
3920+
'post_parent' => $post_id,
3921+
'post_status' => 'inherit',
3922+
'post_type' => 'attachment',
3923+
'post_mime_type' => 'image/jpeg',
3924+
)
3925+
);
3926+
}
3927+
3928+
$actual = gallery_shortcode(
3929+
array(
3930+
'id' => $post_id,
3931+
'exclude' => $attachment_ids[0],
3932+
'limit' => 2,
3933+
)
3934+
);
3935+
3936+
$count = substr_count( $actual, '<figure' ) + substr_count( $actual, '<dl' );
3937+
$this->assertSame( 2, $count );
3938+
}
3939+
3940+
/**
3941+
* @ticket 12799
3942+
*/
3943+
public function test_gallery_shortcode_limit_with_orderby_rand() {
3944+
$post_id = self::factory()->post->create(
3945+
array(
3946+
'post_status' => 'publish',
3947+
)
3948+
);
3949+
3950+
for ( $i = 0; $i < 5; $i++ ) {
3951+
self::factory()->attachment->create_object(
3952+
array(
3953+
'post_parent' => $post_id,
3954+
'post_status' => 'inherit',
3955+
'post_type' => 'attachment',
3956+
'post_mime_type' => 'image/jpeg',
3957+
)
3958+
);
3959+
}
3960+
3961+
$actual = gallery_shortcode(
3962+
array(
3963+
'id' => $post_id,
3964+
'limit' => 2,
3965+
'orderby' => 'rand',
3966+
)
3967+
);
3968+
3969+
$count = substr_count( $actual, '<figure' ) + substr_count( $actual, '<dl' );
3970+
$this->assertSame( 2, $count );
3971+
}
3972+
3973+
public static function data_gallery_shortcode_limit() {
3974+
return array(
3975+
'default no limit' => array( -1, 5 ),
3976+
'zero means no limit' => array( 0, 5 ),
3977+
'limit of 2' => array( 2, 2 ),
3978+
'limit of 5 same as total' => array( 5, 5 ),
3979+
'limit greater than total' => array( 10, 5 ),
3980+
);
3981+
}
3982+
38113983
/**
38123984
* Test attachment permalinks based on parent post status.
38133985
*

0 commit comments

Comments
 (0)