CssOption.php
1 week ago
CustomFieldDisabledOption.php
1 week ago
CustomFieldOption.php
1 week ago
DefaultOption.php
1 week ago
DisplayOnAccountAddressOption.php
1 week ago
DisplayOnAccountOrderOption.php
1 week ago
DisplayOnEmailsOption.php
1 week ago
DisplayOnOnlyAddressOption.php
1 week ago
DisplayOnOption.php
1 week ago
DisplayOnThankYouOption.php
1 week ago
DisplayOnWithoutAddressOption.php
1 week ago
EnabledOption.php
1 week ago
ExternalFieldInfoOption.php
1 week ago
ExternalFieldOption.php
1 week ago
FieldTypeDefaultOption.php
1 week ago
FieldTypeOption.php
1 week ago
FormattingFieldLabelOption.php
1 week ago
FormattingNewLineOption.php
1 week ago
FormattingOption.php
1 week ago
FormattingStateAbbrOption.php
1 week ago
FormattingStateCommaOption.php
1 week ago
FormattingStateOption.php
1 week ago
FormattingWcOption.php
1 week ago
ImageOption.php
1 week ago
ImageWidthOption.php
1 week ago
LabelOption.php
1 week ago
LabelOptionallyOption.php
1 week ago
LogicAdvOption.php
1 week ago
NameOption.php
1 week ago
OptionAbstract.php
1 week ago
OptionIntegration.php
1 week ago
OptionInterface.php
1 week ago
PlaceholderCheckboxOption.php
1 week ago
PlaceholderOption.php
1 week ago
PricingAdvOption.php
1 week ago
PriorityOption.php
1 week ago
RegexMessageOption.php
1 week ago
RegexPhoneOption.php
1 week ago
RequiredHiddenOption.php
1 week ago
RequiredOption.php
1 week ago
RequiredWcHiddenOption.php
1 week ago
SettingJqueryCssOption.php
1 week ago
SettingJqueryOption.php
1 week ago
SettingSectionsAdvOption.php
1 week ago
ValidationInfoOption.php
1 week ago
ValidationOption.php
1 week ago
ValidationPostcodeOption.php
1 week ago
ValidationWcOption.php
1 week ago
ValueMaxOption.php
1 week ago
ValueMinOption.php
1 week ago
ValueStepOption.php
1 week ago
OptionAbstract.php
271 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Settings\Option; |
| 4 | |
| 5 | /** |
| 6 | * {@inheritdoc} |
| 7 | */ |
| 8 | abstract class OptionAbstract implements OptionInterface { |
| 9 | |
| 10 | const FIELD_TYPE_CHECKBOX = 'CheckboxField'; |
| 11 | const FIELD_TYPE_CHECKBOX_LIST = 'CheckboxListField'; |
| 12 | const FIELD_TYPE_GROUP = 'GroupField'; |
| 13 | const FIELD_TYPE_HIDDEN = 'HiddenField'; |
| 14 | const FIELD_TYPE_INFO = 'InfoField'; |
| 15 | const FIELD_TYPE_INFO_ADV = 'InfoAdvField'; |
| 16 | const FIELD_TYPE_INFO_NOTICE = 'InfoNoticeField'; |
| 17 | const FIELD_TYPE_NUMBER = 'NumberField'; |
| 18 | const FIELD_TYPE_RADIO = 'RadioField'; |
| 19 | const FIELD_TYPE_RADIO_LIST = 'RadioListField'; |
| 20 | const FIELD_TYPE_REPEATER = 'RepeaterField'; |
| 21 | const FIELD_TYPE_SELECT = 'SelectField'; |
| 22 | const FIELD_TYPE_SELECT_MULTI = 'SelectMultiField'; |
| 23 | const FIELD_TYPE_TEXTAREA = 'TextareaField'; |
| 24 | const FIELD_TYPE_TEXT = 'TextField'; |
| 25 | const FIELD_TYPE_IMAGE = 'ImageField'; |
| 26 | const FIELD_TYPE_COLOR = 'ColorField'; |
| 27 | const FIELD_TYPE_REPEATER_GROUP = 'RepeaterGroupField'; |
| 28 | const FIELD_TYPE_REPEATER_RULES = 'RepeaterRulesField'; |
| 29 | const FIELD_TYPE_DISPATCHER = 'DispatcherField'; |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function get_option_tab(): string { |
| 35 | return ''; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * {@inheritdoc} |
| 40 | */ |
| 41 | public function get_option_type(): string { |
| 42 | return ''; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public function get_option_label(): string { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * {@inheritdoc} |
| 54 | */ |
| 55 | public function get_option_row_label(): string { |
| 56 | return 'Row #%s'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * {@inheritdoc} |
| 61 | */ |
| 62 | public function get_label_tooltip(): string { |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * {@inheritdoc} |
| 68 | */ |
| 69 | public function get_label_tooltip_url(): string { |
| 70 | return ''; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritdoc} |
| 75 | */ |
| 76 | public function get_input_atts(): array { |
| 77 | return []; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritdoc} |
| 82 | */ |
| 83 | public function get_print_pattern(): string { |
| 84 | return '%s'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function is_readonly(): bool { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | public function get_validation_rules(): array { |
| 98 | return []; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * {@inheritdoc} |
| 103 | */ |
| 104 | public function get_options_regexes_to_display(): array { |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * {@inheritdoc} |
| 110 | */ |
| 111 | public function get_option_name_to_rows(): string { |
| 112 | return ''; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * {@inheritdoc} |
| 117 | */ |
| 118 | public function get_values(): array { |
| 119 | return []; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function get_default_value() { |
| 126 | return ''; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * {@inheritdoc} |
| 131 | */ |
| 132 | public function get_endpoint_route(): string { |
| 133 | return ''; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * {@inheritdoc} |
| 138 | */ |
| 139 | public function get_endpoint_option_names(): array { |
| 140 | return []; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * {@inheritdoc} |
| 145 | */ |
| 146 | public function is_endpoint_autorefreshed(): bool { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * {@inheritdoc} |
| 152 | */ |
| 153 | public function is_refresh_trigger(): bool { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * {@inheritdoc} |
| 159 | */ |
| 160 | public function get_children(): array { |
| 161 | return []; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * {@inheritdoc} |
| 166 | */ |
| 167 | public function sanitize_option_value( $field_value ) { |
| 168 | switch ( $this->get_option_type() ) { |
| 169 | case self::FIELD_TYPE_CHECKBOX: |
| 170 | return ( $field_value ) ? '1' : '0'; |
| 171 | case self::FIELD_TYPE_RADIO: |
| 172 | case self::FIELD_TYPE_RADIO_LIST: |
| 173 | case self::FIELD_TYPE_SELECT: |
| 174 | return $this->sanitize_select( $field_value ); |
| 175 | case self::FIELD_TYPE_SELECT_MULTI: |
| 176 | if ( ! is_array( $field_value ) ) { |
| 177 | $field_value = []; |
| 178 | } |
| 179 | return $this->sanitize_select_multi( $field_value ); |
| 180 | case self::FIELD_TYPE_CHECKBOX_LIST: |
| 181 | case self::FIELD_TYPE_GROUP: |
| 182 | case self::FIELD_TYPE_REPEATER: |
| 183 | case self::FIELD_TYPE_REPEATER_GROUP: |
| 184 | case self::FIELD_TYPE_REPEATER_RULES: |
| 185 | return $field_value; |
| 186 | default: |
| 187 | return \sanitize_text_field( wp_unslash( $field_value ) ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Sanitizes the input value for select fields. |
| 193 | * |
| 194 | * @param string $field_value The value of the field to be sanitized. |
| 195 | * @return string The sanitized field value. |
| 196 | */ |
| 197 | private function sanitize_select( string $field_value ): string { |
| 198 | $values = $this->get_values(); |
| 199 | if ( $values ) { |
| 200 | return ( array_key_exists( $field_value, $values ) ) ? $field_value : $this->get_default_value(); |
| 201 | } |
| 202 | return $field_value; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sanitizes the input value for multi select fields. |
| 207 | * |
| 208 | * @param array<int, string> $field_value The field value to be sanitized. |
| 209 | * @return array<int, string> The sanitized field value. |
| 210 | */ |
| 211 | private function sanitize_select_multi( array $field_value ): array { |
| 212 | foreach ( $field_value as $value_index => $value ) { |
| 213 | $field_value[ $value_index ] = \sanitize_text_field( \wp_unslash( $value ) ); |
| 214 | } |
| 215 | return $field_value; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * {@inheritdoc} |
| 220 | */ |
| 221 | public function update_field_data( array $field_data, array $field_settings ): array { |
| 222 | $option_name = $this->get_option_name(); |
| 223 | |
| 224 | switch ( $this->get_option_type() ) { |
| 225 | case self::FIELD_TYPE_CHECKBOX_LIST: |
| 226 | case self::FIELD_TYPE_GROUP: |
| 227 | foreach ( $this->get_children() as $option_children ) { |
| 228 | $field_data = $option_children->update_field_data( $field_data, $field_settings ); |
| 229 | } |
| 230 | break; |
| 231 | case self::FIELD_TYPE_REPEATER: |
| 232 | case self::FIELD_TYPE_REPEATER_GROUP: |
| 233 | $rows = $field_settings[ $option_name ] ?? []; |
| 234 | if ( ! $rows ) { |
| 235 | return $field_data; |
| 236 | } |
| 237 | |
| 238 | foreach ( (array) $rows as $row_index => $row ) { |
| 239 | if ( ! $row ) { |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | foreach ( $this->get_children() as $option_children ) { |
| 244 | $field_data[ $option_name ][ $row_index ] = $option_children->update_field_data( |
| 245 | $field_data[ $option_name ][ $row_index ] ?? [], |
| 246 | $row |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | $field_data[ $option_name ] = $this->sanitize_option_value( |
| 251 | $field_data[ $option_name ] ?? $this->get_default_value() |
| 252 | ); |
| 253 | break; |
| 254 | default: |
| 255 | $field_data[ $option_name ] = $this->sanitize_option_value( |
| 256 | $field_settings[ $option_name ] ?? $this->get_default_value() |
| 257 | ); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | return $field_data; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * {@inheritdoc} |
| 266 | */ |
| 267 | public function save_field_data( array $field_data, array $field_settings ): array { |
| 268 | return $this->update_field_data( $field_data, $field_settings ); |
| 269 | } |
| 270 | } |
| 271 |