Skip to content
This repository was archived by the owner on Apr 4, 2020. It is now read-only.

Commit f38e69b

Browse files
Merge pull request #8 from kjbenk/add/term-meta-endpoints
Added Term Meta Endpoints
2 parents 44a9e07 + 3d19dcc commit f38e69b

File tree

4 files changed

+872
-0
lines changed

4 files changed

+872
-0
lines changed

lib/class-wp-rest-meta-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ protected function get_parent_column() {
156156
return 'user_id';
157157
case 'comment':
158158
return 'comment_id';
159+
case 'term':
160+
return 'term_id';
159161
default:
160162
return $parent_column;
161163
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

plugin.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,29 @@ function meta_rest_api_init() {
3131
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-comments-controller.php';
3232
}
3333

34+
if ( class_exists( 'WP_REST_Controller' )
35+
&& ! class_exists( 'WP_REST_Meta_Terms_Controller' ) ) {
36+
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-terms-controller.php';
37+
}
38+
3439
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
3540
if ( post_type_supports( $post_type->name, 'custom-fields' ) ) {
3641
$meta_controller = new WP_REST_Meta_Posts_Controller( $post_type->name );
3742
$meta_controller->register_routes();
3843
}
3944
}
4045

46+
foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) as $taxonomy ) {
47+
$terms_meta_controller = new WP_REST_Meta_Terms_Controller( $taxonomy->name );
48+
$terms_meta_controller->register_routes();
49+
}
50+
4151
$user_meta_controller = new WP_REST_Meta_Users_Controller();
4252
$user_meta_controller->register_routes();
4353

4454
$comment_meta_controller = new WP_REST_Meta_Comments_Controller();
4555
$comment_meta_controller->register_routes();
56+
4657
}
4758

4859
add_action( 'rest_api_init', 'meta_rest_api_init', 11 );

0 commit comments

Comments
 (0)