| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Modules\CheckoutEditor; |
| 4 |
|
| 5 |
// Do not allow directly accessing this file. |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit( 'This script cannot be accessed directly.' ); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* CheckoutEditorInit |
| 12 |
*/ |
| 13 |
class CheckoutEditorBase { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $cache = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Billing Fields Settings |
| 22 |
*/ |
| 23 |
protected $options = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Billing Fields Settings |
| 27 |
*/ |
| 28 |
protected $billing_settings = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* All Custom Fields Here |
| 32 |
*/ |
| 33 |
protected $checkout_custom_fields = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* Billing Custom Fields |
| 37 |
*/ |
| 38 |
protected $billing_custom_field = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* Billing Custom Fields |
| 42 |
*/ |
| 43 |
protected $shipping_custom_field = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* Shippping Field Settings |
| 47 |
*/ |
| 48 |
protected $shipping_settings = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* Additional Field Settings |
| 52 |
*/ |
| 53 |
protected $additional_settings = []; |
| 54 |
/** |
| 55 |
* Billing Custom Fields |
| 56 |
*/ |
| 57 |
protected $additional_custom_field = []; |
| 58 |
|
| 59 |
/** |
| 60 |
* Notifications hooks. |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
global $checkout_editor_settings; // Define global variable. |
| 64 |
$this->options = $checkout_editor_settings; |
| 65 |
// If the cached result doesn't exist, fetch it from the database. |
| 66 |
$this->billing_settings = $this->fields_generator( 'billing' ); |
| 67 |
$this->shipping_settings = $this->fields_generator( 'shipping' ); |
| 68 |
$this->additional_settings = $this->fields_generator( 'additional' ); |
| 69 |
} |
| 70 |
/** |
| 71 |
* @param array $settings Field generator settings. |
| 72 |
* @param array $address_fields Previous Field. |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
protected function render_fields( $settings, $address_fields ) { |
| 76 |
$modified_fields = []; |
| 77 |
if ( empty( $settings ) || empty( $address_fields ) ) { |
| 78 |
return $address_fields; |
| 79 |
} |
| 80 |
$item = 1; |
| 81 |
foreach ( $settings as $key => $field ) { |
| 82 |
$field['priority'] = absint( $item ) * 10; |
| 83 |
if ( ! empty( $address_fields[ $key ] ) ) { |
| 84 |
$modified_fields[ $key ] = wp_parse_args( $field, $address_fields[ $key ] ); |
| 85 |
} else { |
| 86 |
$modified_fields[ $key ] = $field; |
| 87 |
} |
| 88 |
$item++; |
| 89 |
} |
| 90 |
return $modified_fields; |
| 91 |
} |
| 92 |
/** |
| 93 |
* Billing Custom Field. |
| 94 |
* |
| 95 |
* @return array|mixed |
| 96 |
*/ |
| 97 |
private function get_multiselect_fields_value( $data ) { |
| 98 |
if ( empty( $data ) ) { |
| 99 |
return []; |
| 100 |
} |
| 101 |
return array_column( $data, 'value' ); |
| 102 |
} |
| 103 |
/** |
| 104 |
* @param string $group group name. |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
private function fields_generator( $group ) { |
| 108 |
$cache_key = 'checkout_' . $group . '_fields'; |
| 109 |
if ( isset( $this->cache[ $cache_key ] ) ) { |
| 110 |
return $this->cache[ $cache_key ]; |
| 111 |
} |
| 112 |
$customize_fields = $this->options[ 'checkout_' . $group . '_fields' ] ?? []; |
| 113 |
$fields = []; |
| 114 |
if ( ! empty( $customize_fields ) ) { |
| 115 |
$fields = json_decode( stripslashes( $customize_fields ), true ); |
| 116 |
} |
| 117 |
$generated_fields = []; |
| 118 |
$user = wp_get_current_user(); |
| 119 |
foreach ( $fields as $field ) { |
| 120 |
if ( empty( $field['name'] ) || 'on' !== ( $field['isEnable'] ?? '' ) ) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
if ( rtsb()->has_pro() && 'specific' === ( $field['fieldVisibility'] ?? '' ) && ! empty( $field['fieldVisibilityRoles'] ) ) { |
| 124 |
$roles = $this->get_multiselect_fields_value( $field['fieldVisibilityRoles'] ); |
| 125 |
if ( ! array_intersect( $roles, $user->roles ) ) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$field['required'] = 'on' === ( $field['isRequired'] ?? '' ); |
| 131 |
$field['class'] = ! empty( $field['custom_class'] ) ? explode( ',', $field['custom_class'] ) : []; |
| 132 |
$field['class'][] = 'rtsb-input-field'; |
| 133 |
$field['class'][] = 'rtsb-form-field-type-' . $field['type']; |
| 134 |
if ( ! empty( $field['validation'] ) && 'default' !== $field['validation'] ) { |
| 135 |
$field['validate'] = [ $field['validation'] ]; |
| 136 |
} |
| 137 |
if ( 'phone' === $field['name'] ) { |
| 138 |
$field['type'] = 'tel'; |
| 139 |
} elseif ( 'country' === $field['name'] ) { |
| 140 |
$field['type'] = 'country'; |
| 141 |
$field['value'] = $field['default'] ?? null; |
| 142 |
} elseif ( 'state' === $field['name'] ) { |
| 143 |
$field['type'] = 'state'; |
| 144 |
} elseif ( 'email' === $field['name'] ) { |
| 145 |
$field['type'] = 'email'; |
| 146 |
} |
| 147 |
|
| 148 |
if ( rtsb()->has_pro() && 'custom_field' === $field['name'] ) { |
| 149 |
$field_name = $group . '_' . ( $field['custom_key'] ?? 'custom_field_' . $field['id'] ); |
| 150 |
$selected_value = []; |
| 151 |
if ( in_array( $field['type'], [ 'select', 'radio', 'checkboxgroup', 'multiselect' ], true ) ) { |
| 152 |
$options = $field['options'] ?? []; |
| 153 |
unset( $field['options'] ); |
| 154 |
$is_multiple = in_array( $field['type'], [ 'checkboxgroup', 'multiselect' ], true ); |
| 155 |
foreach ( $options as $option ) { |
| 156 |
$field['options'][ $option['value'] ] = $option['label']; |
| 157 |
if ( $is_multiple ) { |
| 158 |
if ( 'on' === ( $option['isDefault'] ?? 'off' ) ) { |
| 159 |
$selected_value[] = $option['value']; |
| 160 |
} |
| 161 |
} elseif ( empty( $selected_value ) ) { |
| 162 |
$selected_value = 'on' === ( $option['isDefault'] ?? 'off' ) ? $option['value'] : ''; |
| 163 |
} |
| 164 |
} |
| 165 |
$field['default'] = $selected_value; |
| 166 |
} |
| 167 |
if ( in_array( $field['type'], [ 'checkbox' ], true ) ) { |
| 168 |
$field['checked_value'] = $field['default'] ?? null; |
| 169 |
$field['value'] = 'on' === $field['isChecked'] ? $field['checked_value'] : 'N/A'; |
| 170 |
if ( 'off' === $field['isChecked'] ) { |
| 171 |
$field['default'] = 'N/A'; |
| 172 |
} |
| 173 |
} |
| 174 |
if ( in_array( $field['type'], [ 'heading' ], true ) ) { |
| 175 |
unset( |
| 176 |
$field['options'], |
| 177 |
$field['isRequired'], |
| 178 |
$field['required'], |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
if ( 'billing' === $group ) { |
| 183 |
$this->billing_custom_field[ $field_name ] = $this->remeove_unused_attr( $field ); |
| 184 |
} elseif ( 'shipping' === $group ) { |
| 185 |
$this->shipping_custom_field[ $field_name ] = $this->remeove_unused_attr( $field ); |
| 186 |
} elseif ( 'additional' === $group ) { |
| 187 |
$this->additional_custom_field[ $field_name ] = $this->remeove_unused_attr( $field ); |
| 188 |
} |
| 189 |
$this->checkout_custom_fields[ $field_name ] = $this->remeove_unused_attr( $field ); |
| 190 |
|
| 191 |
} else { |
| 192 |
$prefix = 'additional' === $group ? 'order' : $group; |
| 193 |
$field_name = $prefix . '_' . $field['name']; |
| 194 |
unset( |
| 195 |
$field['options'] |
| 196 |
); |
| 197 |
} |
| 198 |
$generated_fields[ $field_name ] = $this->remeove_unused_attr( $field ); |
| 199 |
} |
| 200 |
$this->cache[ $cache_key ] = $generated_fields; |
| 201 |
return $generated_fields; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Billing Custom Field. |
| 206 |
* |
| 207 |
* @return array|mixed |
| 208 |
*/ |
| 209 |
private function remeove_unused_attr( $field ) { |
| 210 |
unset( |
| 211 |
$field['isEnable'], |
| 212 |
$field['id'], |
| 213 |
$field['deletable'], |
| 214 |
$field['validation'], |
| 215 |
$field['custom_key'], |
| 216 |
$field['custom_class'], |
| 217 |
$field['isRequired'], |
| 218 |
$field['name'], |
| 219 |
); |
| 220 |
return $field; |
| 221 |
} |
| 222 |
} |
| 223 |
|