class-wc-shipping-legacy-local-pickup.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Class WC_Shipping_Legacy_Local_Pickup file.
  4. *
  5. * @package WooCommerce\Shipping
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * Local Pickup Shipping Method.
  12. *
  13. * This class is here for backwards compatibility for methods existing before zones existed.
  14. *
  15. * @deprecated 2.6.0
  16. * @version 2.3.0
  17. * @package WooCommerce/Classes/Shipping
  18. */
  19. class WC_Shipping_Legacy_Local_Pickup extends WC_Shipping_Method {
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct() {
  24. $this->id = 'legacy_local_pickup';
  25. $this->method_title = __( 'Local pickup (legacy)', 'woocommerce' );
  26. /* translators: %s: Admin shipping settings URL */
  27. $this->method_description = '<strong>' . sprintf( __( 'This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping zones</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping' ) ) . '</strong>';
  28. $this->init();
  29. }
  30. /**
  31. * Process and redirect if disabled.
  32. */
  33. public function process_admin_options() {
  34. parent::process_admin_options();
  35. if ( 'no' === $this->settings['enabled'] ) {
  36. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) );
  37. exit;
  38. }
  39. }
  40. /**
  41. * Return the name of the option in the WP DB.
  42. *
  43. * @since 2.6.0
  44. * @return string
  45. */
  46. public function get_option_key() {
  47. return $this->plugin_id . 'local_pickup_settings';
  48. }
  49. /**
  50. * Init function.
  51. */
  52. public function init() {
  53. // Load the settings.
  54. $this->init_form_fields();
  55. $this->init_settings();
  56. // Define user set variables.
  57. $this->enabled = $this->get_option( 'enabled' );
  58. $this->title = $this->get_option( 'title' );
  59. $this->codes = $this->get_option( 'codes' );
  60. $this->availability = $this->get_option( 'availability' );
  61. $this->countries = $this->get_option( 'countries' );
  62. // Actions.
  63. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  64. }
  65. /**
  66. * Calculate shipping.
  67. *
  68. * @param array $package Package information.
  69. */
  70. public function calculate_shipping( $package = array() ) {
  71. $rate = array(
  72. 'id' => $this->id,
  73. 'label' => $this->title,
  74. 'package' => $package,
  75. );
  76. $this->add_rate( $rate );
  77. }
  78. /**
  79. * Initialize form fields.
  80. */
  81. public function init_form_fields() {
  82. $this->form_fields = array(
  83. 'enabled' => array(
  84. 'title' => __( 'Enable', 'woocommerce' ),
  85. 'type' => 'checkbox',
  86. 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),
  87. 'default' => 'no',
  88. ),
  89. 'title' => array(
  90. 'title' => __( 'Title', 'woocommerce' ),
  91. 'type' => 'text',
  92. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  93. 'default' => __( 'Local pickup', 'woocommerce' ),
  94. 'desc_tip' => true,
  95. ),
  96. 'codes' => array(
  97. 'title' => __( 'Allowed ZIP/post codes', 'woocommerce' ),
  98. 'type' => 'text',
  99. 'desc_tip' => __( 'What ZIP/post codes are available for local pickup?', 'woocommerce' ),
  100. 'default' => '',
  101. 'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ),
  102. 'placeholder' => 'e.g. 12345, 56789',
  103. ),
  104. 'availability' => array(
  105. 'title' => __( 'Method availability', 'woocommerce' ),
  106. 'type' => 'select',
  107. 'default' => 'all',
  108. 'class' => 'availability wc-enhanced-select',
  109. 'options' => array(
  110. 'all' => __( 'All allowed countries', 'woocommerce' ),
  111. 'specific' => __( 'Specific countries', 'woocommerce' ),
  112. ),
  113. ),
  114. 'countries' => array(
  115. 'title' => __( 'Specific countries', 'woocommerce' ),
  116. 'type' => 'multiselect',
  117. 'class' => 'wc-enhanced-select',
  118. 'css' => 'width: 400px;',
  119. 'default' => '',
  120. 'options' => WC()->countries->get_shipping_countries(),
  121. 'custom_attributes' => array(
  122. 'data-placeholder' => __( 'Select some countries', 'woocommerce' ),
  123. ),
  124. ),
  125. );
  126. }
  127. /**
  128. * Get postcodes for this method.
  129. *
  130. * @return array
  131. */
  132. public function get_valid_postcodes() {
  133. $codes = array();
  134. if ( '' !== $this->codes ) {
  135. foreach ( explode( ',', $this->codes ) as $code ) {
  136. $codes[] = strtoupper( trim( $code ) );
  137. }
  138. }
  139. return $codes;
  140. }
  141. /**
  142. * See if a given postcode matches valid postcodes.
  143. *
  144. * @param string $postcode Postcode to check.
  145. * @param string $country code Code of the country to check postcode against.
  146. * @return boolean
  147. */
  148. public function is_valid_postcode( $postcode, $country ) {
  149. $codes = $this->get_valid_postcodes();
  150. $postcode = $this->clean( $postcode );
  151. $formatted_postcode = wc_format_postcode( $postcode, $country );
  152. if ( in_array( $postcode, $codes, true ) || in_array( $formatted_postcode, $codes, true ) ) {
  153. return true;
  154. }
  155. // Pattern matching.
  156. foreach ( $codes as $c ) {
  157. $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i';
  158. if ( preg_match( $pattern, $postcode ) ) {
  159. return true;
  160. }
  161. }
  162. // Wildcard search.
  163. $wildcard_postcode = $formatted_postcode . '*';
  164. $postcode_length = strlen( $formatted_postcode );
  165. for ( $i = 0; $i < $postcode_length; $i++ ) {
  166. if ( in_array( $wildcard_postcode, $codes, true ) ) {
  167. return true;
  168. }
  169. $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*';
  170. }
  171. return false;
  172. }
  173. /**
  174. * See if the method is available.
  175. *
  176. * @param array $package Package information.
  177. * @return bool
  178. */
  179. public function is_available( $package ) {
  180. $is_available = 'yes' === $this->enabled;
  181. if ( $is_available && $this->get_valid_postcodes() ) {
  182. $is_available = $this->is_valid_postcode( $package['destination']['postcode'], $package['destination']['country'] );
  183. }
  184. if ( $is_available ) {
  185. if ( 'specific' === $this->availability ) {
  186. $ship_to_countries = $this->countries;
  187. } else {
  188. $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
  189. }
  190. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries, true ) ) {
  191. $is_available = false;
  192. }
  193. }
  194. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
  195. }
  196. /**
  197. * Clean function.
  198. *
  199. * @access public
  200. * @param mixed $code Code.
  201. * @return string
  202. */
  203. public function clean( $code ) {
  204. return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
  205. }
  206. }