Completed
Pull Request — master (#11176)
by
unknown
18:43
created

WC_REST_Shipping_Zones_Controller::create_item()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 4
eloc 12
c 2
b 0
f 2
nc 8
nop 1
dl 0
loc 20
rs 9.2
1
<?php
2
/**
3
 * REST API Shipping Zones controller
4
 *
5
 * Handles requests to the /shipping/zones endpoint.
6
 *
7
 * @author   WooThemes
8
 * @category API
9
 * @package  WooCommerce/API
10
 * @since    2.7.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * REST API Shipping Zones class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Shipping_Zones_Controller_Base
22
 */
23
class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controller_Base {
24
25
	/**
26
	 * Register the routes for Shipping Zones.
27
	 */
28
	public function register_routes() {
29
		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
30
			array(
31
				'methods'             => WP_REST_Server::READABLE,
32
				'callback'            => array( $this, 'get_items' ),
33
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
34
			),
35
			array(
36
				'methods'             => WP_REST_Server::CREATABLE,
37
				'callback'            => array( $this, 'create_item' ),
38
				'permission_callback' => array( $this, 'update_items_permissions_check' ),
39
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
40
			),
41
			'schema' => array( $this, 'get_public_item_schema' ),
42
		) );
43
44
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d-]+)', array(
45
			array(
46
				'methods'             => WP_REST_Server::READABLE,
47
				'callback'            => array( $this, 'get_item' ),
48
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
49
			),
50
			array(
51
				'methods'             => WP_REST_Server::EDITABLE,
52
				'callback'            => array( $this, 'update_item' ),
53
				'permission_callback' => array( $this, 'update_items_permissions_check' ),
54
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
55
			),
56
			'schema' => array( $this, 'get_public_item_schema' ),
57
		) );
58
	}
59
60
	/**
61
	 * Get a single Shipping Zone.
62
	 *
63
	 * @param WP_REST_Request $request
64
	 * @return WP_REST_Response
65
	 */
66
	public function get_item( $request ) {
67
		$zone = $this->get_zone( $request->get_param( 'id' ) );
68
69
		if ( is_wp_error( $zone ) ) {
70
			return $zone;
71
		}
72
73
		$data = $zone->get_data();
74
		$data = $this->prepare_item_for_response( $data, $request );
75
		$data = $this->prepare_response_for_collection( $data );
76
77
		return rest_ensure_response( $data );
78
	}
79
80
	/**
81
	 * Get all Shipping Zones.
82
	 *
83
	 * @param WP_REST_Request $request
84
	 * @return WP_REST_Response
85
	 */
86
	public function get_items( $request ) {
87
		$rest_of_the_world = WC_Shipping_Zones::get_zone_by( 'zone_id', 0 );
88
89
		$zones = WC_Shipping_Zones::get_zones();
90
		array_unshift( $zones, $rest_of_the_world->get_data() );
91
		$data  = array();
92
93
		foreach ( $zones as $zone_obj ) {
94
			$zone   = $this->prepare_item_for_response( $zone_obj, $request );
95
			$zone   = $this->prepare_response_for_collection( $zone );
96
			$data[] = $zone;
97
		}
98
99
		return rest_ensure_response( $data );
100
	}
101
102
	/**
103
	 * Create a single Shipping Zone.
104
	 *
105
	 * @param WP_REST_Request $request Full details about the request.
106
	 * @return WP_REST_Request|WP_Error
107
	 */
108
	public function create_item( $request ) {
109
		$zone = new WC_Shipping_Zone( null );
110
111
		if ( ! is_null( $request->get_param( 'name' ) ) ) {
112
			$zone->set_zone_name( $request->get_param( 'name' ) );
113
		}
114
115
		if ( ! is_null( $request->get_param( 'order' ) ) ) {
116
			$zone->set_zone_order( $request->get_param( 'order' ) );
117
		}
118
119
		$zone->create();
120
121
		if ( $zone->get_id() !== 0 ) {
122
			$request->set_param( 'id', $zone->get_id() );
123
			return $this->get_item( $request );
124
		} else {
125
			return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( "Resource cannot be created. Check to make sure 'order' and 'name' are present.", 'woocommerce' ), array( 'status' => 500 ) );
126
		}
