PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.2
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.2
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Migration / Src / SettingsMigration.php

SettingsMigration.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.2, at includes/Migration/Src/SettingsMigration.php

314 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings Migration for Easy Invoice
4 *
5 * @package EasyInvoice
6 * @subpackage Migration
7 * @since 2.0.0
8 */
9
10 namespace EasyInvoice\Migration\Src;
11
12 /**
13 * Settings migration class.
14 *
15 * Handles migration of all plugin settings and options.
16 *
17 * @since 2.0.0
18 */
19 class SettingsMigration extends AbstractMigration implements MigrationInterface {
20
21 /**
22 * Migration description.
23 *
24 * @since 2.0.0
25 * @var string
26 */
27 protected $description = 'Migrate plugin settings and options to new structure';
28
29 /**
30 * Simple settings mapping: old option ID => new option ID
31 *
32 * @since 2.0.0
33 * @var array
34 */
35 protected $settings_mapping = [
36
37 // Company settings - these exist in the plugin
38 'easy_invoice_business_logo' => 'easy_invoice_company_logo',
39 'easy_invoice_business_name' => 'easy_invoice_company_name',
40 'easy_invoice_business_address' => 'easy_invoice_company_address',
41 'easy_invoice_business_website_url' => 'easy_invoice_company_website',
42 'easy_invoice_business_additional_info' => 'easy_invoice_company_additional_info',
43 'easy_invoice_business_phone' => 'easy_invoice_company_phone',
44 'easy_invoice_business_email' => 'easy_invoice_company_email',
45 'easy_invoice_business_vat' => 'easy_invoice_tax_number',
46 'easy_invoice_company_phone' => 'easy_invoice_company_phone',
47 'easy_invoice_company_email' => 'easy_invoice_company_email',
48 'easy_invoice_company_additional_info' => 'easy_invoice_company_additional_info',
49
50 // Email settings - these exist in the plugin
51 'easy_invoice_email_from_address' => 'easy_invoice_email_from_address',
52 'easy_invoice_email_from_name' => 'easy_invoice_email_from_name',
53 'easy_invoice_email_send_admin_copy' => 'easy_invoice_bcc_admin',
54 'easy_invoice_email_reply_to' => 'easy_invoice_email_reply_to',
55 'easy_invoice_email_reply_to_name' => 'easy_invoice_email_reply_to_name',
56 'easy_invoice_email_logo' => 'easy_invoice_email_logo',
57 'easy_invoice_email_footer_text' => 'easy_invoice_email_footer_text',
58 'easy_invoice_enable_email_styling' => 'easy_invoice_enable_email_styling',
59 'easy_invoice_admin_email' => 'easy_invoice_admin_email',
60 'easy_invoice_test_email' => 'easy_invoice_test_email',
61
62 // Invoice settings - these exist in the plugin
63 'easy_invoice_number_prefix' => 'easy_invoice_invoice_prefix',
64 'easy_invoice_invoice_number' => 'easy_invoice_invoice_starting_number',
65 'easy_invoice_show_hide_adjust' => 'easy_invoice_invoice_show_adjust_field',
66 'easy_invoice_terms_conditions' => 'easy_invoice_invoice_terms_conditions',
67 'easy_invoice_footer_text' => 'easy_invoice_invoice_footer_text',
68
69 // Quote settings - these exist in the plugin
70 'easy_invoice_quote_number_prefix' => 'easy_invoice_quote_prefix',
71 'easy_invoice_quote_number' => 'easy_invoice_quote_starting_number',
72 'easy_invoice_quote_show_hide_adjust' => 'easy_invoice_quote_show_adjust_field',
73 'easy_invoice_quote_terms_conditions' => 'easy_invoice_quote_terms_conditions',
74 'easy_invoice_quote_footer_text' => 'easy_invoice_quote_footer_text',
75 'easy_invoice_quote_accept_quote_button' => 'easy_invoice_quote_accept_button',
76 'easy_invoice_quote_accept_quote_button_action' => 'easy_invoice_quote_accept_action',
77 'easy_invoice_accept_quote_text' => 'easy_invoice_quote_accept_text',
78 'easy_invoice_accepted_quote_message' => 'easy_invoice_quote_accepted_message',
79 'easy_invoice_decline_quote_reason_required' => 'easy_invoice_quote_decline_reason_required',
80 'easy_invoice_declined_quote_message' => 'easy_invoice_quote_declined_message',
81
82 // Currency settings - these exist in the plugin
83 'easy_invoice_currency' => 'easy_invoice_currency_code',
84 'easy_invoice_currency_symbol_type' => 'easy_invoice_currency_symbol_type',
85 'easy_invoice_currency_position' => 'easy_invoice_currency_position',
86 'easy_invoice_thousand_separator' => 'easy_invoice_thousands_separator',
87 'easy_invoice_decimal_separator' => 'easy_invoice_decimal_separator',
88 'easy_invoice_price_number_decimals' => 'easy_invoice_decimal_precision',
89
90 // Tax settings - these exist in the plugin
91 'easy_invoice_tax_type' => 'easy_invoice_tax_entry_method',
92 'easy_invoice_tax_percentage' => 'easy_invoice_tax_rate',
93
94 // Payment settings - these exist in the plugin
95 'easy_invoice_payment_methods' => 'easy_invoice_payment_methods',
96 'easy_invoice_payment_gateway_order' => 'easy_invoice_payment_gateway_order',
97 'easy_invoice_payment_email_enabled' => 'easy_invoice_payment_email_enabled',
98 'easy_invoice_payment_email_subject' => 'easy_invoice_payment_email_subject',
99 'easy_invoice_payment_email_body' => 'easy_invoice_payment_email_body',
100 'easy_invoice_payment_reminder_days' => 'easy_invoice_payment_reminder_days',
101 'easy_invoice_payment_terms' => 'easy_invoice_payment_terms',
102
103 // Additional settings that might exist
104 'easy_invoice_pre_defined_line_items' => 'easy_invoice_pre_defined_line_items',
105 'easy_invoice_client_portal_enabled' => 'easy_invoice_client_portal_enabled',
106 'easy_invoice_client_registration_enabled' => 'easy_invoice_client_registration_enabled',
107 'easy_invoice_client_login_redirect' => 'easy_invoice_client_login_redirect',
108 'easy_invoice_client_registration_redirect' => 'easy_invoice_client_registration_redirect',
109 ];
110
111 /**
112 * Check if migration is needed.
113 *
114 * @since 2.0.0
115 * @return bool
116 */
117 public function is_needed(): bool {
118 // Check if settings have already been migrated
119 if (get_option('easy_invoice_settings_migrated', false)) {
120 return false;
121 }
122
123 // Check if any old settings exist
124 foreach ($this->settings_mapping as $old_key => $new_key) {
125 if ($this->option_exists($old_key)) {
126 $this->log(sprintf('Found setting %s that needs migration', $old_key));
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 /**
135 * Run the migration.
136 *
137 * @since 2.0.0
138 * @return array
139 */
140 public function migrate(): array {
141 $this->log('Starting settings migration');
142
143 try {
144 $migrated_count = 0;
145 $errors = [];
146
147 // Simple migration: copy data from old options to new options
148 foreach ($this->settings_mapping as $old_key => $new_key) {
149 try {
150 $old_value = get_option($old_key);
151 if ($old_value !== false) {
152 // Transform the value if needed
153 $transformed_value = $this->transform_setting_value($old_key, $old_value);
154
155 // Copy the transformed value to new option
156 update_option($new_key, $transformed_value);
157
158 // Delete old option
159 //delete_option($old_key);
160
161 $migrated_count++;
162 $this->log(sprintf('Successfully migrated setting %s to %s (value: %s)', $old_key, $new_key, var_export($transformed_value, true)));
163 }
164 } catch (\Exception $e) {
165 $errors[] = sprintf('Failed to migrate setting %s: %s', $old_key, $e->getMessage());
166 $this->log(sprintf('Failed to migrate setting %s: %s', $old_key, $e->getMessage()), 'error');
167 }
168 }
169
170 // Special handling for payment gateways
171 $this->migrate_payment_gateways();
172
173 if (empty($errors)) {
174 // Set a flag to indicate settings migration is complete
175 update_option('easy_invoice_settings_migrated', true);
176 return $this->success_result(sprintf('Successfully migrated %d settings', $migrated_count));
177 } else {
178 return $this->error_result('Settings migration completed with errors: ' . implode(', ', $errors));
179 }
180
181 } catch (\Exception $e) {
182 $this->log('Settings migration failed: ' . $e->getMessage(), 'error');
183 return $this->error_result('Settings migration failed: ' . $e->getMessage());
184 }
185 }
186
187 /**
188 * Check if an option exists.
189 *
190 * @since 2.0.0
191 * @param string $option Option name
192 * @return bool
193 */
194 protected function option_exists(string $option): bool {
195 return get_option($option) !== false;
196 }
197
198 /**
199 * Transform setting value based on the setting type.
200 *
201 * @since 2.0.0
202 * @param string $old_key Old setting key
203 * @param mixed $value Setting value
204 * @return mixed Transformed value
205 */
206 protected function transform_setting_value(string $old_key, $value) {
207 // Handle tax_type transformation
208 if ($old_key === 'easy_invoice_tax_type') {
209 return $value === 'inclusive' ? 'yes' : 'no';
210 }
211
212 // Handle tax_percentage - ensure it's numeric
213 if ($old_key === 'easy_invoice_tax_percentage') {
214 return floatval($value);
215 }
216
217 // Handle number fields - ensure they're numeric
218 if (in_array($old_key, [
219 'easy_invoice_invoice_number',
220 'easy_invoice_quote_number',
221 'easy_invoice_price_number_decimals'
222 ])) {
223 return intval($value);
224 }
225
226 // Handle checkbox fields - ensure they're boolean strings
227 if (in_array($old_key, [
228 'easy_invoice_show_hide_adjust',
229 'easy_invoice_quote_show_hide_adjust',
230 'easy_invoice_quote_accept_quote_button',
231 'easy_invoice_decline_quote_reason_required',
232 'easy_invoice_payment_gateway_test_mode',
233 'easy_invoice_enable_proceed_to_payment'
234 ])) {
235 return $value ? 'yes' : 'no';
236 }
237
238 // Handle payment gateways - ensure it's an array
239 if ($old_key === 'easy_invoice_payment_gateways') {
240 if (is_array($value)) {
241 return $value;
242 } elseif (is_string($value)) {
243 // If it's a serialized string, unserialize it
244 $unserialized = maybe_unserialize($value);
245 return is_array($unserialized) ? $unserialized : [];
246 }
247 return [];
248 }
249
250 // Default: return value as-is
251 return $value;
252 }
253
254 /**
255 * Migrate payment gateways with special handling.
256 *
257 * @since 2.0.0
258 * @return void
259 */
260 protected function migrate_payment_gateways(): void {
261 $this->log('Starting payment gateways migration');
262
263 // Get old payment gateways
264 $old_gateways = get_option('easy_invoice_payment_gateways', []);
265 if (empty($old_gateways)) {
266 $this->log('No payment gateways found to migrate');
267 return;
268 }
269
270 // Transform old gateway format to new format
271 $new_gateways = [];
272 foreach ($old_gateways as $gateway_id => $enabled) {
273 if ($enabled) {
274 $new_gateways[] = $gateway_id;
275 }
276 }
277
278 // Save new payment gateways
279 if (!empty($new_gateways)) {
280 update_option('easy_invoice_payment_methods', $new_gateways);
281 $this->log(sprintf('Migrated payment gateways: %s', implode(', ', $new_gateways)));
282 }
283
284 // Handle individual gateway settings
285 $this->migrate_individual_gateway_settings();
286 }
287
288 /**
289 * Migrate individual gateway settings.
290 *
291 * @since 2.0.0
292 * @return void
293 */
294 protected function migrate_individual_gateway_settings(): void {
295 // Common gateway settings that might exist
296 $gateway_settings = [
297 'easy_invoice_paypal_enabled' => 'easy_invoice_paypal_enabled',
298 'easy_invoice_stripe_enabled' => 'easy_invoice_stripe_enabled',
299 'easy_invoice_bank_transfer_enabled' => 'easy_invoice_bank_transfer_enabled',
300 'easy_invoice_cash_enabled' => 'easy_invoice_cash_enabled',
301 'easy_invoice_cheque_enabled' => 'easy_invoice_cheque_enabled',
302 ];
303
304 foreach ($gateway_settings as $old_key => $new_key) {
305 $old_value = get_option($old_key);
306 if ($old_value !== false) {
307 $transformed_value = $this->transform_setting_value($old_key, $old_value);
308 update_option($new_key, $transformed_value);
309 $this->log(sprintf('Migrated gateway setting %s to %s', $old_key, $new_key));
310 }
311 }
312 }
313 }
314