<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38118] trunk/src: Post Thumbnails: Only update featured images when saving a post.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38118">38118</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38118","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joemcgill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-07-20 16:23:36 +0000 (Wed, 20 Jul 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/12922">#12922</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminadminajaxphp">trunk/src/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminincludespostphp">trunk/src/wp-admin/includes/post.php</a></li>
<li><a href="#trunksrcwpincludesjsmediaeditorjs">trunk/src/wp-includes/js/media-editor.js</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesrevisionphp">trunk/src/wp-includes/revision.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminadminajaxphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/admin-ajax.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/admin-ajax.php 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-admin/admin-ajax.php 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -64,7 +64,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
</span><span class="cx" style="display: block; padding: 0 10px"> 'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
</span><span class="cx" style="display: block; padding: 0 10px"> 'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'install-theme', 'test_url',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'install-theme', 'test_url', 'get-post-thumbnail-html',
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Deprecated
</span></span></pre></div>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-admin/includes/ajax-actions.php 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2164,6 +2164,31 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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 );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Ajax handler for setting the featured image for an attachment.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span></span></pre></div>
<a id="trunksrcwpadminincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/post.php 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-admin/includes/post.php 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1428,10 +1428,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $thumbnail_html
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $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>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the admin post thumbnail HTML markup to return.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1753,8 +1755,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query_args['preview_id'] = $post->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $_POST['post_format'] ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $_POST['post_format'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
+
+ if ( isset( $_POST['_thumbnail_id'] ) ) {
+ $query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return get_preview_post_link( $post, $query_args );
</span></span></pre></div>
<a id="trunksrcwpincludesjsmediaeditorjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/js/media-editor.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/media-editor.js 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-includes/js/media-editor.js 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -657,16 +657,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> settings.post.featuredImageId = id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp.media.post( 'set-post-thumbnail', {
- json: true,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp.media.post( 'get-post-thumbnail-html', {
</ins><span class="cx" style="display: block; padding: 0 10px"> post_id: settings.post.id,
</span><span class="cx" style="display: block; padding: 0 10px"> thumbnail_id: settings.post.featuredImageId,
</span><span class="cx" style="display: block; padding: 0 10px"> _wpnonce: settings.post.nonce
</span><span class="cx" style="display: block; padding: 0 10px"> }).done( function( html ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( html == '0' ) {
+ window.alert( window.setPostThumbnailL10n.error );
+ return;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> $( '.inside', '#postimagediv' ).html( html );
</span><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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 );
+ },
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * The Featured Image workflow
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wp.media.controller.FeaturedImage
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -743,7 +753,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp.media.featuredImage.frame().open();
</span><span class="cx" style="display: block; padding: 0 10px"> }).on( 'click', '#remove-post-thumbnail', function() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp.media.view.settings.post.featuredImageId = -1;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp.media.featuredImage.remove();
+ return false;
</ins><span class="cx" style="display: block; padding: 0 10px"> });
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-includes/post.php 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3260,6 +3260,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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 );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $postarr['meta_input'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $postarr['meta_input'] as $field => $value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $post_ID, $field, $value );
</span></span></pre></div>
<a id="trunksrcwpincludesrevisionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/revision.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/revision.php 2016-07-20 15:06:19 UTC (rev 38117)
+++ trunk/src/wp-includes/revision.php 2016-07-20 16:23:36 UTC (rev 38118)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -530,6 +530,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $post->post_excerpt = $preview->post_excerpt;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $post;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -578,6 +579,34 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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 );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Gets the post revision version.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.6.0
</span></span></pre>
</div>
</div>
</body>
</html>