Type
1 week ago
FieldData.php
1 week ago
FieldTemplateLoader.php
1 week ago
FieldTranslator.php
1 week ago
Types.php
1 week ago
FieldData.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Field; |
| 4 | |
| 5 | use WPDesk\FCF\Free\Field\Type\DefaultType; |
| 6 | use WPDesk\FCF\Free\Settings\Option\OptionInterface; |
| 7 | |
| 8 | /** |
| 9 | * Generates field data based on options for field type. |
| 10 | */ |
| 11 | class FieldData { |
| 12 | |
| 13 | /** |
| 14 | * Returns parsed data for field. |
| 15 | * |
| 16 | * @param array $field_settings Settings of field. |
| 17 | * @param bool $is_decode Is it decoding (used saved settings) data instead of encoding (for settings save). |
| 18 | * |
| 19 | * @return array Data of field. |
| 20 | */ |
| 21 | public static function get_field_data( array $field_settings, bool $is_decode = true ): array { |
| 22 | $field_data = []; |
| 23 | $option_objects = self::get_field_options( $field_settings ); |
| 24 | |
| 25 | if ( ! $option_objects ) { |
| 26 | return $field_data; |
| 27 | } |
| 28 | |
| 29 | $field_data['name'] = $field_settings['name']; |
| 30 | foreach ( $option_objects as $field_option ) { |
| 31 | $field_data = $field_option[ ( $is_decode ) ? 'update_field_callback' : 'save_field_callback' ]( |
| 32 | $field_data, |
| 33 | $field_settings |
| 34 | ); |
| 35 | } |
| 36 | return $field_data; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns list of option objects. |
| 41 | * |
| 42 | * @param array $field_settings Settings of field. |
| 43 | * |
| 44 | * @return OptionInterface[] List of options. |
| 45 | */ |
| 46 | public static function get_field_options( array $field_settings ): array { |
| 47 | $field_types = apply_filters( 'flexible_checkout_fields/field_types', [] ); |
| 48 | foreach ( $field_types as $field_type ) { |
| 49 | if ( in_array( $field_settings['name'], $field_type['reserved_field_names'], true ) ) { |
| 50 | return $field_type['options']; |
| 51 | } |
| 52 | } |
| 53 | foreach ( $field_types as $field_type ) { |
| 54 | if ( isset( $field_settings['type'] ) && ( $field_settings['type'] === $field_type['type'] ) ) { |
| 55 | return $field_type['options']; |
| 56 | } |
| 57 | } |
| 58 | return $field_types[ DefaultType::FIELD_TYPE ]['options'] ?? []; |
| 59 | } |
| 60 | } |
| 61 |