PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.22
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.22
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.22, at Inc/Core/Migrator.php

518 lines 10.7 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.22 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 $this->addMissingCapabilities();
57
58 // Update firebox version
59 if ($this->run)
60 {
61 // Update version
62 update_option('firebox_version', FBOX_VERSION);
63 }
64 }
65
66 /**
67 * Moves plugin data to its own /wp-content/uploads/firebox folder.
68 *
69 * @since 1.1.10
70 *
71 * @return void
72 */
73 private function moveFireBoxDataToUploadsDirectory()
74 {
75 if (version_compare($this->installedVersion, '1.1.10', '>='))
76 {
77 return;
78 }
79
80 // Create dirs
81 \FireBox\Core\Helpers\Activation::createLibraryDirectories();
82
83 $this->run = true;
84 }
85
86 /**
87 * Creates the forms tables.
88 *
89 * @since 1.2.0
90 *
91 * @return void
92 */
93 private function installFormsTables()
94 {
95 if (version_compare($this->installedVersion, '1.2.0', '>='))
96 {
97 return;
98 }
99
100 if (!class_exists('Activation'))
101 {
102 require_once FBOX_PLUGIN_DIR . '/Inc/Framework/Inc/Admin/Includes/Activation.php';
103 }
104 $activation = new \Activation(firebox()->hook_data);
105 $activation->initTables();
106
107 $this->run = true;
108 }
109
110 /**
111 * Improves indexes on form and box logs tables.
112 *
113 * @since 1.2.4
114 *
115 * @return void
116 */
117 private function improveIndexes()
118 {
119 if (version_compare($this->installedVersion, '1.2.4', '>='))
120 {
121 return;
122 }
123
124 if (!$this->hasIndex('idx_box_id', 'firebox_logs'))
125 {
126 global $wpdb;
127 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs ADD INDEX `idx_box_id` (`box`, `id`)");
128 }
129
130 if (!$this->hasIndex('idx_meta_key', 'firebox_submission_meta'))
131 {
132 global $wpdb;
133 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_submission_meta ADD INDEX `idx_meta_key` (`meta_key`)");
134 }
135
136 $this->run = true;
137 }
138
139 /**
140 * Update old Publishing Rules to Condition Builder.
141 *
142 * @since 2.0.0
143 *
144 * @return void
145 */
146 private function updatePublishingRulesToConditionBuilder()
147 {
148 if (version_compare($this->installedVersion, '2.0.0', '>='))
149 {
150 return;
151 }
152
153 // Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned.
154 $boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]);
155
156 if (!$boxes->posts)
157 {
158 $this->run = true;
159 return;
160 }
161
162 foreach ($boxes->posts as $box)
163 {
164 // Get post meta and add it to its object
165 $meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID);
166
167 // Skip popup that already has rules
168 if (isset($meta['rules']))
169 {
170 continue;
171 }
172
173 $box->params = new \FPFramework\Libs\Registry($meta);
174
175 // Pass only params
176 \FPFramework\Base\Conditions\Migrator::run($box->params);
177
178 // Update popup settings
179 update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(wp_json_encode($box->params), true)));
180 }
181
182 $this->run = true;
183 }
184
185 /**
186 * Updates the following conditions to match the new condition settings.
187 *
188 * EDD\CartContainsProducts
189 * WooCommerce\CartContainsProducts
190 *
191 * @since 2.0.10
192 *
193 * @return void
194 */
195 private function updateEcommerceProductsInCartConditions()
196 {
197 if (version_compare($this->installedVersion, '2.0.10', '>='))
198 {
199 return;
200 }
201
202 // Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned.
203 $boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]);
204
205 if (!$boxes->posts)
206 {
207 $this->run = true;
208 return;
209 }
210
211 foreach ($boxes->posts as $box)
212 {
213 $meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID);
214
215 if (!isset($meta['rules']) || !is_array($meta['rules']))
216 {
217 continue;
218 }
219
220 $allowed_rules = [
221 'EDD\CartContainsProducts',
222 'WooCommerce\CartContainsProducts'
223 ];
224
225 $changed = false;
226
227 foreach ($meta['rules'] as &$ruleset)
228 {
229 if (!isset($ruleset['rules']))
230 {
231 continue;
232 }
233
234 foreach ($ruleset['rules'] as &$rule)
235 {
236 if (!isset($rule['name']))
237 {
238 continue;
239 }
240
241 if (!in_array($rule['name'], $allowed_rules))
242 {
243 continue;
244 }
245
246 if (!isset($rule['value']))
247 {
248 continue;
249 }
250
251 if (!is_array($rule['value']))
252 {
253 continue;
254 }
255
256 if (!count($rule['value']))
257 {
258 continue;
259 }
260
261 $changed = true;
262
263 $rule['value'] = array_map(function($id) {
264 // Old value wasn't an array, skip if new value was found
265 if (is_array($id))
266 {
267 return $id;
268 }
269
270 return [
271 'value' => $id,
272 'quantity_operator' => 'any',
273 'quantity_value1' => '1',
274 'quantity_value2' => '1'
275 ];
276 }, $rule['value']);
277 }
278 }
279
280 if (!$changed)
281 {
282 continue;
283 }
284
285 update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(wp_json_encode($meta), true)));
286 }
287
288 $this->run = true;
289 }
290
291 /**
292 * Updates the Analytics Storage Period setting.
293 *
294 * @since 2.1.1
295 *
296 * @return void
297 */
298 public function updateAnalyticsStoragePeriodSetting()
299 {
300 if (version_compare($this->installedVersion, '2.1.1', '>='))
301 {
302 return;
303 }
304
305 $params = get_option('firebox_settings');
306
307 $statsdays = isset($params['statsdays']) ? $params['statsdays'] : 730;
308 $statsdays_custom = isset($params['statsdays_custom']) ? $params['statsdays_custom'] : false;
309
310 if ($statsdays === 'custom')
311 {
312 $statsdays = $statsdays_custom;
313 }
314
315 $statsdays = (int) $statsdays;
316
317 if ($statsdays <= 365)
318 {
319 $statsdays = 365;
320 }
321 else if ($statsdays <= 365 * 2)
322 {
323 $statsdays = 365 * 2;
324 }
325 else
326 {
327 $statsdays = 365 * 5;
328 }
329
330 $params['statsdays'] = $statsdays;
331
332 update_option('firebox_settings', $params);
333
334 $this->run = true;
335 }
336
337 public function update213version()
338 {
339 $this->updateLogsDetailsParamsColumn();
340 $this->moveFormConversionsToLogsDetailsTable();
341 }
342
343 /**
344 * Adds a new column called event_source after event column.
345 * Renames the params column in the logs details table to event_label.
346 *
347 * @since 2.1.4
348 *
349 * @return void
350 */
351 public function updateLogsDetailsParamsColumn()
352 {
353 if (version_compare($this->installedVersion, '2.1.4', '>='))
354 {
355 return;
356 }
357
358 global $wpdb;
359
360 // If has column params
361 $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'");
362 if (!$row)
363 {
364 $this->run = true;
365 return;
366 }
367
368 // Add column "event_source" after "event"
369 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs_details ADD COLUMN `event_source` varchar(200) NOT NULL AFTER `event`");
370
371 // Rename params to event_label
372 $wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs_details CHANGE COLUMN `params` `event_label` text NOT NULL");
373
374 $this->run = true;
375 }
376
377 /**
378 * Moves form conversions from submission meta table to logs details table.
379 *
380 * @since 2.1.4
381 *
382 * @return void
383 */
384 private function moveFormConversionsToLogsDetailsTable()
385 {
386 if (version_compare($this->installedVersion, '2.1.4', '>='))
387 {
388 return;
389 }
390
391 global $wpdb;
392
393 // Get all submission meta data
394 $boxlogids = firebox()->tables->submissionmeta->getResults([
395 'select' => [
396 "{$wpdb->prefix}firebox_submission_meta.id",
397 'l.box',
398 's.form_id',
399 "{$wpdb->prefix}firebox_submission_meta.meta_value",
400 "{$wpdb->prefix}firebox_submission_meta.created_at"
401 ],
402 'where' => [
403 'meta_key' => " = '" . esc_sql('box_log_id') . "'"
404 ],
405 'join' => [
406 "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",
407 ]
408 ]);
409
410 if (!$boxlogids)
411 {
412 $this->run = true;
413 return;
414 }
415
416 global $wpdb;
417
418 // Migrate to the box logs details table
419 foreach ($boxlogids as $row)
420 {
421 if (!$row->box)
422 {
423 continue;
424 }
425
426 $form = Form::getFormByID($row->form_id);
427
428 $data = [
429 'log_id' => $row->meta_value,
430 'event' => 'conversion',
431 'event_source' => 'form',
432 'event_label' => isset($form['block']['attrs']['formName']) ? $form['block']['attrs']['formName'] : 'FireBox #' . $row->box . ' Form',
433 'date' => $row->created_at
434 ];
435
436 firebox()->tables->boxlogdetails->insert($data);
437 }
438
439 // Delete all submission_meta rows
440 $ids = implode(',', array_map('intval', array_column($boxlogids, 'id')));
441 $sm_table_name = firebox()->tables->submissionmeta->getFullTableName();
442 $wpdb->query("DELETE FROM $sm_table_name WHERE id IN($ids)");// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
443
444 $this->run = true;
445 }
446
447 public function addMissingCapabilities()
448 {
449 if (version_compare($this->installedVersion, '2.1.15', '>='))
450 {
451 return;
452 }
453
454 $cap_name = 'read_fireboxes';
455
456 $admin = get_role('administrator');
457
458 if ($admin)
459 {
460 if ($admin->has_cap($cap_name))
461 {
462 return true;
463 }
464
465 $admin->add_cap($cap_name);
466 }
467 else
468 {
469 $roles = get_editable_roles();
470
471 foreach ($roles as $role_name => $data)
472 {
473 if (isset($data['capabilities']['manage_options']) && $data['capabilities']['manage_options'])
474 {
475 $role = get_role($role_name);
476
477 if ($role->has_cap($cap_name))
478 {
479 continue;
480 }
481
482 $role->add_cap($cap_name);
483 }
484 }
485 }
486
487 wp_get_current_user()->get_role_caps();
488
489 return true;
490 }
491
492 /**
493 * Checks whether an index exists in a table.
494 *
495 * @param string $index
496 * @param string $table
497 *
498 * @return bool
499 */
500 private function hasIndex($index = '', $table = '')
501 {
502 if (empty($index) || empty($table))
503 {
504 return;
505 }
506
507 global $wpdb;
508
509 $existing = $wpdb->get_row("SHOW CREATE TABLE `{$wpdb->prefix}{$table}`", ARRAY_N);// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
510
511 if (isset($existing[1]) && strpos(strtolower($existing[1]), 'key `' . $index . '` (') !== false)
512 {
513 return true;
514 }
515
516 return false;
517 }
518 }