<!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>[49000] trunk: Posts, Post Types: Ensure default terms are added by `wp_publish_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 { 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/49000">49000</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/49000","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-09-19 01:20:48 +0000 (Sat, 19 Sep 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'>Posts, Post Types: Ensure default terms are added by `wp_publish_post()`.

Transitioning posts from `auto-draft` to `publish` via `wp_publish_post()` could result in published posts without the default category or custom taxonomy default terms.

Props frank-klein, TimothyBlynJacobs, peterwilsoncc.
Fixes <a href="https://core.trac.wordpress.org/ticket/51292">#51292</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestspostwpPublishPostsphp">trunk/tests/phpunit/tests/post/wpPublishPosts.php</a></li>
<li><a href="#trunktestsphpunitteststermtermCountsphp">trunk/tests/phpunit/tests/term/termCounts.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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    2020-09-18 14:11:11 UTC (rev 48999)
+++ trunk/src/wp-includes/post.php      2020-09-19 01:20:48 UTC (rev 49000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4375,6 +4375,33 @@
</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">+        // Ensure at least one term is applied for taxonomies with a default term.
+       foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
+               // Skip taxonomy if no default term is set.
+               if (
+                       'category' !== $taxonomy &&
+                       empty( $tax_object->default_term )
+               ) {
+                       continue;
+               }
+
+               // Do not modify previously set terms.
+               if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
+                       continue;
+               }
+
+               if ( 'category' === $taxonomy ) {
+                       $default_term_id = (int) get_option( 'default_category', 0 );
+               } else {
+                       $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
+               }
+
+               if ( ! $default_term_id ) {
+                       continue;
+               }
+               wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        clean_post_cache( $post->ID );
</span></span></pre></div>
<a id="trunktestsphpunittestspostwpPublishPostsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/post/wpPublishPosts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/wpPublishPosts.php                         (rev 0)
+++ trunk/tests/phpunit/tests/post/wpPublishPosts.php   2020-09-19 01:20:48 UTC (rev 49000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,153 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group post
+ */
+class Tests_WPPublishPost extends WP_UnitTestCase {
+
+       /**
+        * Auto-draft post ID.
+        *
+        * @var int
+        */
+       public static $auto_draft_id;
+
+       /**
+        * Create shared fixtures.
+        *
+        * @param WP_UnitTest_Factory $factory Test suite factory.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$auto_draft_id = $factory->post->create( array( 'post_status' => 'auto-draft' ) );
+       }
+
+       /**
+        * Ensure wp_publish_post does not add default category in error.
+        *
+        * @ticket 51292
+        */
+       function test_wp_publish_post_respects_current_categories() {
+               $post_id     = self::$auto_draft_id;
+               $category_id = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
+               wp_set_post_categories( $post_id, $category_id );
+               wp_publish_post( $post_id );
+
+               $post_categories = get_the_category( $post_id );
+               $this->assertCount( 1, $post_categories );
+               $this->assertSame(
+                       $category_id,
+                       $post_categories[0]->term_id,
+                       'wp_publish_post replaced set category.'
+               );
+       }
+
+       /**
+        * Ensure wp_publish_post adds default category.
+        *
+        * @covers wp_publish_post
+        * @ticket 51292
+        */
+       function test_wp_publish_post_adds_default_category() {
+               $post_id = self::$auto_draft_id;
+
+               wp_publish_post( $post_id );
+
+               $post_categories = get_the_category( $post_id );
+               $this->assertCount( 1, $post_categories );
+               $this->assertSame(
+                       (int) get_option( 'default_category' ),
+                       $post_categories[0]->term_id,
+                       'wp_publish_post failed to add default category.'
+               );
+       }
+
+       /**
+        * Ensure wp_publish_post adds default category when tagged.
+        *
+        * @covers wp_publish_post
+        * @ticket 51292
+        */
+       function test_wp_publish_post_adds_default_category_when_tagged() {
+               $post_id = self::$auto_draft_id;
+               $tag_id  = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) );
+               wp_set_post_tags( $post_id, array( $tag_id ) );
+               wp_publish_post( $post_id );
+
+               $post_categories = get_the_category( $post_id );
+               $this->assertCount( 1, $post_categories );
+               $this->assertSame(
+                       (int) get_option( 'default_category' ),
+                       $post_categories[0]->term_id,
+                       'wp_publish_post failed to add default category.'
+               );
+       }
+
+       /**
+        * Ensure wp_publish_post does not add default term in error.
+        *
+        * @covers wp_publish_post
+        * @ticket 51292
+        */
+       function test_wp_publish_post_respects_current_terms() {
+               // Create custom taxonomy to test with.
+               register_taxonomy(
+                       'tax_51292',
+                       'post',
+                       array(
+                               'hierarchical' => true,
+                               'public'       => true,
+                               'default_term' => array(
+                                       'name' => 'Default 51292',
+                                       'slug' => 'default-51292',
+                               ),
+                       )
+               );
+
+               $post_id = self::$auto_draft_id;
+               $term_id = $this->factory->term->create( array( 'taxonomy' => 'tax_51292' ) );
+               wp_set_object_terms( $post_id, array( $term_id ), 'tax_51292' );
+               wp_publish_post( $post_id );
+
+               $post_terms = get_the_terms( $post_id, 'tax_51292' );
+               $this->assertCount( 1, $post_terms );
+               $this->assertSame(
+                       $term_id,
+                       $post_terms[0]->term_id,
+                       'wp_publish_post replaced set term for custom taxonomy.'
+               );
+       }
+
+       /**
+        * Ensure wp_publish_post adds default term.
+        *
+        * @covers wp_publish_post
+        * @ticket 51292
+        */
+       function test_wp_publish_post_adds_default_term() {
+               // Create custom taxonomy to test with.
+               register_taxonomy(
+                       'tax_51292',
+                       'post',
+                       array(
+                               'hierarchical' => true,
+                               'public'       => true,
+                               'default_term' => array(
+                                       'name' => 'Default 51292',
+                                       'slug' => 'default-51292',
+                               ),
+                       )
+               );
+
+               $post_id = self::$auto_draft_id;
+
+               wp_publish_post( $post_id );
+
+               $post_terms = get_the_terms( $post_id, 'tax_51292' );
+               $this->assertCount( 1, $post_terms );
+               $this->assertSame(
+                       get_term_by( 'slug', 'default-51292', 'tax_51292' )->term_id,
+                       $post_terms[0]->term_id,
+                       'wp_publish_post failed to add default term for custom taxonomy.'
+               );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/post/wpPublishPosts.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunitteststermtermCountsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/term/termCounts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/termCounts.php                             (rev 0)
+++ trunk/tests/phpunit/tests/term/termCounts.php       2020-09-19 01:20:48 UTC (rev 49000)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,186 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group taxonomy
+ */
+class Tests_Term_termCount extends WP_UnitTestCase {
+
+       /**
+        * Term ID for testing attachment counts.
+        *
+        * @var int
+        */
+       public static $attachment_term;
+
+       /**
+        * Post IDs of shared posts.
+        *
+        * @var int[]
+        */
+       public static $post_ids;
+
+       /**
+        * Create shared fixtures.
+        *
+        * @param WP_UnitTest_Factory $factory Test suite factory.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               $statuses = array( 'publish', 'auto-draft', 'draft', 'private' );
+               foreach ( $statuses as $status ) {
+                       self::$post_ids[ $status ] = $factory->post->create( array( 'post_status' => $status ) );
+               }
+
+               register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
+               self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) );
+       }
+
+       public function setUp() {
+               parent::setUp();
+
+               register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
+       }
+
+       /**
+        * Term counts increments correctly when post status becomes published.
+        *
+        * @covers wp_publish_post
+        * @covers wp_count_terms
+        * @dataProvider data_term_counts_incremented_on_publish
+        * @ticket 40351
+        * @ticket 51292
+        *
+        * @param string $original_post_status Post status prior to change to publish.
+        * @param int    $change               Expected change upon publish.
+        */
+       public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
+               $post_id    = self::$post_ids[ $original_post_status ];
+               $term_count = get_term( get_option( 'default_category' ) )->count;
+
+               wp_publish_post( $post_id );
+
+               $expected = $term_count + $change;
+               $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
+       }
+
+       /**
+        * Data provider for test_term_count_changes_for_post_statuses.
+        *
+        * @return array[] {
+        *     @type string $original_post_status Post status prior to change to publish.
+        *     @type int    $change               Expected change upon publish.
+        * }
+        */
+       function data_term_counts_incremented_on_publish() {
+               return array(
+                       // 0. Published post
+                       array( 'publish', 0 ),
+                       // 1. Auto draft
+                       array( 'auto-draft', 1 ),
+                       // 2. Draft
+                       array( 'draft', 1 ),
+                       // 3. Private post
+                       array( 'private', 1 ),
+               );
+       }
+
+       /**
+        * Term counts increments correctly when post status becomes published.
+        *
+        * @covers wp_publish_post
+        * @dataProvider data_term_counts_incremented_on_publish_with_attachments
+        * @ticket 40351
+        * @ticket 51292
+        *
+        * @param string $original_post_status Post status prior to change to publish.
+        * @param int    $change               Expected change upon publish.
+        */
+       public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
+               $post_id = self::$post_ids[ $original_post_status ];
+               wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
+               $attachment_id = self::factory()->attachment->create_object(
+                       array(
+                               'file'        => 'image.jpg',
+                               'post_parent' => $post_id,
+                               'post_status' => 'inherit',
+                       )
+               );
+               wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
+               $term_count = get_term( self::$attachment_term )->count;
+
+               wp_publish_post( $post_id );
+
+               $expected = $term_count + $change;
+               $this->assertSame( $expected, get_term( self::$attachment_term )->count );
+       }
+
+       /**
+        * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
+        *
+        * @return array[] {
+        *     @type string $original_post_status Post status prior to change to publish.
+        *     @type int    $change               Expected change upon publish.
+        * }
+        */
+       function data_term_counts_incremented_on_publish_with_attachments() {
+               return array(
+                       // 0. Published post
+                       array( 'publish', 0 ),
+                       // 1. Auto draft
+                       array( 'auto-draft', 2 ),
+                       // 2. Draft
+                       array( 'draft', 2 ),
+                       // 3. Private post
+                       array( 'private', 2 ),
+               );
+       }
+
+       /**
+        * Term counts increments correctly when post status becomes published.
+        *
+        * @covers wp_publish_post
+        * @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments
+        * @ticket 40351
+        * @ticket 51292
+        *
+        * @param string $original_post_status Post status prior to change to publish.
+        * @param int    $change               Expected change upon publish.
+        */
+       public function test_term_counts_incremented_on_publish_with_untermed_attachments( $original_post_status, $change ) {
+               $post_id = self::$post_ids[ $original_post_status ];
+               wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
+               $attachment_id = self::factory()->attachment->create_object(
+                       array(
+                               'file'        => 'image.jpg',
+                               'post_parent' => $post_id,
+                               'post_status' => 'inherit',
+                       )
+               );
+               $term_count    = get_term( self::$attachment_term )->count;
+
+               wp_publish_post( $post_id );
+
+               $expected = $term_count + $change;
+               $this->assertSame( $expected, get_term( self::$attachment_term )->count );
+       }
+
+       /**
+        * Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments.
+        *
+        * @return array[] {
+        *     @type string $original_post_status Post status prior to change to publish.
+        *     @type int    $change               Expected change upon publish.
+        * }
+        */
+       function data_term_counts_incremented_on_publish_with_untermed_attachments() {
+               return array(
+                       // 0. Published post
+                       array( 'publish', 0 ),
+                       // 1. Auto draft
+                       array( 'auto-draft', 1 ),
+                       // 2. Draft
+                       array( 'draft', 1 ),
+                       // 3. Private post
+                       array( 'private', 1 ),
+               );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/term/termCounts.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>