2016-02-22 19:43:52 +00:00
< ? php
2018-01-24 17:07:51 +00:00
/**
* Abstract Rest Terms Controller
*
* @ package WooCommerce / Abstracts
* @ version 3.3 . 0
*/
2016-02-22 19:43:52 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
2018-01-24 17:07:51 +00:00
* Terms controller class .
2016-02-22 19:43:52 +00:00
*/
2016-05-11 19:34:53 +00:00
abstract class WC_REST_Terms_Controller extends WC_REST_Controller {
2016-02-22 19:43:52 +00:00
/**
* Route base .
*
* @ var string
*/
protected $rest_base = '' ;
2016-03-02 23:22:12 +00:00
/**
* Taxonomy .
*
* @ var string
*/
protected $taxonomy = '' ;
/**
* Register the routes for terms .
*/
public function register_routes () {
2018-01-24 17:09:50 +00:00
register_rest_route (
$this -> namespace , '/' . $this -> rest_base , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
array (
'methods' => WP_REST_Server :: CREATABLE ,
'callback' => array ( $this , 'create_item' ),
'permission_callback' => array ( $this , 'create_item_permissions_check' ),
'args' => array_merge (
$this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: CREATABLE ), array (
'name' => array (
'type' => 'string' ,
'description' => __ ( 'Name for the resource.' , 'woocommerce' ),
'required' => true ,
),
)
2016-03-02 23:22:12 +00:00
),
2017-01-26 20:33:39 +00:00
),
2018-01-24 17:09:50 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
register_rest_route (
$this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d]+)' , array (
'args' => array (
'id' => array (
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
),
2016-03-02 23:22:12 +00:00
),
2018-01-24 17:09:50 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_item_permissions_check' ),
'args' => array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
2016-03-02 23:22:12 +00:00
),
),
2018-01-24 17:09:50 +00:00
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'update_item_permissions_check' ),
'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' => array (
'force' => array (
'default' => false ,
'type' => 'boolean' ,
'description' => __ ( 'Required to be true, as resource does not support trashing.' , 'woocommerce' ),
),
),
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
register_rest_route (
$this -> namespace , '/' . $this -> rest_base . '/batch' , array (
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'batch_items' ),
'permission_callback' => array ( $this , 'batch_items_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
'schema' => array ( $this , 'get_public_batch_schema' ),
)
);
2016-03-02 23:22:12 +00:00
}
/**
* Check if a given request has access to read the terms .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
2016-03-31 19:57:55 +00:00
$permissions = $this -> check_permissions ( $request , 'read' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
2016-03-07 19:39:24 +00:00
}
2016-03-02 23:22:12 +00:00
2016-03-31 19:57:55 +00:00
if ( ! $permissions ) {
2016-03-30 17:17:40 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list resources.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-03-02 23:22:12 +00:00
}
return true ;
}
2016-03-07 17:49:14 +00:00
/**
* Check if a given request has access to create a term .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function create_item_permissions_check ( $request ) {
2016-03-31 19:57:55 +00:00
$permissions = $this -> check_permissions ( $request , 'create' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
2016-03-07 19:39:24 +00:00
}
2016-03-07 17:49:14 +00:00
2016-03-31 19:57:55 +00:00
if ( ! $permissions ) {
2016-08-08 21:52:46 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_create' , __ ( 'Sorry, you are not allowed to create resources.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-03-07 17:49:14 +00:00
}
return true ;
}
2016-03-02 23:22:12 +00:00
/**
* Check if a given request has access to read a term .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_item_permissions_check ( $request ) {
2016-03-31 19:57:55 +00:00
$permissions = $this -> check_permissions ( $request , 'read' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
2016-03-30 17:17:40 +00:00
}
2016-03-31 19:57:55 +00:00
if ( ! $permissions ) {
2016-03-30 17:49:22 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot view this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-03-30 17:17:40 +00:00
}
return true ;
2016-03-02 23:22:12 +00:00
}
2016-03-07 17:49:14 +00:00
/**
* Check if a given request has access to update a term .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function update_item_permissions_check ( $request ) {
2016-03-31 19:57:55 +00:00
$permissions = $this -> check_permissions ( $request , 'edit' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
2016-03-07 19:39:24 +00:00
}
2016-03-31 19:57:55 +00:00
if ( ! $permissions ) {
2016-08-08 21:52:46 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_edit' , __ ( 'Sorry, you are not allowed to edit this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-03-07 17:49:14 +00:00
}
return true ;
}
2016-03-03 21:19:02 +00:00
/**
* Check if a given request has access to delete a term .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function delete_item_permissions_check ( $request ) {
2016-03-31 19:57:55 +00:00
$permissions = $this -> check_permissions ( $request , 'delete' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
}
if ( ! $permissions ) {
2016-08-08 21:52:46 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_delete' , __ ( 'Sorry, you are not allowed to delete this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-03-31 19:57:55 +00:00
}
return true ;
}
2016-05-11 19:34:53 +00:00
/**
* Check if a given request has access batch create , update and delete items .
*
* @ param WP_REST_Request $request Full details about the request .
2017-05-15 11:50:52 +00:00
* @ return boolean | WP_Error
2016-05-11 19:34:53 +00:00
*/
public function batch_items_permissions_check ( $request ) {
$permissions = $this -> check_permissions ( $request , 'batch' );
if ( is_wp_error ( $permissions ) ) {
return $permissions ;
}
if ( ! $permissions ) {
2016-08-08 21:52:46 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_batch' , __ ( 'Sorry, you are not allowed to batch manipulate this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
2016-05-11 19:34:53 +00:00
}
return true ;
}
2016-03-31 19:57:55 +00:00
/**
* Check permissions .
*
* @ param WP_REST_Request $request Full details about the request .
2018-01-24 17:07:51 +00:00
* @ param string $context Request context .
2016-03-31 19:57:55 +00:00
* @ return bool | WP_Error
*/
protected function check_permissions ( $request , $context = 'read' ) {
// Get taxonomy.
2016-03-07 19:39:24 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
2018-01-24 17:04:32 +00:00
if ( ! $taxonomy || ! taxonomy_exists ( $taxonomy ) ) {
2016-09-06 20:57:30 +00:00
return new WP_Error ( 'woocommerce_rest_taxonomy_invalid' , __ ( 'Taxonomy does not exist.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-03-07 19:39:24 +00:00
}
2016-03-31 19:57:55 +00:00
// Check permissions for a single term.
2018-01-24 17:04:32 +00:00
$id = intval ( $request [ 'id' ] );
if ( $id ) {
2016-03-31 19:57:55 +00:00
$term = get_term ( $id , $taxonomy );
2018-01-24 17:04:32 +00:00
if ( is_wp_error ( $term ) || ! $term || $term -> taxonomy !== $taxonomy ) {
2016-09-06 20:57:30 +00:00
return new WP_Error ( 'woocommerce_rest_term_invalid' , __ ( 'Resource does not exist.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-03-31 19:57:55 +00:00
}
return wc_rest_check_product_term_permissions ( $taxonomy , $context , $term -> term_id );
2016-03-03 21:19:02 +00:00
}
2016-03-31 19:57:55 +00:00
return wc_rest_check_product_term_permissions ( $taxonomy , $context );
2016-03-03 21:19:02 +00:00
}
2016-03-02 23:22:12 +00:00
/**
* Get terms associated with a taxonomy .
*
2016-03-07 19:39:24 +00:00
* @ param WP_REST_Request $request Full details about the request .
2016-03-02 23:22:12 +00:00
* @ return WP_REST_Response | WP_Error
*/
public function get_items ( $request ) {
2016-03-31 19:14:18 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
2016-03-02 23:22:12 +00:00
$prepared_args = array (
'exclude' => $request [ 'exclude' ],
'include' => $request [ 'include' ],
'order' => $request [ 'order' ],
'orderby' => $request [ 'orderby' ],
'product' => $request [ 'product' ],
'hide_empty' => $request [ 'hide_empty' ],
'number' => $request [ 'per_page' ],
'search' => $request [ 'search' ],
'slug' => $request [ 'slug' ],
);
if ( ! empty ( $request [ 'offset' ] ) ) {
$prepared_args [ 'offset' ] = $request [ 'offset' ];
} else {
2018-01-24 17:09:50 +00:00
$prepared_args [ 'offset' ] = ( $request [ 'page' ] - 1 ) * $prepared_args [ 'number' ];
2016-03-02 23:22:12 +00:00
}
2016-03-07 19:39:24 +00:00
$taxonomy_obj = get_taxonomy ( $taxonomy );
2016-03-02 23:22:12 +00:00
2016-03-07 19:39:24 +00:00
if ( $taxonomy_obj -> hierarchical && isset ( $request [ 'parent' ] ) ) {
2016-03-02 23:22:12 +00:00
if ( 0 === $request [ 'parent' ] ) {
// Only query top-level terms.
$prepared_args [ 'parent' ] = 0 ;
} else {
if ( $request [ 'parent' ] ) {
$prepared_args [ 'parent' ] = $request [ 'parent' ];
}
}
}
/**
* Filter the query arguments , before passing them to `get_terms()` .
*
* Enables adding extra arguments or setting defaults for a terms
* collection request .
*
* @ see https :// developer . wordpress . org / reference / functions / get_terms /
*
* @ param array $prepared_args Array of arguments to be
* passed to get_terms .
* @ param WP_REST_Request $request The current request .
*/
2016-03-07 19:39:24 +00:00
$prepared_args = apply_filters ( " woocommerce_rest_ { $taxonomy } _query " , $prepared_args , $request );
2016-03-02 23:22:12 +00:00
2018-01-24 17:07:51 +00:00
if ( ! empty ( $prepared_args [ 'product' ] ) ) {
2017-06-09 21:10:55 +00:00
$query_result = $this -> get_terms_for_product ( $prepared_args , $request );
2018-01-24 17:09:50 +00:00
$total_terms = $this -> total_terms ;
2016-03-02 23:22:12 +00:00
} else {
2016-03-07 19:39:24 +00:00
$query_result = get_terms ( $taxonomy , $prepared_args );
2016-03-02 23:22:12 +00:00
$count_args = $prepared_args ;
unset ( $count_args [ 'number' ] );
unset ( $count_args [ 'offset' ] );
2016-03-07 19:39:24 +00:00
$total_terms = wp_count_terms ( $taxonomy , $count_args );
2016-03-02 23:22:12 +00:00
// Ensure we don't return results when offset is out of bounds.
2018-01-24 17:07:51 +00:00
// See https://core.trac.wordpress.org/ticket/35935.
2016-03-02 23:22:12 +00:00
if ( $prepared_args [ 'offset' ] >= $total_terms ) {
$query_result = array ();
}
// wp_count_terms can return a falsy value when the term has no children.
if ( ! $total_terms ) {
$total_terms = 0 ;
}
}
$response = array ();
foreach ( $query_result as $term ) {
2018-01-24 17:09:50 +00:00
$data = $this -> prepare_item_for_response ( $term , $request );
2016-03-02 23:22:12 +00:00
$response [] = $this -> prepare_response_for_collection ( $data );
}
$response = rest_ensure_response ( $response );
2017-07-17 10:10:52 +00:00
// Store pagination values for headers then unset for count query.
2016-03-02 23:22:12 +00:00
$per_page = ( int ) $prepared_args [ 'number' ];
2018-01-24 17:09:50 +00:00
$page = ceil ( ( ( ( int ) $prepared_args [ 'offset' ] ) / $per_page ) + 1 );
2016-03-02 23:22:12 +00:00
$response -> header ( 'X-WP-Total' , ( int ) $total_terms );
$max_pages = ceil ( $total_terms / $per_page );
$response -> header ( 'X-WP-TotalPages' , ( int ) $max_pages );
2016-03-07 18:36:17 +00:00
$base = add_query_arg ( $request -> get_query_params (), rest_url ( '/' . $this -> namespace . '/' . $this -> rest_base ) );
2016-03-02 23:22:12 +00:00
if ( $page > 1 ) {
$prev_page = $page - 1 ;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages ;
}
$prev_link = add_query_arg ( 'page' , $prev_page , $base );
$response -> link_header ( 'prev' , $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1 ;
$next_link = add_query_arg ( 'page' , $next_page , $base );
$response -> link_header ( 'next' , $next_link );
}
return $response ;
}
2016-03-07 17:49:14 +00:00
/**
* Create a single term for a taxonomy .
*
2016-03-07 19:39:24 +00:00
* @ param WP_REST_Request $request Full details about the request .
2016-03-07 17:49:14 +00:00
* @ return WP_REST_Request | WP_Error
*/
public function create_item ( $request ) {
2016-03-07 19:39:24 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
$name = $request [ 'name' ];
$args = array ();
2016-03-31 19:14:18 +00:00
$schema = $this -> get_item_schema ();
2016-03-07 17:49:14 +00:00
2016-03-07 19:39:24 +00:00
if ( ! empty ( $schema [ 'properties' ][ 'description' ] ) && isset ( $request [ 'description' ] ) ) {
2016-03-07 17:49:14 +00:00
$args [ 'description' ] = $request [ 'description' ];
}
if ( isset ( $request [ 'slug' ] ) ) {
$args [ 'slug' ] = $request [ 'slug' ];
}
if ( isset ( $request [ 'parent' ] ) ) {
2016-03-07 19:39:24 +00:00
if ( ! is_taxonomy_hierarchical ( $taxonomy ) ) {
2016-03-07 17:49:14 +00:00
return new WP_Error ( 'woocommerce_rest_taxonomy_not_hierarchical' , __ ( 'Can not set resource parent, taxonomy is not hierarchical.' , 'woocommerce' ), array ( 'status' => 400 ) );
}
2016-03-07 19:39:24 +00:00
$parent = get_term ( ( int ) $request [ 'parent' ], $taxonomy );
2016-03-07 17:49:14 +00:00
2017-10-03 20:59:58 +00:00
// If is null or WP_Error is invalid parent term.
if ( ! $parent || is_wp_error ( $parent ) ) {
2016-09-06 20:57:30 +00:00
return new WP_Error ( 'woocommerce_rest_term_invalid' , __ ( 'Parent resource does not exist.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-03-07 17:49:14 +00:00
}
$args [ 'parent' ] = $parent -> term_id ;
}
2016-03-07 19:39:24 +00:00
$term = wp_insert_term ( $name , $taxonomy , $args );
2016-03-07 17:49:14 +00:00
if ( is_wp_error ( $term ) ) {
2017-01-23 19:07:36 +00:00
$error_data = array ( 'status' => 400 );
2016-03-07 17:49:14 +00:00
2017-01-23 19:07:36 +00:00
// If we're going to inform the client that the term exists,
// give them the identifier they can actually use.
2018-01-24 17:07:51 +00:00
$term_id = $term -> get_error_data ( 'term_exists' );
if ( $term_id ) {
2017-01-23 19:07:36 +00:00
$error_data [ 'resource_id' ] = $term_id ;
2016-03-07 17:49:14 +00:00
}
2017-01-23 19:07:36 +00:00
return new WP_Error ( $term -> get_error_code (), $term -> get_error_message (), $error_data );
2016-03-07 17:49:14 +00:00
}
2016-03-07 19:39:24 +00:00
$term = get_term ( $term [ 'term_id' ], $taxonomy );
2016-03-07 17:49:14 +00:00
$this -> update_additional_fields_for_object ( $term , $request );
// Add term data.
$meta_fields = $this -> update_term_meta_fields ( $term , $request );
if ( is_wp_error ( $meta_fields ) ) {
2017-04-18 07:30:04 +00:00
wp_delete_term ( $term -> term_id , $taxonomy );
2016-06-30 18:24:23 +00:00
2016-03-07 17:49:14 +00:00
return $meta_fields ;
}
/**
* Fires after a single term is created or updated via the REST API .
*
* @ param WP_Term $term Inserted Term object .
* @ param WP_REST_Request $request Request object .
* @ param boolean $creating True when creating term , false when updating .
*/
2016-03-07 19:39:24 +00:00
do_action ( " woocommerce_rest_insert_ { $taxonomy } " , $term , $request , true );
2016-03-07 17:49:14 +00:00
2016-03-31 19:17:39 +00:00
$request -> set_param ( 'context' , 'edit' );
2016-03-07 17:49:14 +00:00
$response = $this -> prepare_item_for_response ( $term , $request );
$response = rest_ensure_response ( $response );
$response -> set_status ( 201 );
2016-03-22 18:34:27 +00:00
$base = '/' . $this -> namespace . '/' . $this -> rest_base ;
if ( ! empty ( $request [ 'attribute_id' ] ) ) {
$base = str_replace ( '(?P<attribute_id>[\d]+)' , ( int ) $request [ 'attribute_id' ], $base );
}
$response -> header ( 'Location' , rest_url ( $base . '/' . $term -> term_id ) );
2016-03-07 17:49:14 +00:00
return $response ;
}
2016-03-02 23:22:12 +00:00
/**
* Get a single term from a taxonomy .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Request | WP_Error
*/
public function get_item ( $request ) {
2016-03-07 19:39:24 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
$term = get_term ( ( int ) $request [ 'id' ], $taxonomy );
2016-03-02 23:22:12 +00:00
if ( is_wp_error ( $term ) ) {
return $term ;
}
$response = $this -> prepare_item_for_response ( $term , $request );
return rest_ensure_response ( $response );
}
2016-03-07 17:49:14 +00:00
/**
2016-03-07 19:39:24 +00:00
* Update a single term from a taxonomy .
2016-03-07 17:49:14 +00:00
*
2016-03-07 19:39:24 +00:00
* @ param WP_REST_Request $request Full details about the request .
2016-03-07 17:49:14 +00:00
* @ return WP_REST_Request | WP_Error
*/
public function update_item ( $request ) {
2016-03-31 19:57:55 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
$term = get_term ( ( int ) $request [ 'id' ], $taxonomy );
2016-03-30 17:17:40 +00:00
$schema = $this -> get_item_schema ();
2016-03-07 17:49:14 +00:00
$prepared_args = array ();
2016-03-31 19:57:55 +00:00
2016-03-07 17:49:14 +00:00
if ( isset ( $request [ 'name' ] ) ) {
$prepared_args [ 'name' ] = $request [ 'name' ];
}
2016-03-07 19:39:24 +00:00
if ( ! empty ( $schema [ 'properties' ][ 'description' ] ) && isset ( $request [ 'description' ] ) ) {
2016-03-07 17:49:14 +00:00
$prepared_args [ 'description' ] = $request [ 'description' ];
}
if ( isset ( $request [ 'slug' ] ) ) {
$prepared_args [ 'slug' ] = $request [ 'slug' ];
}
if ( isset ( $request [ 'parent' ] ) ) {
2016-03-07 19:39:24 +00:00
if ( ! is_taxonomy_hierarchical ( $taxonomy ) ) {
2016-03-07 17:49:14 +00:00
return new WP_Error ( 'woocommerce_rest_taxonomy_not_hierarchical' , __ ( 'Can not set resource parent, taxonomy is not hierarchical.' , 'woocommerce' ), array ( 'status' => 400 ) );
}
2017-01-23 18:43:26 +00:00
$parent_id = ( int ) $request [ 'parent' ];
2016-03-07 17:49:14 +00:00
2017-01-23 18:43:26 +00:00
if ( 0 === $parent_id ) {
$prepared_args [ 'parent' ] = $parent_id ;
} else {
$parent = get_term ( $parent_id , $taxonomy );
2016-03-07 17:49:14 +00:00
2017-01-23 18:43:26 +00:00
if ( ! $parent ) {
return new WP_Error ( 'woocommerce_rest_term_invalid' , __ ( 'Parent resource does not exist.' , 'woocommerce' ), array ( 'status' => 400 ) );
}
$prepared_args [ 'parent' ] = $parent -> term_id ;
}
2016-03-07 17:49:14 +00:00
}
// Only update the term if we haz something to update.
if ( ! empty ( $prepared_args ) ) {
$update = wp_update_term ( $term -> term_id , $term -> taxonomy , $prepared_args );
if ( is_wp_error ( $update ) ) {
return $update ;
}
}
2016-03-07 19:39:24 +00:00
$term = get_term ( ( int ) $request [ 'id' ], $taxonomy );
2016-03-07 17:49:14 +00:00
$this -> update_additional_fields_for_object ( $term , $request );
// Update term data.
$meta_fields = $this -> update_term_meta_fields ( $term , $request );
if ( is_wp_error ( $meta_fields ) ) {
return $meta_fields ;
}
/**
* Fires after a single term is created or updated via the REST API .
*
* @ param WP_Term $term Inserted Term object .
* @ param WP_REST_Request $request Request object .
* @ param boolean $creating True when creating term , false when updating .
*/
2016-03-07 19:39:24 +00:00
do_action ( " woocommerce_rest_insert_ { $taxonomy } " , $term , $request , false );
2016-03-07 17:49:14 +00:00
2016-03-31 19:17:39 +00:00
$request -> set_param ( 'context' , 'edit' );
2016-03-07 17:49:14 +00:00
$response = $this -> prepare_item_for_response ( $term , $request );
return rest_ensure_response ( $response );
}
2016-03-03 21:19:02 +00:00
/**
* Delete a single term from a taxonomy .
*
2016-03-07 19:39:24 +00:00
* @ param WP_REST_Request $request Full details about the request .
2016-03-03 21:19:02 +00:00
* @ return WP_REST_Response | WP_Error
*/
public function delete_item ( $request ) {
2016-03-07 19:39:24 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
2016-03-31 19:57:55 +00:00
$force = isset ( $request [ 'force' ] ) ? ( bool ) $request [ 'force' ] : false ;
2016-03-03 21:19:02 +00:00
// We don't support trashing for this type, error out.
if ( ! $force ) {
return new WP_Error ( 'woocommerce_rest_trash_not_supported' , __ ( 'Resource does not support trashing.' , 'woocommerce' ), array ( 'status' => 501 ) );
}
2016-03-07 19:39:24 +00:00
$term = get_term ( ( int ) $request [ 'id' ], $taxonomy );
2016-03-31 19:17:39 +00:00
$request -> set_param ( 'context' , 'edit' );
2016-03-03 21:19:02 +00:00
$response = $this -> prepare_item_for_response ( $term , $request );
$retval = wp_delete_term ( $term -> term_id , $term -> taxonomy );
if ( ! $retval ) {
return new WP_Error ( 'woocommerce_rest_cannot_delete' , __ ( 'The resource cannot be deleted.' , 'woocommerce' ), array ( 'status' => 500 ) );
}
/**
* Fires after a single term is deleted via the REST API .
*
* @ param WP_Term $term The deleted term .
* @ param WP_REST_Response $response The response data .
* @ param WP_REST_Request $request The request sent to the API .
*/
2016-03-07 19:39:24 +00:00
do_action ( " woocommerce_rest_delete_ { $taxonomy } " , $term , $response , $request );
2016-03-03 21:19:02 +00:00
return $response ;
}
2016-03-02 23:22:12 +00:00
/**
* Prepare links for the request .
*
2018-01-24 17:07:51 +00:00
* @ param object $term Term object .
2016-03-07 19:39:24 +00:00
* @ param WP_REST_Request $request Full details about the request .
2016-03-02 23:22:12 +00:00
* @ return array Links for the given term .
*/
2016-03-07 19:39:24 +00:00
protected function prepare_links ( $term , $request ) {
2016-03-07 18:36:17 +00:00
$base = '/' . $this -> namespace . '/' . $this -> rest_base ;
2016-03-07 19:39:24 +00:00
2016-03-07 20:28:52 +00:00
if ( ! empty ( $request [ 'attribute_id' ] ) ) {
2016-03-07 19:39:24 +00:00
$base = str_replace ( '(?P<attribute_id>[\d]+)' , ( int ) $request [ 'attribute_id' ], $base );
}
2016-03-02 23:22:12 +00:00
$links = array (
2018-01-24 17:09:50 +00:00
'self' => array (
2016-03-02 23:22:12 +00:00
'href' => rest_url ( trailingslashit ( $base ) . $term -> term_id ),
),
'collection' => array (
'href' => rest_url ( $base ),
),
);
if ( $term -> parent ) {
$parent_term = get_term ( ( int ) $term -> parent , $term -> taxonomy );
if ( $parent_term ) {
$links [ 'up' ] = array (
'href' => rest_url ( trailingslashit ( $base ) . $parent_term -> term_id ),
);
}
}
return $links ;
}
2016-03-07 17:49:14 +00:00
/**
* Update term meta fields .
*
2018-01-24 17:07:51 +00:00
* @ param WP_Term $term Term object .
* @ param WP_REST_Request $request Full details about the request .
2016-03-07 17:49:14 +00:00
* @ return bool | WP_Error
*/
protected function update_term_meta_fields ( $term , $request ) {
return true ;
}
2016-03-02 23:22:12 +00:00
/**
* Get the terms attached to a product .
*
* This is an alternative to `get_terms()` that uses `get_the_terms()`
* instead , which hits the object cache . There are a few things not
* supported , notably `include` , `exclude` . In `self::get_items()` these
* are instead treated as a full query .
*
2018-01-24 17:07:51 +00:00
* @ param array $prepared_args Arguments for `get_terms()` .
* @ param WP_REST_Request $request Full details about the request .
2016-03-02 23:22:12 +00:00
* @ return array List of term objects . ( Total count in `$this->total_terms` ) .
*/
2017-06-09 21:10:55 +00:00
protected function get_terms_for_product ( $prepared_args , $request ) {
2016-03-07 19:39:24 +00:00
$taxonomy = $this -> get_taxonomy ( $request );
$query_result = get_the_terms ( $prepared_args [ 'product' ], $taxonomy );
2016-03-02 23:22:12 +00:00
if ( empty ( $query_result ) ) {
$this -> total_terms = 0 ;
return array ();
}
// get_items() verifies that we don't have `include` set, and default.
// ordering is by `name`.
2018-01-24 17:09:50 +00:00
if ( ! in_array ( $prepared_args [ 'orderby' ], array ( 'name' , 'none' , 'include' ), true ) ) {
2016-03-02 23:22:12 +00:00
switch ( $prepared_args [ 'orderby' ] ) {
2018-01-24 17:07:51 +00:00
case 'id' :
2016-03-02 23:22:12 +00:00
$this -> sort_column = 'term_id' ;
break ;
2018-01-24 17:07:51 +00:00
case 'slug' :
case 'term_group' :
case 'description' :
case 'count' :
2016-03-02 23:22:12 +00:00
$this -> sort_column = $prepared_args [ 'orderby' ];
break ;
}
usort ( $query_result , array ( $this , 'compare_terms' ) );
}
if ( strtolower ( $prepared_args [ 'order' ] ) !== 'asc' ) {
$query_result = array_reverse ( $query_result );
}
// Pagination.
$this -> total_terms = count ( $query_result );
2018-01-24 17:09:50 +00:00
$query_result = array_slice ( $query_result , $prepared_args [ 'offset' ], $prepared_args [ 'number' ] );
2016-03-02 23:22:12 +00:00
return $query_result ;
}
/**
* Comparison function for sorting terms by a column .
*
* Uses `$this->sort_column` to determine field to sort by .
*
* @ param stdClass $left Term object .
* @ param stdClass $right Term object .
* @ return int < 0 if left is higher " priority " than right , 0 if equal , > 0 if right is higher " priority " than left .
*/
protected function compare_terms ( $left , $right ) {
$col = $this -> sort_column ;
$left_val = $left -> $col ;
$right_val = $right -> $col ;
if ( is_int ( $left_val ) && is_int ( $right_val ) ) {
return $left_val - $right_val ;
}
return strcmp ( $left_val , $right_val );
}
/**
* Get the query params for collections
*
* @ return array
*/
public function get_collection_params () {
2016-03-09 04:46:04 +00:00
$params = parent :: get_collection_params ();
2016-03-07 19:39:24 +00:00
2018-01-24 17:04:32 +00:00
if ( '' !== $this -> taxonomy && taxonomy_exists ( $this -> taxonomy ) ) {
2016-03-07 19:39:24 +00:00
$taxonomy = get_taxonomy ( $this -> taxonomy );
} else {
2018-01-24 17:09:50 +00:00
$taxonomy = new stdClass ();
2016-03-07 19:39:24 +00:00
$taxonomy -> hierarchical = true ;
}
2016-03-02 23:22:12 +00:00
2016-03-09 04:46:04 +00:00
$params [ 'context' ][ 'default' ] = 'view' ;
2016-03-02 23:22:12 +00:00
2016-03-09 04:46:04 +00:00
$params [ 'exclude' ] = array (
2017-01-26 20:33:39 +00:00
'description' => __ ( 'Ensure result set excludes specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'items' => array (
2018-01-24 17:09:50 +00:00
'type' => 'integer' ,
2017-01-26 20:33:39 +00:00
),
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
2016-03-02 23:22:12 +00:00
);
2016-03-09 04:46:04 +00:00
$params [ 'include' ] = array (
2017-01-26 20:33:39 +00:00
'description' => __ ( 'Limit result set to specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'items' => array (
2018-01-24 17:09:50 +00:00
'type' => 'integer' ,
2017-01-26 20:33:39 +00:00
),
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
2016-03-02 23:22:12 +00:00
);
if ( ! $taxonomy -> hierarchical ) {
2016-03-09 04:46:04 +00:00
$params [ 'offset' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Offset the result set by a specific number of items.' , 'woocommerce' ),
'type' => 'integer' ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
}
2016-03-09 04:46:04 +00:00
$params [ 'order' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Order sort attribute ascending or descending.' , 'woocommerce' ),
'type' => 'string' ,
'sanitize_callback' => 'sanitize_key' ,
'default' => 'asc' ,
'enum' => array (
2016-03-02 23:22:12 +00:00
'asc' ,
'desc' ,
),
2018-01-24 17:09:50 +00:00
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
2016-03-09 04:46:04 +00:00
$params [ 'orderby' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Sort collection by resource attribute.' , 'woocommerce' ),
'type' => 'string' ,
'sanitize_callback' => 'sanitize_key' ,
'default' => 'name' ,
'enum' => array (
2016-03-02 23:22:12 +00:00
'id' ,
'include' ,
'name' ,
'slug' ,
'term_group' ,
'description' ,
'count' ,
),
2018-01-24 17:09:50 +00:00
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
2016-03-09 04:46:04 +00:00
$params [ 'hide_empty' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Whether to hide resources not assigned to any products.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
if ( $taxonomy -> hierarchical ) {
2016-03-09 04:46:04 +00:00
$params [ 'parent' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Limit result set to resources assigned to a specific parent.' , 'woocommerce' ),
'type' => 'integer' ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
}
2016-03-09 04:46:04 +00:00
$params [ 'product' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Limit result set to resources assigned to a specific product.' , 'woocommerce' ),
'type' => 'integer' ,
'default' => null ,
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
2016-03-09 04:46:04 +00:00
$params [ 'slug' ] = array (
2018-01-24 17:09:50 +00:00
'description' => __ ( 'Limit result set to resources with a specific slug.' , 'woocommerce' ),
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
2016-03-02 23:22:12 +00:00
);
2016-03-07 19:39:24 +00:00
2016-03-09 04:46:04 +00:00
return $params ;
2016-03-02 23:22:12 +00:00
}
2016-03-07 19:39:24 +00:00
/**
* Get taxonomy .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return int | WP_Error
*/
protected function get_taxonomy ( $request ) {
// Check if taxonomy is defined.
// Prevents check for attribute taxonomy more than one time for each query.
if ( '' !== $this -> taxonomy ) {
return $this -> taxonomy ;
}
2016-03-07 20:28:52 +00:00
if ( ! empty ( $request [ 'attribute_id' ] ) ) {
2016-03-07 19:39:24 +00:00
$taxonomy = wc_attribute_taxonomy_name_by_id ( ( int ) $request [ 'attribute_id' ] );
$this -> taxonomy = $taxonomy ;
}
return $this -> taxonomy ;
}
2016-02-22 19:43:52 +00:00
}