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

Commit 3d19dcc

Browse files
committed
Merge remote-tracking branch 'WP-API/master' into add/term-meta-endpoints
# Conflicts: # lib/class-wp-rest-meta-controller.php # plugin.php
2 parents 5631f84 + 44a9e07 commit 3d19dcc

File tree

5 files changed

+2087
-0
lines changed

5 files changed

+2087
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
class WP_REST_Meta_Comments_Controller extends WP_REST_Meta_Controller {
4+
/**
5+
* Associated object type.
6+
*
7+
* @var string Type "comment"
8+
*/
9+
protected $parent_type = 'comment';
10+
11+
/**
12+
* Associated comment controller class object.
13+
*
14+
* @var WP_REST_Comments_Controller
15+
*/
16+
protected $parent_controller;
17+
18+
/**
19+
* Base path for parent meta type endpoints.
20+
*
21+
* @var string "comments"
22+
*/
23+
protected $parent_base = 'comments';
24+
25+
public function __construct() {
26+
$this->parent_controller = new WP_REST_Comments_Controller();
27+
$this->namespace = 'wp/v2';
28+
$this->rest_base = 'meta';
29+
}
30+
31+
/**
32+
* Check if a given request has access to get meta for a comment.
33+
*
34+
* @param WP_REST_Request $request Full data about the request.
35+
* @return WP_Error|boolean
36+
*/
37+
public function get_items_permissions_check( $request ) {
38+
$comment_id = (int) $request['parent_id'];
39+
$comment = get_comment( $comment_id );
40+
41+
if ( empty( $comment ) || empty( $comment->comment_ID ) ) {
42+
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) );
43+
}
44+
45+
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
46+
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
47+
}
48+
return true;
49+
}
50+
51+
/**
52+
* Check if a given request has access to get a specific meta entry for a comment.
53+
*
54+
* @param WP_REST_Request $request Full data about the request.
55+
* @return WP_Error|boolean
56+
*/
57+
public function get_item_permissions_check( $request ) {
58+
return $this->get_items_permissions_check( $request );
59+
}
60+
61+
/**
62+
* Check if a given request has access to create a meta entry for a comment.
63+
*
64+
* @param WP_REST_Request $request Full data about the request.
65+
* @return WP_Error|boolean
66+
*/
67+
public function create_item_permissions_check( $request ) {
68+
return $this->get_items_permissions_check( $request );
69+
}
70+
71+
/**
72+
* Check if a given request has access to update a meta entry for a comment.
73+
*
74+
* @param WP_REST_Request $request Full data about the request.
75+
* @return WP_Error|boolean
76+
*/
77+
public function update_item_permissions_check( $request ) {
78+
return $this->get_items_permissions_check( $request );
79+
}
80+
81+
/**
82+
* Check if a given request has access to delete meta for a comment.
83+
*
84+
* @param WP_REST_Request $request Full details about the request.
85+
* @return WP_Error|boolean
86+
*/
87+
public function delete_item_permissions_check( $request ) {
88+
$comment_id = (int) $request['parent_id'];
89+
$comment = get_comment( $comment_id );
90+
91+
if ( empty( $comment ) || empty( $comment->comment_ID ) ) {
92+
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) );
93+
}
94+
95+
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
96+
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
97+
}
98+
return true;
99+
}
100+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
class WP_REST_Meta_Users_Controller extends WP_REST_Meta_Controller {
4+
5+
/**
6+
* Associated object type.
7+
*
8+
* @var string "user"
9+
*/
10+
protected $parent_type = 'user';
11+
12+
/**
13+
* Base path for parent meta type endpoints.
14+
*
15+
* @var string "users"
16+
*/
17+
protected $parent_base = 'users';
18+
19+
/**
20+
* User controller class object.
21+
*
22+
* @var WP_REST_Users_Controller
23+
*/
24+
protected $parent_controller;
25+
26+
public function __construct() {
27+
$this->parent_controller = new WP_REST_Users_Controller();
28+
$this->namespace = 'wp/v2';
29+
$this->rest_base = 'meta';
30+
}
31+
32+
/**
33+
* Check if a given request has access to get meta for a user.
34+
*
35+
* @param WP_REST_Request $request Full data about the request.
36+
* @return WP_Error|boolean
37+
*/
38+
public function get_items_permissions_check( $request ) {
39+
$user = get_user_by( 'id', (int) $request['parent_id'] );
40+
41+
if ( empty( $user ) || empty( $user->ID ) ) {
42+
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) );
43+
}
44+
45+
if ( ! current_user_can( 'edit_user', $user->ID ) ) {
46+
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) );
47+
}
48+
return true;
49+
}
50+
51+
/**
52+
* Check if a given request has access to get a specific meta entry for a user.
53+
*
54+
* @param WP_REST_Request $request Full data about the request.
55+
* @return WP_Error|boolean
56+
*/
57+
public function get_item_permissions_check( $request ) {
58+
return $this->get_items_permissions_check( $request );
59+
}
60+
61+
/**
62+
* Check if a given request has access to create a meta entry for a user.
63+
*
64+
* @param WP_REST_Request $request Full data about the request.
65+
* @return WP_Error|boolean
66+
*/
67+
public function create_item_permissions_check( $request ) {
68+
return $this->get_items_permissions_check( $request );
69+
}
70+
71+
/**
72+
* Check if a given request has access to update a meta entry for a user.
73+
*
74+
* @param WP_REST_Request $request Full data about the request.
75+
* @return WP_Error|boolean
76+
*/
77+
public function update_item_permissions_check( $request ) {
78+
return $this->get_items_permissions_check( $request );
79+
}
80+
81+
/**
82+
* Check if a given request has access to delete meta for a user.
83+
*
84+
* @param WP_REST_Request $request Full details about the request.
85+
* @return WP_Error|boolean
86+
*/
87+
public function delete_item_permissions_check( $request ) {
88+
$user = get_user_by( 'id', (int) $request['parent_id'] );
89+
90+
if ( empty( $user ) || empty( $user->ID ) ) {
91+
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) );
92+
}
93+
94+
if ( ! current_user_can( 'delete_user', $user->ID ) ) {
95+
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) );
96+
}
97+
return true;
98+
}
99+
}

plugin.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ function meta_rest_api_init() {
2121
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-posts-controller.php';
2222
}
2323

24+
if ( class_exists( 'WP_REST_Controller' )
25+
&& ! class_exists( 'WP_REST_Meta_Users_Controller' ) ) {
26+
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-users-controller.php';
27+
}
28+
29+
if ( class_exists( 'WP_REST_Controller' )
30+
&& ! class_exists( 'WP_REST_Meta_Comments_Controller' ) ) {
31+
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-comments-controller.php';
32+
}
33+
2434
if ( class_exists( 'WP_REST_Controller' )
2535
&& ! class_exists( 'WP_REST_Meta_Terms_Controller' ) ) {
2636
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-terms-controller.php';
@@ -37,6 +47,13 @@ function meta_rest_api_init() {
3747
$terms_meta_controller = new WP_REST_Meta_Terms_Controller( $taxonomy->name );
3848
$terms_meta_controller->register_routes();
3949
}
50+
51+
$user_meta_controller = new WP_REST_Meta_Users_Controller();
52+
$user_meta_controller->register_routes();
53+
54+
$comment_meta_controller = new WP_REST_Meta_Comments_Controller();
55+
$comment_meta_controller->register_routes();
56+
4057
}
4158

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

0 commit comments

Comments
 (0)