クラス
Validation
Contains methods that verify whether an item can be added to the wishlist or cart
ソース ソース
ファイル: src/API/Validation.php
class Validation { /** * Master object * * @var Master */ private $master; /** * Sets up `Validation` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param Master $master * @return void */ public function __construct(Master $master) { $this->master = $master; } /** * Returns `GenericError` instance if the user is not logged in * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return GenericError|void */ public function isLoggedIn() { if (!usces_is_login()) { return $this->master->estore->getErrorResponse(Store::NOT_LOGGED_IN); } } /** * Returns `GenericError` instance if the item has been deleted or put in the 'trash', * `true` otherwise * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @param int $post_id * @param string $sku Optional. This is only used for error message construction. * @return GenericError|true */ public function itemExists($post_id, $sku = '') { global $wpdb; $p = get_post($post_id); if ($p === null) { return $this->master->estore->getErrorResponse(Store::ITEM_DELETED, [$post_id], [$sku]); } $pstatus = $wpdb->get_var($wpdb->prepare("SELECT post_status FROM {$wpdb->posts} WHERE ID = %d", $post_id)); if ($pstatus === 'trash') { return $this->master->estore->getErrorResponse(Store::POST_IN_TRASH, [$post_id], [$sku]); } return true; } /** * Checks for dlseller cart restrictions and returns a descriptive error if the item is not allowed * to be added to cart * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @return GenericError|true */ public function cartAllowsThisItem($post_id) { global $usces; if (!defined('WCEX_DLSELLER')) { // don't worry about charge types if dlseller isn't active return true; } if (empty($usces->cart->num_row())) { return true; } $chargingtype = $usces->getItemChargingType($post_id); $chargingtype = !empty($chargingtype) ? strtolower($chargingtype) : ''; $carts = $usces->cart->get_cart(); $carts = !empty($carts) ? (array)$carts : []; // check if a continue charge type item already exists in cart $exists = false; foreach ($carts as $cart) { $cartitemct = $usces->getItemChargingType($cart['post_id']); $cartitemct = !empty($cartitemct) ? strtolower($cartitemct) : ''; if ($cartitemct === 'continue') { $exists = true; break; } } if ($exists === true) { if ($chargingtype === 'continue') { $emessage = __('You can add only one continuation charging item in your shopping cart.', 'dlseller'); return $this->master->estore->getErrorResponse( Store::CART_CONTAINS_CONTINUE_CHARGE, [$emessage], [$emessage] ); } $emessage = __('This is the continuation charging item. If you want to add this item, you have to clear your cart. Is it ok to clear your cart?', 'dlseller'); return $this->master->estore->getErrorResponse( Store::CART_CONTAINS_CONTINUE_CHARGE, [$emessage], [$emessage] ); } if ($chargingtype === 'continue') { $emessage = __( 'You have the continuation charging item in your shopping cart. If you want to add this item, you have to clear the item in your cart. Is it ok to clear your cart?', 'dlseller' ); return $this->master->estore->getErrorResponse( Store::CONTINUE_CHARGE_REQUIRES_EMPTY_CART, [$emessage], [$emessage] ); } return true; } /** * Returns `GenericError` instance if the given item and quantity prevents adding * to cart because of stock, `true` otherwise * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @param string $sku **MUST** be urlencoded * @param int $quant * @return GenericError|true */ public function inStock($post_id, $sku, $quant = 1) { global $usces; $quant = (int)$quant > 0 ? (int)$quant : 1; $sku_code = urldecode($sku); $stock = $usces->getItemZaikoNum($post_id, $sku_code); if (!isset($stocks[$post_id][$sku])) { if (!WCUtils::is_blank($stock)) { $stocks[$post_id][$sku] = $stock; } else { $stocks[$post_id][$sku] = null; } } $checkstock = $stocks[$post_id][$sku]; $stocks[$post_id][$sku] = $stocks[$post_id][$sku] - $quant; $itemRestriction = get_post_meta($post_id, '_itemRestriction', true); $itemOrderAcceptable = $usces->getItemOrderAcceptable($post_id); $post_status = get_post_status($post_id); if ( !$usces->is_item_zaiko($post_id, $sku_code) || ($itemOrderAcceptable != 1 && WCUtils::is_zero($stock)) || 'publish' != $post_status ) { return $this->master->estore->getErrorResponse(Store::ITEM_SOLD_OUT, [$sku], [$sku]); } if ( $quant > (int)$itemRestriction && !WCUtils::is_blank($itemRestriction) && !WCUtils::is_zero($itemRestriction) ) { return $this->master->estore->getErrorResponse( Store::PER_PURCHASE_LIMIT_EXCEEDED, [$itemRestriction, $sku], [$itemRestriction, $sku] ); } if ( $itemOrderAcceptable != 1 && 0 > $stocks[$post_id][$sku] && !WCUtils::is_blank($stock) ) { return $this->master->estore->getErrorResponse( Store::QUANT_EXCEEDS_STOCK_LIMIT, [$sku, $checkstock], [$sku, $checkstock] ); } return true; } /** * Validates option fields and returns a `GenericError` instance if any required options are * blank or if any input values are malformed. Returns `true` if all checks pass. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @param string $sku If using `wcex_widget_cart`, **MUST** be URL *decoded*. If not using * `wcex_widget_cart`, **MUST** be URL *encoded*. * * NOTE: the URL encoded SKU **MUST NOT** be encoded with `rawurlencode`. * Use `urlencode` instead. * @param array $item_option * @return GenericError|true */ public function requiredOptionsProvided($post_id, $sku, array $item_option) { global $usces; $errorsm = []; $ioptkeys = $usces->get_itemOptionKey($post_id, true); if ($ioptkeys) { foreach ($ioptkeys as $key => $value) { $optValues = $usces->get_itemOptions(urldecode($value), $post_id); if (0 == $optValues['means']) { // select if ($optValues['essential'] && '#NONE#' == $item_option[$post_id][$sku][$value]) { $errorsm[] = sprintf(__('Chose the %s', 'usces'), urldecode($value)); } } elseif (1 == $optValues['means']) { // multiselect if ($optValues['essential']) { $mselect = 0; foreach ((array)$item_option[$post_id][$sku][$value] as $mvalue) { if (!empty($mvalue) && '#NONE#' != $mvalue) { $mselect++; } } if ($mselect == 0) { $errorsm[] = sprintf(__('Chose the %s', 'usces'), urldecode($value)); } } } elseif (in_array($optValues['means'], [3, 4])) { // radio & checkbox if ($optValues['essential']) { if (!isset($item_option[$post_id][$sku][$value])) { $errorsm[] = sprintf(__('Chose the %s', 'usces'), urldecode($value)); } } } else { // text if ($optValues['essential'] && WCUtils::is_blank($item_option[$post_id][$sku][$value])) { $errorsm[] = sprintf(__('Input the %s', 'usces'), urldecode($value)); } } } } if (!empty($errorsm)) { return $this->master->estore->getErrorResponse(Store::REQUIRED_OPTIONS_MISSING, [$errorsm], [$errorsm]); } return true; } /** * Checks whether the provided option array is valid for an item * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @param array $item_option * @return bool */ public static function optionsAreValid($post_id, array $item_option) { global $usces; $ioptkeys = $usces->get_itemOptionKey($post_id, true); $ioptkeys = is_array($ioptkeys) ? $ioptkeys : []; // first, make sure our option array contains options that actually exist for the item foreach ((array)$item_option as $key => $val) { if (!in_array($key, $ioptkeys, true)) { return false; } } // next, make sure selected values actually exist for their respective options and that // required options have values foreach ($ioptkeys as $key => $value) { $optValues = $usces->get_itemOptions(urldecode($value), $post_id); $means = (int)$optValues['means']; $essential = (int)$optValues['essential']; $curval = isset($item_option[$value]) ? $item_option[$value] : null; if (0 === $means) { // select if ($essential && ('#NONE#' == $curval || $curval === null)) { return false; } } elseif (1 === $means) { // multiselect if ($essential) { $mselect = 0; foreach ((array)$curval as $mvalue) { if (!empty($mvalue) && '#NONE#' != $mvalue) { $mselect++; } } if ($mselect == 0) { return false; } } } elseif (in_array($means, [3, 4], true)) { // radio & checkbox if ($essential) { if (empty($curval)) { return false; } } } else { // text if ($essential && WCUtils::is_blank($curval)) { return false; } } // an empty value here means that the current option is not required, so an // existence check for the value on a selection based option isn't necessary if (empty($curval)) { continue; } // check whether the selection exists for the current option switch ($means) { case 0: case 3: $possiblevals = array_map(function ($v) { return trim($v); }, explode("\n", $optValues['value'])); if (!in_array(urldecode($curval), $possiblevals, true)) { return false; } break; case 1: case 4: $possiblevals = array_map(function ($v) { return trim($v); }, explode("\n", $optValues['value'])); if (defined('WCEX_WIDGET_CART') && $means === 1) { // `wcex_widget_cart` concatenates multi-select values with a comma for serialization $curval = explode(',', urldecode($curval[array_keys($curval)[0]])); } foreach ($curval as $selkey => $selval) { $selval = defined('WCEX_WIDGET_CART') && $means === 1 ? $selval : urldecode($selval); if (!in_array($selval, $possiblevals, true)) { return false; } } break; } } return true; } }
- __construct — Sets up Validation
- cartAllowsThisItem — Checks for dlseller cart restrictions and returns a descriptive error if the item is not allowed to be added to cart
- inStock — Returns GenericError instance if the given item and quantity prevents adding to cart because of stock, true otherwise
- isLoggedIn — Returns GenericError instance if the user is not logged in
- itemExists — Returns GenericError instance if the item has been deleted or put in the 'trash', true otherwise
- optionsAreValid — Checks whether the provided option array is valid for an item
- requiredOptionsProvided — Validates option fields and returns a GenericError instance if any required options are blank or if any input values are malformed. Returns true if all checks pass.