class-wc-rest-product-attribute-terms-controller.php 7.6 KB

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