| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Rest_API\Base_Info file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Rest_API |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Rest_API; |
| 9 |
|
| 10 |
use PostNLWooCommerce\Shipping_Method\Settings; |
| 11 |
use PostNLWooCommerce\Utils; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Base_Info |
| 19 |
* |
| 20 |
* @package PostNLWooCommerce\Rest_API |
| 21 |
*/ |
| 22 |
abstract class Base_Info { |
| 23 |
/** |
| 24 |
* API args. |
| 25 |
* |
| 26 |
* @var api_args |
| 27 |
*/ |
| 28 |
protected $api_args; |
| 29 |
|
| 30 |
/** |
| 31 |
* Settings class instance. |
| 32 |
* |
| 33 |
* @var PostNLWooCommerce\Shipping_Method\Settings |
| 34 |
*/ |
| 35 |
protected $settings; |
| 36 |
|
| 37 |
/** |
| 38 |
* Class constructor. |
| 39 |
* |
| 40 |
* @param array $post_data Set of API arguments. |
| 41 |
*/ |
| 42 |
public function __construct( $post_data ) { |
| 43 |
$this->settings = Settings::get_instance(); |
| 44 |
|
| 45 |
$this->convert_data_to_args( $post_data ); |
| 46 |
$this->set_store_address_data(); |
| 47 |
$this->set_settings_data(); |
| 48 |
$this->set_extra_data_to_api_args(); |
| 49 |
$this->parse_args(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parses the arguments and sets the instance's properties. |
| 54 |
* |
| 55 |
* @throws Exception If some data in $args did not pass validation. |
| 56 |
*/ |
| 57 |
abstract protected function parse_args(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Method to convert the post data to API args. |
| 61 |
* |
| 62 |
* @param Array $post_data Data from post variable in checkout page. |
| 63 |
*/ |
| 64 |
abstract public function convert_data_to_args( $post_data ); |
| 65 |
|
| 66 |
/** |
| 67 |
* Set API args with store address data from WC settings. |
| 68 |
*/ |
| 69 |
public function set_store_address_data() { |
| 70 |
$this->api_args['store_address'] = array( |
| 71 |
'company' => $this->settings->get_return_company_name(), |
| 72 |
'email' => get_bloginfo( 'admin_email' ), |
| 73 |
'address_1' => WC()->countries->get_base_address(), |
| 74 |
'address_2' => WC()->countries->get_base_address_2(), |
| 75 |
'city' => WC()->countries->get_base_city(), |
| 76 |
'state' => WC()->countries->get_base_state(), |
| 77 |
'country' => WC()->countries->get_base_country(), |
| 78 |
'postcode' => WC()->countries->get_base_postcode(), |
| 79 |
'shipping_confirmation_email' => $this->settings->get_shipping_confirmation_email(), |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Set API args with data from the shipping settings. |
| 85 |
*/ |
| 86 |
public function set_settings_data() { |
| 87 |
$is_return_to_home_enabled = $this->settings->is_return_to_home_enabled(); |
| 88 |
$this->api_args['settings'] = array( |
| 89 |
'location_code' => $this->settings->get_location_code(), |
| 90 |
'customer_code' => $this->settings->get_customer_code(), |
| 91 |
'customer_num' => $this->settings->get_customer_num(), |
| 92 |
'cut_off_time' => $this->settings->get_cut_off_time(), |
| 93 |
'dropoff_days' => $this->settings->get_dropoff_days(), |
| 94 |
'excluded_dropoff_days' => $this->settings->get_excluded_dropoff_days(), |
| 95 |
'pickup_points_enabled' => $this->settings->is_pickup_points_enabled(), |
| 96 |
'delivery_days_enabled' => true, |
| 97 |
'morning_delivery_enabled' => $this->settings->is_morning_delivery_enabled(), |
| 98 |
'evening_delivery_enabled' => $this->settings->is_evening_delivery_enabled(), |
| 99 |
'transit_time' => $this->settings->get_transit_time(), |
| 100 |
/* Temporarily hardcoded in Settings::get_number_pickup_points(). */ |
| 101 |
'number_pickup_points' => $this->settings->get_number_pickup_points(), |
| 102 |
'number_delivery_days' => $this->settings->get_number_delivery_days(), |
| 103 |
'return_company' => $this->settings->get_return_company_name(), |
| 104 |
'is_return_to_home_enabled' => $is_return_to_home_enabled, |
| 105 |
'return_replynumber' => $this->settings->get_return_reply_number(), |
| 106 |
'return_address_street' => $this->settings->get_return_address_street(), |
| 107 |
'return_address_house_no' => $this->settings->get_return_address_house_no(), |
| 108 |
'return_address_house_noext' => $this->settings->get_return_address_house_noext(), |
| 109 |
'return_address_city' => $is_return_to_home_enabled ? $this->settings->get_return_city() : $this->settings->get_freepost_city(), |
| 110 |
'return_address_zip' => $is_return_to_home_enabled ? $this->settings->get_return_zipcode() : $this->settings->get_freepost_zipcode(), |
| 111 |
'return_customer_code' => $this->settings->get_return_customer_code(), |
| 112 |
'globalpack_barcode_type' => $this->settings->get_globalpack_barcode_type(), |
| 113 |
'globalpack_customer_code' => $this->settings->get_globalpack_customer_code(), |
| 114 |
'hs_tariff_code' => $this->settings->get_hs_tariff_code(), |
| 115 |
'country_origin' => $this->settings->get_country_origin(), |
| 116 |
'printer_type' => $this->settings->get_printer_type(), |
| 117 |
'label_format' => $this->settings->get_label_format(), |
| 118 |
'woocommerce_email' => $this->settings->is_woocommerce_email_enabled(), |
| 119 |
'woocommerce_email_text' => $this->settings->get_woocommerce_email_text(), |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set extra API args. |
| 125 |
*/ |
| 126 |
public function set_extra_data_to_api_args() { |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Retrieves the args scheme to use with for parsing shipping address info. |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
protected function get_receiver_info_schema() { |
| 135 |
// Closures in PHP 5.3 do not inherit class context |
| 136 |
// So we need to copy $this into a lexical variable and pass it to closures manually. |
| 137 |
$self = $this; |
| 138 |
|
| 139 |
return array( |
| 140 |
'first_name' => array( |
| 141 |
'error' => __( 'Shipping "First Name" is empty!', 'postnl-for-woocommerce' ), |
| 142 |
'validate' => function ( $name ) use ( $self ) { |
| 143 |
if ( empty( $name ) ) { |
| 144 |
throw new \Exception( |
| 145 |
__( 'Shipping "First Name" is empty!', 'postnl-for-woocommerce' ) |
| 146 |
); |
| 147 |
} |
| 148 |
}, |
| 149 |
'sanitize' => function ( $name ) use ( $self ) { |
| 150 |
return $self->string_length_sanitization( $name, 50 ); |
| 151 |
}, |
| 152 |
), |
| 153 |
'last_name' => array( |
| 154 |
'default' => '', |
| 155 |
'sanitize' => function ( $name ) use ( $self ) { |
| 156 |
return $self->string_length_sanitization( $name, 50 ); |
| 157 |
}, |
| 158 |
), |
| 159 |
'company' => array( |
| 160 |
'default' => '', |
| 161 |
), |
| 162 |
'address_1' => array( |
| 163 |
'error' => __( 'Shipping "Address 1" is empty!', 'postnl-for-woocommerce' ), |
| 164 |
'validate' => function ( $name ) use ( $self ) { |
| 165 |
if ( empty( $name ) ) { |
| 166 |
throw new \Exception( |
| 167 |
__( 'Shipping "Address 1" is empty!', 'postnl-for-woocommerce' ) |
| 168 |
); |
| 169 |
} |
| 170 |
}, |
| 171 |
'sanitize' => function ( $name ) use ( $self ) { |
| 172 |
return $self->string_length_sanitization( $name, 50 ); |
| 173 |
}, |
| 174 |
), |
| 175 |
'address_2' => array( |
| 176 |
'default' => '', |
| 177 |
), |
| 178 |
'city' => array( |
| 179 |
'error' => __( 'Shipping "City" is empty!', 'postnl-for-woocommerce' ), |
| 180 |
), |
| 181 |
'postcode' => array( |
| 182 |
'error' => __( 'Shipping "Postcode" is empty!', 'postnl-for-woocommerce' ), |
| 183 |
'sanitize' => function ( $value ) use ( $self ) { |
| 184 |
return str_replace( ' ', '', $value ); |
| 185 |
}, |
| 186 |
), |
| 187 |
'state' => array( |
| 188 |
'default' => '', |
| 189 |
), |
| 190 |
'country' => array( |
| 191 |
'error' => __( 'Shipping "Country" is empty!', 'postnl-for-woocommerce' ), |
| 192 |
), |
| 193 |
'house_number' => array( |
| 194 |
'default' => '', |
| 195 |
), |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Retrieves the args scheme to use with for parsing store address info. |
| 201 |
* |
| 202 |
* @return array |
| 203 |
*/ |
| 204 |
protected function get_store_info_schema() { |
| 205 |
// Closures in PHP 5.3 do not inherit class context |
| 206 |
// So we need to copy $this into a lexical variable and pass it to closures manually. |
| 207 |
$self = $this; |
| 208 |
|
| 209 |
return array( |
| 210 |
'company' => array( |
| 211 |
'default' => '', |
| 212 |
'sanitize' => function ( $name ) use ( $self ) { |
| 213 |
return htmlspecialchars_decode( $name ); |
| 214 |
}, |
| 215 |
), |
| 216 |
'email' => array( |
| 217 |
'default' => '', |
| 218 |
), |
| 219 |
'address_1' => array( |
| 220 |
'error' => __( 'Store address 1 is not configured!', 'postnl-for-woocommerce' ), |
| 221 |
'validate' => function ( $address_1 ) use ( $self ) { |
| 222 |
if ( empty( $address_1 ) ) { |
| 223 |
throw new \Exception( |
| 224 |
__( 'Store address 1 is not configured!', 'postnl-for-woocommerce' ) |
| 225 |
); |
| 226 |
} |
| 227 |
}, |
| 228 |
'sanitize' => function ( $address_1 ) use ( $self ) { |
| 229 |
return $self->string_length_sanitization( $address_1, 50 ); |
| 230 |
}, |
| 231 |
), |
| 232 |
'address_2' => array( |
| 233 |
'default' => '', |
| 234 |
), |
| 235 |
'city' => array( |
| 236 |
'error' => __( 'Store city is not configured!', 'postnl-for-woocommerce' ), |
| 237 |
), |
| 238 |
'postcode' => array( |
| 239 |
'error' => __( 'Store postcode is not configured!', 'postnl-for-woocommerce' ), |
| 240 |
'sanitize' => function ( $value ) use ( $self ) { |
| 241 |
$value = str_replace( ' ', '', $value ); |
| 242 |
|
| 243 |
return $self->string_length_sanitization( $value, 7 ); |
| 244 |
}, |
| 245 |
), |
| 246 |
'state' => array( |
| 247 |
'default' => '', |
| 248 |
), |
| 249 |
'country' => array( |
| 250 |
'error' => __( 'Store country is not configured!', 'postnl-for-woocommerce' ), |
| 251 |
), |
| 252 |
'shipping_confirmation_email' => array( |
| 253 |
'default' => '', |
| 254 |
), |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Sanitization for float value. |
| 260 |
* |
| 261 |
* @param Float $float Float value. |
| 262 |
* @param Int $numcomma Number of character after the comma. |
| 263 |
* |
| 264 |
* @return Float |
| 265 |
*/ |
| 266 |
protected function float_round_sanitization( $float, $numcomma ) { |
| 267 |
$float = floatval( $float ); |
| 268 |
|
| 269 |
return round( $float, $numcomma ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Converts a given weight into grams, if necessary. |
| 274 |
* |
| 275 |
* @param float $weight The weight amount. |
| 276 |
* @param string $uom The unit of measurement of the $weight parameter. |
| 277 |
* |
| 278 |
* @return float The potentially converted weight. |
| 279 |
*/ |
| 280 |
protected function maybe_convert_to_grams( $weight, $uom ) { |
| 281 |
return round( wc_get_weight( $weight, Utils::used_api_uom(), $uom ) ); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Get cutoff times value from the settings. |
| 286 |
* |
| 287 |
* @return String |
| 288 |
*/ |
| 289 |
public function get_current_time() { |
| 290 |
return current_time( 'd-m-Y H:i:s' ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Sanitization for string length. |
| 295 |
* |
| 296 |
* @param String $string String value. |
| 297 |
* @param Int $max Maximum of character. |
| 298 |
* |
| 299 |
* @return String |
| 300 |
*/ |
| 301 |
protected function string_length_sanitization( $string, $max ) { |
| 302 |
$max = intval( $max ); |
| 303 |
|
| 304 |
if ( strlen( $string ) <= $max ) { |
| 305 |
return $string; |
| 306 |
} |
| 307 |
|
| 308 |
return substr( $string, 0, ( $max - 1 ) ); |
| 309 |
} |
| 310 |
} |
| 311 |
|