Completed
Pull Request — master (#11176)
by Jeff
07:52
created

get_items()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
c 1
b 0
f 1
nc 3
nop 1
dl 18
loc 18
rs 9.4285
1
<?php
2
/**
3
 * REST API Shipping Zone Locations controller
4
 *
5
 * Handles requests to the /shipping/zones/<id>/locations 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 Zone Locations class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Shipping_Zones_Controller_Base
22
 */
23
class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller_Base {
24
25
	/**
26
	 * Register the routes for Shipping Zone Locations.
27
	 */
28
	public function register_routes() {
29
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d-]+)/locations', 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::EDITABLE,
37
				'callback'            => array( $this, 'update_items' ),
38
				'permission_callback' => array( $this, 'update_items_permissions_check' ),
39
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
40
			),
41
			'schema' => array( $this, 'get_public_item_schema' ),
42
		) );
43
	}
44
45
	/**
46
	 * Get all Shipping Zone Locations.
47
	 *
48
	 * @param WP_REST_Request $request
49
	 * @return WP_REST_Response|WP_Error
50
	 */
51 View Code Duplication
	public function get_items( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
		$zone = $this->get_zone( $request['id'] );
53
54
		if ( is_wp_error( $zone ) ) {
55
			return $zone;
56
		}
57
		
58
		$locations = $zone->get_zone_locations();
59
		$data      = array();
60
61
		foreach ( $locations as $location_obj ) {
62
			$location = $this->prepare_item_for_response( $location_obj, $request );
63
			$location = $this->prepare_response_for_collection( $location );
64
			$data[]   = $location;
65
		}
66
67
		return rest_ensure_response( $data );
68
	}
69
70
	/**
71
	 * Update all Shipping Zone Locations.
72
	 *
73
	 * @param WP_REST_Request $request
74
	 * @return WP_REST_Response|WP_Error
75
	 */
76
	public function update_items( $request ) {
77
		$zone = $this->get_zone( $request['id'] );
78
79
		if ( is_wp_error( $zone ) ) {
80
			return $zone;
81
		}
82
83
		$raw_locations = $request->get_json_params();
84
		$locations     = array();
85
86
		foreach ( (array) $raw_locations as $raw_location ) {
87
			if ( empty( $raw_location['code'] ) || empty( $raw_location['type'] ) ) {
88
				continue;
89
			}
90
			$locations[] = $raw_location;
91
		}
92
93
		$zone->set_locations( $locations );
94
		$zone->save();
95
96
		return $this->get_items( $request );
97
	}
98
99
	/**
100
	 * Prepare the Shipping Zone Location for the REST response.
101
	 *
102
	 * @param array $item Shipping Zone Location.
103
	 * @param WP_REST_Request $request Request object.
104
	 * @return WP_REST_Response $response
105
	 */
106
	public function prepare_item_for_response( $item, $request ) {
107
		$context = empty( $request['context'] ) ? 'view' : $request['context'];
108
		$data    = $this->add_additional_fields_to_object( $item, $request );
109
		$data    = $this->filter_response_by_context( $data, $context );
110
111
		// Wrap the data in a response object.
112
		$response = rest_ensure_response( $data );
113
114
		$response->add_links( $this->prepare_links( $request['id'] ) );
115
116
		return $response;
117
	}
118
119
	/**
120
	 * Prepare links for the request.
121
	 *
122
	 * @param int $zone_id Given Shipping Zone ID.
123
	 * @return array Links for the given Shipping Zone Location.
124
	 */
125
	protected function prepare_links( $zone_id ) {
126
		$base  = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
127
		$links = array(
128
			'collection' => array(
129
				'href' => rest_url( $base . '/locations' ),
130
			),
131
			'describes'  => array(
132
				'href' => rest_url( $base ),
133
			),
134
		);
135
136
		return $links;
137
	}
138
139
	/**
140
	 * Get the Shipping Zone Locations schema, conforming to JSON Schema
141
	 *
142
	 * @return array
143
	 */
144
	public function get_item_schema() {
145
		$schema = array(
146
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
147
			'title'      => 'shipping_zone_location',
148
			'type'       => 'object',
149
			'properties' => array(
150
				'code' => array(
151
					'description' => __( 'Shipping zone location code.', 'woocommerce' ),
152
					'type'        => 'string',
153
					'context'     => array( 'view', 'edit' ),
154
					'required'    => true,
155
					'arg_options' => array(
156
						'sanitize_callback' => 'sanitize_text_field',
157
					),
158
				),
159
				'type' => array(
160
					'description' => __( 'Shipping zone location type.', 'woocommerce' ),
161
					'type'        => 'string',
162
					'context'     => array( 'view', 'edit' ),
163
					'required'    => true,
164
					'arg_options' => array(
165
						'sanitize_callback' => 'sanitize_text_field',
166
					),
167
					'enum'        => array(
168
						'postcode',
169
						'state',
170
						'country',
171
						'continent',
172
					),
173
				),
174
			),
175
		);
176
177
		return $this->add_additional_fields_schema( $schema );
178
	}
179
}