class-fl-builder-update.php
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/**
* Helper class for builder updates.
*
* @since 1.2.8
*/
final class FLBuilderUpdate {
/**
* Initialize hooks.
*
* @since 1.8
* @return void
*/
static public function init()
{
add_action( 'init', __CLASS__ . '::maybe_run', 11 );
}
/**
* Checks to see if an update should be run. If it should,
* the appropriate update method is run and the version
* number is updated in the database.
*
* @since 1.2.8
* @return void
*/
static public function maybe_run()
{
// Make sure the user is logged in.
if ( ! is_user_logged_in() ) {
return;
}
// Don't update for dev copies.
if ( FL_BUILDER_VERSION == '{FL_BUILDER_VERSION}' ) {
return;
}
// Get the saved version.
$saved_version = get_site_option( '_fl_builder_version' );
// No saved version number. This must be a fresh install.
if ( ! $saved_version ) {
update_site_option( '_fl_builder_version', FL_BUILDER_VERSION );
return;
}
// Only run updates if the version numbers don't match.
else if ( ! version_compare( $saved_version, FL_BUILDER_VERSION, '=' ) ) {
if ( is_multisite() ) {
self::run_multisite( $saved_version );
}
else {
self::run( $saved_version );
}
update_site_option( '_fl_builder_version', FL_BUILDER_VERSION );
}
}
/**
* Runs the update for a specific version.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function run( $saved_version )
{
// Update to 1.2.8 or greater.
if ( version_compare( $saved_version, '1.2.8', '<' ) ) {
self::v_1_2_8();
}
// Update to 1.4.6 or greater.
if ( version_compare( $saved_version, '1.4.6', '<' ) ) {
self::v_1_4_6();
}
// Update to 1.6.3 or greater.
if ( version_compare( $saved_version, '1.6.3', '<' ) ) {
self::v_1_6_3();
}
// Clear all asset cache.
FLBuilderModel::delete_asset_cache_for_all_posts();
}
/**
* Runs the update for all sites on a network install.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function run_multisite( $saved_version )
{
global $blog_id;
global $wpdb;
// Save the original blog id.
$original_blog_id = $blog_id;
// Get all blog ids.
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
// Loop through the blog ids and run the update.
foreach ( $blog_ids as $id ) {
switch_to_blog( $id );
self::run( $saved_version );
}
// Revert to the original blog.
switch_to_blog( $original_blog_id );
}
/**
* Check for the fl_builder_nodes table that existed before 1.2.8.
*
* @since 1.2.8
* @access private
* @return bool
*/
static private function pre_1_2_8_table_exists()
{
global $wpdb;
$table = $wpdb->prefix . 'fl_builder_nodes';
$results = $wpdb->get_results("SHOW TABLES LIKE '{$table}'");
return count($results) > 0;
}
/**
* Check to see if the fl_builder_nodes table that existed before 1.2.8
* is empty or not.
*
* @since 1.2.8
* @access private
* @return bool
*/
static private function pre_1_2_8_table_is_empty()
{
global $wpdb;
if(self::pre_1_2_8_table_exists()) {
$table = $wpdb->prefix . 'fl_builder_nodes';
$nodes = $wpdb->get_results("SELECT * FROM {$table}");
return count($nodes) === 0;
}
return true;
}
/**
* Saves a backup of the pre 1.2.8 database table.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function pre_1_2_8_backup()
{
global $wpdb;
if(self::pre_1_2_8_table_exists()) {
$cache_dir = FLBuilderModel::get_cache_dir();
$table = $wpdb->prefix . 'fl_builder_nodes';
// Get the data to backup.
$nodes = $wpdb->get_results("SELECT * FROM {$table}");
$meta = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_fl_builder_layout'");
// Build the export object.
$data = new StdClass();
$data->version = FL_BUILDER_VERSION;
$data->nodes = $nodes;
$data->meta = $meta;
// Save the backup.
file_put_contents($cache_dir['path'] . 'backup.dat', serialize($data));
}
}
/**
* Restores a site to pre 1.2.8.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function pre_1_2_8_restore()
{
global $wpdb;
if(!self::pre_1_2_8_table_exists() || self::pre_1_2_8_table_is_empty()) {
$cache_dir = FLBuilderModel::get_cache_dir();
$backup_path = $cache_dir['path'] . 'backup.dat';
// Install the database.
FLBuilderModel::install_database();
// Check for the backup file.
if(file_exists($backup_path)) {
// Get the backup data.
$backup = unserialize(file_get_contents($backup_path));
// Check for the correct backup data.
if(!isset($backup->nodes) || !isset($backup->meta)) {
return;
}
// Restore the nodes.
foreach($backup->nodes as $node) {
$wpdb->insert("{$wpdb->prefix}fl_builder_nodes",
array(
'node' => $node->node,
'type' => $node->type,
'layout' => $node->layout,
'parent' => $node->parent,
'position' => $node->position,
'settings' => $node->settings,
'status' => $node->status
),
array('%s', '%s', '%s', '%s', '%d', '%s', '%s')
);
}
// Restore the meta.
foreach($backup->meta as $meta) {
update_post_meta($meta->post_id, '_fl_builder_layout', $meta->meta_value);
}
}
}
}
/**
* Update to version 1.2.8 or later.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function v_1_2_8()
{
global $wpdb;
if(self::pre_1_2_8_table_exists()) {
$table = $wpdb->prefix . 'fl_builder_nodes';
$metas = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_fl_builder_layout'");
$cache_dir = FLBuilderModel::get_cache_dir();
// Loop through the layout ids for each post.
foreach($metas as $meta) {
// Get the old layout nodes from the database.
$published = $wpdb->get_results("SELECT * FROM {$table} WHERE layout = '{$meta->meta_value}' AND status = 'published'");
$draft = $wpdb->get_results("SELECT * FROM {$table} WHERE layout = '{$meta->meta_value}' AND status = 'draft'");
// Convert the old nodes to new ones.
$published = self::v_1_2_8_convert_nodes($published);
$draft = self::v_1_2_8_convert_nodes($draft);
// Add the new layout post meta.
update_post_meta($meta->post_id, '_fl_builder_data', $published);
update_post_meta($meta->post_id, '_fl_builder_draft', $draft);
}
// Backup the old builder table.
self::pre_1_2_8_backup();
// Drop the old builder table.
if(file_exists($cache_dir['path'] . 'backup.dat')) {
$wpdb->query("DROP TABLE {$wpdb->prefix}fl_builder_nodes");
}
// Delete old post meta.
delete_post_meta_by_key('_fl_builder_layout');
delete_post_meta_by_key('_fl_builder_layout_export');
delete_post_meta_by_key('_fl_builder_css');
delete_post_meta_by_key('_fl_builder_css-draft');
delete_post_meta_by_key('_fl_builder_js');
delete_post_meta_by_key('_fl_builder_js-draft');
// Convert global settings.
self::v_1_2_8_convert_global_settings();
// Delete all asset cache.
$css = glob( $cache_dir['path'] . '*.css' );
$js = glob( $cache_dir['path'] . '*.js' );
if ( is_array( $css ) ) {
array_map( 'unlink', $css );
}
if ( is_array( $js ) ) {
array_map( 'unlink', $js );
}
}
}
/**
* Convert the global settings for 1.2.8 or later.
*
* @since 1.2.8
* @access private
* @return void
*/
static private function v_1_2_8_convert_global_settings()
{
$settings = get_option('_fl_builder_settings');
if($settings && is_string($settings)) {
update_option('_fl_builder_settings', json_decode($settings));
}
}
/**
* Convert the nodes for 1.2.8 or earlier.
*
* @since 1.2.8
* @access private
* @param array $nodes An array of node data.
* @return array
*/
static private function v_1_2_8_convert_nodes($nodes)
{
$new_nodes = array();
foreach($nodes as $node) {
unset($node->id);
unset($node->layout);
unset($node->status);
if($node->type == 'row') {
$node->parent = null;
}
$node->settings = self::v_1_2_8_json_decode_settings($node->settings);
$new_nodes[$node->node] = $node;
}
return $new_nodes;
}
/**
* Convert a JSON encoded settings string for 1.2.8 or earlier.
*
* @since 1.2.8
* @access private
* @param object $settings The settings object.
* @return object
*/
static private function v_1_2_8_json_decode_settings($settings)
{
if(!$settings || empty($settings)) {
return null;
}
$settings = json_decode($settings);
foreach($settings as $key => $val) {
if(is_string($val)) {
$decoded = json_decode($val);
if(is_object($decoded) || is_array($decoded)) {
$settings->{$key} = $decoded;
}
}
else if(is_array($val)) {
foreach($val as $sub_key => $sub_val) {
if(is_string($sub_val)) {
$decoded = json_decode($sub_val);
if(is_object($decoded) || is_array($decoded)) {
$settings->{$key}[$sub_key] = $decoded;
}
}
}
}
}
return $settings;
}
/**
* Update to version 1.4.6 or later.
*
* @since 1.4.6
* @access private
* @return void
*/
static private function v_1_4_6()
{
// Remove the old fl-builder uploads folder.
$filesystem = FLBuilderUtils::get_filesystem();
$upload_dir = wp_upload_dir();
$path = trailingslashit( $upload_dir['basedir'] ) . 'fl-builder';
if ( file_exists( $path ) ) {
$filesystem->rmdir( $path, true );
}
}
/**
* Update to version 1.6.3 or later.
*
* @since 1.6.3
* @access private
* @return void
*/
static private function v_1_6_3()
{
$posts = get_posts( array(
'post_type' => 'fl-builder-template',
'posts_per_page' => '-1'
) );
foreach ( $posts as $post ) {
$type = wp_get_post_terms( $post->ID, 'fl-builder-template-type' );
if ( 0 === count( $type ) ) {
wp_set_post_terms( $post->ID, 'layout', 'fl-builder-template-type' );
}
}
}
}
FLBuilderUpdate::init();