PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.14
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Migrator.php

Migrator.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 2.1.14, at Inc/Core/Migrator.php

472 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 2.1.14 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2024 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FireBox\Core\Helpers\Form\Form;
20
21 class Migrator
22 {
23 /**
24 * The currently installed version.
25 *
26 * @var string
27 */
28 private $installedVersion;
29
30 /**
31 * Whether we run a migration.
32 *
33 * @var bool
34 */
35 private $run = false;
36
37 public function __construct($installedVersion = '')
38 {
39 $this->installedVersion = $installedVersion;
40 }
41
42 public function run()
43 {
44 if (!$this->installedVersion)
45 {
46 return;
47 }
48
49 $this->moveFireBoxDataToUploadsDirectory();
50 $this->installFormsTables();
51 $this->improveIndexes();
52 $this->updatePublishingRulesToConditionBuilder();
53 $this->updateEcommerceProductsInCartConditions();
54 $this->updateAnalyticsStoragePeriodSetting();
55 $this->update213version();
56
57 // Update firebox version
58 if ($this->run)
59 {
60 // Update version
61 update_option('firebox_version', FBOX_VERSION);
62 }
63 }
64
65 /**
66 * Moves plugin data to its own /wp-content/uploads/firebox folder.
67 *
68 * @since 1.1.10
69 *
70 * @return void
71 */
72 private function moveFireBoxDataToUploadsDirectory()
73 {
74 if (version_compare($this->installedVersion, '1.1.10', '>='))
75 {
76 return;
77 }
78
79 // Create dirs
80 \FireBox\Core\Helpers\Activation::createLibraryDirectories();
81
82 $this->run = true;
83 }
84
85 /**
86 * Creates the forms tables.
87 *
88 * @since 1.2.0
89 *
90 * @return void
91 */
92 private function installFormsTables()
93 {
94 if (version_compare($this->installedVersion, '1.2.0', '>='))
95 {
96 return;
97 }
98
99 if (!class_exists('Activation'))
100 {
101 require_once FBOX_PLUGIN_DIR . '/Inc/Framework/Inc/Admin/Includes/Activation.php';
102 }
103 $activation = new \Activation(firebox()->hook_data);
104 $activation->initTables();
105
106 $this->run = true;
107 }
108
109 /**
110 * Improves indexes on form and box logs tables.
111 *
112 * @since 1.2.4
113 *
114 * @return void
115 */
116 private function improveIndexes()
117 {
118 if (version_compare($this->installedVersion, '1.2.4', '>='))
119 {
120 return;
121 }
122
123 if (!$this->hasIndex('idx_box_id', 'firebox_logs'))
124 {
125 global $wpdb;
126 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs ADD INDEX `idx_box_id` (`box`, `id`)");
127 }
128
129 if (!$this->hasIndex('idx_meta_key', 'firebox_submission_meta'))
130 {
131 global $wpdb;
132 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_submission_meta ADD INDEX `idx_meta_key` (`meta_key`)");
133 }
134
135 $this->run = true;
136 }
137
138 /**
139 * Update old Publishing Rules to Condition Builder.
140 *
141 * @since 2.0.0
142 *
143 * @return void
144 */
145 private function updatePublishingRulesToConditionBuilder()
146 {
147 if (version_compare($this->installedVersion, '2.0.0', '>='))
148 {
149 return;
150 }
151
152 // Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned.
153 $boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]);
154
155 if (!$boxes->posts)
156 {
157 $this->run = true;
158 return;
159 }
160
161 foreach ($boxes->posts as $box)
162 {
163 // Get post meta and add it to its object
164 $meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID);
165
166 // Skip popup that already has rules
167 if (isset($meta['rules']))
168 {
169 continue;
170 }
171
172 $box->params = new \FPFramework\Libs\Registry($meta);
173
174 // Pass only params
175 \FPFramework\Base\Conditions\Migrator::run($box->params);
176
177 // Update popup settings
178 update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(wp_json_encode($box->params), true)));
179 }
180
181 $this->run = true;
182 }
183
184 /**
185 * Updates the following conditions to match the new condition settings.
186 *
187 * EDD\CartContainsProducts
188 * WooCommerce\CartContainsProducts
189 *
190 * @since 2.0.10
191 *
192 * @return void
193 */
194 private function updateEcommerceProductsInCartConditions()
195 {
196 if (version_compare($this->installedVersion, '2.0.10', '>='))
197 {
198 return;
199 }
200
201 // Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned.
202 $boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]);
203
204 if (!$boxes->posts)
205 {
206 $this->run = true;
207 return;
208 }
209
210 foreach ($boxes->posts as $box)
211 {
212 $meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID);
213
214 if (!isset($meta['rules']) || !is_array($meta['rules']))
215 {
216 continue;
217 }
218
219 $allowed_rules = [
220 'EDD\CartContainsProducts',
221 'WooCommerce\CartContainsProducts'
222 ];
223
224 $changed = false;
225
226 foreach ($meta['rules'] as &$ruleset)
227 {
228 if (!isset($ruleset['rules']))
229 {
230 continue;
231 }
232
233 foreach ($ruleset['rules'] as &$rule)
234 {
235 if (!isset($rule['name']))
236 {
237 continue;
238 }
239
240 if (!in_array($rule['name'], $allowed_rules))
241 {
242 continue;
243 }
244
245 if (!isset($rule['value']))
246 {
247 continue;
248 }
249
250 if (!is_array($rule['value']))
251 {
252 continue;
253 }
254
255 if (!count($rule['value']))
256 {
257 continue;
258 }
259
260 $changed = true;
261
262 $rule['value'] = array_map(function($id) {
263 // Old value wasn't an array, skip if new value was found
264 if (is_array($id))
265 {
266 return $id;
267 }
268
269 return [
270 'value' => $id,
271 'quantity_operator' => 'any',
272 'quantity_value1' => '1',
273 'quantity_value2' => '1'
274 ];
275 }, $rule['value']);
276 }
277 }
278
279 if (!$changed)
280 {
281 continue;
282 }
283
284 update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(wp_json_encode($meta), true)));
285 }
286
287 $this->run = true;
288 }
289
290 /**
291 * Updates the Analytics Storage Period setting.
292 *
293 * @since 2.1.1
294 *
295 * @return void
296 */
297 public function updateAnalyticsStoragePeriodSetting()
298 {
299 if (version_compare($this->installedVersion, '2.1.1', '>='))
300 {
301 return;
302 }
303
304 $params = get_option('firebox_settings');
305
306 $statsdays = isset($params['statsdays']) ? $params['statsdays'] : 730;
307 $statsdays_custom = isset($params['statsdays_custom']) ? $params['statsdays_custom'] : false;
308
309 if ($statsdays === 'custom')
310 {
311 $statsdays = $statsdays_custom;
312 }
313
314 $statsdays = (int) $statsdays;
315
316 if ($statsdays <= 365)
317 {
318 $statsdays = 365;
319 }
320 else if ($statsdays <= 365 * 2)
321 {
322 $statsdays = 365 * 2;
323 }
324 else
325 {
326 $statsdays = 365 * 5;
327 }
328
329 $params['statsdays'] = $statsdays;
330
331 update_option('firebox_settings', $params);
332
333 $this->run = true;
334 }
335
336 public function update213version()
337 {
338 $this->updateLogsDetailsParamsColumn();
339 $this->moveFormConversionsToLogsDetailsTable();
340 }
341
342 /**
343 * Adds a new column called event_source after event column.
344 * Renames the params column in the logs details table to event_label.
345 *
346 * @since 2.1.4
347 *
348 * @return void
349 */
350 public function updateLogsDetailsParamsColumn()
351 {
352 if (version_compare($this->installedVersion, '2.1.4', '>='))
353 {
354 return;
355 }
356
357 global $wpdb;
358
359 // If has column params
360 $row = $wpdb->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '{$wpdb->dbname}' AND table_name = '{$wpdb->prefix}firebox_logs_details' AND column_name = 'params'");
361 if (!$row)
362 {
363 $this->run = true;
364 return;
365 }
366
367 // Add column "event_source" after "event"
368 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs_details ADD COLUMN `event_source` varchar(200) NOT NULL AFTER `event`");
369
370 // Rename params to event_label
371 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs_details CHANGE COLUMN `params` `event_label` text NOT NULL");
372
373 $this->run = true;
374 }
375
376 /**
377 * Moves form conversions from submission meta table to logs details table.
378 *
379 * @since 2.1.4
380 *
381 * @return void
382 */
383 private function moveFormConversionsToLogsDetailsTable()
384 {
385 if (version_compare($this->installedVersion, '2.1.4', '>='))
386 {
387 return;
388 }
389
390 global $wpdb;
391
392 // Get all submission meta data
393 $boxlogids = firebox()->tables->submissionmeta->getResults([
394 'select' => [
395 "{$wpdb->prefix}firebox_submission_meta.id",
396 'l.box',
397 's.form_id',
398 "{$wpdb->prefix}firebox_submission_meta.meta_value",
399 "{$wpdb->prefix}firebox_submission_meta.created_at"
400 ],
401 'where' => [
402 'meta_key' => " = '" . esc_sql('box_log_id') . "'"
403 ],
404 'join' => [
405 "LEFT JOIN" => " {$wpdb->prefix}firebox_submissions as s ON s.id = submission_id LEFT JOIN {$wpdb->prefix}firebox_logs as l ON l.id = meta_value",
406 ]
407 ]);
408
409 if (!$boxlogids)
410 {
411 $this->run = true;
412 return;
413 }
414
415 global $wpdb;
416
417 // Migrate to the box logs details table
418 foreach ($boxlogids as $row)
419 {
420 if (!$row->box)
421 {
422 continue;
423 }
424
425 $form = Form::getFormByID($row->form_id);
426
427 $data = [
428 'log_id' => $row->meta_value,
429 'event' => 'conversion',
430 'event_source' => 'form',
431 'event_label' => isset($form['block']['attrs']['formName']) ? $form['block']['attrs']['formName'] : 'FireBox #' . $row->box . ' Form',
432 'date' => $row->created_at
433 ];
434
435 firebox()->tables->boxlogdetails->insert($data);
436 }
437
438 // Delete all submission_meta rows
439 $ids = implode(',', array_map('intval', array_column($boxlogids, 'id')));
440 $sm_table_name = firebox()->tables->submissionmeta->getFullTableName();
441 $wpdb->query("DELETE FROM $sm_table_name WHERE id IN($ids)");// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
442
443 $this->run = true;
444 }
445
446 /**
447 * Checks whether an index exists in a table.
448 *
449 * @param string $index
450 * @param string $table
451 *
452 * @return bool
453 */
454 private function hasIndex($index = '', $table = '')
455 {
456 if (empty($index) || empty($table))
457 {
458 return;
459 }
460
461 global $wpdb;
462
463 $existing = $wpdb->get_row("SHOW CREATE TABLE `{$wpdb->prefix}{$table}`", ARRAY_N);// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
464
465 if (isset($existing[1]) && strpos(strtolower($existing[1]), 'key `' . $index . '` (') !== false)
466 {
467 return true;
468 }
469
470 return false;
471 }
472 }