PluginProbe ʕ •ᴥ•ʔ
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager / trunk
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager vtrunk
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 / Migrations / Migration400.php
flexible-checkout-fields / src / Settings / Migrations Last commit date
Migration.php 2 weeks ago Migration320.php 2 weeks ago Migration400.php 2 weeks ago
Migration400.php
200 lines
1 <?php
2
3 namespace WPDesk\FCF\Free\Settings\Migrations;
4
5 use WPDesk\FCF\Free\Settings\Form\EditFieldsForm;
6
7 /**
8 * Two major changes in version 4.0.0:
9 * - convert conditional logic options to a new format
10 * - settings option_name in options table is now wpdesk_checkout_fields_settings
11 */
12 class Migration400 implements Migration {
13
14 const OLD_SETTINGS_OPTION_NAME = 'inspire_checkout_fields_settings';
15
16 const ACTION_SHOW = 'show';
17 const ACTION_HIDE = 'hide';
18
19 const OPERATOR_AND = 'and';
20 const OPERATOR_OR = 'or';
21
22 const CATEGORY_CART = 'cart_contains';
23 const CATEGORY_FCF_FIELD = 'fcf_field';
24 const CATEGORY_SHIPPING_METHOD = 'shipping_method';
25
26 /**
27 * {@inheritdoc}
28 */
29 public function get_version(): string {
30 return '4.0.0';
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 public function up() {
37 $plugin_settings = get_option( self::OLD_SETTINGS_OPTION_NAME, [] );
38
39 foreach ( $plugin_settings as $section_id => $section_fields ) {
40 foreach ( $section_fields as $field_id => $field_data ) {
41
42 $conditional_logic_info = $this->extract_conditional_logic_info( $field_data );
43 $field_data['conditional_logic'] = $this->format_conditional_logic( $conditional_logic_info );
44 $field_data = $this->remove_deprecated_fields( $field_data );
45
46 $plugin_settings[ $section_id ][ $field_id ] = $field_data;
47 }
48 }
49
50 update_option( EditFieldsForm::SETTINGS_OPTION_NAME, $plugin_settings );
51 }
52
53
54 /**
55 * Extract conditional logic info from field data, in more readable format.
56 *
57 * @param array<string, mixed> $field_data
58 *
59 * @return array<string, mixed>
60 */
61 private function extract_conditional_logic_info( array $field_data ): array {
62 $categories = [
63 'conditional_logic' => self::CATEGORY_CART,
64 'conditional_logic_fields' => self::CATEGORY_FCF_FIELD,
65 'conditional_logic_shipping_fields' => self::CATEGORY_SHIPPING_METHOD,
66 ];
67
68 $conditional_logic = [];
69 foreach ( $categories as $field_key => $category ) {
70 $action_key = $field_key . '_action';
71 $rules_key = $field_key . '_rules';
72 $operator_key = $field_key . '_operator';
73
74 if (
75 isset( $field_data[ $field_key ] )
76 && isset( $field_data[ $action_key ] )
77 && isset( $field_data[ $rules_key ] )
78 && isset( $field_data[ $operator_key ] )
79 ) {
80 $action = $field_data[ $action_key ];
81 $rules = $field_data[ $rules_key ];
82 $operator = $field_data[ $operator_key ];
83
84 $group_rules = $this->format_group( $category, $rules, $operator );
85 $conditional_logic[ $action ] = array_merge( $conditional_logic[ $action ] ?? [], $group_rules );
86 }
87 }
88
89 return $conditional_logic;
90 }
91
92 /**
93 * Formats conditional logic group settings.
94 *
95 * @param string $category
96 * @param array<mixed> $rules
97 * @param string $operator
98 *
99 * @return array<mixed>
100 */
101 private function format_group( string $category, array $rules, string $operator ): array {
102 $group = [];
103
104 if ( self::OPERATOR_OR === $operator ) {
105 foreach ( $rules as $rule ) {
106 $group[] = [ $this->format_rule( $category, $rule ) ];
107 }
108 } elseif ( self::OPERATOR_AND === $operator ) {
109 $and_rules = [];
110 foreach ( $rules as $rule ) {
111 $and_rules[] = $this->format_rule( $category, $rule );
112 }
113 $group[] = $and_rules;
114 }
115
116 return $group;
117 }
118
119 /**
120 * Formats conditional logic rule settings.
121 *
122 * @param string $category
123 * @param array<mixed> $rule
124 *
125 * @return array<mixed>
126 */
127 private function format_rule( string $category, array $rule ): array {
128 $new_rule = [];
129
130 switch ( $category ) {
131 case self::CATEGORY_CART:
132 $new_rule['category'] = self::CATEGORY_CART;
133 $new_rule['selection'] = $rule['what'];
134 $new_rule['comparison'] = 'which_is';
135 $new_rule['values'] = $rule['what'] === 'product' ? $rule['products'] : $rule['product_categories'];
136 break;
137 case self::CATEGORY_FCF_FIELD:
138 $new_rule['category'] = self::CATEGORY_FCF_FIELD;
139 $new_rule['selection'] = $rule['field'];
140 $new_rule['comparison'] = 'is';
141 $new_rule['values'] = [ $rule['value'] ];
142 break;
143 case self::CATEGORY_SHIPPING_METHOD:
144 $new_rule['category'] = self::CATEGORY_SHIPPING_METHOD;
145 $new_rule['selection'] = $rule['field'];
146 $new_rule['comparison'] = 'is';
147 $new_rule['values'] = [ $rule['value'] ];
148 break;
149 }
150
151 return $new_rule;
152 }
153
154 /**
155 * Formats final conditional logic settings.
156 *
157 * @param array<mixed> $conditional_logic_info
158 *
159 * @return array<mixed>
160 */
161 public function format_conditional_logic( array $conditional_logic_info ): array {
162 $conditional_logic = [];
163
164 foreach ( $conditional_logic_info as $action => $rules ) {
165 $conditional_logic[] = [
166 'action' => $action,
167 'rules' => $rules,
168 ];
169 }
170
171 return $conditional_logic;
172 }
173
174 /**
175 * Remove not needed fields.
176 *
177 * @param array<mixed> $field_data
178 *
179 * @return array<mixed>
180 */
181 private function remove_deprecated_fields( array $field_data ): array {
182 unset(
183 $field_data['conditional_logic_action'],
184 $field_data['conditional_logic_operator'],
185 $field_data['conditional_logic_rules'],
186 $field_data['conditional_logic_fields'],
187 $field_data['conditional_logic_fields_action'],
188 $field_data['conditional_logic_fields_operator'],
189 $field_data['conditional_logic_fields_rules'],
190 $field_data['conditional_logic_shipping_fields'],
191 $field_data['conditional_logic_shipping_fields_action'],
192 $field_data['conditional_logic_shipping_fields_operator'],
193 $field_data['conditional_logic_shipping_fields_rules'],
194 $field_data['conditional_logic_info']
195 );
196
197 return $field_data;
198 }
199 }
200