diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
index e431d4b743..32c543ce19 100644
|
a
|
b
|
class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
|
| 84 | 84 | $taxonomies = get_taxonomies( '', 'objects' ); |
| 85 | 85 | } |
| 86 | 86 | foreach ( $taxonomies as $taxonomy ) { |
| 87 | | if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->manage_terms ) ) { |
| | 87 | if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) { |
| 88 | 88 | return true; |
| 89 | 89 | } |
| 90 | 90 | } |
| … |
… |
class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
|
| 113 | 113 | } |
| 114 | 114 | $data = array(); |
| 115 | 115 | foreach ( $taxonomies as $tax_type => $value ) { |
| 116 | | if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->manage_terms ) ) ) { |
| | 116 | if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) { |
| 117 | 117 | continue; |
| 118 | 118 | } |
| 119 | 119 | $tax = $this->prepare_item_for_response( $value, $request ); |
| … |
… |
class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
|
| 145 | 145 | if ( empty( $tax_obj->show_in_rest ) ) { |
| 146 | 146 | return false; |
| 147 | 147 | } |
| 148 | | if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->manage_terms ) ) { |
| | 148 | if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) { |
| 149 | 149 | return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 150 | 150 | } |
| 151 | 151 | } |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
index ea2d0d19a7..af806b812f 100644
|
a
|
b
|
class WP_REST_Terms_Controller extends WP_REST_Controller {
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | $taxonomy_obj = get_taxonomy( $this->taxonomy ); |
| 383 | | if ( ! current_user_can( $taxonomy_obj->cap->edit_terms ) ) { |
| | 383 | if ( ( is_taxonomy_hierarchical( $this->taxonomy ) |
| | 384 | && ! current_user_can( $taxonomy_obj->cap->edit_terms ) ) |
| | 385 | || ( ! is_taxonomy_hierarchical( $this->taxonomy ) |
| | 386 | && ! current_user_can( $taxonomy_obj->cap->assign_terms ) ) ) { |
| 384 | 387 | return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create new terms.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 385 | 388 | } |
| 386 | 389 | |
diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php
index 2e01943158..ac8d02a0ea 100644
|
a
|
b
|
|
| 12 | 12 | */ |
| 13 | 13 | class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcase { |
| 14 | 14 | protected static $administrator; |
| | 15 | protected static $contributor; |
| 15 | 16 | protected static $subscriber; |
| 16 | 17 | |
| 17 | 18 | public static function wpSetUpBeforeClass( $factory ) { |
| … |
… |
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
|
| 20 | 21 | 'role' => 'administrator', |
| 21 | 22 | ) |
| 22 | 23 | ); |
| | 24 | self::$contributor = $factory->user->create( |
| | 25 | array( |
| | 26 | 'role' => 'subscriber', |
| | 27 | ) |
| | 28 | ); |
| 23 | 29 | self::$subscriber = $factory->user->create( |
| 24 | 30 | array( |
| 25 | 31 | 'role' => 'subscriber', |
| … |
… |
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
|
| 726 | 732 | $this->assertErrorResponse( 'rest_cannot_create', $response, 403 ); |
| 727 | 733 | } |
| 728 | 734 | |
| | 735 | public function test_create_item_incorrect_permissions_contributor() { |
| | 736 | wp_set_current_user( self::$contributor ); |
| | 737 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); |
| | 738 | $request->set_param( 'name', 'Incorrect permissions' ); |
| | 739 | $response = rest_get_server()->dispatch( $request ); |
| | 740 | $this->assertErrorResponse( 'rest_cannot_create', $response, 403 ); |
| | 741 | } |
| | 742 | |
| 729 | 743 | public function test_create_item_missing_arguments() { |
| 730 | 744 | wp_set_current_user( self::$administrator ); |
| 731 | 745 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); |
diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php
index f2cd6a74a8..2b3cd11250 100644
|
a
|
b
|
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
|
| 13 | 13 | protected static $superadmin; |
| 14 | 14 | protected static $administrator; |
| 15 | 15 | protected static $editor; |
| | 16 | protected static $contributor; |
| 16 | 17 | protected static $subscriber; |
| 17 | 18 | |
| 18 | 19 | public static function wpSetUpBeforeClass( $factory ) { |
| … |
… |
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
|
| 32 | 33 | 'role' => 'editor', |
| 33 | 34 | ) |
| 34 | 35 | ); |
| | 36 | self::$contributor = $factory->user->create( |
| | 37 | array( |
| | 38 | 'role' => 'contributor', |
| | 39 | ) |
| | 40 | ); |
| 35 | 41 | self::$subscriber = $factory->user->create( |
| 36 | 42 | array( |
| 37 | 43 | 'role' => 'subscriber', |
| … |
… |
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
|
| 624 | 630 | $this->assertEquals( 'so-awesome', $data['slug'] ); |
| 625 | 631 | } |
| 626 | 632 | |
| | 633 | public function test_create_item_contributor() { |
| | 634 | wp_set_current_user( self::$contributor ); |
| | 635 | $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); |
| | 636 | $request->set_param( 'name', 'My Awesome Term' ); |
| | 637 | $request->set_param( 'description', 'This term is so awesome.' ); |
| | 638 | $request->set_param( 'slug', 'so-awesome' ); |
| | 639 | $response = rest_get_server()->dispatch( $request ); |
| | 640 | $this->assertEquals( 201, $response->get_status() ); |
| | 641 | $headers = $response->get_headers(); |
| | 642 | $data = $response->get_data(); |
| | 643 | $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] ); |
| | 644 | $this->assertEquals( 'My Awesome Term', $data['name'] ); |
| | 645 | $this->assertEquals( 'This term is so awesome.', $data['description'] ); |
| | 646 | $this->assertEquals( 'so-awesome', $data['slug'] ); |
| | 647 | } |
| | 648 | |
| 627 | 649 | public function test_create_item_incorrect_permissions() { |
| 628 | 650 | wp_set_current_user( self::$subscriber ); |
| 629 | 651 | $request = new WP_REST_Request( 'POST', '/wp/v2/tags' ); |
diff --git a/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php b/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php
index 635969cb38..3eea3be489 100644
|
a
|
b
|
class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
|
| 62 | 62 | $this->assertEquals( 'tags', $data['post_tag']['rest_base'] ); |
| 63 | 63 | } |
| 64 | 64 | |
| | 65 | public function test_get_items_context_edit() { |
| | 66 | wp_set_current_user( self::$contributor_id ); |
| | 67 | $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); |
| | 68 | $request->set_param( 'context', 'edit' ); |
| | 69 | $response = rest_get_server()->dispatch( $request ); |
| | 70 | $data = $response->get_data(); |
| | 71 | $taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) ); |
| | 72 | $this->assertEquals( count( $taxonomies ), count( $data ) ); |
| | 73 | $this->assertEquals( 'Categories', $data['category']['name'] ); |
| | 74 | $this->assertEquals( 'category', $data['category']['slug'] ); |
| | 75 | $this->assertEquals( true, $data['category']['hierarchical'] ); |
| | 76 | $this->assertEquals( 'Tags', $data['post_tag']['name'] ); |
| | 77 | $this->assertEquals( 'post_tag', $data['post_tag']['slug'] ); |
| | 78 | $this->assertEquals( false, $data['post_tag']['hierarchical'] ); |
| | 79 | $this->assertEquals( 'tags', $data['post_tag']['rest_base'] ); |
| | 80 | } |
| | 81 | |
| 65 | 82 | public function test_get_items_invalid_permission_for_context() { |
| 66 | 83 | wp_set_current_user( 0 ); |
| 67 | 84 | $request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); |