EditFieldsForm.php
2 weeks ago
FormAbstract.php
2 weeks ago
FormIntegration.php
2 weeks ago
FormInterface.php
2 weeks ago
SettingsPageForm.php
2 weeks ago
EditFieldsForm.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Settings\Form; |
| 4 | |
| 5 | use WPDesk\FCF\Free\Field\FieldData; |
| 6 | use WPDesk\FCF\Free\Settings\Option\ExternalFieldOption; |
| 7 | |
| 8 | /** |
| 9 | * {@inheritdoc} |
| 10 | */ |
| 11 | class EditFieldsForm extends FormAbstract implements FormInterface { |
| 12 | |
| 13 | const FORM_TYPE = 'fields'; |
| 14 | const SETTINGS_OPTION_NAME = 'wpdesk_checkout_fields_settings'; |
| 15 | |
| 16 | /** |
| 17 | * {@inheritdoc} |
| 18 | */ |
| 19 | public function get_form_type(): string { |
| 20 | return self::FORM_TYPE; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * {@inheritdoc} |
| 25 | */ |
| 26 | public function get_form_data( array $form_data, string $form_key = '' ): array { |
| 27 | $settings = get_option( self::SETTINGS_OPTION_NAME, [] ); |
| 28 | $section_fields = $this->combine_fields_settings( |
| 29 | $this->get_section_form_data( $form_key ), |
| 30 | ( $settings[ $form_key ] ?? [] ) ?: [] |
| 31 | ); |
| 32 | if ( ! $section_fields ) { |
| 33 | return $form_data; |
| 34 | } |
| 35 | |
| 36 | foreach ( $section_fields as $field_name => $field_data ) { |
| 37 | $field_data['name'] = $field_name; |
| 38 | $new_field_data = FieldData::get_field_data( $field_data ); |
| 39 | if ( ! $new_field_data ) { |
| 40 | continue; |
| 41 | } |
| 42 | $form_data[ $field_name ] = $new_field_data; |
| 43 | } |
| 44 | |
| 45 | uasort( |
| 46 | $form_data, |
| 47 | function ( $a, $b ) { |
| 48 | if ( ( $a['priority'] ?? 0 ) === 0 ) { |
| 49 | return 1; |
| 50 | } elseif ( ( $b['priority'] ?? 0 ) === 0 ) { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | return ( $a['priority'] < $b['priority'] ) ? -1 : 1; |
| 55 | } |
| 56 | ); |
| 57 | |
| 58 | return $form_data; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns default settings for form of checkout section. |
| 63 | * |
| 64 | * @param string $section_key Key of section. |
| 65 | * |
| 66 | * @return array Settings of form. |
| 67 | */ |
| 68 | private function get_section_form_data( string $section_key ): array { |
| 69 | $countries = new \WC_Countries(); |
| 70 | $sections = [ |
| 71 | 'billing' => $countries->get_address_fields( $countries->get_base_country(), 'billing_' ), |
| 72 | 'shipping' => $countries->get_address_fields( $countries->get_base_country(), 'shipping_' ), |
| 73 | 'order' => [ |
| 74 | 'order_comments' => [ |
| 75 | 'type' => 'textarea', |
| 76 | 'class' => [ 'notes' ], |
| 77 | 'label' => __( 'Order Notes', 'flexible-checkout-fields' ), |
| 78 | 'placeholder' => __( 'Notes about your order, e.g. special notes for delivery.', 'flexible-checkout-fields' ), |
| 79 | ], |
| 80 | ], |
| 81 | ]; |
| 82 | |
| 83 | $sections += $this->get_custom_sections(); |
| 84 | return $sections[ $section_key ] ?? []; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns list of custom checkout sections. |
| 89 | * |
| 90 | * @return array List of sections. |
| 91 | */ |
| 92 | private function get_custom_sections(): array { |
| 93 | $custom_sections = apply_filters( 'flexible_checkout_fields_all_sections', [] ); |
| 94 | |
| 95 | $sections = []; |
| 96 | foreach ( $custom_sections as $custom_section ) { |
| 97 | $sections[ $custom_section['section'] ] = []; |
| 98 | } |
| 99 | return $sections; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Combines default field settings with settings saved by plugin. |
| 104 | * |
| 105 | * @param array $checkout_fields Default field settings. |
| 106 | * @param array $settings_fields Field settings saved by plugin. |
| 107 | * |
| 108 | * @return array Final field settings. |
| 109 | */ |
| 110 | private function combine_fields_settings( array $checkout_fields, array $settings_fields ): array { |
| 111 | $fields = $checkout_fields; |
| 112 | foreach ( $fields as $field_name => $field ) { |
| 113 | if ( ! isset( $settings_fields[ $field_name ] ) ) { |
| 114 | $fields[ $field_name ][ ExternalFieldOption::FIELD_NAME ] = 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | foreach ( $settings_fields as $field_name => $settings_field ) { |
| 119 | $fields[ $field_name ] = array_merge( $fields[ $field_name ] ?? [], $settings_field ); |
| 120 | } |
| 121 | return $fields; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Saves settings for form. |
| 126 | * |
| 127 | * @param array $params Params for endpoint. |
| 128 | * |
| 129 | * @return bool Status of process. |
| 130 | * @throws \Exception . |
| 131 | */ |
| 132 | public function save_form_data( array $params ): bool { |
| 133 | $posted_fields = []; |
| 134 | foreach ( $params['form_fields'] as $field ) { |
| 135 | $posted_fields[ $field['name'] ] = $field; |
| 136 | } |
| 137 | |
| 138 | $section_fields = []; |
| 139 | foreach ( $params['form_fields'] as $field_data ) { |
| 140 | $new_field_data = FieldData::get_field_data( $posted_fields[ $field_data['name'] ], false ); |
| 141 | if ( ! $new_field_data ) { |
| 142 | continue; |
| 143 | } |
| 144 | $section_fields[ $field_data['name'] ] = $new_field_data; |
| 145 | } |
| 146 | |
| 147 | $settings = get_option( self::SETTINGS_OPTION_NAME, [] ) ?: []; |
| 148 | if ( ! $section_fields ) { |
| 149 | if ( isset( $settings[ $params['form_section'] ] ) ) { |
| 150 | unset( $settings[ $params['form_section'] ] ); |
| 151 | } |
| 152 | } else { |
| 153 | $settings[ $params['form_section'] ] = $section_fields; |
| 154 | } |
| 155 | |
| 156 | update_option( self::SETTINGS_OPTION_NAME, $settings ); |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 |