PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/shipping/local-pickup/class-wc-shipping-local-pickup.php

https://gitlab.com/webkod3r/tripolis
PHP | 201 lines | 127 code | 23 blank | 51 comment | 15 complexity | f886c0fdc4a5a12e5ad69eb7cfae32ca MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * Local Pickup Shipping Method.
  7. *
  8. * A simple shipping method allowing free pickup as a shipping method.
  9. *
  10. * @class WC_Shipping_Local_Pickup
  11. * @version 2.3.0
  12. * @package WooCommerce/Classes/Shipping
  13. * @author WooThemes
  14. */
  15. class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct() {
  20. $this->id = 'local_pickup';
  21. $this->method_title = __( 'Local Pickup', 'woocommerce' );
  22. $this->method_description = __( 'Local pickup is a simple method which allows customers to pick up orders themselves.', 'woocommerce' );
  23. $this->init();
  24. }
  25. /**
  26. * Initialize local pickup.
  27. */
  28. public function init() {
  29. // Load the settings.
  30. $this->init_form_fields();
  31. $this->init_settings();
  32. // Define user set variables
  33. $this->enabled = $this->get_option( 'enabled' );
  34. $this->title = $this->get_option( 'title' );
  35. $this->codes = $this->get_option( 'codes' );
  36. $this->availability = $this->get_option( 'availability' );
  37. $this->countries = $this->get_option( 'countries' );
  38. // Actions
  39. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  40. }
  41. /**
  42. * calculate_shipping function.
  43. * Calculate local pickup shipping.
  44. */
  45. public function calculate_shipping() {
  46. $rate = array(
  47. 'id' => $this->id,
  48. 'label' => $this->title,
  49. );
  50. $this->add_rate( $rate );
  51. }
  52. /**
  53. * Init form fields.
  54. */
  55. public function init_form_fields() {
  56. $this->form_fields = array(
  57. 'enabled' => array(
  58. 'title' => __( 'Enable', 'woocommerce' ),
  59. 'type' => 'checkbox',
  60. 'label' => __( 'Enable local pickup', 'woocommerce' ),
  61. 'default' => 'no'
  62. ),
  63. 'title' => array(
  64. 'title' => __( 'Title', 'woocommerce' ),
  65. 'type' => 'text',
  66. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  67. 'default' => __( 'Local Pickup', 'woocommerce' ),
  68. 'desc_tip' => true,
  69. ),
  70. 'codes' => array(
  71. 'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ),
  72. 'type' => 'text',
  73. 'desc_tip' => __( 'What ZIP/post codes are available for local pickup?', 'woocommerce' ),
  74. 'default' => '',
  75. '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' ),
  76. 'placeholder' => 'e.g. 12345, 56789'
  77. ),
  78. 'availability' => array(
  79. 'title' => __( 'Method availability', 'woocommerce' ),
  80. 'type' => 'select',
  81. 'default' => 'all',
  82. 'class' => 'availability wc-enhanced-select',
  83. 'options' => array(
  84. 'all' => __( 'All allowed countries', 'woocommerce' ),
  85. 'specific' => __( 'Specific Countries', 'woocommerce' )
  86. )
  87. ),
  88. 'countries' => array(
  89. 'title' => __( 'Specific Countries', 'woocommerce' ),
  90. 'type' => 'multiselect',
  91. 'class' => 'wc-enhanced-select',
  92. 'css' => 'width: 450px;',
  93. 'default' => '',
  94. 'options' => WC()->countries->get_shipping_countries(),
  95. 'custom_attributes' => array(
  96. 'data-placeholder' => __( 'Select some countries', 'woocommerce' )
  97. )
  98. )
  99. );
  100. }
  101. /**
  102. * Get postcodes for this method.
  103. * @return array
  104. */
  105. public function get_valid_postcodes() {
  106. $codes = array();
  107. if ( $this->codes != '' ) {
  108. foreach( explode( ',', $this->codes ) as $code ) {
  109. $codes[] = strtoupper( trim( $code ) );
  110. }
  111. }
  112. return $codes;
  113. }
  114. /**
  115. * See if a given postcode matches valid postcodes.
  116. * @param string postcode
  117. * @param string country code
  118. * @return boolean
  119. */
  120. public function is_valid_postcode( $postcode, $country ) {
  121. $codes = $this->get_valid_postcodes();
  122. $postcode = $this->clean( $postcode );
  123. $formatted_postcode = wc_format_postcode( $postcode, $country );
  124. if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) {
  125. return true;
  126. }
  127. // Pattern matching
  128. foreach ( $codes as $c ) {
  129. $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i';
  130. if ( preg_match( $pattern, $postcode ) ) {
  131. return true;
  132. }
  133. }
  134. // Wildcard search
  135. $wildcard_postcode = $formatted_postcode . '*';
  136. $postcode_length = strlen( $formatted_postcode );
  137. for ( $i = 0; $i < $postcode_length; $i++ ) {
  138. if ( in_array( $wildcard_postcode, $codes ) ) {
  139. return true;
  140. }
  141. $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*';
  142. }
  143. return false;
  144. }
  145. /**
  146. * See if the method is available.
  147. *
  148. * @param array $package
  149. * @return bool
  150. */
  151. public function is_available( $package ) {
  152. $is_available = "yes" === $this->enabled;
  153. if ( $is_available && $this->get_valid_postcodes() ) {
  154. $is_available = $this->is_valid_postcode( $package['destination']['postcode'], $package['destination']['country'] );
  155. }
  156. if ( $is_available ) {
  157. if ( $this->availability === 'specific' ) {
  158. $ship_to_countries = $this->countries;
  159. } else {
  160. $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
  161. }
  162. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
  163. $is_available = false;
  164. }
  165. }
  166. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
  167. }
  168. /**
  169. * Clean code string.
  170. *
  171. * @access public
  172. * @param mixed $code
  173. * @return string
  174. */
  175. public function clean( $code ) {
  176. return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
  177. }
  178. }