<!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>[49732] branches/5.6: REST API: Pass the previous state of the post as a parameter to the `wp_after_insert_post` hook.</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 { white-space: pre-line; 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/49732">49732</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/49732","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-12-01 20:55:03 +0000 (Tue, 01 Dec 2020)</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'>REST API: Pass the previous state of the post as a parameter to the `wp_after_insert_post` hook.

This enables, for example, the previous post status to be used by this hook without the need to first capture it on an earlier hook.

This also fixes the value of the `$fire_after_hooks` parameter in `get_default_post_to_edit()` so the `wp_after_insert_post` action correctly fires just once on the new post screen.

This merges <a href="https://core.trac.wordpress.org/changeset/45114">[45114]</a> into the 5.6 branch

See <a href="https://core.trac.wordpress.org/ticket/45114">#45114</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches56srcwpadminincludespostphp">branches/5.6/src/wp-admin/includes/post.php</a></li>
<li><a href="#branches56srcwpincludesclasswpcustomizemanagerphp">branches/5.6/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#branches56srcwpincludespostphp">branches/5.6/src/wp-includes/post.php</a></li>
<li><a href="#branches56srcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp">branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</a></li>
<li><a href="#branches56srcwpincludesrestapiendpointsclasswprestpostscontrollerphp">branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branches56testsphpunittestspostwpAfterInsertPostphp">branches/5.6/tests/phpunit/tests/post/wpAfterInsertPost.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#branches56">branches/5.6/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/5.6
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/5.6 2020-12-01 20:45:43 UTC (rev 49731)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/5.6  2020-12-01 20:55:03 UTC (rev 49732)
</ins><a id="branches56"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/5.6</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557,43622
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43747,43751-43754,43758,43760-43765,43767-43770,43772,43774-43781,43783,43785,43790-43806,43808-43821,43825,43828,43830-43834,43836-43843,43846-43863,43867-43889,43891-43894,43897-43905,43908-43909,43911-43929,43931-43942,43946-43947,43949-43956,43959-43964,43967-43969,43988,43994,44014,44017,44047,44183,44185,44187-44206,44208-44213,44231-44232,44235,44248,44284,44287-44288
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.5:49373-49379,49381
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:49645,49649-49651,49668,49673-49674,49677,49681,49685,49691,49702-49705,49708-49710,49713,49716,49718,49720,49722,49725-49726
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:49645,49649-49651,49668,49673-49674,49677,49681,49685,49691,49702-49705,49708-49710,49713,49716,49718,49720,49722,49725-49726,49731
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches56srcwpadminincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.6/src/wp-admin/includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/src/wp-admin/includes/post.php       2020-12-01 20:45:43 UTC (rev 49731)
+++ branches/5.6/src/wp-admin/includes/post.php 2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -687,13 +687,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_status' => 'auto-draft',
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        false,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        true
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 false
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px">                $post    = get_post( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        set_post_format( $post, get_option( 'default_post_format' ) );
</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_after_insert_post( $post, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $post, false, null );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Schedule auto-draft cleanup.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
</span></span></pre></div>
<a id="branches56srcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.6/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/src/wp-includes/class-wp-customize-manager.php       2020-12-01 20:45:43 UTC (rev 49731)
+++ branches/5.6/src/wp-includes/class-wp-customize-manager.php 2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3105,7 +3105,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                /** This action is documented in wp-includes/post.php */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'wp_insert_post', $post->ID, $post, true );
</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_after_insert_post( $post, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( get_post( $post_id ), true, $post );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                wp_trash_post_comments( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="branches56srcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.6/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/src/wp-includes/post.php     2020-12-01 20:45:43 UTC (rev 49731)
+++ branches/5.6/src/wp-includes/post.php       2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3717,6 +3717,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $previous_status = get_post_field( 'post_status', $post_ID );
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                $previous_status = 'new';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $post_before     = null;
</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">        $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4319,7 +4320,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        do_action( 'wp_insert_post', $post_ID, $post, $update );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $fire_after_hooks ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp_after_insert_post( $post, $update );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $post, $update, $post_before );
</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 $post_ID;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4432,6 +4433,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</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">+        $post_before = get_post( $post->ID );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Ensure at least one term is applied for taxonomies with a default term.
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Skip taxonomy if no default term is set.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4482,7 +4485,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /** This action is documented in wp-includes/post.php */
</span><span class="cx" style="display: block; padding: 0 10px">        do_action( 'wp_insert_post', $post->ID, $post, true );
</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_after_insert_post( $post, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_after_insert_post( $post, true, $post_before );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4937,10 +4940,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.6.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int|WP_Post $post   The post ID or object that has been saved.
- * @param bool        $update Whether this is an existing post being updated.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int|WP_Post  $post        The post ID or object that has been saved.
+ * @param bool         $update      Whether this is an existing post being updated.
+ * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
+ *                                  to the update for updated posts.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_after_insert_post( $post, $update ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_after_insert_post( $post, $update, $post_before ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $post = get_post( $post );
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4953,11 +4958,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.6.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param int     $post_id Post ID.
-        * @param WP_Post $post    Post object.
-        * @param bool    $update  Whether this is an existing post being updated.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param int          $post_id     Post ID.
+        * @param WP_Post      $post        Post object.
+        * @param bool         $update      Whether this is an existing post being updated.
+        * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
+        *                                  to the update for updated posts.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        do_action( 'wp_after_insert_post', $post_id, $post, $update );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
</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="branches56srcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php  2020-12-01 20:45:43 UTC (rev 49731)
+++ branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php    2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -191,7 +191,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'rest_after_insert_attachment', $attachment, $request, true );
</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_after_insert_post( $attachment, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $attachment, false, null );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Set a custom header with the attachment_id.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -321,7 +321,8 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $response = parent::update_item( $request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $attachment_before = get_post( $request['id'] );
+               $response          = parent::update_item( $request );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $response ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return $response;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -347,7 +348,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( 'rest_after_insert_attachment', $attachment, $request, false );
</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_after_insert_post( $attachment, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $attachment, true, $attachment_before );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->prepare_item_for_response( $attachment, $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = rest_ensure_response( $response );
</span></span></pre></div>
<a id="branches56srcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php        2020-12-01 20:45:43 UTC (rev 49731)
+++ branches/5.6/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php  2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -677,7 +677,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
</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_after_insert_post( $post, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $post, false, null );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->prepare_item_for_response( $post, $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = rest_ensure_response( $response );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -753,7 +753,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $valid_check;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $post = $this->prepare_item_for_database( $request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $post_before = get_post( $request['id'] );
+               $post        = $this->prepare_item_for_database( $request );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $post ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return $post;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -830,7 +831,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
</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_after_insert_post( $post, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_after_insert_post( $post, true, $post_before );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->prepare_item_for_response( $post, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="branches56testsphpunittestspostwpAfterInsertPostphpfromrev49731trunktestsphpunittestspostwpAfterInsertPostphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/5.6/tests/phpunit/tests/post/wpAfterInsertPost.php (from rev 49731, trunk/tests/phpunit/tests/post/wpAfterInsertPost.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.6/tests/phpunit/tests/post/wpAfterInsertPost.php                               (rev 0)
+++ branches/5.6/tests/phpunit/tests/post/wpAfterInsertPost.php 2020-12-01 20:55:03 UTC (rev 49732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,221 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group post
+ */
+class Tests_Post_wpAfterInsertPost extends WP_UnitTestCase {
+
+       /**
+        * Admin user ID.
+        *
+        * @var int
+        */
+       public static $admin_id;
+
+       /**
+        * Attachment ID (no media attached).
+        *
+        * @var int
+        */
+       public static $attachment_id;
+
+       /**
+        * Post ID for testing updates.
+        *
+        * @var int
+        */
+       public static $post_id;
+
+       /**
+        * Title as passed to hook.
+        *
+        * @var string
+        */
+       public static $passed_post_title = '';
+
+       /**
+        * Status as passed to hook.
+        *
+        * @var string
+        */
+       public static $passed_post_status = '';
+
+       /**
+        * Before update title as passed to hook.
+        *
+        * @var string
+        */
+       public static $passed_post_before_title = '';
+
+       /**
+        * Before update status as passed to hook.
+        *
+        * @var string
+        */
+       public static $passed_post_before_status = '';
+
+       public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+               self::$admin_id = $factory->user->create(
+                       array(
+                               'role'       => 'administrator',
+                               'user_login' => 'administrator',
+                       )
+               );
+
+               self::$post_id = $factory->post->create(
+                       array(
+                               'post_status' => 'draft',
+                               'post_title'  => '45114 to be updated',
+                       )
+               );
+
+               self::$attachment_id = $factory->attachment->create(
+                       array(
+                               'post_status' => 'inherit',
+                               'post_title'  => '45114 attachment to be updated',
+                               'post_parent' => self::$post_id,
+                       )
+               );
+       }
+
+       public function setUp() {
+               parent::setUp();
+               add_action( 'wp_after_insert_post', array( $this, 'action_wp_after_insert_post' ), 10, 4 );
+       }
+
+       public function tearDown() {
+               self::$passed_post_title         = '';
+               self::$passed_post_status        = '';
+               self::$passed_post_before_title  = '';
+               self::$passed_post_before_status = '';
+               parent::tearDown();
+       }
+
+       /**
+        * Helper function to obtain data running on the hook `wp_after_insert_post`.
+        *
+        * @param int          $post_id     Post ID.
+        * @param WP_Post      $post        Post object.
+        * @param bool         $update      Whether this is an existing post being updated.
+        * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
+        *                                  to the update for updated posts.
+        */
+       function action_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
+               self::$passed_post_title  = $post->post_title;
+               self::$passed_post_status = $post->post_status;
+
+               if ( null === $post_before ) {
+                       self::$passed_post_before_title  = null;
+                       self::$passed_post_before_status = null;
+                       return;
+               }
+
+               self::$passed_post_before_title  = $post_before->post_title;
+               self::$passed_post_before_status = $post_before->post_status;
+
+               // Prevent this firing when the revision is generated.
+               remove_action( 'wp_after_insert_post', array( $this, 'action_wp_after_insert_post' ), 10 );
+       }
+
+       /**
+        * Ensure before post is correct when updating a post object.
+        *
+        * @ticket 45114
+        */
+       public function test_update_via_wp_update_post() {
+               $post               = get_post( self::$post_id, ARRAY_A );
+               $post['post_title'] = 'new title';
+               wp_update_post( $post );
+
+               $this->assertSame( '45114 to be updated', self::$passed_post_before_title );
+               $this->assertSame( 'new title', self::$passed_post_title );
+       }
+
+       /**
+        * Ensure before post is correct when publishing a post object.
+        *
+        * @ticket 45114
+        */
+       public function test_update_via_wp_publish_post() {
+               wp_publish_post( self::$post_id );
+
+               $this->assertSame( 'draft', self::$passed_post_before_status );
+               $this->assertSame( 'publish', self::$passed_post_status );
+       }
+
+       /**
+        * Ensure before post is correct when inserting a new post.
+        *
+        * @ticket 45114
+        */
+       public function test_new_post_via_wp_insert_post() {
+               wp_insert_post(
+                       array(
+                               'post_status'  => 'draft',
+                               'post_title'   => 'a new post',
+                               'post_content' => 'new',
+                       )
+               );
+
+               $this->assertSame( null, self::$passed_post_before_status );
+               $this->assertSame( 'a new post', self::$passed_post_title );
+       }
+
+       /**
+        * Ensure before post is correct when updating post via REST API.
+        *
+        * @ticket 45114
+        */
+       public function test_update_via_rest_contoller() {
+               wp_set_current_user( self::$admin_id );
+               $post_id = self::$post_id;
+
+               $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
+               $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+               $request->set_body_params( array( 'title' => 'new title' ) );
+               rest_get_server()->dispatch( $request );
+
+               $this->assertSame( '45114 to be updated', self::$passed_post_before_title );
+               $this->assertSame( 'new title', self::$passed_post_title );
+       }
+
+       /**
+        * Ensure before post is correct when creating post via REST API.
+        *
+        * @ticket 45114
+        */
+       public function test_new_post_via_rest_contoller() {
+               wp_set_current_user( self::$admin_id );
+
+               $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts' ) );
+               $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+               $request->set_body_params(
+                       array(
+                               'title'  => 'new title',
+                               'status' => 'draft',
+                       )
+               );
+               rest_get_server()->dispatch( $request );
+
+               $this->assertSame( null, self::$passed_post_before_title );
+               $this->assertSame( 'new title', self::$passed_post_title );
+       }
+
+       /**
+        * Ensure before post is correct when updating post via REST API.
+        *
+        * @ticket 45114
+        */
+       public function test_update_attachment_via_rest_contoller() {
+               wp_set_current_user( self::$admin_id );
+               $attachment_id = self::$attachment_id;
+
+               $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/media/%d', $attachment_id ) );
+               $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+               $request->set_body_params( array( 'title' => 'new attachment title' ) );
+               rest_get_server()->dispatch( $request );
+
+               $this->assertSame( '45114 attachment to be updated', self::$passed_post_before_title );
+               $this->assertSame( 'new attachment title', self::$passed_post_title );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>