WC_REST_Product_Categories_V1_Controller

REST API Product Categories controller class.

Defined (1)

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

/includes/api/v1/class-wc-rest-product-categories-controller.php  
  1. class WC_REST_Product_Categories_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/categories'; 
  14.  
  15. /** 
  16. * Taxonomy. 
  17. * @var string 
  18. */ 
  19. protected $taxonomy = 'product_cat'; 
  20.  
  21. /** 
  22. * Prepare a single product category output for response. 
  23. * @param WP_Term $item Term object. 
  24. * @param WP_REST_Request $request 
  25. * @return WP_REST_Response $response 
  26. */ 
  27. public function prepare_item_for_response( $item, $request ) { 
  28. // Get category display type. 
  29. $display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' ); 
  30.  
  31. // Get category order. 
  32. $menu_order = get_woocommerce_term_meta( $item->term_id, 'order' ); 
  33.  
  34. $data = array( 
  35. 'id' => (int) $item->term_id,  
  36. 'name' => $item->name,  
  37. 'slug' => $item->slug,  
  38. 'parent' => (int) $item->parent,  
  39. 'description' => $item->description,  
  40. 'display' => $display_type ? $display_type : 'default',  
  41. 'image' => array(),  
  42. 'menu_order' => (int) $menu_order,  
  43. 'count' => (int) $item->count,  
  44. ); 
  45.  
  46. // Get category image. 
  47. if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) { 
  48. $attachment = get_post( $image_id ); 
  49.  
  50. $data['image'] = array( 
  51. 'id' => (int) $image_id,  
  52. 'date_created' => wc_rest_prepare_date_response( $attachment->post_date_gmt ),  
  53. 'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),  
  54. 'src' => wp_get_attachment_url( $image_id ),  
  55. 'title' => get_the_title( $attachment ),  
  56. 'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),  
  57. ); 
  58.  
  59. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  60. $data = $this->add_additional_fields_to_object( $data, $request ); 
  61. $data = $this->filter_response_by_context( $data, $context ); 
  62.  
  63. $response = rest_ensure_response( $data ); 
  64.  
  65. $response->add_links( $this->prepare_links( $item, $request ) ); 
  66.  
  67. /** 
  68. * Filter a term item returned from the API. 
  69. * Allows modification of the term data right before it is returned. 
  70. * @param WP_REST_Response $response The response object. 
  71. * @param object $item The original term object. 
  72. * @param WP_REST_Request $request Request used to generate the response. 
  73. */ 
  74. return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); 
  75.  
  76. /** 
  77. * Update term meta fields. 
  78. * @param WP_Term $term 
  79. * @param WP_REST_Request $request 
  80. * @return bool|WP_Error 
  81. */ 
  82. protected function update_term_meta_fields( $term, $request ) { 
  83. $id = (int) $term->term_id; 
  84.  
  85. if ( isset( $request['display'] ) ) { 
  86. update_woocommerce_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] ); 
  87.  
  88. if ( isset( $request['menu_order'] ) ) { 
  89. update_woocommerce_term_meta( $id, 'order', $request['menu_order'] ); 
  90.  
  91. if ( isset( $request['image'] ) ) { 
  92. if ( empty( $request['image']['id'] ) && ! empty( $request['image']['src'] ) ) { 
  93. $upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image']['src'] ) ); 
  94.  
  95. if ( is_wp_error( $upload ) ) { 
  96. return $upload; 
  97.  
  98. $image_id = wc_rest_set_uploaded_image_as_attachment( $upload ); 
  99. } else { 
  100. $image_id = isset( $request['image']['id'] ) ? absint( $request['image']['id'] ) : 0; 
  101.  
  102. // Check if image_id is a valid image attachment before updating the term meta. 
  103. if ( $image_id && wp_attachment_is_image( $image_id ) ) { 
  104. update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id ); 
  105.  
  106. // Set the image alt. 
  107. if ( ! empty( $request['image']['alt'] ) ) { 
  108. update_post_meta( $image_id, '_wp_attachment_image_alt', wc_clean( $request['image']['alt'] ) ); 
  109.  
  110. // Set the image title. 
  111. if ( ! empty( $request['image']['title'] ) ) { 
  112. wp_update_post( array( 'ID' => $image_id, 'post_title' => wc_clean( $request['image']['title'] ) ) ); 
  113. } else { 
  114. delete_woocommerce_term_meta( $id, 'thumbnail_id' ); 
  115.  
  116. return true; 
  117.  
  118. /** 
  119. * Get the Category schema, conforming to JSON Schema. 
  120. * @return array 
  121. */ 
  122. public function get_item_schema() { 
  123. $schema = array( 
  124. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  125. 'title' => $this->taxonomy,  
  126. 'type' => 'object',  
  127. 'properties' => array( 
  128. 'id' => array( 
  129. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  130. 'type' => 'integer',  
  131. 'context' => array( 'view', 'edit' ),  
  132. 'readonly' => true,  
  133. ),  
  134. 'name' => array( 
  135. 'description' => __( 'Category name.', 'woocommerce' ),  
  136. 'type' => 'string',  
  137. 'context' => array( 'view', 'edit' ),  
  138. 'arg_options' => array( 
  139. 'sanitize_callback' => 'sanitize_text_field',  
  140. ),  
  141. ),  
  142. 'slug' => array( 
  143. 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),  
  144. 'type' => 'string',  
  145. 'context' => array( 'view', 'edit' ),  
  146. 'arg_options' => array( 
  147. 'sanitize_callback' => 'sanitize_title',  
  148. ),  
  149. ),  
  150. 'parent' => array( 
  151. 'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ),  
  152. 'type' => 'integer',  
  153. 'context' => array( 'view', 'edit' ),  
  154. ),  
  155. 'description' => array( 
  156. 'description' => __( 'HTML description of the resource.', 'woocommerce' ),  
  157. 'type' => 'string',  
  158. 'context' => array( 'view', 'edit' ),  
  159. 'arg_options' => array( 
  160. 'sanitize_callback' => 'wp_filter_post_kses',  
  161. ),  
  162. ),  
  163. 'display' => array( 
  164. 'description' => __( 'Category archive display type.', 'woocommerce' ),  
  165. 'type' => 'string',  
  166. 'default' => 'default',  
  167. 'enum' => array( 'default', 'products', 'subcategories', 'both' ),  
  168. 'context' => array( 'view', 'edit' ),  
  169. ),  
  170. 'image' => array( 
  171. 'description' => __( 'Image data.', 'woocommerce' ),  
  172. 'type' => 'object',  
  173. 'context' => array( 'view', 'edit' ),  
  174. 'properties' => array( 
  175. 'id' => array( 
  176. 'description' => __( 'Image ID.', 'woocommerce' ),  
  177. 'type' => 'integer',  
  178. 'context' => array( 'view', 'edit' ),  
  179. ),  
  180. 'date_created' => array( 
  181. 'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),  
  182. 'type' => 'date-time',  
  183. 'context' => array( 'view', 'edit' ),  
  184. 'readonly' => true,  
  185. ),  
  186. 'date_modified' => array( 
  187. 'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),  
  188. 'type' => 'date-time',  
  189. 'context' => array( 'view', 'edit' ),  
  190. 'readonly' => true,  
  191. ),  
  192. 'src' => array( 
  193. 'description' => __( 'Image URL.', 'woocommerce' ),  
  194. 'type' => 'string',  
  195. 'format' => 'uri',  
  196. 'context' => array( 'view', 'edit' ),  
  197. ),  
  198. 'name' => array( 
  199. 'description' => __( 'Image name.', 'woocommerce' ),  
  200. 'type' => 'string',  
  201. 'context' => array( 'view', 'edit' ),  
  202. ),  
  203. 'alt' => array( 
  204. 'description' => __( 'Image alternative text.', 'woocommerce' ),  
  205. 'type' => 'string',  
  206. 'context' => array( 'view', 'edit' ),  
  207. ),  
  208. ),  
  209. ),  
  210. 'menu_order' => array( 
  211. 'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),  
  212. 'type' => 'integer',  
  213. 'context' => array( 'view', 'edit' ),  
  214. ),  
  215. 'count' => array( 
  216. 'description' => __( 'Number of published products for the resource.', 'woocommerce' ),  
  217. 'type' => 'integer',  
  218. 'context' => array( 'view', 'edit' ),  
  219. 'readonly' => true,  
  220. ),  
  221. ),  
  222. ); 
  223.  
  224. return $this->add_additional_fields_schema( $schema );