| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.16 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 |
$this->updateEcommerceProductsInCartConditions(); |
| 52 |
|
| 53 |
// Update firebox version |
| 54 |
if ($this->run) |
| 55 |
{ |
| 56 |
// Update version |
| 57 |
update_option('firebox_version', FBOX_VERSION); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Moves plugin data to its own /wp-content/uploads/firebox folder. |
| 63 |
* |
| 64 |
* @since 1.1.10 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
private function moveFireBoxDataToUploadsDirectory() |
| 69 |
{ |
| 70 |
if (version_compare($this->installedVersion, '1.1.10', '>=')) |
| 71 |
{ |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// Create dirs |
| 76 |
\FireBox\Core\Helpers\Activation::createLibraryDirectories(); |
| 77 |
|
| 78 |
$this->run = true; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Creates the forms tables. |
| 83 |
* |
| 84 |
* @since 1.2.0 |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
private function installFormsTables() |
| 89 |
{ |
| 90 |
if (version_compare($this->installedVersion, '1.2.0', '>=')) |
| 91 |
{ |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (!class_exists('Activation')) |
| 96 |
{ |
| 97 |
require_once FBOX_PLUGIN_DIR . '/Inc/Framework/Inc/Admin/Includes/Activation.php'; |
| 98 |
} |
| 99 |
$activation = new \Activation(firebox()->hook_data); |
| 100 |
$activation->initTables(); |
| 101 |
|
| 102 |
$this->run = true; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Improves indexes on form and box logs tables. |
| 107 |
* |
| 108 |
* @since 1.2.4 |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function improveIndexes() |
| 113 |
{ |
| 114 |
if (version_compare($this->installedVersion, '1.2.4', '>=')) |
| 115 |
{ |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if (!$this->hasIndex('idx_box_id', 'firebox_logs')) |
| 120 |
{ |
| 121 |
global $wpdb; |
| 122 |
$wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_logs ADD INDEX `idx_box_id` (`box`, `id`)"); |
| 123 |
} |
| 124 |
|
| 125 |
if (!$this->hasIndex('idx_meta_key', 'firebox_submission_meta')) |
| 126 |
{ |
| 127 |
global $wpdb; |
| 128 |
$wpdb->query("ALTER TABLE {$wpdb->prefix}firebox_submission_meta ADD INDEX `idx_meta_key` (`meta_key`)"); |
| 129 |
} |
| 130 |
|
| 131 |
$this->run = true; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update old Publishing Rules to Condition Builder. |
| 136 |
* |
| 137 |
* @since 2.0.0 |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
private function updatePublishingRulesToConditionBuilder() |
| 142 |
{ |
| 143 |
if (version_compare($this->installedVersion, '2.0.0', '>=')) |
| 144 |
{ |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
// Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned. |
| 149 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]); |
| 150 |
|
| 151 |
if (!$boxes->posts) |
| 152 |
{ |
| 153 |
$this->run = true; |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
foreach ($boxes->posts as $box) |
| 158 |
{ |
| 159 |
// Get post meta and add it to its object |
| 160 |
$meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID); |
| 161 |
|
| 162 |
// Skip popup that already has rules |
| 163 |
if (isset($meta['rules'])) |
| 164 |
{ |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
$box->params = new \FPFramework\Libs\Registry($meta); |
| 169 |
|
| 170 |
// Pass only params |
| 171 |
\FPFramework\Base\Conditions\Migrator::run($box->params); |
| 172 |
|
| 173 |
// Update popup settings |
| 174 |
update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(json_encode($box->params), true))); |
| 175 |
} |
| 176 |
|
| 177 |
$this->run = true; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Updates the following conditions to match the new condition settings. |
| 182 |
* |
| 183 |
* EDD\CartContainsProducts |
| 184 |
* WooCommerce\CartContainsProducts |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
private function updateEcommerceProductsInCartConditions() |
| 189 |
{ |
| 190 |
if (version_compare($this->installedVersion, '2.0.10', '>=')) |
| 191 |
{ |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// Get all boxes, regardless of post status, thus we pass [] to ensure all popups are returned. |
| 196 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes([]); |
| 197 |
|
| 198 |
if (!$boxes->posts) |
| 199 |
{ |
| 200 |
$this->run = true; |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
foreach ($boxes->posts as $box) |
| 205 |
{ |
| 206 |
$meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID); |
| 207 |
|
| 208 |
if (!isset($meta['rules']) || !is_array($meta['rules'])) |
| 209 |
{ |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
$allowed_rules = [ |
| 214 |
'EDD\CartContainsProducts', |
| 215 |
'WooCommerce\CartContainsProducts' |
| 216 |
]; |
| 217 |
|
| 218 |
$changed = false; |
| 219 |
|
| 220 |
foreach ($meta['rules'] as &$ruleset) |
| 221 |
{ |
| 222 |
if (!isset($ruleset['rules'])) |
| 223 |
{ |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
foreach ($ruleset['rules'] as &$rule) |
| 228 |
{ |
| 229 |
if (!isset($rule['name'])) |
| 230 |
{ |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
if (!in_array($rule['name'], $allowed_rules)) |
| 235 |
{ |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
if (!isset($rule['value'])) |
| 240 |
{ |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
if (!is_array($rule['value'])) |
| 245 |
{ |
| 246 |
continue; |
| 247 |
} |
| 248 |
|
| 249 |
if (!count($rule['value'])) |
| 250 |
{ |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
$changed = true; |
| 255 |
|
| 256 |
$rule['value'] = array_map(function($id) { |
| 257 |
// Old value wasn't an array, skip if new value was found |
| 258 |
if (is_array($id)) |
| 259 |
{ |
| 260 |
return $id; |
| 261 |
} |
| 262 |
|
| 263 |
return [ |
| 264 |
'value' => $id, |
| 265 |
'quantity_operator' => 'any', |
| 266 |
'quantity_value1' => '1', |
| 267 |
'quantity_value2' => '1' |
| 268 |
]; |
| 269 |
}, $rule['value']); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (!$changed) |
| 274 |
{ |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
update_post_meta($box->ID, 'fpframework_meta_settings', wp_slash(json_decode(json_encode($meta), true))); |
| 279 |
} |
| 280 |
|
| 281 |
$this->run = true; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Checks whether an index exists in a table. |
| 286 |
* |
| 287 |
* @param string $index |
| 288 |
* @param string $table |
| 289 |
* |
| 290 |
* @return bool |
| 291 |
*/ |
| 292 |
private function hasIndex($index = '', $table = '') |
| 293 |
{ |
| 294 |
if (empty($index) || empty($table)) |
| 295 |
{ |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
global $wpdb; |
| 300 |
|
| 301 |
$existing = $wpdb->get_row("SHOW CREATE TABLE `{$wpdb->prefix}{$table}`", ARRAY_N); |
| 302 |
|
| 303 |
if (isset($existing[1]) && strpos(strtolower($existing[1]), 'key `' . $index . '` (') !== false) |
| 304 |
{ |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
return false; |
| 309 |
} |
| 310 |
} |