Make WordPress Core


Ignore:
Timestamp:
07/20/2016 04:23:36 PM (8 years ago)
Author:
joemcgill
Message:

Post Thumbnails: Only update featured images when saving a post.

Previously, changing the post thumbnail of a published post in the edit screen
would immediately apply the change, rather than waiting for the post to be
saved before applying the update. This could lead to someone unintentionally
editing the post thumbnail on a published post, and made it impossible to
preview changes to post thumbnails on published posts before saving the change.

This introduces a new Ajax handler, wp_ajax_get_post_thumbnail_html() to
retrieve the HTML for the post thumbnail meta box without updating the post
meta value for _thumbnail_id. It also allows post thumbnail changes to be
previewed by passing the _thumbnail_id as a query variable to the preview
screen and adding a new filter, _wp_preview_post_thumbnail_filter(), which
gets applied to get_post_metadata during the post preview process.

Props flixos90.
Fixes #12922.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/revision.php

    r37914 r38118  
    531531
    532532    add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
     533    add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
    533534
    534535    return $post;
     
    576577
    577578    return $terms;
     579}
     580
     581/**
     582 * Filters post thumbnail lookup to set the post thumbnail.
     583 *
     584 * @since 4.6.0
     585 * @access private
     586 *
     587 * @param null|array|string $value    The value to return - a single metadata value, or an array of values.
     588 * @param int               $post_id  Post ID.
     589 * @param string            $meta_key Meta key.
     590 * @return null|array The default return value or the post thumbnail meta array.
     591 */
     592function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
     593    if ( ! $post = get_post() ) {
     594        return $value;
     595    }
     596
     597    if ( empty( $_REQUEST['_thumbnail_id'] ) || $post->ID != $post_id || '_thumbnail_id' != $meta_key || 'revision' == $post->post_type ) {
     598        return $value;
     599    }
     600
     601    $thumbnail_id = intval( $_REQUEST['_thumbnail_id'] );
     602    if ( $thumbnail_id <= 0 ) {
     603        return '';
     604    }
     605
     606    return strval( $thumbnail_id );
    578607}
    579608
Note: See TracChangeset for help on using the changeset viewer.