woocommerce
/
includes
/
rest-api
/
Controllers
/
Version1
/
class-wc-rest-product-shipping-classes-v1-controller.php
class-wc-rest-product-shipping-classes-v1-controller.php
| 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\RestApi |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * REST API Product Shipping Classes controller class. |
| 19 | * |
| 20 | * @package WooCommerce\RestApi |
| 21 | * @extends WC_REST_Terms_Controller |
| 22 | */ |
| 23 | class WC_REST_Product_Shipping_Classes_V1_Controller extends WC_REST_Terms_Controller { |
| 24 | |
| 25 | /** |
| 26 | * Endpoint namespace. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $namespace = 'wc/v1'; |
| 31 | |
| 32 | /** |
| 33 | * Route base. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $rest_base = 'products/shipping_classes'; |
| 38 | |
| 39 | /** |
| 40 | * Taxonomy. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $taxonomy = 'product_shipping_class'; |
| 45 | |
| 46 | /** |
| 47 | * Prepare a single product shipping class output for response. |
| 48 | * |
| 49 | * @param obj $item Term object. |
| 50 | * @param WP_REST_Request $request |
| 51 | * @return WP_REST_Response $response |
| 52 | */ |
| 53 | public function prepare_item_for_response( $item, $request ) { |
| 54 | $data = array( |
| 55 | 'id' => (int) $item->term_id, |
| 56 | 'name' => $item->name, |
| 57 | 'slug' => $item->slug, |
| 58 | 'description' => $item->description, |
| 59 | 'count' => (int) $item->count, |
| 60 | ); |
| 61 | |
| 62 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 63 | $data = $this->add_additional_fields_to_object( $data, $request ); |
| 64 | $data = $this->filter_response_by_context( $data, $context ); |
| 65 | |
| 66 | $response = rest_ensure_response( $data ); |
| 67 | |
| 68 | $response->add_links( $this->prepare_links( $item, $request ) ); |
| 69 | |
| 70 | /** |
| 71 | * Filter a term item returned from the API. |
| 72 | * |
| 73 | * Allows modification of the term data right before it is returned. |
| 74 | * |
| 75 | * @param WP_REST_Response $response The response object. |
| 76 | * @param object $item The original term object. |
| 77 | * @param WP_REST_Request $request Request used to generate the response. |
| 78 | */ |
| 79 | return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the Shipping Class schema, conforming to JSON Schema. |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | public function get_item_schema() { |
| 88 | $schema = array( |
| 89 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 90 | 'title' => $this->taxonomy, |
| 91 | 'type' => 'object', |
| 92 | 'properties' => array( |
| 93 | 'id' => array( |
| 94 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
| 95 | 'type' => 'integer', |
| 96 | 'context' => array( 'view', 'edit' ), |
| 97 | 'readonly' => true, |
| 98 | ), |
| 99 | 'name' => array( |
| 100 | 'description' => __( 'Shipping class name.', 'woocommerce' ), |
| 101 | 'type' => 'string', |
| 102 | 'context' => array( 'view', 'edit' ), |
| 103 | 'arg_options' => array( |
| 104 | 'sanitize_callback' => 'sanitize_text_field', |
| 105 | ), |
| 106 | ), |
| 107 | 'slug' => array( |
| 108 | 'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ), |
| 109 | 'type' => 'string', |
| 110 | 'context' => array( 'view', 'edit' ), |
| 111 | 'arg_options' => array( |
| 112 | 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
| 113 | ), |
| 114 | ), |
| 115 | 'description' => array( |
| 116 | 'description' => __( 'HTML description of the resource.', 'woocommerce' ), |
| 117 | 'type' => 'string', |
| 118 | 'context' => array( 'view', 'edit' ), |
| 119 | 'arg_options' => array( |
| 120 | 'sanitize_callback' => 'wp_filter_post_kses', |
| 121 | ), |
| 122 | ), |
| 123 | 'count' => array( |
| 124 | 'description' => __( 'Number of published products for the resource.', 'woocommerce' ), |
| 125 | 'type' => 'integer', |
| 126 | 'context' => array( 'view', 'edit' ), |
| 127 | 'readonly' => true, |
| 128 | ), |
| 129 | ), |
| 130 | ); |
| 131 | |
| 132 | return $this->add_additional_fields_schema( $schema ); |
| 133 | } |
| 134 | } |
| 135 |