クラス
CustomFields
ソース ソース
ファイル: src/Components/CustomFields/CustomFields.php
class CustomFields extends VueComponent
{
/**
* Used for name attribute of hidden reg fields (customer, member, etc.)
*
* @var string
*/
public $ident = 'customer';
/**
* True to display form errors in component html, false to not show errors
*
* @var boolean
*/
public $display_errors = true;
/**
* Flag for custom fields meta
*
* @var boolean
*/
public $hasCustomFields;
/**
* Array of custom fields
*
* @var array
*/
public $customFields;
/**
* Template string
*
* @var string
*/
public $template = 'welcart';
/**
* Template object
*
* @var mixed
*/
public $templateObject;
/**
* Initialize type of registration
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global array usces_essential_mark
* @global \usc_e_shop $usces
* @param array $config
* @param bool $controlledComponent
*/
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;
}
}
/**
* Initializes component
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function init() {
if (method_exists($this->templateObject, 'init')) {
$this->templateObject->init();
}
add_action('wcexaap_custom_' . $this->ident . '_hidden_fields', [$this, 'hiddenRegFieldsHtml']);
}
/**
* Returns component name as is recognized by Vue.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getComponentName() {
return 'customFieldsComponent';
}
/**
* Inline template for our Vue.js component.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function template() {
if (empty($this->customFields)) {
return;
}
$js_custom_fields = '{}';
if (is_array($this->customFields) && count($this->customFields) > 0) {
$js_custom_fields = WelcartUtils::localizeAssociativeArray($this->customFields);
}
$propsAndEvents = [];
if ($this->controlledComponent === true) {
$propsAndEvents[] = '@update-payload="updatePayload"';
$propsAndEvents[] = '@update-error-messages="updateErrorMessages"';
}
$allPropsAndEvents = join(' ', $propsAndEvents);
?>
<custom-fields-component
ident='<?php echo $this->ident ?>'
v-bind:uscescustomfields='<?php echo $js_custom_fields ?>'
v-bind:displayerrors="<?php echo $this->display_errors ? 'true' : 'false' ?>"
<?php echo $allPropsAndEvents ?>
inline-template
>
<div>
<?php ob_start() ?>
<div v-cloak v-for="error in errorMessages" v-if="displayerrors && error" class="error_message">
{{ error }}
</div>
<?php $this->customFieldsMeta(); ?>
<?php
$html = ob_get_clean();
/**
* Filters the `custom-fields-component` Vue HTML shown on the Quickpay page
*
* @param string $html
*/
$html = apply_filters('wcexaap_filter_custom_' . $this->ident . '_component_container', $html);
echo $html;
?>
</div>
</custom-fields-component>
<?php
}
/**
* Hidden fields for Vue app
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function hiddenFormFields() {
ob_start();
?>
<template v-for="(obj, key) in customFields">
<template v-if="obj.means == 4">
<template v-for="(nestedobj, nestedkey) in obj.value">
<input
:key="'nested_' + nestedkey"
:name="aap_ident + '[' + key + '][' + nestedkey + ']'"
type="hidden"
:value="nestedobj"
/>
</template>
</template>
<input
v-else
type="hidden"
:name="aap_ident + '[' + key + ']'"
:value="obj.value"
:key="key"
/>
</template>
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
/**
* Adds custom fields meta.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @todo add option to include meta that isnt required
* @global array $usces_entries
* @return void
*/
public function customFieldsMeta() {
global $usces_entries;
$position_keys = ['name_pre', 'name_after', 'fax_after'];
if ($this->ident !== 'order') {
foreach ($position_keys as $position) {
$this->templateObject->uscesCustomFieldInput($usces_entries, $this->ident, $position);
}
} else {
$this->templateObject->uscesCustomFieldInput($usces_entries, $this->ident, '');
}
}
}- __construct — Initialize type of registration
- customFieldsMeta — Adds custom fields meta.
- getComponentName — Returns component name as is recognized by Vue.
- hiddenFormFields — Hidden fields for Vue app
- init — Initializes component
- template — Inline template for our Vue.js component.