クラス
RestRouteGuards
ソース ソース
ファイル: src/Routing/RestRouteGuards.php
class RestRouteGuards
{
/**
* Amazon Pay module object
*
* @var AmazonPay
*/
private $module;
/**
* Instantiates REST route guards
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param AmazonPay $module
*/
public function __construct(AmazonPay $module) {
$this->module = $module;
}
/**
* Return `GenericError` if any items in the cart are out of stock
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7dcaa1c1
*/
public function itemsInStockCheck() {
return function () {
global $usces;
$usces->error_message = $usces->zaiko_check();
/**
* Mirrored Welcart filter
*
* @ignore
*/
$usces->error_message = apply_filters('usces_filter_cart_check', $usces->error_message);
if (!\WCUtils::is_blank($usces->error_message)) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::USCES_ITEM_OUT_OF_STOCK,
[$usces->error_message],
[$usces->error_message]
);
}
};
}
/**
* Return `GenericError` if cart session has expired
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7dbce81b
*/
public function cartNotEmptyCheck() {
return function () {
global $usces;
if (empty($usces->cart->num_row())) {
return $this->module->errors->getErrorResponse(GenericErrorStore::USCES_CART_IS_EMPTY);
}
};
}
/**
* Return `GenericError` if any errors are returned by `usces_filter_delivery_check_custom_order`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#1e3a201d
*/
public function customOrderFieldsCheck() {
return function () {
if ($this->module->getActingOpts()['quickpay_custom_order_fields'] === 'on') {
$mes = '';
$mes = usces_filter_delivery_check_custom_order($mes);
if (!\WCUtils::is_blank($mes)) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::USCES_CUSTOM_FIELDS_ERR,
[$mes],
[$mes]
);
}
}
};
}
/**
* Return `GenericError` if any errors are returned by custom fields checks
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7dd9fc1a
*/
public function customFieldsCheck() {
return function () {
$opts = $this->module->getActingOpts();
$mes = '';
if (!usces_is_login()) {
$mes = usces_filter_customer_check_custom_customer($mes);
}
if ($opts['quickpay_custom_delivery_fields'] === 'on') {
$_POST['delivery']['delivery_flag'] = 1;
$mes = usces_filter_delivery_check_custom_delivery($mes);
}
if ($opts['quickpay_custom_order_fields'] === 'on') {
$mes = usces_filter_delivery_check_custom_order($mes);
}
if (!\WCUtils::is_blank($mes)) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::USCES_CUSTOM_FIELDS_ERR,
[$mes],
[$mes]
);
}
};
}
/**
* Performs validation checks on a selected delivery method and returns a
* `GenericError` instance if validation fails
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7dda1662
*/
public function deliveryMethodCheck() {
return function () {
global $usces, $usces_settings;
$mes = '';
if (!isset($_POST['offer']['delivery_method']) || (empty($_POST['offer']['delivery_method']) && !\WCUtils::is_zero($_POST['offer']['delivery_method']))) {
$mes .= __('chose one from delivery method.', 'usces') . '<br />';
} else {
$d_method_index = $usces->get_delivery_method_index((int)$_POST['offer']['delivery_method']);
if (0 > $d_method_index) {
$mes .= __('chose one from delivery method.', 'usces') . '<br />';
} else {
$country = $_SESSION['usces_entry']['delivery']['country'];
$WPLANG = get_option('WPLANG');
$wplang = !empty($WPLANG) ? $WPLANG : get_locale();
$locale = empty($wplang) ? 'en' : $wplang;
$local_country = array_key_exists($locale, $usces_settings['lungage2country']) ? $usces_settings['lungage2country'][$locale] : 'US';
if ($country == $local_country) {
if ($usces->options['delivery_method'][$d_method_index]['intl'] == 1) {
$mes .= __('Delivery method is incorrect. Can not specify an international flight.', 'usces') . '<br />';
}
} else {
if (\WCUtils::is_zero($usces->options['delivery_method'][$d_method_index]['intl'])) {
$mes .= __('Delivery method is incorrect. Specify the international flights.', 'usces') . '<br />';
}
}
}
}
if (!\WCUtils::is_blank($mes)) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::USCES_DELIVERY_METHOD_INVALID,
[$mes],
[$mes]
);
}
};
}
/**
* If the country of the selected delivery address is Japan and the prefecture is
* invalid, a `GenericError` instance will be returned.
*
* A `GenericError` instance will also be returned if the address country is not a
* target market country
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#d40ab5f
*/
public function addressCheck() {
return function () {
global $usces;
$delivery = $_SESSION['usces_entry']['delivery'];
if ($delivery['country'] === 'JP') {
$pref = $delivery['pref'];
if (!in_array($pref, Constants::PREFECTURES, true)) {
return $this->module->errors->getErrorResponse(GenericErrorStore::USCES_DELIVERY_PREF_INVALID);
}
}
if (!in_array($delivery['country'], $usces->options['system']['target_market'], true)) {
return $this->module->errors->getErrorResponse(GenericErrorStore::USCES_COUNTRY_NOT_TARGET_MARGET);
}
};
}
/**
* Validates `$_POST['customer']` fields if ギフト発送 is checked
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Closure&#Function#7b5ce532
*/
public function giftShippingCustomerAddressCheck() {
return function () {
$giftShipping = isset($_REQUEST['giftShipping']) ? (bool)$_REQUEST['giftShipping'] : false;
if ($giftShipping === true) {
$mes = $this->validateCustomerAddressPayload();
if (!empty(trim($mes))) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::USCES_GIFT_SHIPPING_CUSTOMER_ADDRESS_INVALID,
[$mes],
[$mes]
);
}
}
};
}
/**
* Validates `$_POST['customer']` fields and returns a string of error messages if any values
* are not valid
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function validateCustomerAddressPayload() {
$mes = '';
if (WCUtils::is_blank($_POST['customer']['name1'])) {
$mes .= __('Name is not correct', 'usces') . '<br />';
}
if (WCUtils::is_blank($_POST['customer']['zipcode'])) {
if (usces_is_required_field('zipcode')) {
$mes .= __('postal code is not correct', 'usces') . '<br />';
}
} else {
if (!preg_match('/^[a-zA-Z0-9]+$/', $_POST['customer']['zipcode'])) {
$_SESSION['usces_entry']['customer']['zipcode'] = usces_convert_zipcode($_POST['customer']['zipcode']);
}
if (!preg_match('/^(([0-9]{3}-[0-9]{4})|([0-9]{7}))$/', $_SESSION['usces_entry']['customer']['zipcode'])) {
$mes .= __('postal code is not correct', 'usces') . '<br />';
}
}
if (($_POST['customer']['pref'] == __('-- Select --', 'usces') || $_POST['customer']['pref'] == '-- Select --') && usces_is_required_field('states')) {
$mes .= __('enter the prefecture', 'usces') . '<br />';
}
if (WCUtils::is_blank($_POST['customer']['address1']) && usces_is_required_field('address1')) {
$mes .= __('enter the city name', 'usces') . '<br />';
}
if (WCUtils::is_blank($_POST['customer']['address2']) && usces_is_required_field('address2')) {
$mes .= __('enter house numbers', 'usces') . '<br />';
}
if (WCUtils::is_blank($_POST['customer']['tel']) && usces_is_required_field('tel')) {
$mes .= __('enter phone numbers', 'usces') . '<br />';
}
if (!WCUtils::is_blank($_POST['customer']['tel']) && preg_match('/[^\d\-+]/', trim($_POST['customer']['tel'])) && usces_is_required_field('tel')) {
$mes .= __('Please input a phone number with a half size number.', 'usces') . '<br />';
}
return $mes;
}
}
- __construct — Instantiates REST route guards
- addressCheck — If the country of the selected delivery address is Japan and the prefecture is invalid, a `GenericError` instance will be returned.
- cartNotEmptyCheck — Return `GenericError` if cart session has expired
- customFieldsCheck — Return `GenericError` if any errors are returned by custom fields checks
- customOrderFieldsCheck — Return `GenericError` if any errors are returned by `usces_filter_delivery_check_custom_order`
- deliveryMethodCheck — Performs validation checks on a selected delivery method and returns a `GenericError` instance if validation fails
- giftShippingCustomerAddressCheck — Validates `$_POST['customer']` fields if ギフト発送 is checked
- itemsInStockCheck — Return `GenericError` if any items in the cart are out of stock
- validateCustomerAddressPayload — Validates `$_POST['customer']` fields and returns a string of error messages if any values are not valid