Posts, Post Types: Introduce default_category_post_types filter.

The filter allows custom post types associated with the `category` taxonomy to opt in to requiring a default category, same as regular posts.

Props enrico.sorcinelli.
Fixes #43516.

git-svn-id: https://develop.svn.wordpress.org/trunk@48043 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-06-14 21:40:10 +00:00
parent 90d8fdb5bb
commit eb06a59f53
2 changed files with 52 additions and 4 deletions

View File

@ -4689,8 +4689,7 @@ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $a
/**
* Set categories for a post.
*
* If the post categories parameter is not set, then the default category is
* going used.
* If no categories are provided, the default category is used.
*
* @since 2.1.0
*
@ -4706,10 +4705,27 @@ function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $appe
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
// If $post_categories isn't already an array, make it one:
// If $post_categories isn't already an array, make it one.
$post_categories = (array) $post_categories;
if ( empty( $post_categories ) ) {
if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
/**
* Filters post types (in addition to 'post') that require a default category.
*
* @since 5.5.0
*
* @param array $post_types An array of post types. Default empty array.
*/
$default_category_post_types = apply_filters( 'default_category_post_types', array() );
// Regular posts always require a default category.
$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
if ( in_array( $post_type, $default_category_post_types, true )
&& is_object_in_taxonomy( $post_type, 'category' )
&& 'auto-draft' !== $post_status
) {
$post_categories = array( get_option( 'default_category' ) );
$append = false;
} else {

View File

@ -156,9 +156,11 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertInternalType( 'array', $post->post_category );
$this->assertEquals( 1, count( $post->post_category ) );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
$term1 = wp_insert_term( 'Foo', 'category' );
$term2 = wp_insert_term( 'Bar', 'category' );
$term3 = wp_insert_term( 'Baz', 'category' );
wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
$this->assertEquals( 2, count( $post->post_category ) );
$this->assertEquals( array( $term2['term_id'], $term1['term_id'] ), $post->post_category );
@ -167,6 +169,7 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
$term4 = wp_insert_term( 'Burrito', 'category' );
wp_set_post_categories( $post_id, $term4['term_id'] );
$this->assertEquals( array( $term4['term_id'] ), $post->post_category );
@ -182,6 +185,35 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
}
/**
* @ticket 43516
*/
function test_wp_set_post_categories_sets_default_category_for_custom_post_types() {
add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
$post_id = self::factory()->post->create( array( 'post_type' => 'cpt' ) );
$post = get_post( $post_id );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
$term = wp_insert_term( 'Foo', 'category' );
wp_set_post_categories( $post_id, $term['term_id'] );
$this->assertEquals( $term['term_id'], $post->post_category[0] );
wp_set_post_categories( $post_id, array() );
$this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
}
function filter_default_category_post_types( $post_types ) {
$post_types[] = 'cpt';
return $post_types;
}
/**
* @ticket 25852
*/