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 |