WC_REST_Product_Attribute_Terms_V1_Controller

REST API Product Attribute Terms controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/v1/class-wc-rest-product-attribute-terms-controller.php  
  1. class WC_REST_Product_Attribute_Terms_V1_Controller extends WC_REST_Terms_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v1'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'products/attributes/(?P<attribute_id>[\d]+)/terms'; 
  14.  
  15. /** 
  16. * Register the routes for terms. 
  17. */ 
  18. public function register_routes() { 
  19. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  20. 'args' => array( 
  21. 'attribute_id' => array( 
  22. 'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),  
  23. 'type' => 'integer',  
  24. ),  
  25. ),  
  26. array( 
  27. 'methods' => WP_REST_Server::READABLE,  
  28. 'callback' => array( $this, 'get_items' ),  
  29. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  30. 'args' => $this->get_collection_params(),  
  31. ),  
  32. array( 
  33. 'methods' => WP_REST_Server::CREATABLE,  
  34. 'callback' => array( $this, 'create_item' ),  
  35. 'permission_callback' => array( $this, 'create_item_permissions_check' ),  
  36. 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( 
  37. 'name' => array( 
  38. 'type' => 'string',  
  39. 'description' => __( 'Name for the resource.', 'woocommerce' ),  
  40. 'required' => true,  
  41. ),  
  42. ) ),  
  43. ),  
  44. 'schema' => array( $this, 'get_public_item_schema' ),  
  45. )); 
  46.  
  47. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 
  48. 'args' => array( 
  49. 'id' => array( 
  50. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  51. 'type' => 'integer',  
  52. ),  
  53. 'attribute_id' => array( 
  54. 'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),  
  55. 'type' => 'integer',  
  56. ),  
  57. ),  
  58. array( 
  59. 'methods' => WP_REST_Server::READABLE,  
  60. 'callback' => array( $this, 'get_item' ),  
  61. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  62. 'args' => array( 
  63. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  64. ),  
  65. ),  
  66. array( 
  67. 'methods' => WP_REST_Server::EDITABLE,  
  68. 'callback' => array( $this, 'update_item' ),  
  69. 'permission_callback' => array( $this, 'update_item_permissions_check' ),  
  70. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  71. ),  
  72. array( 
  73. 'methods' => WP_REST_Server::DELETABLE,  
  74. 'callback' => array( $this, 'delete_item' ),  
  75. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),  
  76. 'args' => array( 
  77. 'force' => array( 
  78. 'default' => false,  
  79. 'type' => 'boolean',  
  80. 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),  
  81. ),  
  82. ),  
  83. ),  
  84. 'schema' => array( $this, 'get_public_item_schema' ),  
  85. ) ); 
  86.  
  87. register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( 
  88. 'args' => array( 
  89. 'attribute_id' => array( 
  90. 'description' => __( 'Unique identifier for the attribute of the terms.', 'woocommerce' ),  
  91. 'type' => 'integer',  
  92. ),  
  93. ),  
  94. array( 
  95. 'methods' => WP_REST_Server::EDITABLE,  
  96. 'callback' => array( $this, 'batch_items' ),  
  97. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),  
  98. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  99. ),  
  100. 'schema' => array( $this, 'get_public_batch_schema' ),  
  101. ) ); 
  102.  
  103. /** 
  104. * Prepare a single product attribute term output for response. 
  105. * @param WP_Term $item Term object. 
  106. * @param WP_REST_Request $request 
  107. * @return WP_REST_Response $response 
  108. */ 
  109. public function prepare_item_for_response( $item, $request ) { 
  110. // Get term order. 
  111. $menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy ); 
  112.  
  113. $data = array( 
  114. 'id' => (int) $item->term_id,  
  115. 'name' => $item->name,  
  116. 'slug' => $item->slug,  
  117. 'description' => $item->description,  
  118. 'menu_order' => (int) $menu_order,  
  119. 'count' => (int) $item->count,  
  120. ); 
  121.  
  122. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  123. $data = $this->add_additional_fields_to_object( $data, $request ); 
  124. $data = $this->filter_response_by_context( $data, $context ); 
  125.  
  126. $response = rest_ensure_response( $data ); 
  127.  
  128. $response->add_links( $this->prepare_links( $item, $request ) ); 
  129.  
  130. /** 
  131. * Filter a term item returned from the API. 
  132. * Allows modification of the term data right before it is returned. 
  133. * @param WP_REST_Response $response The response object. 
  134. * @param object $item The original term object. 
  135. * @param WP_REST_Request $request Request used to generate the response. 
  136. */ 
  137. return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); 
  138.  
  139. /** 
  140. * Update term meta fields. 
  141. * @param WP_Term $term 
  142. * @param WP_REST_Request $request 
  143. * @return bool|WP_Error 
  144. */ 
  145. protected function update_term_meta_fields( $term, $request ) { 
  146. $id = (int) $term->term_id; 
  147.  
  148. update_woocommerce_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] ); 
  149.  
  150. return true; 
  151.  
  152. /** 
  153. * Get the Attribute Term's schema, conforming to JSON Schema. 
  154. * @return array 
  155. */ 
  156. public function get_item_schema() { 
  157. $schema = array( 
  158. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  159. 'title' => 'product_attribute_term',  
  160. 'type' => 'object',  
  161. 'properties' => array( 
  162. 'id' => array( 
  163. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  164. 'type' => 'integer',  
  165. 'context' => array( 'view', 'edit' ),  
  166. 'readonly' => true,  
  167. ),  
  168. 'name' => array( 
  169. 'description' => __( 'Term name.', 'woocommerce' ),  
  170. 'type' => 'string',  
  171. 'context' => array( 'view', 'edit' ),  
  172. 'arg_options' => array( 
  173. 'sanitize_callback' => 'sanitize_text_field',  
  174. ),  
  175. ),  
  176. 'slug' => array( 
  177. 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),  
  178. 'type' => 'string',  
  179. 'context' => array( 'view', 'edit' ),  
  180. 'arg_options' => array( 
  181. 'sanitize_callback' => 'sanitize_title',  
  182. ),  
  183. ),  
  184. 'description' => array( 
  185. 'description' => __( 'HTML description of the resource.', 'woocommerce' ),  
  186. 'type' => 'string',  
  187. 'context' => array( 'view', 'edit' ),  
  188. 'arg_options' => array( 
  189. 'sanitize_callback' => 'wp_filter_post_kses',  
  190. ),  
  191. ),  
  192. 'menu_order' => array( 
  193. 'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),  
  194. 'type' => 'integer',  
  195. 'context' => array( 'view', 'edit' ),  
  196. ),  
  197. 'count' => array( 
  198. 'description' => __( 'Number of published products for the resource.', 'woocommerce' ),  
  199. 'type' => 'integer',  
  200. 'context' => array( 'view', 'edit' ),  
  201. 'readonly' => true,  
  202. ),  
  203. ),  
  204. ); 
  205.  
  206. return $this->add_additional_fields_schema( $schema );