| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.6 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 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 |
class Migrator |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The currently installed version. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $installedVersion; |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether we run a migration. |
| 30 |
* |
| 31 |
* @var bool |
| 32 |
*/ |
| 33 |
private $run = false; |
| 34 |
|
| 35 |
public function __construct($installedVersion = '') |
| 36 |
{ |
| 37 |
$this->installedVersion = $installedVersion; |
| 38 |
} |
| 39 |
|
| 40 |
public function run() |
| 41 |
{ |
| 42 |
if (!$this->installedVersion) |
| 43 |
{ |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$this->moveFireBoxDataToUploadsDirectory(); |
| 48 |
$this->installFormsTables(); |
| 49 |
$this->improveIndexes(); |
| 50 |
$this->updatePublishingRulesToConditionBuilder(); |
| 51 |
|
| 52 |
// Update firebox version |
| 53 |
if ($this->run) |
| 54 |
{ |
| 55 |
// Update version |
| 56 |
update_option('firebox_version', FBOX_VERSION); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Moves plugin data to its own /wp-content/uploads/firebox folder. |
| 62 |
* |
| 63 |
* @since 1.1.10 |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
private function moveFireBoxDataToUploadsDirectory() |
| 68 |
{ |
| 69 |
if (version_compare($this->installedVersion, '1.1.10', '>=')) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Create dirs |
| 75 |
\FireBox\Core\Helpers\Activation::createLibraryDirectories(); |
| 76 |
|
| 77 |
$this->run = true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Creates the forms tables. |
| 82 |
* |
| 83 |
* @since 1.2.0 |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
private function installFormsTables() |
| 88 |
{ |
| 89 |
if (version_compare($this->installedVersion, '1.2.0', '>=')) |
| 90 |
{ |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if (!class_exists('Activation')) |
| 95 |
{ |
| 96 |
require_once FBOX_PLUGIN_DIR . '/Inc/Framework/Inc/Admin/Includes/Activation.php'; |
| 97 |
} |
| 98 |
$activation = new \Activation(firebox()->hook_data); |
| 99 |
$activation->initTables(); |
| 100 |
|
| 101 |
$this->run = true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Improves indexes on form and box logs tables. |
| 106 |
* |
| 107 |
* @since 1.2.4 |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function improveIndexes() |
| 112 |
{ |
| 113 |
if (version_compare($this->installedVersion, '1.2.4', '>=')) |
| 114 |
{ |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (!$this->hasIndex('idx_box_id', 'firebox_logs')) |
| 119 |
{ |
| 120 |
global $wpdb; |
| 121 |
$wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs ADD INDEX `idx_box_id` (`box`, `id`)"); |
| 122 |
} |
| 123 |
|
| 124 |
if (!$this->hasIndex('idx_meta_key', 'firebox_submission_meta')) |
| 125 |
{ |
| 126 |
global $wpdb; |
| 127 |
$wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_submission_meta ADD INDEX `idx_meta_key` (`meta_key`)"); |
| 128 |
} |
| 129 |
|
| 130 |
$this->run = true; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Update old Publishing Rules to Condition Builder. |
| 135 |
* |
| 136 |
* @since 2.0.0 |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
private function updatePublishingRulesToConditionBuilder() |
| 141 |
{ |
| 142 |
if (version_compare($this->installedVersion, '2.0.0', '>=')) |
| 143 |
{ |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
// Get all boxes, thus we pass [] to ensure all popups are returned. |
| 148 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]); |
| 149 |
|
| 150 |
if (!$boxes->posts) |
| 151 |
{ |
| 152 |
$this->run = true; |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
foreach ($boxes->posts as $box) |
| 157 |
{ |
| 158 |
// Get post meta and add it to its object |
| 159 |
$meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID); |
| 160 |
|
| 161 |
// Skip popup that already has rules |
| 162 |
if (isset($meta['rules'])) |
| 163 |
{ |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$box->params = new \FPFramework\Libs\Registry($meta); |
| 168 |
|
| 169 |
// Pass only params |
| 170 |
\FPFramework\Base\Conditions\Migrator::run($box->params); |
| 171 |
|
| 172 |
// Update popup settings |
| 173 |
update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(json_encode($box->params), true))); |
| 174 |
} |
| 175 |
|
| 176 |
$this->run = true; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Checks whether an index exists in a table. |
| 181 |
* |
| 182 |
* @param string $index |
| 183 |
* @param string $table |
| 184 |
* |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
private function hasIndex($index = '', $table = '') |
| 188 |
{ |
| 189 |
if (empty($index) || empty($table)) |
| 190 |
{ |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
global $wpdb; |
| 195 |
|
| 196 |
$existing = $wpdb->get_row("SHOW CREATE TABLE `{$wpdb->prefix}{$table}`", ARRAY_N); |
| 197 |
|
| 198 |
if (isset($existing[1]) && strpos(strtolower($existing[1]), 'key `' . $index . '` (') !== false) |
| 199 |
{ |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
return false; |
| 204 |
} |
| 205 |
} |