Make WordPress Core


Ignore:
Timestamp:
09/30/2016 10:39:32 PM (9 years ago)
Author:
johnbillion
Message:

Taxonomy: Introduce more fine grained capabilities for managing taxonomy terms.

This introduces the singular edit_term, delete_term, and assign_term meta capabilities for terms, and switches the base capability name for tags from manage_categories to manage_post_tags and the corresponding edit_post_tags, delete_post_tags, and assign_post_tags.

All of these capabilities ultimately map to manage_categories so by default there is no change in the behaviour of the capabilities for categories, tags, or custom taxonomies. The map_meta_cap filter and the capabilities argument when registering a taxonomy now allow for control over editing, deleting, and assigning individual terms, as well as a separation of capabilities for tags from those of categories.

Fixes #35614
Props johnjamesjacoby for feedback

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r38697 r38698  
    224224            'delete_site'            => array( 'administrator' ),
    225225            'add_users'              => array( 'administrator' ),
     226
     227            'edit_categories'        => array( 'administrator', 'editor' ),
     228            'delete_categories'      => array( 'administrator', 'editor' ),
     229            'manage_post_tags'       => array( 'administrator', 'editor' ),
     230            'edit_post_tags'         => array( 'administrator', 'editor' ),
     231            'delete_post_tags'       => array( 'administrator', 'editor' ),
     232
     233            'assign_categories'      => array( 'administrator', 'editor', 'author', 'contributor' ),
     234            'assign_post_tags'       => array( 'administrator', 'editor', 'author', 'contributor' ),
    226235        );
    227236    }
     
    243252            'delete_site'            => array( 'administrator' ),
    244253            'add_users'              => array( 'administrator' ),
     254
     255            'edit_categories'        => array( 'administrator', 'editor' ),
     256            'delete_categories'      => array( 'administrator', 'editor' ),
     257            'manage_post_tags'       => array( 'administrator', 'editor' ),
     258            'edit_post_tags'         => array( 'administrator', 'editor' ),
     259            'delete_post_tags'       => array( 'administrator', 'editor' ),
     260
     261            'assign_categories'      => array( 'administrator', 'editor', 'author', 'contributor' ),
     262            'assign_post_tags'       => array( 'administrator', 'editor', 'author', 'contributor' ),
    245263        );
    246264    }
     
    400418            $expected['add_post_meta'],
    401419            $expected['edit_comment'],
     420            $expected['edit_term'],
     421            $expected['delete_term'],
     422            $expected['assign_term'],
    402423            $expected['delete_user']
    403424        );
     
    10771098            ), $caps, "Meta cap: {$meta_cap}" );
    10781099        }
     1100    }
     1101
     1102    /**
     1103     * @dataProvider dataTaxonomies
     1104     *
     1105     * @ticket 35614
     1106     */
     1107    public function test_default_taxonomy_term_cannot_be_deleted( $taxonomy ) {
     1108        if ( ! taxonomy_exists( $taxonomy ) ) {
     1109            register_taxonomy( $taxonomy, 'post' );
     1110        }
     1111
     1112        $tax  = get_taxonomy( $taxonomy );
     1113        $user = self::$users['administrator'];
     1114        $term = self::factory()->term->create_and_get( array(
     1115            'taxonomy' => $taxonomy,
     1116        ) );
     1117
     1118        update_option( "default_{$taxonomy}", $term->term_id );
     1119
     1120        $this->assertTrue( user_can( $user->ID, $tax->cap->delete_terms ) );
     1121        $this->assertFalse( user_can( $user->ID, 'delete_term', $term->term_id ) );
     1122    }
     1123
     1124    /**
     1125     * @dataProvider dataTaxonomies
     1126     *
     1127     * @ticket 35614
     1128     */
     1129    public function test_taxonomy_caps_map_correctly_to_their_meta_cap( $taxonomy ) {
     1130        if ( ! taxonomy_exists( $taxonomy ) ) {
     1131            register_taxonomy( $taxonomy, 'post' );
     1132        }
     1133
     1134        $tax  = get_taxonomy( $taxonomy );
     1135        $term = self::factory()->term->create_and_get( array(
     1136            'taxonomy' => $taxonomy,
     1137        ) );
     1138
     1139        foreach ( self::$users as $role => $user ) {
     1140            $this->assertSame(
     1141                user_can( $user->ID, 'edit_term', $term->term_id ),
     1142                user_can( $user->ID, $tax->cap->edit_terms ),
     1143                "Role: {$role}"
     1144            );
     1145            $this->assertSame(
     1146                user_can( $user->ID, 'delete_term', $term->term_id ),
     1147                user_can( $user->ID, $tax->cap->delete_terms ),
     1148                "Role: {$role}"
     1149            );
     1150            $this->assertSame(
     1151                user_can( $user->ID, 'assign_term', $term->term_id ),
     1152                user_can( $user->ID, $tax->cap->assign_terms ),
     1153                "Role: {$role}"
     1154            );
     1155        }
     1156
    10791157    }
    10801158
Note: See TracChangeset for help on using the changeset viewer.