class-wc-rest-product-shipping-classes-controller.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * REST API Product Shipping Classes controller
  4. *
  5. * Handles requests to the products/shipping_classes 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 Shipping Classes controller class.
  17. *
  18. * @package WooCommerce/API
  19. * @extends WC_REST_Terms_Controller
  20. */
  21. class WC_REST_Product_Shipping_Classes_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/shipping_classes';
  34. /**
  35. * Taxonomy.
  36. *
  37. * @var string
  38. */
  39. protected $taxonomy = 'product_shipping_class';
  40. /**
  41. * Prepare a single product shipping class output for response.
  42. *
  43. * @param obj $item Term object.
  44. * @param WP_REST_Request $request
  45. * @return WP_REST_Response $response
  46. */
  47. public function prepare_item_for_response( $item, $request ) {
  48. $data = array(
  49. 'id' => (int) $item->term_id,
  50. 'name' => $item->name,
  51. 'slug' => $item->slug,
  52. 'description' => $item->description,
  53. 'count' => (int) $item->count,
  54. );
  55. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  56. $data = $this->add_additional_fields_to_object( $data, $request );
  57. $data = $this->filter_response_by_context( $data, $context );
  58. $response = rest_ensure_response( $data );
  59. $response->add_links( $this->prepare_links( $item, $request ) );
  60. /**
  61. * Filter a term item returned from the API.
  62. *
  63. * Allows modification of the term data right before it is returned.
  64. *
  65. * @param WP_REST_Response $response The response object.
  66. * @param object $item The original term object.
  67. * @param WP_REST_Request $request Request used to generate the response.
  68. */
  69. return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
  70. }
  71. /**
  72. * Get the Shipping Class schema, conforming to JSON Schema.
  73. *
  74. * @return array
  75. */
  76. public function get_item_schema() {
  77. $schema = array(
  78. '$schema' => 'http://json-schema.org/draft-04/schema#',
  79. 'title' => $this->taxonomy,
  80. 'type' => 'object',
  81. 'properties' => array(
  82. 'id' => array(
  83. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  84. 'type' => 'integer',
  85. 'context' => array( 'view', 'edit' ),
  86. 'readonly' => true,
  87. ),
  88. 'name' => array(
  89. 'description' => __( 'Shipping class name.', 'woocommerce' ),
  90. 'type' => 'string',
  91. 'context' => array( 'view', 'edit' ),
  92. 'arg_options' => array(
  93. 'sanitize_callback' => 'sanitize_text_field',
  94. ),
  95. ),
  96. 'slug' => array(
  97. 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
  98. 'type' => 'string',
  99. 'context' => array( 'view', 'edit' ),
  100. 'arg_options' => array(
  101. 'sanitize_callback' => 'sanitize_title',
  102. ),
  103. ),
  104. 'description' => array(
  105. 'description' => __( 'HTML description of the resource.', 'woocommerce' ),
  106. 'type' => 'string',
  107. 'context' => array( 'view', 'edit' ),
  108. 'arg_options' => array(
  109. 'sanitize_callback' => 'wp_filter_post_kses',
  110. ),
  111. ),
  112. 'count' => array(
  113. 'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
  114. 'type' => 'integer',
  115. 'context' => array( 'view', 'edit' ),
  116. 'readonly' => true,
  117. ),
  118. ),
  119. );
  120. return $this->add_additional_fields_schema( $schema );
  121. }
  122. }