Make WordPress Core

Ticket #43517: 43517.patch

File 43517.patch, 6.9 KB (added by enrico.sorcinelli, 7 years ago)
  • src/wp-includes/capabilities.php

    diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php
    index 6b1a672..25da5e2 100644
    a b function map_meta_cap( $cap, $user_id ) { 
    511511                                break;
    512512                        }
    513513
    514                         if ( 'delete_term' === $cap && ( $term->term_id == get_option( 'default_' . $term->taxonomy ) ) ) {
     514                        if ( 'delete_term' === $cap && ( $term->term_id == get_option( 'default_' . $term->taxonomy ) || $term->term_id == get_option( 'default_taxonomy_' . $term->taxonomy ) ) ) {
    515515                                $caps[] = 'do_not_allow';
    516516                                break;
    517517                        }
  • src/wp-includes/class-wp-taxonomy.php

    diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php
    index ef6f1a0..7c5a402 100644
    a b final class WP_Taxonomy { 
    205205        public $rest_controller_class;
    206206
    207207        /**
     208         * The default term name. If you pass an array you have to set
     209         * 'name' and optionally slug' and 'description'.
     210         *
     211         * @since 5.0.0
     212         * @var array|string
     213         */
     214        public $default_term;
     215
     216        /**
    208217         * Whether it is a built-in taxonomy.
    209218         *
    210219         * @since 4.7.0
    final class WP_Taxonomy { 
    273282                        'show_in_rest'          => false,
    274283                        'rest_base'             => false,
    275284                        'rest_controller_class' => false,
     285                        'default_term'          => null,
    276286                        '_builtin'              => false,
    277287                );
    278288
    final class WP_Taxonomy { 
    370380                        }
    371381                }
    372382
     383                // Default taxonomy term.
     384                if ( ! empty( $args['default_term'] ) ) {
     385                        if ( ! is_array( $args['default_term'] ) ) {
     386                                $args['default_term'] = array( 'name' => $args['default_term'] );
     387                        }
     388                        $args['default_term'] = wp_parse_args( $args['default_term'], array( 'name' => '', 'slug' => '', 'description' => '' ) );
     389                }
     390
    373391                foreach ( $args as $property_name => $property_value ) {
    374392                        $this->$property_name = $property_value;
    375393                }
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 39e1891..7cd0588 100644
    a b function wp_insert_post( $postarr, $wp_error = false ) { 
    35733573                wp_set_post_tags( $post_ID, $postarr['tags_input'] );
    35743574        }
    35753575
     3576        // Add default term for all associated custom taxonomies.
     3577        if ( 'auto-draft' != $post_status ) {
     3578                foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
     3579                        if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) {
     3580                                $postarr['tax_input'][ $taxonomy ] = array();
     3581                        }
     3582                }
     3583        }
     3584
    35763585        // New-style support for all custom taxonomies.
    35773586        if ( ! empty( $postarr['tax_input'] ) ) {
    35783587                foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
  • src/wp-includes/taxonomy.php

    diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
    index 4744dd4..d791e66 100644
    a b function is_taxonomy_hierarchical( $taxonomy ) { 
    324324 * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
    325325 *              arguments to register the Taxonomy in REST API.
    326326 * @since 5.0.0 Introduced `meta_box_sanitize_cb` argument.
     327 * @since 5.0.0 Introduced `default_term` argument.
    327328 *
    328329 * @global array $wp_taxonomies Registered taxonomies.
    329330 *
    function is_taxonomy_hierarchical( $taxonomy ) { 
    394395 *                                                to post types, which confirms that the objects are published before
    395396 *                                                counting them. Default _update_generic_term_count() for taxonomies
    396397 *                                                attached to other object types, such as users.
     398 *     @type string|array  $default_term {
     399 *         Default term to be used for the taxonomy.
     400 *
     401 *         @type string $name         Name of default term.
     402 *         @type string $slug         Slug for default term (default empty).
     403 *         @type string $description  Description for default term (default empty).
     404 *     }
    397405 *     @type bool          $_builtin              This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!
    398406 *                                                Default false.
    399407 * }
    function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 
    420428
    421429        $taxonomy_object->add_hooks();
    422430
     431        // Add default term.
     432        if ( ! empty( $taxonomy_object->default_term ) ) {
     433                if ( $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy ) ) {
     434                        update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id'] );
     435                }
     436                else {
     437                        $term = wp_insert_term( $taxonomy_object->default_term['name'], $taxonomy, array(
     438                                'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ),
     439                                'description' => $taxonomy_object->default_term['description']
     440                        ));
     441
     442                        // Update term id in options.
     443                        if ( ! is_wp_error( $term ) ) {
     444                                update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id']);
     445                        }
     446                }
     447        }
     448
    423449        /**
    424450         * Fires after a taxonomy is registered.
    425451         *
    function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 
    23752401                $terms = array( $terms );
    23762402        }
    23772403
     2404        // Add default term
     2405        $taxonomy_obj = get_taxonomy( $taxonomy );
     2406
     2407        // Default term for this taxonomy.
     2408        if ( empty( $terms ) && ! empty ( $taxonomy_obj->default_term ) && ! empty( $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ) ) ) {
     2409                $terms[] = (int) $default_term_id;
     2410        }
     2411
    23782412        if ( ! $append ) {
    23792413                $old_tt_ids = wp_get_object_terms(
    23802414                        $object_id, $taxonomy, array(
  • tests/phpunit/tests/taxonomy.php

    diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php
    index d651b12..bcaac7e 100644
    a b class Tests_Taxonomy extends WP_UnitTestCase { 
    926926
    927927                $this->assertEquals( $problematic_term, $term_name );
    928928        }
     929
     930        /**
     931         * Test default term for custom taxonomy.
     932         *
     933         * @ticket 43517
     934         */
     935        function test_default_term_for_custom_taxonomy() {
     936
     937                wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     938
     939                $tax = 'custom-tax';
     940
     941                // Create custom taxonomy to test with.
     942                register_taxonomy( $tax, 'post', array(
     943                        'hierarchical' => true,
     944                        'public' => true,
     945                        'default_term' => array( 'name' => 'Default category', 'slug' => 'default-category' )
     946                ) );
     947
     948                // Add post.
     949                $post_id = wp_insert_post(
     950                        array(
     951                                'post_title' => 'Foo',
     952                                'post_type' => 'post',
     953                        )
     954                );
     955                $term = wp_get_post_terms( $post_id, $tax );
     956                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     957
     958                // Add custom post.
     959                register_post_type( 'post-custom-tax', array( 'taxonomies' => array( $tax ) ) );
     960                $post_id = wp_insert_post(
     961                        array(
     962                                'post_title' => 'Foo',
     963                                'post_type' => 'post-custom-tax',
     964                        )
     965                );
     966                $term = wp_get_post_terms( $post_id, $tax );
     967                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     968        }
    929969}