関数
CustomFields::__construct( array $config, bool $controlledComponent = true )
登録の種類を初期化する
パラメータ パラメータ
- $config
(配列) (必須)
- $controlledComponent
(bool) (任意)
ファイル: src/Components/CustomFields/CustomFields.php
public function __construct($config, $controlledComponent = true) {
global $usces, $usces_essential_mark;
parent::__construct($controlledComponent);
foreach ($config as $key => $val) {
if (isset($this->{$key})) {
$this->{$key} = $val;
}
}
$meta = usces_has_custom_field_meta($this->ident);
$this->hasCustomFields = !empty($meta) ? true : false;
$this->customFields = [];
$label = 'custom_' . $this->ident;
$data = $this->ident === 'member' ? $usces->get_member() : $usces->cart->get_entry();
if (!empty($meta) && is_array($meta)) {
foreach ($meta as $key => $entry) {
$value = '';
$means = (int)$entry['means'];
$name = $entry['name'];
$selects = [];
// if select box, radio, or checkbox
if ($means === 0 || $means === 1 || $means === 3 || $means === 4) {
if (is_array($entry['value'])) {
foreach ($entry['value'] as $k => $v) {
$value .= $v . "\n";
}
}
$value = usces_change_line_break($value);
$selects = explode("\n", $value);
}
switch ($means) {
case 0: // シングルセレクト
case 1: // マルチセレクト NOTE: multi-select isnt an option for custom fields
$value = (int)$entry['essential'] === 1 ? '#NONE#' : '';
foreach ($selects as $v) {
$value = (isset($data[$label][$key]) && $data[$label][$key] == $v) ? esc_html($v) : $value;
}
$value = !empty($value) ? $value : $selects[0];
/**
* Filters the custom fields default value for a **select** field shown on the Quickpay page
*
* @param string $value
* @param array $selects
* @param string $ident `customer`, `order`, `delivery`, or `member`
* @param string $key Custom field key
* @param array $entry {
* Custom field data
*
* @type int $means Flag denoting the type of field (checkbox, radio, etc.)
* @type string $name Custom field display name
* @type string $value Custom field default value
* @type int $essential 1 if the field is required, 0 if optional
* }
*/
$value = apply_filters(
'wcexaap_custom_fields_select_default_value',
$value,
$selects,
$this->ident,
$key,
$entry
);
break;
case 2: // テキスト
$value = isset($data[$label][$key]) ? $data[$label][$key] : '';
/**
* Filters the custom fields default value for a **text** field shown on the Quickpay page
*
* @param string $value
* @param array $selects
* @param string $ident `customer`, `order`, `delivery`, or `member`
* @param string $key Custom field key
* @param array $entry See {@see 'wcexaap_custom_fields_select_default_value'}
*/
$value = apply_filters(
'wcexaap_custom_fields_text_default_value',
$value,
$selects,
$this->ident,
$key,
$entry
);
break;
case 3: // ラジオボタン
$value = '';
foreach ($selects as $v) {
$value = (isset($data[$label][$key]) && $data[$label][$key] == $v) ? esc_html($v) : $value;
}
/**
* Filters the custom fields default value for a **radio** field shown on the Quickpay page
*
* @param string $value
* @param array $selects
* @param string $ident `customer`, `order`, `delivery`, or `member`
* @param string $key Custom field key
* @param array $entry See {@see 'wcexaap_custom_fields_select_default_value'}
*/
$value = apply_filters(
'wcexaap_custom_fields_radio_default_value',
$value,
$selects,
$this->ident,
$key,
$entry
);
break;
case 4: // チェックボックス
$value = [];
foreach ($selects as $v) {
if (isset($data[$label][$key]) && is_array($data[$label][$key])) {
$value[$v] = (isset($data[$label][$key]) && array_key_exists($v, $data[$label][$key])) ? [$v] : [];
} else {
$value[$v] = (isset($data[$label][$key]) && $data[$label][$key] == $v) ? [$v] : [];
}
}
/**
* Filters the custom fields default value for a **checkbox** field shown on the Quickpay page
*
* @param string $value
* @param array $selects
* @param string $ident `customer`, `order`, `delivery`, or `member`
* @param string $key Custom field key
* @param array $entry See {@see 'wcexaap_custom_fields_select_default_value'}
*/
$value = apply_filters(
'wcexaap_custom_fields_checkbox_default_value',
$value,
$selects,
$this->ident,
$key,
$entry
);
break;
case 5: // Text-area
$value = isset($data[$label][$key]) ? $data[$label][$key] : '';
/**
* Filters the custom fields default value for a **textarea** field shown on the Quickpay page
*
* @param string $value
* @param array $selects
* @param string $ident `customer`, `order`, `delivery`, or `member`
* @param string $key Custom field key
* @param array $entry See {@see 'wcexaap_custom_fields_select_default_value'}
*/
$value = apply_filters(
'wcexaap_custom_fields_textarea_default_value',
$value,
$this->ident,
$key,
$entry
);
break;
}
$e = '';
if ((int)$entry['essential'] === 1) {
$e = isset($usces_essential_mark[$key])
? usces_get_essential_mark($key)
: '<em>' . __('*', 'usces') . '</em>';
}
$this->customFields[$key] = [
'value' => $value,
'name' => $name,
'error_message' =>
// 2 is text, 5 is text-area
$means !== 2 && $means !== 5
? sprintf(__('Chose the %s', 'usces'), $name)
: sprintf(__('Input the %s', 'usces'), $name),
'means' => $means,
'essential' => ((int)$entry['essential'] === 1),
'e_mark' => $e,
];
}
}
switch ($this->template) {
case 'semantic':
$this->templateObject = new Templates\Semantic(
$this->hasCustomFields,
$this->customFields
);
break;
case 'welcart':
$this->templateObject = new Templates\Welcart();
break;
default:
$this->templateObject = new Templates\Welcart();
break;
}
}