|
| 1 | +<?php |
| 2 | + |
| 3 | +class WP_REST_Meta_Terms_Controller extends WP_REST_Meta_Controller { |
| 4 | + /** |
| 5 | + * Associated object type. |
| 6 | + * |
| 7 | + * @var string Type "term" |
| 8 | + */ |
| 9 | + protected $parent_type = 'term'; |
| 10 | + |
| 11 | + /** |
| 12 | + * Associated term controller class object. |
| 13 | + * |
| 14 | + * @var WP_REST_Terms_Controller |
| 15 | + */ |
| 16 | + protected $parent_controller; |
| 17 | + |
| 18 | + /** |
| 19 | + * Base path for taxonomy endpoints. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $parent_base; |
| 24 | + |
| 25 | + /** |
| 26 | + * Associated taxonomy. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $parent_taxonomy; |
| 31 | + |
| 32 | + public function __construct( $parent_taxonomy ) { |
| 33 | + $this->parent_taxonomy = $parent_taxonomy; |
| 34 | + $this->parent_controller = new WP_REST_Terms_Controller( $this->parent_taxonomy ); |
| 35 | + $tax_obj = get_taxonomy( $this->parent_taxonomy ); |
| 36 | + $this->parent_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name; |
| 37 | + $this->namespace = 'wp/v2'; |
| 38 | + $this->rest_base = 'meta'; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check if a given request has access to get meta for a taxonomy. |
| 43 | + * |
| 44 | + * @param WP_REST_Request $request Full data about the request. |
| 45 | + * @return WP_Error|boolean |
| 46 | + */ |
| 47 | + public function get_items_permissions_check( $request ) { |
| 48 | + $tax_obj = get_taxonomy( $this->parent_taxonomy ); |
| 49 | + $parent = get_term( (int) $request['parent_id'], $this->parent_taxonomy ); |
| 50 | + |
| 51 | + if ( empty( $parent ) || empty( $parent->term_id ) ) { |
| 52 | + return new WP_Error( 'rest_term_invalid_id', __( 'Invalid term id.' ), array( 'status' => 404 ) ); |
| 53 | + } |
| 54 | + |
| 55 | + if ( ! current_user_can( $tax_obj->cap->edit_terms ) ) { |
| 56 | + return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 57 | + } |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check if a given request has access to get a specific meta entry for a taxonomy. |
| 63 | + * |
| 64 | + * @param WP_REST_Request $request Full data about the request. |
| 65 | + * @return WP_Error|boolean |
| 66 | + */ |
| 67 | + public function get_item_permissions_check( $request ) { |
| 68 | + return $this->get_items_permissions_check( $request ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check if a given request has access to create a meta entry for a taxonomy. |
| 73 | + * |
| 74 | + * @param WP_REST_Request $request Full data about the request. |
| 75 | + * @return WP_Error|boolean |
| 76 | + */ |
| 77 | + public function create_item_permissions_check( $request ) { |
| 78 | + return $this->get_items_permissions_check( $request ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if a given request has access to update a meta entry for a taxonomy. |
| 83 | + * |
| 84 | + * @param WP_REST_Request $request Full data about the request. |
| 85 | + * @return WP_Error|boolean |
| 86 | + */ |
| 87 | + public function update_item_permissions_check( $request ) { |
| 88 | + return $this->get_items_permissions_check( $request ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Check if a given request has access to delete meta for a taxonomy. |
| 93 | + * |
| 94 | + * @param WP_REST_Request $request Full details about the request. |
| 95 | + * @return WP_Error|boolean |
| 96 | + */ |
| 97 | + public function delete_item_permissions_check( $request ) { |
| 98 | + $tax_obj = get_taxonomy( $this->parent_taxonomy ); |
| 99 | + $parent = get_term( (int) $request['parent_id'], $this->parent_taxonomy ); |
| 100 | + |
| 101 | + if ( empty( $parent ) || empty( $parent->term_id ) ) { |
| 102 | + return new WP_Error( 'rest_term_invalid_id', __( 'Invalid term id.' ), array( 'status' => 404 ) ); |
| 103 | + } |
| 104 | + |
| 105 | + if ( ! current_user_can( $tax_obj->cap->delete_terms ) ) { |
| 106 | + return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | +} |
0 commit comments