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. git-svn-id: https://develop.svn.wordpress.org/trunk@38118 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f5f8d1df2b
commit
2586eeeeca
@ -64,7 +64,7 @@ $core_actions_post = array(
|
||||
'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
|
||||
'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
|
||||
'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
|
||||
'install-theme', 'test_url',
|
||||
'install-theme', 'test_url', 'get-post-thumbnail-html',
|
||||
);
|
||||
|
||||
// Deprecated
|
||||
|
@ -2163,6 +2163,31 @@ function wp_ajax_set_post_thumbnail() {
|
||||
wp_die( 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax handler for retrieving HTML for the featured image.
|
||||
*
|
||||
* @since 4.6.0
|
||||
*/
|
||||
function wp_ajax_get_post_thumbnail_html() {
|
||||
$post_ID = intval( $_POST['post_id'] );
|
||||
|
||||
check_ajax_referer( "update-post_$post_ID" );
|
||||
|
||||
if ( ! current_user_can( 'edit_post', $post_ID ) ) {
|
||||
wp_die( -1 );
|
||||
}
|
||||
|
||||
$thumbnail_id = intval( $_POST['thumbnail_id'] );
|
||||
|
||||
// For backward compatibility, -1 refers to no featured image.
|
||||
if ( -1 === $thumbnail_id ) {
|
||||
$thumbnail_id = null;
|
||||
}
|
||||
|
||||
$return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
|
||||
wp_send_json_success( $return );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax handler for setting the featured image for an attachment.
|
||||
*
|
||||
|
@ -1428,10 +1428,12 @@ function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
|
||||
$thumbnail_html
|
||||
);
|
||||
$content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
|
||||
$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>';
|
||||
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
|
||||
}
|
||||
}
|
||||
|
||||
$content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
|
||||
|
||||
/**
|
||||
* Filters the admin post thumbnail HTML markup to return.
|
||||
*
|
||||
@ -1753,8 +1755,13 @@ function post_preview() {
|
||||
$query_args['preview_id'] = $post->ID;
|
||||
$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
|
||||
|
||||
if ( isset( $_POST['post_format'] ) )
|
||||
if ( isset( $_POST['post_format'] ) ) {
|
||||
$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
|
||||
}
|
||||
|
||||
if ( isset( $_POST['_thumbnail_id'] ) ) {
|
||||
$query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
return get_preview_post_link( $post, $query_args );
|
||||
|
@ -657,15 +657,25 @@
|
||||
|
||||
settings.post.featuredImageId = id;
|
||||
|
||||
wp.media.post( 'set-post-thumbnail', {
|
||||
json: true,
|
||||
wp.media.post( 'get-post-thumbnail-html', {
|
||||
post_id: settings.post.id,
|
||||
thumbnail_id: settings.post.featuredImageId,
|
||||
_wpnonce: settings.post.nonce
|
||||
}).done( function( html ) {
|
||||
if ( html == '0' ) {
|
||||
window.alert( window.setPostThumbnailL10n.error );
|
||||
return;
|
||||
}
|
||||
$( '.inside', '#postimagediv' ).html( html );
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Remove the featured image id, save the post thumbnail data and
|
||||
* set the HTML in the post meta box to no featured image.
|
||||
*/
|
||||
remove: function() {
|
||||
wp.media.featuredImage.set( -1 );
|
||||
},
|
||||
/**
|
||||
* The Featured Image workflow
|
||||
*
|
||||
@ -743,7 +753,8 @@
|
||||
|
||||
wp.media.featuredImage.frame().open();
|
||||
}).on( 'click', '#remove-post-thumbnail', function() {
|
||||
wp.media.view.settings.post.featuredImageId = -1;
|
||||
wp.media.featuredImage.remove();
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -3260,6 +3260,16 @@ 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 );
|
||||
|
@ -530,6 +530,7 @@ function _set_preview( $post ) {
|
||||
$post->post_excerpt = $preview->post_excerpt;
|
||||
|
||||
add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
|
||||
add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
|
||||
|
||||
return $post;
|
||||
}
|
||||
@ -577,6 +578,34 @@ function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
|
||||
return $terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters post thumbnail lookup to set the post thumbnail.
|
||||
*
|
||||
* @since 4.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param null|array|string $value The value to return - a single metadata value, or an array of values.
|
||||
* @param int $post_id Post ID.
|
||||
* @param string $meta_key Meta key.
|
||||
* @return null|array The default return value or the post thumbnail meta array.
|
||||
*/
|
||||
function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
|
||||
if ( ! $post = get_post() ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ( empty( $_REQUEST['_thumbnail_id'] ) || $post->ID != $post_id || '_thumbnail_id' != $meta_key || 'revision' == $post->post_type ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$thumbnail_id = intval( $_REQUEST['_thumbnail_id'] );
|
||||
if ( $thumbnail_id <= 0 ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return strval( $thumbnail_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the post revision version.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user