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

Commit

Permalink
Merge remote-tracking branch 'WP-API/master' into add/term-meta-endpo…
Browse files Browse the repository at this point in the history
…ints

# Conflicts:
#	lib/class-wp-rest-meta-controller.php
#	plugin.php
  • Loading branch information
kjbenk committed Feb 26, 2016
2 parents 5631f84 + 44a9e07 commit 3d19dcc
Show file tree
Hide file tree
Showing 5 changed files with 2,087 additions and 0 deletions.
100 changes: 100 additions & 0 deletions lib/class-wp-rest-meta-comments-controller.php
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;
}
}
99 changes: 99 additions & 0 deletions lib/class-wp-rest-meta-users-controller.php
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;
}
}
17 changes: 17 additions & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ function meta_rest_api_init() {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-posts-controller.php';
}

if ( class_exists( 'WP_REST_Controller' )
&& ! class_exists( 'WP_REST_Meta_Users_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-users-controller.php';
}

if ( class_exists( 'WP_REST_Controller' )
&& ! class_exists( 'WP_REST_Meta_Comments_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-comments-controller.php';
}

if ( class_exists( 'WP_REST_Controller' )
&& ! class_exists( 'WP_REST_Meta_Terms_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-meta-terms-controller.php';
Expand All @@ -37,6 +47,13 @@ function meta_rest_api_init() {
$terms_meta_controller = new WP_REST_Meta_Terms_Controller( $taxonomy->name );
$terms_meta_controller->register_routes();
}

$user_meta_controller = new WP_REST_Meta_Users_Controller();
$user_meta_controller->register_routes();

$comment_meta_controller = new WP_REST_Meta_Comments_Controller();
$comment_meta_controller->register_routes();

}

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

0 comments on commit 3d19dcc

Please sign in to comment.