Post Thumbnails: Restore thumbnail support for media files.

* Allow to add/remove a featured image to `attachment:audio` and `attachment:video` post types, see [27657].
* Change conditionals to check for theme OR post type support.
* Add tests for #12922.

Broken in [37658].

Props flixos90, joemcgill, DrewAPicture, wonderboymusic.
See #12922.
Fixes #37658.

git-svn-id: https://develop.svn.wordpress.org/trunk@38263 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling 2016-08-15 19:15:48 +00:00
parent 40c9dcfa80
commit dd92169bfe
2 changed files with 103 additions and 10 deletions

View File

@ -3259,16 +3259,6 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}
// Set or remove featured image.
if ( isset( $postarr['_thumbnail_id'] ) && ( post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type ) ) {
$thumbnail_id = intval( $postarr['_thumbnail_id'] );
if ( -1 === $thumbnail_id ) {
delete_post_thumbnail( $post_ID );
} else {
set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
if ( ! empty( $postarr['meta_input'] ) ) {
foreach ( $postarr['meta_input'] as $field => $value ) {
update_post_meta( $post_ID, $field, $value );
@ -3292,6 +3282,27 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}
// Set or remove featured image.
if ( isset( $postarr['_thumbnail_id'] ) ) {
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
if ( wp_attachment_is( 'audio', $post_ID ) ) {
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
} elseif ( wp_attachment_is( 'video', $post_ID ) ) {
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
}
}
if ( $thumbnail_support ) {
$thumbnail_id = intval( $postarr['_thumbnail_id'] );
if ( -1 === $thumbnail_id ) {
delete_post_thumbnail( $post_ID );
} else {
set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
}
clean_post_cache( $post_ID );
$post = get_post( $post_ID );

View File

@ -233,4 +233,86 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
$this->assertEquals( wp_get_attachment_url( self::$attachment_id ), $actual );
}
/**
* @ticket 12922
*/
function test__wp_preview_post_thumbnail_filter() {
$old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
$GLOBALS['post'] = self::$post;
$_REQUEST['_thumbnail_id'] = self::$attachment_id;
$result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
$this->assertEquals( self::$attachment_id, $result );
unset( $_REQUEST['_thumbnail_id'] );
if ( null === $old_post ) {
unset( $GLOBALS['post'] );
} else {
$GLOBALS['post'] = $old_post;
}
}
/**
* @ticket 12922
*/
function test_insert_post_with_post_thumbnail() {
$post_id = wp_insert_post( array(
'ID' => self::$post->ID,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'_thumbnail_id' => self::$attachment_id,
) );
$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEquals( self::$attachment_id, $thumbnail_id );
$post_id = wp_insert_post( array(
'ID' => $post_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'_thumbnail_id' => - 1, // -1 removes post thumbnail.
) );
$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEmpty( $thumbnail_id );
}
/**
* @ticket 37658
*/
function test_insert_attachment_with_post_thumbnail() {
// Audio files support featured images.
$post_id = wp_insert_post( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_mime_type' => 'audio/mpeg',
'post_parent' => 0,
'file' => DIR_TESTDATA . '/audio/test-noise.mp3', // File does not exist, but does not matter here.
'_thumbnail_id' => self::$attachment_id,
) );
$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEquals( self::$attachment_id, $thumbnail_id );
// Images do not support featured images.
$post_id = wp_insert_post( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_mime_type' => 'image/jpeg',
'post_parent' => 0,
'file' => DIR_TESTDATA . '/images/canola.jpg',
'_thumbnail_id' => self::$attachment_id,
) );
$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEmpty( $thumbnail_id );
}
}