関数
Validation::optionsAreValid( int $post_id, array $item_option )
与えられたオプションの配列が商品に対し有効かどうかを確認する
パラメータ パラメータ
- $post_id
(数値) (必須)
- $item_option
(配列) (必須)
ファイル: src/API/Validation.php
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; }