Post Thumbnails: Add helper functions for attachment captions.

This adds three new functions for getting/displaying attachment captions:

* `wp_get_attachment_caption` - Retrieves a caption for a specific attachment.
* `get_the_post_thumbnail_caption()` - Returns the post thumbnail caption.
* `the_post_thumbnail_caption()` - Displays the post thumbnail caption.

These are helpful for displaying a caption associated with an image directly
in a template, rather than using the caption shortcode.

This also introduces two new filters:

* `wp_get_attachment_caption` - Filters the value of `wp_get_attachment_caption()`.
* `the_post_thumbnail_caption` - Filters the display of the post thumbnail caption.

`the_post_thumbnail_caption()` is automatically filtered by `wptexturize()`,
`convert_smilies()`, and `convert_chars()` in `wp-includes/default-filters.php`.

Props flixos90, joemcgill.
Fixes #12235.

git-svn-id: https://develop.svn.wordpress.org/trunk@37915 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill
2016-06-29 17:28:00 +00:00
parent 206380180e
commit 0b4798b2bc
5 changed files with 167 additions and 0 deletions

View File

@@ -901,6 +901,40 @@ EOF;
$this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) );
}
/**
* @ticket 12235
*/
function test_wp_get_attachment_caption() {
$this->assertFalse( wp_get_attachment_caption( 0 ) );
$caption = 'This is a caption.';
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => $caption,
) );
$this->assertFalse( wp_get_attachment_caption( $post_id ) );
$this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
}
/**
* @ticket 12235
*/
function test_wp_get_attachment_caption_empty() {
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => '',
) );
$this->assertEquals( '', wp_get_attachment_caption( $attachment_id ) );
}
/**
* Helper function to get image size array from size "name"
*/

View File

@@ -74,6 +74,63 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
$this->assertTrue( $WP_Query->thumbnails_cached );
}
/**
* @ticket 12235
*/
function test_get_the_post_thumbnail_caption() {
$this->assertEquals( '', get_the_post_thumbnail_caption() );
$caption = 'This is a caption.';
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => $caption,
) );
set_post_thumbnail( $post_id, $attachment_id );
$this->assertEquals( $caption, get_the_post_thumbnail_caption( $post_id ) );
}
/**
* @ticket 12235
*/
function test_get_the_post_thumbnail_caption_empty() {
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => '',
) );
set_post_thumbnail( $post_id, $attachment_id );
$this->assertEquals( '', get_the_post_thumbnail_caption( $post_id ) );
}
/**
* @ticket 12235
*/
function test_the_post_thumbnail_caption() {
$caption = 'This is a caption.';
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => $caption,
) );
set_post_thumbnail( $post_id, $attachment_id );
ob_start();
the_post_thumbnail_caption( $post_id );
$this->assertEquals( $caption, ob_get_clean() );
}
function test_get_the_post_thumbnail() {
$this->assertEquals( '', get_the_post_thumbnail() );
$this->assertEquals( '', get_the_post_thumbnail( self::$post ) );