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

Added Term Meta Endpoints #8

Merged
merged 2 commits into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/class-wp-rest-meta-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ protected function get_parent_column() {
return 'user_id';
case 'comment':
return 'comment_id';
case 'term':
return 'term_id';
default:
return $parent_column;
}
Expand Down
110 changes: 110 additions & 0 deletions lib/class-wp-rest-meta-terms-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

class WP_REST_Meta_Terms_Controller extends WP_REST_Meta_Controller {
/**
* Associated object type.
*
* @var string Type "term"
*/
protected $parent_type = 'term';

/**
* Associated term controller class object.
*
* @var WP_REST_Terms_Controller
*/
protected $parent_controller;

/**
* Base path for taxonomy endpoints.
*
* @var string
*/
protected $parent_base;

/**
* Associated taxonomy.
*
* @var string
*/
protected $parent_taxonomy;

public function __construct( $parent_taxonomy ) {
$this->parent_taxonomy = $parent_taxonomy;
$this->parent_controller = new WP_REST_Terms_Controller( $this->parent_taxonomy );
$tax_obj = get_taxonomy( $this->parent_taxonomy );
$this->parent_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name;
$this->namespace = 'wp/v2';
$this->rest_base = 'meta';
}

/**
* Check if a given request has access to get meta for a taxonomy.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
$tax_obj = get_taxonomy( $this->parent_taxonomy );
$parent = get_term( (int) $request['parent_id'], $this->parent_taxonomy );

if ( empty( $parent ) || empty( $parent->term_id ) ) {
return new WP_Error( 'rest_term_invalid_id', __( 'Invalid term id.' ), array( 'status' => 404 ) );
}

if ( ! current_user_can( $tax_obj->cap->edit_terms ) ) {
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}

/**
* Check if a given request has access to get a specific meta entry for a taxonomy.
*
* @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 taxonomy.
*
* @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 taxonomy.
*
* @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 taxonomy.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_item_permissions_check( $request ) {
$tax_obj = get_taxonomy( $this->parent_taxonomy );
$parent = get_term( (int) $request['parent_id'], $this->parent_taxonomy );

if ( empty( $parent ) || empty( $parent->term_id ) ) {
return new WP_Error( 'rest_term_invalid_id', __( 'Invalid term id.' ), array( 'status' => 404 ) );
}

if ( ! current_user_can( $tax_obj->cap->delete_terms ) ) {
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
}
11 changes: 11 additions & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ function meta_rest_api_init() {
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';
}

foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( post_type_supports( $post_type->name, 'custom-fields' ) ) {
$meta_controller = new WP_REST_Meta_Posts_Controller( $post_type->name );
$meta_controller->register_routes();
}
}

foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) as $taxonomy ) {
$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