127
	}
128
129
	/**
130
	 * Update a single Shipping Zone.
131
	 *
132
	 * @param WP_REST_Request $request Full details about the request.
133
	 * @return WP_REST_Request|WP_Error
134
	 */
135
	public function update_item( $request ) {
136
		$zone = $this->get_zone( $request->get_param( 'id' ) );
137
138
		if ( is_wp_error( $zone ) ) {
139
			return $zone;
140
		}
141
142
		$zone_changed = false;
143
144
		if ( ! is_null( $request->get_param( 'name' ) ) ) {
145
			$zone->set_zone_name( $request->get_param( 'name' ) );
146
			$zone_changed = true;
147
		}
148
149
		if ( ! is_null( $request->get_param( 'order' ) ) ) {
150
			$zone->set_zone_order( $request->get_param( 'order' ) );
151
			$zone_changed = true;
152
		}
153
154
		if ( $zone_changed ) {
155
			$zone->save();
156
		}
157
158
		return $this->get_item( $request );
159
	}
160
161
	/**
162
	 * Prepare the Shipping Zone for the REST response.
163
	 *
164
	 * @param array $item Shipping Zone.
165
	 * @param WP_REST_Request $request Request object.
166
	 * @return WP_REST_Response $response
167
	 */
168
	public function prepare_item_for_response( $item, $request ) {
169
		$data = array(
170
			'id'    => (int) $item['zone_id'],
171
			'name'  => $item['zone_name'],
172
			'order' => (int) $item['zone_order'],
173
		);
174
175
		$context = empty( $request['context'] ) ? 'view' : $request['context'];
176
		$data    = $this->add_additional_fields_to_object( $data, $request );
177
		$data    = $this->filter_response_by_context( $data, $context );
178
179
		// Wrap the data in a response object.
180
		$response = rest_ensure_response( $data );
181
182
		$response->add_links( $this->prepare_links( $data['id'] ) );
183
184
		return $response;
185
	}
186
187
	/**
188
	 * Prepare links for the request.
189
	 *
190
	 * @param int $zone_id Given Shipping Zone ID.
191
	 * @return array Links for the given Shipping Zone.
192
	 */
193
	protected function prepare_links( $zone_id ) {
194
		$base  = '/' . $this->namespace . '/' . $this->rest_base;
195
		$links = array(
196
			'self'       => array(
197
				'href' => rest_url( trailingslashit( $base ) . $zone_id ),
198
			),
199
			'collection' => array(
200
				'href' => rest_url( $base ),
201
			),
202
			'describedby' => array(
203
				'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ),
204
			),
205
		);
206
207
		return $links;
208
	}
209
210
	/**
211
	 * Get the Shipping Zones schema, conforming to JSON Schema
212
	 *
213
	 * @return array
214
	 */
215
	public function get_item_schema() {
216
		$schema = array(
217
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
218
			'title'      => 'shipping_zone',
219
			'type'       => 'object',
220
			'properties' => array(
221
				'id' => array(
222
					'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
223
					'type'        => 'integer',
224
					'context'     => array( 'view', 'edit' ),
225
					'readonly'    => true,
226
				),
227
				'name' => array(
228
					'description' => __( 'Shipping zone name.', 'woocommerce' ),
229
					'type'        => 'string',
230
					'context'     => array( 'view', 'edit' ),
231
					'required'    => true,
232
					'arg_options' => array(
233
						'sanitize_callback' => 'sanitize_text_field',
234
					),
235
				),
236
				'order' => array(
237
					'description' => __( 'Shipping zone order.', 'woocommerce' ),
238
					'type'        => 'integer',
239
					'context'     => array( 'view', 'edit' ),
240
					'required'    => false,
241
					'arg_options' => array(
242
						'sanitize_callback' => 'absint',
243
					),
244
				),
245
			),
246
		);
247
248
		return $this->add_additional_fields_schema( $schema );
249
	}
250
}
251