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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / data-stores / class-wc-shipping-zone-data-store.php
woocommerce / includes / data-stores Last commit date
abstract-wc-order-data-store-cpt.php 2 months ago abstract-wc-order-item-type-data-store.php 7 months ago class-wc-coupon-data-store-cpt.php 2 months ago class-wc-customer-data-store-session.php 9 months ago class-wc-customer-data-store.php 3 months ago class-wc-customer-download-data-store.php 3 years ago class-wc-customer-download-log-data-store.php 1 year ago class-wc-data-store-wp.php 9 months ago class-wc-order-data-store-cpt.php 2 months ago class-wc-order-item-coupon-data-store.php 8 years ago class-wc-order-item-data-store.php 2 months ago class-wc-order-item-fee-data-store.php 8 years ago class-wc-order-item-product-data-store.php 3 years ago class-wc-order-item-shipping-data-store.php 1 year ago class-wc-order-item-tax-data-store.php 6 years ago class-wc-order-refund-data-store-cpt.php 2 months ago class-wc-payment-token-data-store.php 4 months ago class-wc-product-data-store-cpt.php 2 months ago class-wc-product-grouped-data-store-cpt.php 3 months ago class-wc-product-variable-data-store-cpt.php 1 month ago class-wc-product-variation-data-store-cpt.php 9 months ago class-wc-shipping-zone-data-store.php 5 months ago class-wc-webhook-data-store.php 2 years ago
class-wc-shipping-zone-data-store.php
487 lines
1 <?php
2 /**
3 * Class WC_Shipping_Zone_Data_Store file.
4 *
5 * @package WooCommerce\DataStores
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * WC Shipping Zone Data Store.
14 *
15 * @version 3.0.0
16 */
17 class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface, WC_Shipping_Zone_Data_Store_Interface {
18
19 /**
20 * Method to create a new shipping zone.
21 *
22 * @since 3.0.0
23 * @param WC_Shipping_Zone $zone Shipping zone object.
24 */
25 public function create( &$zone ) {
26 global $wpdb;
27 $wpdb->insert(
28 $wpdb->prefix . 'woocommerce_shipping_zones',
29 array(
30 'zone_name' => $zone->get_zone_name(),
31 'zone_order' => $zone->get_zone_order(),
32 )
33 );
34 $zone->set_id( $wpdb->insert_id );
35 $zone->save_meta_data();
36 $this->save_locations( $zone );
37 $zone->apply_changes();
38 WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
39 WC_Cache_Helper::get_transient_version( 'shipping', true );
40 }
41
42 /**
43 * Update zone in the database.
44 *
45 * @since 3.0.0
46 * @param WC_Shipping_Zone $zone Shipping zone object.
47 */
48 public function update( &$zone ) {
49 global $wpdb;
50 if ( $zone->get_id() ) {
51 $wpdb->update(
52 $wpdb->prefix . 'woocommerce_shipping_zones',
53 array(
54 'zone_name' => $zone->get_zone_name(),
55 'zone_order' => $zone->get_zone_order(),
56 ),
57 array( 'zone_id' => $zone->get_id() )
58 );
59 }
60 $zone->save_meta_data();
61 $this->save_locations( $zone );
62 $zone->apply_changes();
63 WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
64 WC_Cache_Helper::get_transient_version( 'shipping', true );
65 }
66
67 /**
68 * Method to read a shipping zone from the database.
69 *
70 * @since 3.0.0
71 * @param WC_Shipping_Zone $zone Shipping zone object.
72 * @throws Exception If invalid data store.
73 */
74 public function read( &$zone ) {
75 $zones = array( $zone->get_id() => $zone );
76 $this->read_multiple( $zones );
77 }
78
79 /**
80 * Reads multiple WC_Shipping_Zone objects from the data store.
81 *
82 * @param WC_Shipping_Zone[] $zones Array of zones to read keyed by the zone_id.
83 *
84 * @return void
85 *
86 * @throws Exception If invalid zone_id givein for data store.
87 */
88 public function read_multiple( array &$zones ) {
89 $zone_ids = array_keys( $zones );
90 $zone_data = $this->get_zone_data_for_ids( $zone_ids );
91 foreach ( $zones as $zone_id => $zone ) {
92 if ( 0 === $zone_id || '0' === $zone_id ) {
93 $zone->set_zone_name( __( 'Locations not covered by your other zones', 'woocommerce' ) );
94 } else {
95 if ( ! isset( $zone_data[ $zone_id ] ) ) {
96 throw new Exception( __( 'Invalid data store.', 'woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
97 }
98 $zone->set_zone_name( $zone_data[ $zone_id ]->zone_name );
99 $zone->set_zone_order( $zone_data[ $zone_id ]->zone_order );
100 }
101 }
102
103 $zone_locations = $this->get_zone_locations_for_ids( $zone_ids );
104 foreach ( $zone_locations as $zone_location ) {
105 if ( isset( $zones[ $zone_location->zone_id ] ) ) {
106 $zones[ $zone_location->zone_id ]->add_location( $zone_location->location_code, $zone_location->location_type );
107 }
108 }
109
110 foreach ( $zones as $zone_id => $zone ) {
111 $zone->set_object_read( true );
112 /**
113 * Indicate that the WooCommerce shipping zone has been loaded.
114 *
115 * @param WC_Shipping_Zone $zone The shipping zone that has been loaded.
116 */
117 do_action( 'woocommerce_shipping_zone_loaded', $zone );
118 }
119 }
120
121 /**
122 * Retrieve the zone data for the given zone_ids.
123 *
124 * @param array $ids The zone_ids to retrieve.
125 *
126 * @return stdClass[] An array of objects containing the zone data, keyed by the zone_id.
127 */
128 private function get_zone_data_for_ids( array $ids ) {
129 global $wpdb;
130
131 if ( empty( $ids ) || array( '0' ) === $ids || array( 0 ) === $ids ) {
132 return array();
133 }
134
135 $zone_ids = array_map( 'absint', $ids );
136
137 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $zone_ids already run through absint.
138 return $wpdb->get_results(
139 "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones " .
140 'WHERE zone_id IN ( ' . implode( ',', $zone_ids ) . ' ) ',
141 OBJECT_K
142 );
143 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
144 }
145
146 /**
147 * Retrieve the zone location data for the given zone_ids.
148 *
149 * @param array $ids The zone_ids to retrieve.
150 *
151 * @return stdClass[] An array of objects containing the zone_id, location_code, and location_type for each zone location.
152 */
153 private function get_zone_locations_for_ids( array $ids ) {
154 global $wpdb;
155
156 if ( empty( $ids ) || array( '0' ) === $ids || array( 0 ) === $ids ) {
157 return array();
158 }
159
160 $zone_ids = array_map( 'absint', $ids );
161 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $zone_ids already run through absint.
162 return $wpdb->get_results(
163 "SELECT zone_id, location_code, location_type FROM {$wpdb->prefix}woocommerce_shipping_zone_locations " .
164 'WHERE zone_id IN ( ' . implode( ',', $zone_ids ) . ' ) '
165 );
166 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
167 }
168
169 /**
170 * Deletes a shipping zone from the database.
171 *
172 * @since 3.0.0
173 * @param WC_Shipping_Zone $zone Shipping zone object.
174 * @param array $args Array of args to pass to the delete method.
175 * @return void
176 */
177 public function delete( &$zone, $args = array() ) {
178 $zone_id = $zone->get_id();
179
180 if ( $zone_id ) {
181 global $wpdb;
182
183 // Delete methods and their settings.
184 $methods = $this->get_methods( $zone_id, false );
185
186 if ( $methods ) {
187 foreach ( $methods as $method ) {
188 $this->delete_method( $method->instance_id );
189 }
190 }
191
192 // Delete zone.
193 $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone_id ) );
194 $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $zone_id ) );
195
196 $zone->set_id( null );
197
198 WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
199 WC_Cache_Helper::get_transient_version( 'shipping', true );
200
201 do_action( 'woocommerce_delete_shipping_zone', $zone_id );
202 }
203 }
204
205 /**
206 * Get a list of shipping methods for a specific zone.
207 *
208 * @since 3.0.0
209 * @param int $zone_id Zone ID.
210 * @param bool $enabled_only True to request enabled methods only.
211 * @return array Array of objects containing method_id, method_order, instance_id, is_enabled
212 */
213 public function get_methods( $zone_id, $enabled_only ) {
214 global $wpdb;
215
216 if ( $enabled_only ) {
217 $raw_methods_sql = "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d AND is_enabled = 1";
218 } else {
219 $raw_methods_sql = "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d";
220 }
221
222 return $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $zone_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
223 }
224
225 /**
226 * Get count of methods for a zone.
227 *
228 * @since 3.0.0
229 * @param int $zone_id Zone ID.
230 * @return int Method Count
231 */
232 public function get_method_count( $zone_id ) {
233 global $wpdb;
234 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d", $zone_id ) );
235 }
236
237 /**
238 * Add a shipping method to a zone.
239 *
240 * @since 3.0.0
241 * @param int $zone_id Zone ID.
242 * @param string $type Method Type/ID.
243 * @param int $order Method Order.
244 * @return int Instance ID
245 */
246 public function add_method( $zone_id, $type, $order ) {
247 global $wpdb;
248 $wpdb->insert(
249 $wpdb->prefix . 'woocommerce_shipping_zone_methods',
250 array(
251 'method_id' => $type,
252 'zone_id' => $zone_id,
253 'method_order' => $order,
254 ),
255 array(
256 '%s',
257 '%d',
258 '%d',
259 )
260 );
261 return $wpdb->insert_id;
262 }
263
264 /**
265 * Delete a method instance.
266 *
267 * @since 3.0.0
268 * @param int $instance_id Instance ID.
269 */
270 public function delete_method( $instance_id ) {
271 global $wpdb;
272
273 $method = $this->get_method( $instance_id );
274
275 if ( ! $method ) {
276 return;
277 }
278
279 delete_option( 'woocommerce_' . $method->method_id . '_' . $instance_id . '_settings' );
280
281 $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
282
283 do_action( 'woocommerce_delete_shipping_zone_method', $instance_id );
284 }
285
286 /**
287 * Get a shipping zone method instance.
288 *
289 * @since 3.0.0
290 * @param int $instance_id Instance ID.
291 * @return object
292 */
293 public function get_method( $instance_id ) {
294 global $wpdb;
295 return $wpdb->get_row( $wpdb->prepare( "SELECT zone_id, method_id, instance_id, method_order, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) );
296 }
297
298 /**
299 * Find a matching zone ID for a given package.
300 *
301 * @since 3.0.0
302 * @param object $package Package information.
303 * @return int
304 */
305 public function get_zone_id_from_package( $package ) {
306 global $wpdb;
307
308 $country = strtoupper( wc_clean( $package['destination']['country'] ) );
309 $state = strtoupper( wc_clean( $package['destination']['state'] ) );
310 $continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
311 $postcode = wc_normalize_postcode( wc_clean( $package['destination']['postcode'] ) );
312
313 // Work out criteria for our zone search.
314 $criteria = array();
315 $criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
316 $criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
317 $criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s )", $continent );
318 $criteria[] = 'OR ( location_type IS NULL ) )';
319
320 // Postcode range and wildcard matching.
321 $postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
322
323 if ( $postcode_locations ) {
324 $zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
325 $matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code', $country );
326 $do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $matches ) ) );
327
328 if ( ! empty( $do_not_match ) ) {
329 $criteria[] = 'AND zones.zone_id NOT IN (' . implode( ',', $do_not_match ) . ')';
330 }
331 }
332
333 /**
334 * Get shipping zone criteria
335 *
336 * @since 3.6.6
337 * @param array $criteria Get zone criteria.
338 * @param array $package Package information.
339 * @param array $postcode_locations Postcode range and wildcard matching.
340 */
341 $criteria = apply_filters( 'woocommerce_get_zone_criteria', $criteria, $package, $postcode_locations );
342
343 // Get matching zones.
344 return $wpdb->get_var(
345 "SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
346 LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id AND location_type != 'postcode'
347 WHERE " . implode( ' ', $criteria ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
348 . ' ORDER BY zone_order ASC, zones.zone_id ASC LIMIT 1'
349 );
350 }
351
352 /**
353 * Return an ordered list of zones.
354 *
355 * @since 3.0.0
356 * @return array An array of objects containing a zone_id, zone_name, and zone_order.
357 */
358 public function get_zones() {
359 global $wpdb;
360 return $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC, zone_id ASC;" );
361 }
362
363
364 /**
365 * Return a zone ID from an instance ID.
366 *
367 * @since 3.0.0
368 * @param int $id Instance ID.
369 * @return int
370 */
371 public function get_zone_id_by_instance_id( $id ) {
372 global $wpdb;
373 return $wpdb->get_var( $wpdb->prepare( "SELECT zone_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods as methods WHERE methods.instance_id = %d LIMIT 1;", $id ) );
374 }
375
376 /**
377 * Save locations to the DB.
378 * This function clears old locations, then re-inserts new if any changes are found.
379 *
380 * @since 3.0.0
381 *
382 * @param WC_Shipping_Zone $zone Shipping zone object.
383 *
384 * @return bool|void
385 */
386 private function save_locations( &$zone ) {
387 $changed_props = array_keys( $zone->get_changes() );
388 if ( ! in_array( 'zone_locations', $changed_props, true ) ) {
389 return false;
390 }
391
392 global $wpdb;
393 $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone->get_id() ) );
394
395 foreach ( $zone->get_zone_locations( 'edit' ) as $location ) {
396 $wpdb->insert(
397 $wpdb->prefix . 'woocommerce_shipping_zone_locations',
398 array(
399 'zone_id' => $zone->get_id(),
400 'location_code' => $location->code,
401 'location_type' => $location->type,
402 )
403 );
404 }
405 }
406
407 /**
408 * Shipping zones do not support meta data.
409 *
410 * This override prevents the parent class from incorrectly reading from wp_postmeta,
411 * which would happen because shipping zones use their own table but there is no
412 * corresponding shipping zone meta table.
413 *
414 * @since 10.5.0
415 * @param WC_Shipping_Zone $zone Shipping zone object.
416 * @return array Empty array - shipping zones have no meta table.
417 */
418 public function read_meta( &$zone ) {
419 return array();
420 }
421
422 /**
423 * Shipping zones do not support meta data.
424 *
425 * @since 10.5.0
426 * @param WC_Shipping_Zone $zone Shipping zone object.
427 * @param stdClass $meta Meta object (containing at least ->id).
428 * @return array Empty array - no meta was deleted.
429 */
430 public function delete_meta( &$zone, $meta ) {
431 wc_get_logger()->warning(
432 'Attempted to delete meta from a shipping zone, but shipping zones do not support meta data.',
433 array(
434 'source' => 'shipping_zone_data_store',
435 'zone_id' => $zone->get_id(),
436 'backtrace' => true,
437 )
438 );
439 return array();
440 }
441
442 /**
443 * Shipping zones do not support meta data.
444 *
445 * Returns 0 to indicate no meta was added. Valid meta IDs are always positive
446 * integers, so 0 indicates failure while remaining type-compatible with parent.
447 *
448 * @since 10.5.0
449 * @param WC_Shipping_Zone $zone Shipping zone object.
450 * @param stdClass $meta Meta object (containing ->key and ->value).
451 * @return int Always returns 0 as shipping zones do not support meta storage.
452 */
453 public function add_meta( &$zone, $meta ) {
454 wc_get_logger()->warning(
455 'Attempted to add meta to a shipping zone, but shipping zones do not support meta data.',
456 array(
457 'source' => 'shipping_zone_data_store',
458 'zone_id' => $zone->get_id(),
459 'key' => $meta->key ?? '',
460 'backtrace' => true,
461 )
462 );
463 return 0;
464 }
465
466 /**
467 * Shipping zones do not support meta data.
468 *
469 * @since 10.5.0
470 * @param WC_Shipping_Zone $zone Shipping zone object.
471 * @param stdClass $meta Meta object (containing ->id, ->key and ->value).
472 * @return bool False - meta was not updated.
473 */
474 public function update_meta( &$zone, $meta ) {
475 wc_get_logger()->warning(
476 'Attempted to update meta on a shipping zone, but shipping zones do not support meta data.',
477 array(
478 'source' => 'shipping_zone_data_store',
479 'zone_id' => $zone->get_id(),
480 'key' => $meta->key ?? '',
481 'backtrace' => true,
482 )
483 );
484 return false;
485 }
486 }
487