Make WordPress Core

Ticket #37658: 37658.4.diff

File 37658.4.diff, 4.4 KB (added by flixos90, 10 years ago)
  • src/wp-includes/post.php

     
    32593259                }
    32603260        }
    32613261
    3262         // Set or remove featured image.
    3263         if ( isset( $postarr['_thumbnail_id'] ) && ( post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type ) ) {
    3264                 $thumbnail_id = intval( $postarr['_thumbnail_id'] );
    3265                 if ( -1 === $thumbnail_id ) {
    3266                         delete_post_thumbnail( $post_ID );
    3267                 } else {
    3268                         set_post_thumbnail( $post_ID, $thumbnail_id );
    3269                 }
    3270         }
    3271 
    32723262        if ( ! empty( $postarr['meta_input'] ) ) {
    32733263                foreach ( $postarr['meta_input'] as $field => $value ) {
    32743264                        update_post_meta( $post_ID, $field, $value );
     
    32923282                }
    32933283        }
    32943284
     3285        // Set or remove featured image.
     3286        if ( isset( $postarr['_thumbnail_id'] ) ) {
     3287                $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
     3288                if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
     3289                        if ( wp_attachment_is( 'audio', $post_ID ) ) {
     3290                                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
     3291                        } elseif ( wp_attachment_is( 'video', $post_ID ) ) {
     3292                                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
     3293                        }
     3294                }
     3295
     3296                if ( $thumbnail_support ) {
     3297                        $thumbnail_id = intval( $postarr['_thumbnail_id'] );
     3298                        if ( -1 === $thumbnail_id ) {
     3299                                delete_post_thumbnail( $post_ID );
     3300                        } else {
     3301                                set_post_thumbnail( $post_ID, $thumbnail_id );
     3302                        }
     3303                }
     3304        }
     3305
    32953306        clean_post_cache( $post_ID );
    32963307
    32973308        $post = get_post( $post_ID );
  • tests/phpunit/tests/post/thumbnails.php

     
    233233
    234234                $this->assertEquals( wp_get_attachment_url( self::$attachment_id ), $actual );
    235235        }
     236
     237        /**
     238         * @ticket 12922
     239         */
     240        function test__wp_preview_post_thumbnail_filter() {
     241                $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
     242
     243                $GLOBALS['post'] = self::$post;
     244                $_REQUEST['_thumbnail_id'] = self::$attachment_id;
     245
     246                $result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
     247                $this->assertEquals( self::$attachment_id, $result );
     248
     249                unset( $_REQUEST['_thumbnail_id'] );
     250                if ( null === $old_post ) {
     251                        unset( $GLOBALS['post'] );
     252                } else {
     253                        $GLOBALS['post'] = $old_post;
     254                }
     255        }
     256
     257        /**
     258         * @ticket 12922
     259         */
     260        function test_insert_post_with_post_thumbnail() {
     261                $post_id = wp_insert_post( array(
     262                        'ID' => self::$post->ID,
     263                        'post_status' => 'publish',
     264                        'post_content' => rand_str(),
     265                        'post_title' => rand_str(),
     266                        '_thumbnail_id' => self::$attachment_id,
     267                ) );
     268
     269                $thumbnail_id = get_post_thumbnail_id( $post_id );
     270                $this->assertEquals( self::$attachment_id, $thumbnail_id );
     271
     272                $post_id = wp_insert_post( array(
     273                        'ID' => $post_id,
     274                        'post_status' => 'publish',
     275                        'post_content' => rand_str(),
     276                        'post_title' => rand_str(),
     277                        '_thumbnail_id' => -1, // -1 removes post thumbnail.
     278                ) );
     279
     280                $thumbnail_id = get_post_thumbnail_id( $post_id );
     281                $this->assertEmpty( $thumbnail_id );
     282        }
     283
     284        /**
     285         * @ticket 37658
     286         */
     287        function test_insert_attachment_with_post_thumbnail() {
     288                // Audio files support featured images.
     289                $post_id = wp_insert_post( array(
     290                        'post_type' => 'attachment',
     291                        'post_status' => 'inherit',
     292                        'post_content' => rand_str(),
     293                        'post_title' => rand_str(),
     294                        'post_mime_type' => 'audio/mpeg',
     295                        'post_parent' => 0,
     296                        'file' => DIR_TESTDATA . '/images/canola.jpg',
     297                        '_thumbnail_id' => self::$attachment_id,
     298                ) );
     299
     300                $thumbnail_id = get_post_thumbnail_id( $post_id );
     301                $this->assertEquals( self::$attachment_id, $thumbnail_id );
     302
     303                // Images do not support featured images.
     304                $post_id = wp_insert_post( array(
     305                        'post_type' => 'attachment',
     306                        'post_status' => 'inherit',
     307                        'post_content' => rand_str(),
     308                        'post_title' => rand_str(),
     309                        'post_mime_type' => 'image/jpeg',
     310                        'post_parent' => 0,
     311                        'file' => DIR_TESTDATA . '/images/canola.jpg',
     312                        '_thumbnail_id' => self::$attachment_id,
     313                ) );
     314
     315                $thumbnail_id = get_post_thumbnail_id( $post_id );
     316                $this->assertEmpty( $thumbnail_id );
     317        }
    236318}