This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'WP-API/master' into add/term-meta-endpo…
…ints # Conflicts: # lib/class-wp-rest-meta-controller.php # plugin.php
- Loading branch information
Showing
5 changed files
with
2,087 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
class WP_REST_Meta_Comments_Controller extends WP_REST_Meta_Controller { | ||
/** | ||
* Associated object type. | ||
* | ||
* @var string Type "comment" | ||
*/ | ||
protected $parent_type = 'comment'; | ||
|
||
/** | ||
* Associated comment controller class object. | ||
* | ||
* @var WP_REST_Comments_Controller | ||
*/ | ||
protected $parent_controller; | ||
|
||
/** | ||
* Base path for parent meta type endpoints. | ||
* | ||
* @var string "comments" | ||
*/ | ||
protected $parent_base = 'comments'; | ||
|
||
public function __construct() { | ||
$this->parent_controller = new WP_REST_Comments_Controller(); | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'meta'; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get meta for a comment. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_items_permissions_check( $request ) { | ||
$comment_id = (int) $request['parent_id']; | ||
$comment = get_comment( $comment_id ); | ||
|
||
if ( empty( $comment ) || empty( $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get a specific meta entry for a comment. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to create a meta entry for a comment. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function create_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to update a meta entry for a comment. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function update_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to delete meta for a comment. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function delete_item_permissions_check( $request ) { | ||
$comment_id = (int) $request['parent_id']; | ||
$comment = get_comment( $comment_id ); | ||
|
||
if ( empty( $comment ) || empty( $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
class WP_REST_Meta_Users_Controller extends WP_REST_Meta_Controller { | ||
|
||
/** | ||
* Associated object type. | ||
* | ||
* @var string "user" | ||
*/ | ||
protected $parent_type = 'user'; | ||
|
||
/** | ||
* Base path for parent meta type endpoints. | ||
* | ||
* @var string "users" | ||
*/ | ||
protected $parent_base = 'users'; | ||
|
||
/** | ||
* User controller class object. | ||
* | ||
* @var WP_REST_Users_Controller | ||
*/ | ||
protected $parent_controller; | ||
|
||
public function __construct() { | ||
$this->parent_controller = new WP_REST_Users_Controller(); | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'meta'; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get meta for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_items_permissions_check( $request ) { | ||
$user = get_user_by( 'id', (int) $request['parent_id'] ); | ||
|
||
if ( empty( $user ) || empty( $user->ID ) ) { | ||
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'edit_user', $user->ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get a specific meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to create a meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function create_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to update a meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function update_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to delete meta for a user. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function delete_item_permissions_check( $request ) { | ||
$user = get_user_by( 'id', (int) $request['parent_id'] ); | ||
|
||
if ( empty( $user ) || empty( $user->ID ) ) { | ||
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'delete_user', $user->ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.