class-wc-shipping-zone-data-store.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Class WC_Shipping_Zone_Data_Store file.
  4. *
  5. * @package WooCommerce\DataStores
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * WC Shipping Zone Data Store.
  12. *
  13. * @version 3.0.0
  14. */
  15. class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Shipping_Zone_Data_Store_Interface, WC_Object_Data_Store_Interface {
  16. /**
  17. * Method to create a new shipping zone.
  18. *
  19. * @since 3.0.0
  20. * @param WC_Shipping_Zone $zone Shipping zone object.
  21. */
  22. public function create( &$zone ) {
  23. global $wpdb;
  24. $wpdb->insert(
  25. $wpdb->prefix . 'woocommerce_shipping_zones', array(
  26. 'zone_name' => $zone->get_zone_name(),
  27. 'zone_order' => $zone->get_zone_order(),
  28. )
  29. );
  30. $zone->set_id( $wpdb->insert_id );
  31. $zone->save_meta_data();
  32. $this->save_locations( $zone );
  33. $zone->apply_changes();
  34. WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
  35. WC_Cache_Helper::get_transient_version( 'shipping', true );
  36. }
  37. /**
  38. * Update zone in the database.
  39. *
  40. * @since 3.0.0
  41. * @param WC_Shipping_Zone $zone Shipping zone object.
  42. */
  43. public function update( &$zone ) {
  44. global $wpdb;
  45. if ( $zone->get_id() ) {
  46. $wpdb->update(
  47. $wpdb->prefix . 'woocommerce_shipping_zones', array(
  48. 'zone_name' => $zone->get_zone_name(),
  49. 'zone_order' => $zone->get_zone_order(),
  50. ), array( 'zone_id' => $zone->get_id() )
  51. );
  52. }
  53. $zone->save_meta_data();
  54. $this->save_locations( $zone );
  55. $zone->apply_changes();
  56. WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
  57. WC_Cache_Helper::get_transient_version( 'shipping', true );
  58. }
  59. /**
  60. * Method to read a shipping zone from the database.
  61. *
  62. * @since 3.0.0
  63. * @param WC_Shipping_Zone $zone Shipping zone object.
  64. * @throws Exception If invalid data store.
  65. */
  66. public function read( &$zone ) {
  67. global $wpdb;
  68. $zone_data = false;
  69. if ( 0 !== $zone->get_id() || '0' !== $zone->get_id() ) {
  70. $zone_data = $wpdb->get_row(
  71. $wpdb->prepare(
  72. "SELECT zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1",
  73. $zone->get_id()
  74. )
  75. );
  76. }
  77. if ( 0 === $zone->get_id() || '0' === $zone->get_id() ) {
  78. $this->read_zone_locations( $zone );
  79. $zone->set_zone_name( __( 'Locations not covered by your other zones', 'woocommerce' ) );
  80. $zone->read_meta_data();
  81. $zone->set_object_read( true );
  82. do_action( 'woocommerce_shipping_zone_loaded', $zone );
  83. } elseif ( $zone_data ) {
  84. $zone->set_zone_name( $zone_data->zone_name );
  85. $zone->set_zone_order( $zone_data->zone_order );
  86. $this->read_zone_locations( $zone );
  87. $zone->read_meta_data();
  88. $zone->set_object_read( true );
  89. do_action( 'woocommerce_shipping_zone_loaded', $zone );
  90. } else {
  91. throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
  92. }
  93. }
  94. /**
  95. * Deletes a shipping zone from the database.
  96. *
  97. * @since 3.0.0
  98. * @param WC_Shipping_Zone $zone Shipping zone object.
  99. * @param array $args Array of args to pass to the delete method.
  100. * @return void
  101. */
  102. public function delete( &$zone, $args = array() ) {
  103. if ( $zone->get_id() ) {
  104. global $wpdb;
  105. $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'zone_id' => $zone->get_id() ) );
  106. $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone->get_id() ) );
  107. $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $zone->get_id() ) );
  108. WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
  109. $id = $zone->get_id();
  110. $zone->set_id( null );
  111. WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
  112. WC_Cache_Helper::get_transient_version( 'shipping', true );
  113. do_action( 'woocommerce_delete_shipping_zone', $id );
  114. }
  115. }
  116. /**
  117. * Get a list of shipping methods for a specific zone.
  118. *
  119. * @since 3.0.0
  120. * @param int $zone_id Zone ID.
  121. * @param bool $enabled_only True to request enabled methods only.
  122. * @return array Array of objects containing method_id, method_order, instance_id, is_enabled
  123. */
  124. public function get_methods( $zone_id, $enabled_only ) {
  125. global $wpdb;
  126. if ( $enabled_only ) {
  127. $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";
  128. } else {
  129. $raw_methods_sql = "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d";
  130. }
  131. return $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $zone_id ) ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
  132. }
  133. /**
  134. * Get count of methods for a zone.
  135. *
  136. * @since 3.0.0
  137. * @param int $zone_id Zone ID.
  138. * @return int Method Count
  139. */
  140. public function get_method_count( $zone_id ) {
  141. global $wpdb;
  142. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d", $zone_id ) );
  143. }
  144. /**
  145. * Add a shipping method to a zone.
  146. *
  147. * @since 3.0.0
  148. * @param int $zone_id Zone ID.
  149. * @param string $type Method Type/ID.
  150. * @param int $order Method Order.
  151. * @return int Instance ID
  152. */
  153. public function add_method( $zone_id, $type, $order ) {
  154. global $wpdb;
  155. $wpdb->insert(
  156. $wpdb->prefix . 'woocommerce_shipping_zone_methods',
  157. array(
  158. 'method_id' => $type,
  159. 'zone_id' => $zone_id,
  160. 'method_order' => $order,
  161. ),
  162. array(
  163. '%s',
  164. '%d',
  165. '%d',
  166. )
  167. );
  168. return $wpdb->insert_id;
  169. }
  170. /**
  171. * Delete a method instance.
  172. *
  173. * @since 3.0.0
  174. * @param int $instance_id Instance ID.
  175. */
  176. public function delete_method( $instance_id ) {
  177. global $wpdb;
  178. $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
  179. do_action( 'woocommerce_delete_shipping_zone_method', $instance_id );
  180. }
  181. /**
  182. * Get a shipping zone method instance.
  183. *
  184. * @since 3.0.0
  185. * @param int $instance_id Instance ID.
  186. * @return object
  187. */
  188. public function get_method( $instance_id ) {
  189. global $wpdb;
  190. 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 ) );
  191. }
  192. /**
  193. * Find a matching zone ID for a given package.
  194. *
  195. * @since 3.0.0
  196. * @param object $package Package information.
  197. * @return int
  198. */
  199. public function get_zone_id_from_package( $package ) {
  200. global $wpdb;
  201. $country = strtoupper( wc_clean( $package['destination']['country'] ) );
  202. $state = strtoupper( wc_clean( $package['destination']['state'] ) );
  203. $continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
  204. $postcode = wc_normalize_postcode( wc_clean( $package['destination']['postcode'] ) );
  205. // Work out criteria for our zone search.
  206. $criteria = array();
  207. $criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
  208. $criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
  209. $criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s )", $continent );
  210. $criteria[] = 'OR ( location_type IS NULL ) )';
  211. // Postcode range and wildcard matching.
  212. $postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
  213. if ( $postcode_locations ) {
  214. $zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
  215. $matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code', $country );
  216. $do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $matches ) ) );
  217. if ( ! empty( $do_not_match ) ) {
  218. $criteria[] = 'AND zones.zone_id NOT IN (' . implode( ',', $do_not_match ) . ')';
  219. }
  220. }
  221. // Get matching zones.
  222. return $wpdb->get_var(
  223. "SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
  224. LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id AND location_type != 'postcode'
  225. WHERE " . implode( ' ', $criteria ) // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
  226. . ' ORDER BY zone_order ASC, zone_id ASC LIMIT 1'
  227. );
  228. }
  229. /**
  230. * Return an ordered list of zones.
  231. *
  232. * @since 3.0.0
  233. * @return array An array of objects containing a zone_id, zone_name, and zone_order.
  234. */
  235. public function get_zones() {
  236. global $wpdb;
  237. 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;" );
  238. }
  239. /**
  240. * Return a zone ID from an instance ID.
  241. *
  242. * @since 3.0.0
  243. * @param int $id Instnace ID.
  244. * @return int
  245. */
  246. public function get_zone_id_by_instance_id( $id ) {
  247. global $wpdb;
  248. 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 ) );
  249. }
  250. /**
  251. * Read location data from the database.
  252. *
  253. * @param WC_Shipping_Zone $zone Shipping zone object.
  254. */
  255. private function read_zone_locations( &$zone ) {
  256. global $wpdb;
  257. $locations = $wpdb->get_results(
  258. $wpdb->prepare(
  259. "SELECT location_code, location_type FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d",
  260. $zone->get_id()
  261. )
  262. );
  263. if ( $locations ) {
  264. foreach ( $locations as $location ) {
  265. $zone->add_location( $location->location_code, $location->location_type );
  266. }
  267. }
  268. }
  269. /**
  270. * Save locations to the DB.
  271. * This function clears old locations, then re-inserts new if any changes are found.
  272. *
  273. * @since 3.0.0
  274. *
  275. * @param WC_Shipping_Zone $zone Shipping zone object.
  276. *
  277. * @return bool|void
  278. */
  279. private function save_locations( &$zone ) {
  280. $changed_props = array_keys( $zone->get_changes() );
  281. if ( ! in_array( 'zone_locations', $changed_props, true ) ) {
  282. return false;
  283. }
  284. global $wpdb;
  285. $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone->get_id() ) );
  286. foreach ( $zone->get_zone_locations( 'edit' ) as $location ) {
  287. $wpdb->insert(
  288. $wpdb->prefix . 'woocommerce_shipping_zone_locations', array(
  289. 'zone_id' => $zone->get_id(),
  290. 'location_code' => $location->code,
  291. 'location_type' => $location->type,
  292. )
  293. );
  294. }
  295. }
  296. }