PluginProbe ʕ •ᴥ•ʔ
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager / 4.1.41
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager v4.1.41
4.1.41 4.1.40 4.1.39 4.1.38 trunk 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2 1.3.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.2 1.9.3 2.0.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.4 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.7.0 2.7.1 2.8.0 2.9.0 2.9.1 2.9.2 3.0.0 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.10 3.5.11 3.5.12 3.5.13 3.5.14 3.5.15 3.5.16 3.5.17 3.5.18 3.5.19 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 4.0.0 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.13 4.1.14 4.1.15 4.1.16 4.1.17 4.1.18 4.1.19 4.1.2 4.1.20 4.1.21 4.1.22 4.1.23 4.1.24 4.1.25 4.1.26 4.1.27 4.1.28 4.1.29 4.1.3 4.1.30 4.1.31 4.1.32 4.1.34 4.1.35 4.1.36 4.1.37 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9
flexible-checkout-fields / src / Settings / Form / EditFieldsForm.php
flexible-checkout-fields / src / Settings / Form Last commit date
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