Adds taxonomy schema information. #792.
This commit is contained in:
parent
a118165836
commit
8b86e8ebae
File diff suppressed because one or more lines are too long
|
@ -9,7 +9,6 @@ use Tainacan\Repositories;
|
||||||
class REST_Taxonomies_Controller extends REST_Controller {
|
class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
private $taxonomy;
|
private $taxonomy;
|
||||||
private $taxonomy_repository;
|
private $taxonomy_repository;
|
||||||
private $collections_repository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST_Taxonomies_Controller constructor.
|
* REST_Taxonomies_Controller constructor.
|
||||||
|
@ -26,7 +25,6 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
public function init_objects() {
|
public function init_objects() {
|
||||||
$this->taxonomy = new Entities\Taxonomy();
|
$this->taxonomy = new Entities\Taxonomy();
|
||||||
$this->taxonomy_repository = Repositories\Taxonomies::get_instance();
|
$this->taxonomy_repository = Repositories\Taxonomies::get_instance();
|
||||||
$this->collections_repository = Repositories\Collections::get_instance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register_routes() {
|
public function register_routes() {
|
||||||
|
@ -45,7 +43,7 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
'permission_callback' => array($this, 'create_item_permissions_check'),
|
'permission_callback' => array($this, 'create_item_permissions_check'),
|
||||||
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE)
|
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE)
|
||||||
),
|
),
|
||||||
'schema' => [$this, 'get_schema']
|
'schema' => [$this, 'get_list_schema']
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
register_rest_route(
|
register_rest_route(
|
||||||
|
@ -56,6 +54,10 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
'callback' => array($this, 'get_item'),
|
'callback' => array($this, 'get_item'),
|
||||||
'permission_callback' => array($this, 'get_item_permissions_check'),
|
'permission_callback' => array($this, 'get_item_permissions_check'),
|
||||||
'args' => array(
|
'args' => array(
|
||||||
|
'taxonomy_id' => array(
|
||||||
|
'description' => __( 'Taxonomy ID', 'tainacan' ),
|
||||||
|
'required' => true,
|
||||||
|
),
|
||||||
'context' => array(
|
'context' => array(
|
||||||
'type' => 'string',
|
'type' => 'string',
|
||||||
'default' => 'view',
|
'default' => 'view',
|
||||||
|
@ -65,21 +67,6 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
'edit'
|
'edit'
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
'fetch_only' => array(
|
|
||||||
'type' => ['string', 'array'],
|
|
||||||
'description' => __( 'Fetch only specific attribute. The specifics attributes are the same in schema.', 'tainacan' ),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'methods' => \WP_REST_Server::DELETABLE,
|
|
||||||
'callback' => array($this, 'delete_item'),
|
|
||||||
'permission_callback' => array($this, 'delete_item_permissions_check'),
|
|
||||||
'args' => array(
|
|
||||||
'permanently' => array(
|
|
||||||
'description' => __('To delete permanently, you can pass \'permanently\' as 1. By default this will only trash collection'),
|
|
||||||
'default' => '0',
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
|
@ -88,6 +75,12 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
'permission_callback' => array($this, 'update_item_permissions_check'),
|
'permission_callback' => array($this, 'update_item_permissions_check'),
|
||||||
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
|
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
'methods' => \WP_REST_Server::DELETABLE,
|
||||||
|
'callback' => array($this, 'delete_item'),
|
||||||
|
'permission_callback' => array($this, 'delete_item_permissions_check'),
|
||||||
|
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::DELETABLE)
|
||||||
|
),
|
||||||
'schema' => [$this, 'get_schema']
|
'schema' => [$this, 'get_schema']
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -98,7 +91,15 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
'methods' => \WP_REST_Server::EDITABLE,
|
'methods' => \WP_REST_Server::EDITABLE,
|
||||||
'callback' => array($this, 'update_item'),
|
'callback' => array($this, 'update_item'),
|
||||||
'permission_callback' => array($this, 'update_item_permissions_check'),
|
'permission_callback' => array($this, 'update_item_permissions_check'),
|
||||||
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
|
'args' => array_merge(
|
||||||
|
array(
|
||||||
|
'collection_id' => array(
|
||||||
|
'description' => __( 'Collection ID', 'tainacan' ),
|
||||||
|
'required' => true,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
$this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
|
||||||
|
),
|
||||||
),
|
),
|
||||||
'schema' => [$this, 'get_schema']
|
'schema' => [$this, 'get_schema']
|
||||||
)
|
)
|
||||||
|
@ -498,26 +499,62 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
* @return array|mixed
|
* @return array|mixed
|
||||||
*/
|
*/
|
||||||
public function get_endpoint_args_for_item_schema( $method = null ) {
|
public function get_endpoint_args_for_item_schema( $method = null ) {
|
||||||
$endpoint_args = [];
|
$endpoint_args = [
|
||||||
if($method === \WP_REST_Server::READABLE) {
|
'taxonomy_id' => [
|
||||||
$endpoint_args = array_merge(
|
'description' => __( 'Taxonomy ID', 'tainacan' ),
|
||||||
$endpoint_args,
|
'required' => true,
|
||||||
parent::get_wp_query_params(),
|
]
|
||||||
parent::get_fetch_only_param()
|
];
|
||||||
);
|
|
||||||
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
|
switch( $method ) {
|
||||||
$map = $this->taxonomy_repository->get_map();
|
case \WP_REST_Server::READABLE:
|
||||||
|
|
||||||
foreach ($map as $mapped => $value){
|
$endpoint_args['fetch_only'] = array(
|
||||||
$set_ = 'set_'. $mapped;
|
'type' => ['string', 'array'],
|
||||||
|
'description' => __( 'Fetch only specific attribute. The specifics attributes are the same in schema.', 'tainacan' ),
|
||||||
|
);
|
||||||
|
$endpoint_args['context'] = array(
|
||||||
|
'description' => __( 'Scope under which the request is made; determines fields present in response.', 'tainacan' ),
|
||||||
|
'type' => 'string',
|
||||||
|
'default' => 'view',
|
||||||
|
'enum' => array(
|
||||||
|
'view',
|
||||||
|
'edit',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$endpoint_args = array_merge(
|
||||||
|
$endpoint_args,
|
||||||
|
parent::get_wp_query_params(),
|
||||||
|
parent::get_fetch_only_param()
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case \WP_REST_Server::CREATABLE:
|
||||||
|
case \WP_REST_Server::EDITABLE:
|
||||||
|
$map = $this->taxonomy_repository->get_map();
|
||||||
|
|
||||||
// Show only args that has a method set
|
foreach ($map as $mapped => $value){
|
||||||
if( !method_exists($this->taxonomy, "$set_") ){
|
$set_ = 'set_'. $mapped;
|
||||||
unset($map[$mapped]);
|
|
||||||
|
// Show only args that has a method set
|
||||||
|
if( !method_exists($this->taxonomy, "$set_") ){
|
||||||
|
unset($map[$mapped]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$endpoint_args = $map;
|
$endpoint_args = array_merge(
|
||||||
|
$endpoint_args,
|
||||||
|
$map
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( $method === \WP_REST_Server::CREATABLE )
|
||||||
|
unset($endpoint_args['taxonomy_id']);
|
||||||
|
break;
|
||||||
|
case \WP_REST_Server::DELETABLE:
|
||||||
|
$endpoint_args['permanently'] = array(
|
||||||
|
'description' => __('To delete permanently, you can pass \'permanently\' as 1. By default this will only trash collection'),
|
||||||
|
'default' => '0',
|
||||||
|
);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $endpoint_args;
|
return $endpoint_args;
|
||||||
|
@ -550,7 +587,8 @@ class REST_Taxonomies_Controller extends REST_Controller {
|
||||||
$schema = [
|
$schema = [
|
||||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||||
'title' => 'taxonomy',
|
'title' => 'taxonomy',
|
||||||
'type' => 'object'
|
'type' => 'object',
|
||||||
|
'tags' => [ $this->rest_base ]
|
||||||
];
|
];
|
||||||
|
|
||||||
$main_schema = parent::get_repository_schema( $this->taxonomy_repository );
|
$main_schema = parent::get_repository_schema( $this->taxonomy_repository );
|
||||||
|
|
|
@ -63,6 +63,7 @@ class Taxonomies extends Repository {
|
||||||
'description' => __( 'Allow/Deny the creation of new terms in the taxonomy', 'tainacan' ),
|
'description' => __( 'Allow/Deny the creation of new terms in the taxonomy', 'tainacan' ),
|
||||||
'on_error' => __( 'Invalid insertion, allowed values are ( yes/no )', 'tainacan' ),
|
'on_error' => __( 'Invalid insertion, allowed values are ( yes/no )', 'tainacan' ),
|
||||||
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
|
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
|
||||||
|
'enum' => [ 'yes', 'no' ],
|
||||||
'default' => 'yes'
|
'default' => 'yes'
|
||||||
],
|
],
|
||||||
'hierarchical' => [
|
'hierarchical' => [
|
||||||
|
@ -72,6 +73,7 @@ class Taxonomies extends Repository {
|
||||||
'description' => __( 'Allow/Deny the existence of terms children to build a hierarchy', 'tainacan' ),
|
'description' => __( 'Allow/Deny the existence of terms children to build a hierarchy', 'tainacan' ),
|
||||||
'on_error' => __( 'Invalid insertion, allowed values are ( yes/no )', 'tainacan' ),
|
'on_error' => __( 'Invalid insertion, allowed values are ( yes/no )', 'tainacan' ),
|
||||||
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
|
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
|
||||||
|
'enum' => [ 'yes', 'no' ],
|
||||||
'default' => 'yes'
|
'default' => 'yes'
|
||||||
],
|
],
|
||||||
'enabled_post_types' => [
|
'enabled_post_types' => [
|
||||||
|
|
Loading…
Reference in New Issue