WC_REST_Product_Tags_V1_Controller

REST API Product Tags controller class.

Defined (1)

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

/includes/api/v1/class-wc-rest-product-tags-controller.php  
  1. class WC_REST_Product_Tags_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/tags'; 
  14.  
  15. /** 
  16. * Taxonomy. 
  17. * @var string 
  18. */ 
  19. protected $taxonomy = 'product_tag'; 
  20.  
  21. /** 
  22. * Prepare a single product tag output for response. 
  23. * @param obj $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. $data = array( 
  29. 'id' => (int) $item->term_id,  
  30. 'name' => $item->name,  
  31. 'slug' => $item->slug,  
  32. 'description' => $item->description,  
  33. 'count' => (int) $item->count,  
  34. ); 
  35.  
  36. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  37. $data = $this->add_additional_fields_to_object( $data, $request ); 
  38. $data = $this->filter_response_by_context( $data, $context ); 
  39.  
  40. $response = rest_ensure_response( $data ); 
  41.  
  42. $response->add_links( $this->prepare_links( $item, $request ) ); 
  43.  
  44. /** 
  45. * Filter a term item returned from the API. 
  46. * Allows modification of the term data right before it is returned. 
  47. * @param WP_REST_Response $response The response object. 
  48. * @param object $item The original term object. 
  49. * @param WP_REST_Request $request Request used to generate the response. 
  50. */ 
  51. return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); 
  52.  
  53. /** 
  54. * Get the Tag's schema, conforming to JSON Schema. 
  55. * @return array 
  56. */ 
  57. public function get_item_schema() { 
  58. $schema = array( 
  59. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  60. 'title' => $this->taxonomy,  
  61. 'type' => 'object',  
  62. 'properties' => array( 
  63. 'id' => array( 
  64. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  65. 'type' => 'integer',  
  66. 'context' => array( 'view', 'edit' ),  
  67. 'readonly' => true,  
  68. ),  
  69. 'name' => array( 
  70. 'description' => __( 'Tag name.', 'woocommerce' ),  
  71. 'type' => 'string',  
  72. 'context' => array( 'view', 'edit' ),  
  73. 'arg_options' => array( 
  74. 'sanitize_callback' => 'sanitize_text_field',  
  75. ),  
  76. ),  
  77. 'slug' => array( 
  78. 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),  
  79. 'type' => 'string',  
  80. 'context' => array( 'view', 'edit' ),  
  81. 'arg_options' => array( 
  82. 'sanitize_callback' => 'sanitize_title',  
  83. ),  
  84. ),  
  85. 'description' => array( 
  86. 'description' => __( 'HTML description of the resource.', 'woocommerce' ),  
  87. 'type' => 'string',  
  88. 'context' => array( 'view', 'edit' ),  
  89. 'arg_options' => array( 
  90. 'sanitize_callback' => 'wp_filter_post_kses',  
  91. ),  
  92. ),  
  93. 'count' => array( 
  94. 'description' => __( 'Number of published products for the resource.', 'woocommerce' ),  
  95. 'type' => 'integer',  
  96. 'context' => array( 'view', 'edit' ),  
  97. 'readonly' => true,  
  98. ),  
  99. ),  
  100. ); 
  101.  
  102. return $this->add_additional_fields_schema( $schema );