2015-12-18 14:24:34 +00:00
< ? php
2018-03-07 18:30:15 +00:00
/**
* Class WC_Shipping_Legacy_Local_Pickup file .
*
* @ package WooCommerce\Shipping
*/
2015-12-18 14:24:34 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2018-03-07 18:30:15 +00:00
exit ; // Exit if accessed directly.
2015-12-18 14:24:34 +00:00
}
/**
* Local Pickup Shipping Method .
*
2017-07-17 10:10:52 +00:00
* This class is here for backwards compatibility for methods existing before zones existed .
2015-12-18 14:24:34 +00:00
*
* @ deprecated 2.6 . 0
2018-03-07 18:04:56 +00:00
* @ version 2.3 . 0
* @ package WooCommerce / Classes / Shipping
2015-12-18 14:24:34 +00:00
*/
class WC_Shipping_Legacy_Local_Pickup extends WC_Shipping_Method {
/**
* Constructor .
*/
public function __construct () {
2018-03-07 18:30:15 +00:00
$this -> id = 'legacy_local_pickup' ;
$this -> method_title = __ ( 'Local pickup (legacy)' , 'woocommerce' );
/* translators: %s: Admin shipping settings URL */
2016-10-24 23:56:38 +00:00
$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>' ;
2015-12-18 14:24:34 +00:00
$this -> init ();
}
2016-04-28 11:36:49 +00:00
/**
* Process and redirect if disabled .
*/
public function process_admin_options () {
parent :: process_admin_options ();
2016-08-27 03:23:21 +00:00
if ( 'no' === $this -> settings [ 'enabled' ] ) {
2016-04-28 11:36:49 +00:00
wp_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=shipping§ion=options' ) );
exit ;
}
}
2016-08-27 04:01:22 +00:00
2015-12-18 14:24:34 +00:00
/**
* Return the name of the option in the WP DB .
2018-03-07 18:04:56 +00:00
*
2015-12-18 14:24:34 +00:00
* @ since 2.6 . 0
* @ return string
*/
public function get_option_key () {
2018-03-07 18:30:15 +00:00
return $this -> plugin_id . 'local_pickup_settings' ;
2015-12-18 14:24:34 +00:00
}
/**
2018-03-07 18:30:15 +00:00
* Init function .
2015-12-18 14:24:34 +00:00
*/
public function init () {
// Load the settings.
$this -> init_form_fields ();
$this -> init_settings ();
2018-03-07 18:30:15 +00:00
// Define user set variables.
2018-03-07 18:04:56 +00:00
$this -> enabled = $this -> get_option ( 'enabled' );
$this -> title = $this -> get_option ( 'title' );
$this -> codes = $this -> get_option ( 'codes' );
$this -> availability = $this -> get_option ( 'availability' );
$this -> countries = $this -> get_option ( 'countries' );
2015-12-18 14:24:34 +00:00
2018-03-07 18:30:15 +00:00
// Actions.
2015-12-18 14:24:34 +00:00
add_action ( 'woocommerce_update_options_shipping_' . $this -> id , array ( $this , 'process_admin_options' ) );
}
/**
2018-03-07 18:30:15 +00:00
* Calculate shipping .
*
* @ param array $package Package information .
2015-12-18 14:24:34 +00:00
*/
2015-12-18 17:10:58 +00:00
public function calculate_shipping ( $package = array () ) {
2015-12-18 14:24:34 +00:00
$rate = array (
2018-03-07 18:04:56 +00:00
'id' => $this -> id ,
'label' => $this -> title ,
'package' => $package ,
2015-12-18 14:24:34 +00:00
);
$this -> add_rate ( $rate );
}
/**
2018-03-07 18:30:15 +00:00
* Initialize form fields .
2015-12-18 14:24:34 +00:00
*/
public function init_form_fields () {
$this -> form_fields = array (
2018-03-07 18:04:56 +00:00
'enabled' => array (
2015-12-18 14:24:34 +00:00
'title' => __ ( 'Enable' , 'woocommerce' ),
'type' => 'checkbox' ,
2016-01-08 12:01:01 +00:00
'label' => __ ( 'Once disabled, this legacy method will no longer be available.' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
'default' => 'no' ,
2015-12-18 14:24:34 +00:00
),
2018-03-07 18:04:56 +00:00
'title' => array (
2015-12-18 14:24:34 +00:00
'title' => __ ( 'Title' , 'woocommerce' ),
'type' => 'text' ,
'description' => __ ( 'This controls the title which the user sees during checkout.' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'default' => __ ( 'Local pickup' , 'woocommerce' ),
2015-12-18 14:24:34 +00:00
'desc_tip' => true ,
),
2018-03-07 18:04:56 +00:00
'codes' => array (
2016-10-12 10:16:30 +00:00
'title' => __ ( 'Allowed ZIP/post codes' , 'woocommerce' ),
2015-12-18 14:24:34 +00:00
'type' => 'text' ,
'desc_tip' => __ ( 'What ZIP/post codes are available for local pickup?' , 'woocommerce' ),
'default' => '' ,
'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' ),
2016-08-27 01:46:45 +00:00
'placeholder' => 'e.g. 12345, 56789' ,
2015-12-18 14:24:34 +00:00
),
'availability' => array (
2018-03-07 18:04:56 +00:00
'title' => __ ( 'Method availability' , 'woocommerce' ),
'type' => 'select' ,
'default' => 'all' ,
'class' => 'availability wc-enhanced-select' ,
'options' => array (
2015-12-18 14:24:34 +00:00
'all' => __ ( 'All allowed countries' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'specific' => __ ( 'Specific countries' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
),
2015-12-18 14:24:34 +00:00
),
2018-03-07 18:04:56 +00:00
'countries' => array (
2016-10-12 10:16:30 +00:00
'title' => __ ( 'Specific countries' , 'woocommerce' ),
2015-12-18 14:24:34 +00:00
'type' => 'multiselect' ,
'class' => 'wc-enhanced-select' ,
2016-12-21 11:36:48 +00:00
'css' => 'width: 400px;' ,
2015-12-18 14:24:34 +00:00
'default' => '' ,
'options' => WC () -> countries -> get_shipping_countries (),
'custom_attributes' => array (
2018-03-07 18:04:56 +00:00
'data-placeholder' => __ ( 'Select some countries' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
),
),
2015-12-18 14:24:34 +00:00
);
}
/**
* Get postcodes for this method .
2018-03-07 18:04:56 +00:00
*
2015-12-18 14:24:34 +00:00
* @ return array
*/
public function get_valid_postcodes () {
$codes = array ();
2016-09-09 00:14:28 +00:00
if ( '' !== $this -> codes ) {
2016-08-27 04:23:02 +00:00
foreach ( explode ( ',' , $this -> codes ) as $code ) {
2015-12-18 14:24:34 +00:00
$codes [] = strtoupper ( trim ( $code ) );
}
}
return $codes ;
}
/**
* See if a given postcode matches valid postcodes .
2018-03-07 18:04:56 +00:00
*
2018-03-07 18:30:15 +00:00
* @ param string $postcode Postcode to check .
* @ param string $country code Code of the country to check postcode against .
2015-12-18 14:24:34 +00:00
* @ return boolean
*/
public function is_valid_postcode ( $postcode , $country ) {
$codes = $this -> get_valid_postcodes ();
$postcode = $this -> clean ( $postcode );
$formatted_postcode = wc_format_postcode ( $postcode , $country );
2018-03-07 18:30:15 +00:00
if ( in_array ( $postcode , $codes , true ) || in_array ( $formatted_postcode , $codes , true ) ) {
2015-12-18 14:24:34 +00:00
return true ;
}
2018-03-07 18:30:15 +00:00
// Pattern matching.
2015-12-18 14:24:34 +00:00
foreach ( $codes as $c ) {
$pattern = '/^' . str_replace ( '_' , '[0-9a-zA-Z]' , preg_quote ( $c ) ) . '$/i' ;
if ( preg_match ( $pattern , $postcode ) ) {
return true ;
}
}
2018-03-07 18:30:15 +00:00
// Wildcard search.
2015-12-18 14:24:34 +00:00
$wildcard_postcode = $formatted_postcode . '*' ;
$postcode_length = strlen ( $formatted_postcode );
for ( $i = 0 ; $i < $postcode_length ; $i ++ ) {
2018-03-07 18:30:15 +00:00
if ( in_array ( $wildcard_postcode , $codes , true ) ) {
2015-12-18 14:24:34 +00:00
return true ;
}
$wildcard_postcode = substr ( $wildcard_postcode , 0 , - 2 ) . '*' ;
}
return false ;
}
/**
* See if the method is available .
*
2018-03-07 18:30:15 +00:00
* @ param array $package Package information .
2015-12-18 14:24:34 +00:00
* @ return bool
*/
public function is_available ( $package ) {
2018-03-07 18:04:56 +00:00
$is_available = 'yes' === $this -> enabled ;
2015-12-18 14:24:34 +00:00
if ( $is_available && $this -> get_valid_postcodes () ) {
$is_available = $this -> is_valid_postcode ( $package [ 'destination' ][ 'postcode' ], $package [ 'destination' ][ 'country' ] );
}
if ( $is_available ) {
2016-09-09 00:14:28 +00:00
if ( 'specific' === $this -> availability ) {
2015-12-18 14:24:34 +00:00
$ship_to_countries = $this -> countries ;
} else {
$ship_to_countries = array_keys ( WC () -> countries -> get_shipping_countries () );
}
2018-03-07 18:30:15 +00:00
if ( is_array ( $ship_to_countries ) && ! in_array ( $package [ 'destination' ][ 'country' ], $ship_to_countries , true ) ) {
2015-12-18 14:24:34 +00:00
$is_available = false ;
}
}
2017-08-11 20:36:18 +00:00
return apply_filters ( 'woocommerce_shipping_' . $this -> id . '_is_available' , $is_available , $package , $this );
2015-12-18 14:24:34 +00:00
}
/**
2018-03-07 18:30:15 +00:00
* Clean function .
2015-12-18 14:24:34 +00:00
*
* @ access public
2018-03-07 18:30:15 +00:00
* @ param mixed $code Code .
2015-12-18 14:24:34 +00:00
* @ return string
*/
public function clean ( $code ) {
return str_replace ( '-' , '' , sanitize_title ( $code ) ) . ( strstr ( $code , '*' ) ? '*' : '' );
}
}