WC_REST_Product_Categories_Controller

REST API Product Categories controller class.

Defined (1)

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

/includes/api/class-wc-rest-product-categories-controller.php  
  1. class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V1_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Prepare a single product category output for response. 
  11. * @param WP_Term $item Term object. 
  12. * @param WP_REST_Request $request 
  13. * @return WP_REST_Response $response 
  14. */ 
  15. public function prepare_item_for_response( $item, $request ) { 
  16. // Get category display type. 
  17. $display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' ); 
  18.  
  19. // Get category order. 
  20. $menu_order = get_woocommerce_term_meta( $item->term_id, 'order' ); 
  21.  
  22. $data = array( 
  23. 'id' => (int) $item->term_id,  
  24. 'name' => $item->name,  
  25. 'slug' => $item->slug,  
  26. 'parent' => (int) $item->parent,  
  27. 'description' => $item->description,  
  28. 'display' => $display_type ? $display_type : 'default',  
  29. 'image' => array(),  
  30. 'menu_order' => (int) $menu_order,  
  31. 'count' => (int) $item->count,  
  32. ); 
  33.  
  34. // Get category image. 
  35. if ( $image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' ) ) { 
  36. $attachment = get_post( $image_id ); 
  37.  
  38. $data['image'] = array( 
  39. 'id' => (int) $image_id,  
  40. 'date_created' => wc_rest_prepare_date_response( $attachment->post_date ),  
  41. 'date_created_gmt' => wc_rest_prepare_date_response( $attachment->post_date_gmt ),  
  42. 'date_modified' => wc_rest_prepare_date_response( $attachment->post_modified ),  
  43. 'date_modified_gmt' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),  
  44. 'src' => wp_get_attachment_url( $image_id ),  
  45. 'title' => get_the_title( $attachment ),  
  46. 'alt' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),  
  47. ); 
  48.  
  49. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  50. $data = $this->add_additional_fields_to_object( $data, $request ); 
  51. $data = $this->filter_response_by_context( $data, $context ); 
  52.  
  53. $response = rest_ensure_response( $data ); 
  54.  
  55. $response->add_links( $this->prepare_links( $item, $request ) ); 
  56.  
  57. /** 
  58. * Filter a term item returned from the API. 
  59. * Allows modification of the term data right before it is returned. 
  60. * @param WP_REST_Response $response The response object. 
  61. * @param object $item The original term object. 
  62. * @param WP_REST_Request $request Request used to generate the response. 
  63. */ 
  64. return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); 
  65.  
  66. /** 
  67. * Get the Category schema, conforming to JSON Schema. 
  68. * @return array 
  69. */ 
  70. public function get_item_schema() { 
  71. $schema = array( 
  72. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  73. 'title' => $this->taxonomy,  
  74. 'type' => 'object',  
  75. 'properties' => array( 
  76. 'id' => array( 
  77. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  78. 'type' => 'integer',  
  79. 'context' => array( 'view', 'edit' ),  
  80. 'readonly' => true,  
  81. ),  
  82. 'name' => array( 
  83. 'description' => __( 'Category name.', 'woocommerce' ),  
  84. 'type' => 'string',  
  85. 'context' => array( 'view', 'edit' ),  
  86. 'arg_options' => array( 
  87. 'sanitize_callback' => 'sanitize_text_field',  
  88. ),  
  89. ),  
  90. 'slug' => array( 
  91. 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),  
  92. 'type' => 'string',  
  93. 'context' => array( 'view', 'edit' ),  
  94. 'arg_options' => array( 
  95. 'sanitize_callback' => 'sanitize_title',  
  96. ),  
  97. ),  
  98. 'parent' => array( 
  99. 'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ),  
  100. 'type' => 'integer',  
  101. 'context' => array( 'view', 'edit' ),  
  102. ),  
  103. 'description' => array( 
  104. 'description' => __( 'HTML description of the resource.', 'woocommerce' ),  
  105. 'type' => 'string',  
  106. 'context' => array( 'view', 'edit' ),  
  107. 'arg_options' => array( 
  108. 'sanitize_callback' => 'wp_filter_post_kses',  
  109. ),  
  110. ),  
  111. 'display' => array( 
  112. 'description' => __( 'Category archive display type.', 'woocommerce' ),  
  113. 'type' => 'string',  
  114. 'default' => 'default',  
  115. 'enum' => array( 'default', 'products', 'subcategories', 'both' ),  
  116. 'context' => array( 'view', 'edit' ),  
  117. ),  
  118. 'image' => array( 
  119. 'description' => __( 'Image data.', 'woocommerce' ),  
  120. 'type' => 'object',  
  121. 'context' => array( 'view', 'edit' ),  
  122. 'properties' => array( 
  123. 'id' => array( 
  124. 'description' => __( 'Image ID.', 'woocommerce' ),  
  125. 'type' => 'integer',  
  126. 'context' => array( 'view', 'edit' ),  
  127. ),  
  128. 'date_created' => array( 
  129. 'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),  
  130. 'type' => 'date-time',  
  131. 'context' => array( 'view', 'edit' ),  
  132. 'readonly' => true,  
  133. ),  
  134. 'date_created_gmt' => array( 
  135. 'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),  
  136. 'type' => 'date-time',  
  137. 'context' => array( 'view', 'edit' ),  
  138. 'readonly' => true,  
  139. ),  
  140. 'date_modified' => array( 
  141. 'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),  
  142. 'type' => 'date-time',  
  143. 'context' => array( 'view', 'edit' ),  
  144. 'readonly' => true,  
  145. ),  
  146. 'date_modified_gmt' => array( 
  147. 'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),  
  148. 'type' => 'date-time',  
  149. 'context' => array( 'view', 'edit' ),  
  150. 'readonly' => true,  
  151. ),  
  152. 'src' => array( 
  153. 'description' => __( 'Image URL.', 'woocommerce' ),  
  154. 'type' => 'string',  
  155. 'format' => 'uri',  
  156. 'context' => array( 'view', 'edit' ),  
  157. ),  
  158. 'name' => array( 
  159. 'description' => __( 'Image name.', 'woocommerce' ),  
  160. 'type' => 'string',  
  161. 'context' => array( 'view', 'edit' ),  
  162. ),  
  163. 'alt' => array( 
  164. 'description' => __( 'Image alternative text.', 'woocommerce' ),  
  165. 'type' => 'string',  
  166. 'context' => array( 'view', 'edit' ),  
  167. ),  
  168. ),  
  169. ),  
  170. 'menu_order' => array( 
  171. 'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),  
  172. 'type' => 'integer',  
  173. 'context' => array( 'view', 'edit' ),  
  174. ),  
  175. 'count' => array( 
  176. 'description' => __( 'Number of published products for the resource.', 'woocommerce' ),  
  177. 'type' => 'integer',  
  178. 'context' => array( 'view', 'edit' ),  
  179. 'readonly' => true,  
  180. ),  
  181. ),  
  182. ); 
  183.  
  184. return $this->add_additional_fields_schema( $schema );