PluginProbe
WooCommerce / 11.0.0
WooCommerce v11.0.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / rest-api / Controllers / Version2 / class-wc-rest-shipping-methods-v2-controller.php
class-wc-rest-shipping-methods-v2-controller.php
237 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API WC Shipping Methods controller
4 *
5 * Handles requests to the /shipping_methods endpoint.
6 *
7 * @package WooCommerce\RestApi
8 * @since 3.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Shipping methods controller class.
15 *
16 * @package WooCommerce\RestApi
17 * @extends WC_REST_Controller
18 */
19 class WC_REST_Shipping_Methods_V2_Controller extends WC_REST_Controller {
20
21 /**
22 * Endpoint namespace.
23 *
24 * @var string
25 */
26 protected $namespace = 'wc/v2';
27
28 /**
29 * Route base.
30 *
31 * @var string
32 */
33 protected $rest_base = 'shipping_methods';
34
35 /**
36 * Register the route for /shipping_methods and /shipping_methods/<method>
37 */
38 public function register_routes() {
39 register_rest_route(
40 $this->namespace, '/' . $this->rest_base, array(
41 array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get_items' ),
44 'permission_callback' => array( $this, 'get_items_permissions_check' ),
45 'args' => $this->get_collection_params(),
46 ),
47 'schema' => array( $this, 'get_public_item_schema' ),
48 )
49 );
50 register_rest_route(
51 $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w-]+)', array(
52 'args' => array(
53 'id' => array(
54 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
55 'type' => 'string',
56 ),
57 ),
58 array(
59 'methods' => WP_REST_Server::READABLE,
60 'callback' => array( $this, 'get_item' ),
61 'permission_callback' => array( $this, 'get_item_permissions_check' ),
62 'args' => array(
63 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
64 ),
65 ),
66 'schema' => array( $this, 'get_public_item_schema' ),
67 )
68 );
69 }
70
71 /**
72 * Check whether a given request has permission to view shipping methods.
73 *
74 * @param WP_REST_Request $request Full details about the request.
75 * @return WP_Error|boolean
76 */
77 public function get_items_permissions_check( $request ) {
78 if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
79 return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
80 }
81 return true;
82 }
83
84 /**
85 * Check if a given request has access to read a shipping method.
86 *
87 * @param WP_REST_Request $request Full details about the request.
88 * @return WP_Error|boolean
89 */
90 public function get_item_permissions_check( $request ) {
91 if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
92 return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
93 }
94 return true;
95 }
96
97 /**
98 * Get shipping methods.
99 *
100 * @param WP_REST_Request $request Full details about the request.
101 * @return WP_Error|WP_REST_Response
102 */
103 public function get_items( $request ) {
104 $wc_shipping = WC_Shipping::instance();
105 $data = array();
106 foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) {
107 $method = $this->prepare_item_for_response( $shipping_method, $request );
108 $method = $this->prepare_response_for_collection( $method );
109 $data[] = $method;
110 }
111
112 $total = count( $data );
113 $response = rest_ensure_response( $data );
114 $response->header( 'X-WP-Total', (int) $total );
115 $response->header( 'X-WP-TotalPages', $total ? 1 : 0 );
116 return $response;
117 }
118
119 /**
120 * Get a single Shipping Method.
121 *
122 * @param WP_REST_Request $request Request data.
123 * @return WP_REST_Response|WP_Error
124 */
125 public function get_item( $request ) {
126 $wc_shipping = WC_Shipping::instance();
127 $methods = $wc_shipping->get_shipping_methods();
128 if ( empty( $methods[ $request['id'] ] ) ) {
129 return new WP_Error( 'woocommerce_rest_shipping_method_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
130 }
131
132 $method = $methods[ $request['id'] ];
133 $response = $this->prepare_item_for_response( $method, $request );
134
135 return rest_ensure_response( $response );
136 }
137
138 /**
139 * Prepare a shipping method for response.
140 *
141 * @param WC_Shipping_Method $method Shipping method object.
142 * @param WP_REST_Request $request Request object.
143 * @return WP_REST_Response $response Response data.
144 */
145 public function prepare_item_for_response( $method, $request ) {
146 $data = array(
147 'id' => $method->id,
148 'title' => $method->method_title,
149 'description' => $method->method_description,
150 );
151
152 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
153 $data = $this->add_additional_fields_to_object( $data, $request );
154 $data = $this->filter_response_by_context( $data, $context );
155
156 // Wrap the data in a response object.
157 $response = rest_ensure_response( $data );
158
159 $response->add_links( $this->prepare_links( $method, $request ) );
160
161 /**
162 * Filter shipping methods object returned from the REST API.
163 *
164 * @param WP_REST_Response $response The response object.
165 * @param WC_Shipping_Method $method Shipping method object used to create response.
166 * @param WP_REST_Request $request Request object.
167 */
168 return apply_filters( 'woocommerce_rest_prepare_shipping_method', $response, $method, $request );
169 }
170
171 /**
172 * Prepare links for the request.
173 *
174 * @param WC_Shipping_Method $method Shipping method object.
175 * @param WP_REST_Request $request Request object.
176 * @return array
177 */
178 protected function prepare_links( $method, $request ) {
179 $links = array(
180 'self' => array(
181 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $method->id ) ),
182 ),
183 'collection' => array(
184 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
185 ),
186 );
187
188 return $links;
189 }
190
191 /**
192 * Get the shipping method schema, conforming to JSON Schema.
193 *
194 * @return array
195 */
196 public function get_item_schema() {
197 $schema = array(
198 '$schema' => 'http://json-schema.org/draft-04/schema#',
199 'title' => 'shipping_method',
200 'type' => 'object',
201 'properties' => array(
202 'id' => array(
203 'description' => __( 'Method ID.', 'woocommerce' ),
204 'type' => 'string',
205 'context' => array( 'view' ),
206 'readonly' => true,
207 ),
208 'title' => array(
209 'description' => __( 'Shipping method title.', 'woocommerce' ),
210 'type' => 'string',
211 'context' => array( 'view' ),
212 'readonly' => true,
213 ),
214 'description' => array(
215 'description' => __( 'Shipping method description.', 'woocommerce' ),
216 'type' => 'string',
217 'context' => array( 'view' ),
218 'readonly' => true,
219 ),
220 ),
221 );
222
223 return $this->add_additional_fields_schema( $schema );
224 }
225
226 /**
227 * Get any query params needed.
228 *
229 * @return array
230 */
231 public function get_collection_params() {
232 return array(
233 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
234 );
235 }
236 }
237