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 #51292.


Built from https://develop.svn.wordpress.org/trunk@49000


git-svn-id: http://core.svn.wordpress.org/trunk@48762 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2020-09-19 01:22:08 +00:00
parent 88ea2929b7
commit b1505c2d9c
2 changed files with 28 additions and 1 deletions

View File

@ -4375,6 +4375,33 @@ function wp_publish_post( $post ) {
return;
}
// 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 );
}
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
clean_post_cache( $post->ID );

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-alpha-48999';
$wp_version = '5.6-alpha-49000';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.