Make WordPress Core

Changeset 38118


Ignore:
Timestamp:
07/20/2016 04:23:36 PM (9 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.

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/admin-ajax.php

    r38028 r38118  
    6565    'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
    6666    'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
    67     'install-theme', 'test_url',
     67    'install-theme', 'test_url', 'get-post-thumbnail-html',
    6868);
    6969
  • trunk/src/wp-admin/includes/ajax-actions.php

    r38091 r38118  
    21652165
    21662166/**
     2167 * Ajax handler for retrieving HTML for the featured image.
     2168 *
     2169 * @since 4.6.0
     2170 */
     2171function wp_ajax_get_post_thumbnail_html() {
     2172    $post_ID = intval( $_POST['post_id'] );
     2173
     2174    check_ajax_referer( "update-post_$post_ID" );
     2175
     2176    if ( ! current_user_can( 'edit_post', $post_ID ) ) {
     2177        wp_die( -1 );
     2178    }
     2179
     2180    $thumbnail_id = intval( $_POST['thumbnail_id'] );
     2181
     2182    // For backward compatibility, -1 refers to no featured image.
     2183    if ( -1 === $thumbnail_id ) {
     2184        $thumbnail_id = null;
     2185    }
     2186
     2187    $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
     2188    wp_send_json_success( $return );
     2189}
     2190
     2191/**
    21672192 * Ajax handler for setting the featured image for an attachment.
    21682193 *
  • trunk/src/wp-admin/includes/post.php

    r38029 r38118  
    14291429            );
    14301430            $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
    1431             $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
    1432         }
    1433     }
     1431            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
     1432        }
     1433    }
     1434
     1435    $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
    14341436
    14351437    /**
     
    17541756        $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
    17551757
    1756         if ( isset( $_POST['post_format'] ) )
     1758        if ( isset( $_POST['post_format'] ) ) {
    17571759            $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
     1760        }
     1761
     1762        if ( isset( $_POST['_thumbnail_id'] ) ) {
     1763            $query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
     1764        }
    17581765    }
    17591766
  • trunk/src/wp-includes/js/media-editor.js

    r38065 r38118  
    658658            settings.post.featuredImageId = id;
    659659
    660             wp.media.post( 'set-post-thumbnail', {
    661                 json:         true,
     660            wp.media.post( 'get-post-thumbnail-html', {
    662661                post_id:      settings.post.id,
    663662                thumbnail_id: settings.post.featuredImageId,
    664663                _wpnonce:     settings.post.nonce
    665664            }).done( function( html ) {
     665                if ( html == '0' ) {
     666                    window.alert( window.setPostThumbnailL10n.error );
     667                    return;
     668                }
    666669                $( '.inside', '#postimagediv' ).html( html );
    667670            });
     671        },
     672        /**
     673         * Remove the featured image id, save the post thumbnail data and
     674         * set the HTML in the post meta box to no featured image.
     675         */
     676        remove: function() {
     677            wp.media.featuredImage.set( -1 );
    668678        },
    669679        /**
     
    744754                wp.media.featuredImage.frame().open();
    745755            }).on( 'click', '#remove-post-thumbnail', function() {
    746                 wp.media.view.settings.post.featuredImageId = -1;
     756                wp.media.featuredImage.remove();
     757                return false;
    747758            });
    748759        }
  • trunk/src/wp-includes/post.php

    r38076 r38118  
    32613261    }
    32623262
     3263    // Set or remove featured image.
     3264    if ( isset( $postarr['_thumbnail_id'] ) && ( post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type ) ) {
     3265        $thumbnail_id = intval( $postarr['_thumbnail_id'] );
     3266        if ( -1 === $thumbnail_id ) {
     3267            delete_post_thumbnail( $post_ID );
     3268        } else {
     3269            set_post_thumbnail( $post_ID, $thumbnail_id );
     3270        }
     3271    }
     3272
    32633273    if ( ! empty( $postarr['meta_input'] ) ) {
    32643274        foreach ( $postarr['meta_input'] as $field => $value ) {
  • 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.