PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
← All changes | shipping/method/method.php +47 -326 trunk3.0.2 View file →
@@ -7,27 +7,13 @@
7 7 *
8 8 * @since 3.0.0
9 9 */
10 10
11 -use WC_Shipping_Method;
12 -use MakeCommerce\Shipping\Method\ShippingClasses;
11 +abstract class Method extends \WC_Shipping_Method {
13 12
14 -abstract class Method extends WC_Shipping_Method {
15 - use ShippingClasses;
16 -
17 13 public $id;
18 14 public $instance_id;
19 - public $availability;
20 - public $countries;
21 15
22 - protected $ext;
23 - protected $order_country;
24 -
25 - private $enable;
26 - private $free_shipping_min_amount;
27 - private $maximum_weight;
28 - private $name_ext;
29 -
30 16 public $supports = array(
31 17 'shipping-zones',
32 18 'instance-settings',
33 19 'instance-settings-modal',
@@ -42,10 +28,10 @@
42 28 public function __construct( $instance_id = 0 ) {
43 29
44 30 //set required properties
45 31 $this->instance_id = absint( $instance_id );
46 - $this->id = $this->identifier . '_' . $this->carrier_id;
47 - $this->ext = $this->carrier_id;
32 + $this->id = $this->identifier . '_' . mb_strtolower( $this->carrier );
33 + $this->ext = mb_strtolower( $this->carrier );
48 34 $this->title = $this->return_method_title();
49 35 $this->method_title = $this->title;
50 36 $this->name_ext = $this->title;
51 37
@@ -62,10 +48,8 @@
62 48 $this->set_hooks();
63 49
64 50 //initilize method type
65 51 $this->initialize();
66 -
67 - $this->fields_validation( $this->settings );
68 52 }
69 53
70 54 /**
71 55 * Set hooks used by all WooCommerce shipping methods
@@ -71,226 +55,21 @@
71 55 * Set hooks used by all WooCommerce shipping methods
72 56 *
73 57 * @since 3.0.0
74 58 */
75 - final public function set_hooks() {
59 + final private function set_hooks() {
76 60
77 61 //update options
78 62 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
79 - add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, array( $this, 'fields_validation' ) );
80 63
81 - //Woocommerce Multilingual overrides (change some titles)
64 + // Woocommerce Multilingual overrides (change some titles)
82 65 if ( \MakeCommerce\i18n::using_language_plugins() ) {
83 66 add_filter( 'woocommerce_package_rates', array( $this, 'override_label_translation' ), 50 );
84 67 add_filter( 'woocommerce_order_get_items', array( $this, 'override_shipping_title_translation' ), 50, 2 );
85 68 }
86 -
87 - //check if a parcelmachine has been chosen in checkout
88 - add_action( 'woocommerce_checkout_process', array( $this, 'check_checkout_fields' ) );
89 - add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_checkout_fields' ) );
90 69 }
91 70
92 71 /**
93 - * Add check for shipping settings
94 - * Checks if certain fields are valid
95 - *
96 - * @since 3.2.0
97 - */
98 - function fields_validation( $fields ) {
99 - // Phone not set, can not validate
100 - if ( !isset( $fields['shop_phone'] ) ) {
101 - return $fields;
102 - }
103 - $fields['shop_phone'] = $this->sanitize_phone_number( $fields['shop_phone'] );
104 - if ( $fields['shop_phone'] != '' && !$this->valid_phone_number($fields['shop_phone']) ) {
105 - add_action( 'admin_notices', array( $this, 'invalid_phone_number_notice' ) );
106 - }
107 - return $fields;
108 - }
109 -
110 - /**
111 - * Add notice for invalid phone number
112 - * Adds shipping method specific notice to wp admin
113 - *
114 - * @since 3.2.0
115 - */
116 - function invalid_phone_number_notice() {
117 -
118 - // keep track of the added notices
119 - static $added = [];
120 -
121 - $user_id = get_current_user_id();
122 -
123 - $unique_id = 'mc_phone_number_notice_dismissed_' . $this->id;
124 - $dismiss_href = '?' . $unique_id;
125 -
126 - if ( isset( $_GET ) && !empty( $_GET ) ) {
127 - $dismiss_href = '&' . $unique_id;
128 - }
129 - // dismiss button pressed, add to db
130 - if ( isset( $_GET[$unique_id] ) ) {
131 - add_user_meta( $user_id, $unique_id, 'true', true );
132 - }
133 - // value is dismissed in db, do not display error
134 - if ( get_user_meta( $user_id, $unique_id ) ) {
135 - return;
136 - }
137 - // error already displayed, do not add duplicate
138 - if ( in_array( $unique_id, $added ) ) {
139 - return;
140 - } else {
141 - // add value to array to eliminate duplicates
142 - array_push( $added, $unique_id );
143 - // display error
144 - echo '
145 - <div class="notice notice-error">
146 - <p>
147 - ' . $this->phone_number_validation_error() . '
148 - <a href="admin.php?page=wc-settings&tab=shipping&section='. $this->id . '">' . __('Update here', 'wc_makecommerce_domain' ) . '</a>
149 - <a style="float: right; " href="' . $dismiss_href . '">' . __( 'Dismiss', 'wc_makecommerce_domain' ) . '</a>
150 - </p>
151 - </div>';
152 - }
153 - }
154 -
155 - /**
156 - * Add check for parcelmachine selectbox
157 - * Checks if something is chosen
158 - *
159 - * @since 3.0.4
160 - */
161 - public function check_checkout_fields() {
162 -
163 - //those checks are needed, otherwise the notification will be added more than once
164 - static $added1 = false;
165 - static $added2 = false;
166 -
167 - $shipping_method = !empty( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : false;
168 -
169 - if ( !empty( $shipping_method[0] ) ) {
170 - $shipping_method_ext = explode( ':', $shipping_method[0] );
171 - }
172 -
173 - if ( !$added1 && $shipping_method_ext[0] === $this->id && empty( $_POST[$shipping_method_ext[0]] ) && $this->type == "apt" ) {
174 - wc_add_notice( __( '<strong>Parcel machine</strong> is a required field.', 'wc_makecommerce_domain' ), 'error' );
175 - $added1 = true;
176 - }
177 -
178 - $shipping_phone_number = null;
179 - if ( isset( $_POST['ship_to_different_address'] ) && isset( $_POST['shipping_phone'] ) ) {
180 - $_POST['shipping_phone'] = $this->sanitize_phone_number( $_POST['shipping_phone'] );
181 - $shipping_phone_number = $_POST['shipping_phone'];
182 - } else {
183 - $_POST['billing_phone'] = $this->sanitize_phone_number( $_POST['billing_phone'] );
184 - $shipping_phone_number = $_POST['billing_phone'];
185 - }
186 -
187 - if ( $shipping_phone_number !== null ) {
188 - if ( !$added2 && $shipping_method_ext[0] === $this->id && !$this->valid_phone_number( $shipping_phone_number ) ) {
189 - wc_add_notice( $this->phone_number_validation_error(), 'error' );
190 - $added2 = true;
191 - }
192 - }
193 - }
194 -
195 - /**
196 - * Sanitize phone number
197 - *
198 - * @since 3.0.4
199 - */
200 - public function sanitize_phone_number( $phone_number ) {
201 -
202 - $phone_number = filter_var ( $phone_number, FILTER_SANITIZE_NUMBER_INT );
203 - $phone_number = trim( $phone_number, '-' );
204 - //convert first 2 zeros to + if needed
205 - if ( substr( $phone_number, 0, 2 ) == "00" ) {
206 - $phone_number = '+' . substr( $phone_number, 2 );
207 - }
208 -
209 - return $phone_number;
210 - }
211 -
212 - /**
213 - * Check if provided phonenumber is valid
214 - *
215 - * @since 3.0.4
216 - */
217 - public function valid_phone_number( $phone_number ) {
218 -
219 - //could be that all that we need to check is international format
220 - if ( $this->international_number_format ) {
221 - return $this->check_international_number( $phone_number );
222 - }
223 -
224 - //allow all numbers if validation is not defined
225 - if ( !isset( $this->valid_phonenumber_country_codes ) ) {
226 - return true;
227 - }
228 -
229 - //remove + signs
230 - $phone_number = trim( $phone_number, '+' );
231 -
232 - //check if it contains country code
233 - foreach ( $this->valid_phonenumber_country_codes as $country_code=>$valid_numbers ) {
234 -
235 - //country code matched
236 - if ( substr( $phone_number, 0, strlen( $country_code ) ) == $country_code ) {
237 - return $this->check_number( substr( $phone_number, strlen( $country_code ) ), $valid_numbers );
238 - }
239 - }
240 -
241 - //country code didn't match, there is nothing we can do but check if its valid in any of the provided countries
242 - foreach ( $this->valid_phonenumber_country_codes as $country_code=>$valid_numbers ) {
243 - if ( $this->check_number( $phone_number, $valid_numbers ) === true ) {
244 - return true;
245 - }
246 - }
247 -
248 - return false;
249 - }
250 -
251 - /**
252 - * Check if phone number is international format
253 - *
254 - * This function could be improved. Either by using https://github.com/google/libphonenumber or creating our own library
255 - *
256 - * @since 3.0.4
257 - */
258 - public function check_international_number( $number ) {
259 -
260 - //https://en.wikipedia.org/wiki/E.164 (Solomon islands has 8)
261 - $min = 8;
262 - $max = 15;
263 -
264 - //without going into any more detail we are just going to check if the number starts with a + and has a minimum amount of numbers and a maximum
265 - if ( substr( $number, 0, strlen( '+' ) ) && strlen( $number ) >= ( $min + 1 ) && strlen( $number ) <= ( $max + 1 ) ) {
266 - return true;
267 - }
268 -
269 - return false;
270 - }
271 -
272 - /**
273 - * Check if number is valid according to an array
274 - *
275 - * @since 3.0.4
276 - */
277 - public function check_number( $number, $valid_numbers ) {
278 -
279 - foreach ( $valid_numbers as $start_digits=>$lenghts ) {
280 - foreach ( $lenghts as $lenght ) {
281 - // If the starting digit(s) matches OR it is '*' for any starting number
282 - // AND the length matches
283 - if ( ( substr( $number, 0, strlen( $start_digits ) ) == $start_digits || $start_digits === '*' ) && strlen( $number ) == $lenght ) {
284 - return true;
285 - }
286 - }
287 - }
288 -
289 - return false;
290 - }
291 -
292 - /**
293 72 * Label translation override for WPML (woocommerce_package_rates)
294 73 *
295 74 * @since 3.0.0
296 75 */
@@ -336,9 +115,9 @@
336 115 * Sets default settings used by all shipping methods
337 116 *
338 117 * @since 3.0.0
339 118 */
340 - final public function set_default_settings() {
119 + final private function set_default_settings() {
341 120
342 121 $this->enable = !empty( $this->settings['active'] ) ? $this->settings['active'] : null;
343 122 $this->availability = 'specific';
344 123 $this->order_country = 'unknown';
@@ -381,24 +160,12 @@
381 160 public function calculate_shipping( $package = array() ) {
382 161
383 162 $price = isset( $this->instance_settings['price'] ) ? $this->instance_settings['price'] : ( isset( $this->settings['price_'.$package['destination']['country']] ) ? $this->settings['price_'.$package['destination']['country']] : 0 );
384 163
385 - $rate = array(
386 - 'id' => $this->get_rate_id(),
387 - 'label' => $this->title,
388 - 'cost' => $price,
389 - 'package' => $package,
390 - 'calc_tax' => 'per_order',
391 - );
392 -
393 - //check shippingclasses
394 - $rate = $this->calculate_shipping_class_price( $rate, $package );
395 -
396 - //check all free shipping options:
397 164 $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount'];
398 165
399 - if ( $free_shipping_min_amount && $package['cart_subtotal'] >= $free_shipping_min_amount ) {
400 - $rate["cost"] = 0;
166 + if ( $free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount ) {
167 + $price = 0;
401 168 }
402 169
403 170 //check only for parcelmachines
404 171 if ( $this->type == "apt" ) {
@@ -404,13 +171,14 @@
404 171 if ( $this->type == "apt" ) {
405 172
406 173 $free_shipping = true;
407 174 foreach ( $package['contents'] as $line ) {
175 +
408 176 $free_shipping = get_post_meta( $line['product_id'], '_no_shipping_cost', true ) === 'yes' ? $free_shipping : false;
409 177 }
410 178
411 179 if ( $free_shipping ) {
412 - $rate["cost"] = 0;
180 + $price = 0;
413 181 }
414 182 }
415 183
416 184 //check if there is a free shipping coupon (if it's free then allow free shipping)
@@ -418,17 +186,24 @@
418 186 foreach ( $package['applied_coupons'] as $coupon_code ) {
419 187
420 188 $coupon = new \WC_Coupon( $coupon_code );
421 189 if ( $coupon->get_free_shipping() === true ) {
422 - $rate["cost"] = 0;
190 + $price = 0;
423 191 break;
424 192 }
425 193 }
426 194 }
427 -
195 +
196 + $rate = array(
197 + 'id' => $this->get_rate_id(),
198 + 'label' => $this->title,
199 + 'cost' => $price,
200 + 'package' => $package,
201 + 'calc_tax' => 'per_order',
202 + );
428 203 $this->add_rate( $rate );
429 204 }
430 -
205 +
431 206 /**
432 207 * This function overrides shipping method title
433 208 * if you have set language specific titles in admin.
434 209 *
@@ -433,9 +208,9 @@
433 208 * if you have set language specific titles in admin.
434 209 *
435 210 * @since 3.0.0
436 211 */
437 - final public function override_title() {
212 + final private function override_title() {
438 213
439 214 $language_code = \MakeCommerce\i18n::get_two_char_locale();
440 215
441 216 //default (no language)
@@ -458,19 +233,16 @@
458 233
459 234 //Initialize instance form fields first
460 235 $this->initialize_instance_form_fields();
461 236
462 - if ( isset ( $_GET['section'] ) && $_GET['section'] == $this->id ) {
463 -
464 - //Initialize basic form fields
465 - $this->initialize_basic_form_fields();
237 + //Initialize basic form fields
238 + $this->initialize_basic_form_fields();
466 239
467 - //Initialize method specific form fields
468 - $this->initialize_method_type_form_fields();
240 + //Initialize method specific form fields
241 + $this->initialize_method_type_form_fields();
469 242
470 - //Initialize return address fields
471 - $this->initialize_return_address_form_fields();
472 - }
243 + //Initialize return address fields
244 + $this->initalize_return_addres_form_fields();
473 245 }
474 246
475 247 /**
476 248 * Initializes basic shared form fields
@@ -475,11 +247,11 @@
475 247 /**
476 248 * Initializes basic shared form fields
477 249 * Used by all shipping methods
478 250 *
479 - * @since 3.0.0
251 + * since 3.0.0
480 252 */
481 - final public function initialize_basic_form_fields() {
253 + final private function initialize_basic_form_fields() {
482 254
483 255 $this->form_fields = array();
484 256
485 257 $this->form_fields['logo'] = array(
@@ -518,9 +290,9 @@
518 290
519 291 $this->form_fields['method_name'] = array(
520 292 'title' => __( 'Shipping Method Title', 'wc_makecommerce_domain' ),
521 293 'type' => 'text',
522 - 'default' => $this->carrier_title . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() )
294 + 'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() )
523 295 );
524 296 } else {
525 297 foreach ( $languages as $language_code => $language ) {
526 298 $this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array(
@@ -525,9 +297,9 @@
525 297 foreach ( $languages as $language_code => $language ) {
526 298 $this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array(
527 299 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
528 300 'type' => 'text',
529 - 'default' => $this->carrier_title . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) )
301 + 'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) )
530 302 );
531 303 }
532 304 }
533 305
@@ -540,9 +312,9 @@
540 312 }
541 313
542 314 $this->form_fields['api_access'] = array(
543 315 'type' => 'title',
544 - 'title' => '<hr><br>'.__( 'API access for', 'wc_makecommerce_domain' ).' '.$this->carrier,
316 + 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext,
545 317 'description' => sprintf(__('You can automatically create shipments into %s system and print the out the package labels right here, at the shop orders view. <br> Please set your %s web services account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain' ), $this->carrier, $this->carrier, admin_url( $a ) )
546 318 );
547 319 }
548 320
@@ -551,9 +323,9 @@
551 323 * Automatically called by \WC_Shipping_Method
552 324 *
553 325 * @since 3.0.0
554 326 */
555 - final public function initialize_instance_form_fields() {
327 + final private function initialize_instance_form_fields() {
556 328
557 329 $this->instance_form_fields = array();
558 330
559 331 $this->instance_form_fields['logo'] = array(
@@ -579,10 +351,8 @@
579 351 'type' => 'checkbox',
580 352 'default' => 'no',
581 353 'desc_tip' => __( 'Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain' ),
582 354 );
583 -
584 - $this->instance_form_fields = $this->add_form_fields( $this->instance_form_fields );
585 355 }
586 356
587 357 /**
588 358 * Initializes return address form fields
@@ -589,14 +359,14 @@
589 359 * Used by all shipping methods
590 360 *
591 361 * @since 3.0.0
592 362 */
593 - public function initialize_return_address_form_fields() {
363 + final private function initalize_return_addres_form_fields() {
594 364
595 365 $this->form_fields['return_address'] = array(
596 366 'type' => 'title',
597 367 'title' => __( 'Return address', 'wc_makecommerce_domain' ),
598 - 'description' => sprintf( __( 'Please define return address for %s shipments.<br><b>All fields are required.</b>', 'wc_makecommerce_domain' ), $this->carrier )
368 + 'description' => __( 'Please define return address for Omniva shipments', 'wc_makecommerce_domain' )
599 369 );
600 370
601 371 $this->form_fields['shop_name'] = array(
602 372 'type' => 'text',
@@ -605,9 +375,9 @@
605 375 );
606 376
607 377 $this->form_fields['shop_phone'] = array(
608 378 'type' => 'text',
609 - 'title' => __( 'Shop phone (mobile)', 'wc_makecommerce_domain' ),
379 + 'title' => __( 'Shop phone', 'wc_makecommerce_domain' ),
610 380 'class' => 'input-text regular-input',
611 381 );
612 382
613 383 $this->form_fields['shop_email'] = array(
@@ -630,73 +400,24 @@
630 400 $this->form_fields['shop_address_city'] = array(
631 401 'type' => 'text',
632 402 'title' => __( 'Shop address city', 'wc_makecommerce_domain' ),
633 403 'class' => 'input-text regular-input',
634 - );
404 + );
635 405
636 - $this->form_fields['shop_postal_code'] = array(
637 - 'type' => 'text',
638 - 'title' => __( 'Shop postal code', 'wc_makecommerce_domain' ),
639 - 'class' => 'input-text regular-input',
640 - );
641 -
642 406 $this->form_fields['shop_address_street'] = array(
643 407 'type' => 'text',
644 408 'title' => __( 'Shop address street', 'wc_makecommerce_domain' ),
645 409 'class' => 'input-text regular-input',
646 410 );
647 - }
648 -
649 - /**
650 - * Initializes method type specific form fields
651 - * Loads method specific fields
652 - *
653 - * @since 3.0.0
654 - */
655 - public function initialize_method_type_form_fields() {
656 -
657 - //Initialize method specific fields
658 - $this->initialize_method_form_fields();
659 -
660 - $this->form_fields['service_user'] = array(
661 - 'title' => sprintf( __( '%s web services username', 'wc_makecommerce_domain' ), $this->service_name ),
662 - 'type' => 'text',
663 - 'default' => ''
664 - );
665 411
666 - $this->form_fields['service_password'] = array(
667 - 'title' => sprintf( __( '%s web services password', 'wc_makecommerce_domain' ), $this->service_name ),
668 - 'type' => 'text',
669 - 'default' => ''
412 + $this->form_fields['shop_postal_code'] = array(
413 + 'type' => 'text',
414 + 'title' => __( 'Shop postal code', 'wc_makecommerce_domain' ),
415 + 'class' => 'input-text regular-input',
670 416 );
671 417 }
672 418
673 419 /**
674 - * Checks if an object of a class already has the filter initialized
675 - * Returns true if it is added, false if it is not
676 - *
677 - * @since 3.5.0
678 - */
679 - public function check_filter_hook_initialization( $filter, $priority = 10 ) {
680 -
681 - global $wp_filter;
682 - $hook = $wp_filter[ $filter ] ?? [];
683 -
684 - if ( empty( $hook ) ) {
685 - return false;
686 - }
687 -
688 - // Check if the object instance is already registered as a callback
689 - foreach ( $hook->callbacks[$priority] ?? [] as $callback ) {
690 - if ( is_array($callback['function'] ) && $callback['function'][0]->id === $this->id ) {
691 - return true;
692 - }
693 - }
694 -
695 - return false;
696 - }
697 -
698 - /**
699 420 * Initializes method type, required function for all method types
700 421 *
701 422 * @since 3.0.0
702 423 */
@@ -702,17 +423,17 @@
702 423 */
703 424 abstract public function initialize();
704 425
705 426 /**
706 - * Sets title of the method
427 + * Initializes method type form fields
707 428 *
708 429 * @since 3.0.0
709 430 */
710 - abstract public function return_method_title();
431 + abstract public function initialize_method_type_form_fields();
711 432
712 433 /**
713 - * Returns phone validation error
434 + * Sets title of the method
714 435 *
715 - * @since 3.0.4
436 + * @since 3.0.0
716 437 */
717 - abstract public function phone_number_validation_error();
718 -}
438 + abstract public function return_method_title();
439 +}