1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Local Pickup Shipping Method. |
9
|
|
|
* |
10
|
|
|
* This class is here for backwards commpatility for methods existing before zones existed. |
11
|
|
|
* |
12
|
|
|
* @deprecated 2.6.0 |
13
|
|
|
* @version 2.3.0 |
14
|
|
|
* @package WooCommerce/Classes/Shipping |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Shipping_Legacy_Local_Pickup extends WC_Shipping_Method { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
23
|
|
|
$this->id = 'legacy_local_pickup'; |
24
|
|
|
$this->method_title = __( 'Local Pickup (Legacy)', 'woocommerce' ); |
25
|
|
|
$this->method_description = sprintf( __( '<strong>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>.</strong>', 'woocommerce' ), admin_url( 'admin.php?page=wc-shipping' ) ); |
26
|
|
|
$this->init(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the name of the option in the WP DB. |
31
|
|
|
* @since 2.6.0 |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function get_option_key() { |
35
|
|
|
return $this->plugin_id . 'local_pickup' . '_settings'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* init function. |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function init() { |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Load the settings. |
44
|
|
|
$this->init_form_fields(); |
45
|
|
|
$this->init_settings(); |
46
|
|
|
|
47
|
|
|
// Define user set variables |
48
|
|
|
$this->enabled = $this->get_option( 'enabled' ); |
49
|
|
|
$this->title = $this->get_option( 'title' ); |
50
|
|
|
$this->codes = $this->get_option( 'codes' ); |
51
|
|
|
$this->availability = $this->get_option( 'availability' ); |
|
|
|
|
52
|
|
|
$this->countries = $this->get_option( 'countries' ); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// Actions |
55
|
|
|
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* calculate_shipping function. |
60
|
|
|
*/ |
61
|
|
|
public function calculate_shipping() { |
62
|
|
|
$rate = array( |
63
|
|
|
'id' => $this->id, |
64
|
|
|
'label' => $this->title, |
65
|
|
|
); |
66
|
|
|
$this->add_rate( $rate ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* init_form_fields function. |
71
|
|
|
*/ |
72
|
|
|
public function init_form_fields() { |
73
|
|
|
$this->form_fields = array( |
74
|
|
|
'enabled' => array( |
75
|
|
|
'title' => __( 'Enable', 'woocommerce' ), |
76
|
|
|
'type' => 'checkbox', |
77
|
|
|
'label' => __( 'Enable local pickup', 'woocommerce' ), |
78
|
|
|
'default' => 'no' |
79
|
|
|
), |
80
|
|
|
'title' => array( |
81
|
|
|
'title' => __( 'Title', 'woocommerce' ), |
82
|
|
|
'type' => 'text', |
83
|
|
|
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), |
84
|
|
|
'default' => __( 'Local Pickup', 'woocommerce' ), |
85
|
|
|
'desc_tip' => true, |
86
|
|
|
), |
87
|
|
|
'codes' => array( |
88
|
|
|
'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ), |
89
|
|
|
'type' => 'text', |
90
|
|
|
'desc_tip' => __( 'What ZIP/post codes are available for local pickup?', 'woocommerce' ), |
91
|
|
|
'default' => '', |
92
|
|
|
'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' ), |
93
|
|
|
'placeholder' => 'e.g. 12345, 56789' |
94
|
|
|
), |
95
|
|
|
'availability' => array( |
96
|
|
|
'title' => __( 'Method availability', 'woocommerce' ), |
97
|
|
|
'type' => 'select', |
98
|
|
|
'default' => 'all', |
99
|
|
|
'class' => 'availability wc-enhanced-select', |
100
|
|
|
'options' => array( |
101
|
|
|
'all' => __( 'All allowed countries', 'woocommerce' ), |
102
|
|
|
'specific' => __( 'Specific Countries', 'woocommerce' ) |
103
|
|
|
) |
104
|
|
|
), |
105
|
|
|
'countries' => array( |
106
|
|
|
'title' => __( 'Specific Countries', 'woocommerce' ), |
107
|
|
|
'type' => 'multiselect', |
108
|
|
|
'class' => 'wc-enhanced-select', |
109
|
|
|
'css' => 'width: 450px;', |
110
|
|
|
'default' => '', |
111
|
|
|
'options' => WC()->countries->get_shipping_countries(), |
112
|
|
|
'custom_attributes' => array( |
113
|
|
|
'data-placeholder' => __( 'Select some countries', 'woocommerce' ) |
114
|
|
|
) |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get postcodes for this method. |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function get_valid_postcodes() { |
|
|
|
|
124
|
|
|
$codes = array(); |
125
|
|
|
|
126
|
|
|
if ( $this->codes != '' ) { |
127
|
|
|
foreach( explode( ',', $this->codes ) as $code ) { |
128
|
|
|
$codes[] = strtoupper( trim( $code ) ); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $codes; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* See if a given postcode matches valid postcodes. |
137
|
|
|
* @param string postcode |
138
|
|
|
* @param string country code |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function is_valid_postcode( $postcode, $country ) { |
|
|
|
|
142
|
|
|
$codes = $this->get_valid_postcodes(); |
143
|
|
|
$postcode = $this->clean( $postcode ); |
144
|
|
|
$formatted_postcode = wc_format_postcode( $postcode, $country ); |
145
|
|
|
|
146
|
|
|
if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Pattern matching |
151
|
|
|
foreach ( $codes as $c ) { |
152
|
|
|
$pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i'; |
153
|
|
|
if ( preg_match( $pattern, $postcode ) ) { |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Wildcard search |
159
|
|
|
$wildcard_postcode = $formatted_postcode . '*'; |
160
|
|
|
$postcode_length = strlen( $formatted_postcode ); |
161
|
|
|
|
162
|
|
|
for ( $i = 0; $i < $postcode_length; $i++ ) { |
163
|
|
|
if ( in_array( $wildcard_postcode, $codes ) ) { |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
$wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* See if the method is available. |
174
|
|
|
* |
175
|
|
|
* @param array $package |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function is_available( $package ) { |
|
|
|
|
179
|
|
|
$is_available = "yes" === $this->enabled; |
180
|
|
|
|
181
|
|
|
if ( $is_available && $this->get_valid_postcodes() ) { |
182
|
|
|
$is_available = $this->is_valid_postcode( $package['destination']['postcode'], $package['destination']['country'] ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ( $is_available ) { |
186
|
|
|
if ( $this->availability === 'specific' ) { |
|
|
|
|
187
|
|
|
$ship_to_countries = $this->countries; |
|
|
|
|
188
|
|
|
} else { |
189
|
|
|
$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() ); |
190
|
|
|
} |
191
|
|
|
if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { |
192
|
|
|
$is_available = false; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* clean function. |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @param mixed $code |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function clean( $code ) { |
207
|
|
|
return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' ); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.