PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / trunk
Pods – Custom Content Types and Fields vtrunk
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / sql / upgrade / PodsUpgrade_2_0_0.php
pods / sql / upgrade Last commit date
PodsUpgrade.php 4 months ago PodsUpgrade_2_0_0.php 4 months ago PodsUpgrade_2_1_0.php 4 months ago
PodsUpgrade_2_0_0.php
1029 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Upgrade
10 */
11 class PodsUpgrade_2_0_0 extends PodsUpgrade {
12
13 /**
14 * @var string
15 */
16 protected $version = '2.0.0';
17
18 /**
19 * @return array|bool|int|mixed|null|void
20 */
21 public function prepare_pods() {
22 /**
23 * @var $wpdb WPDB
24 */
25 global $wpdb;
26
27 if ( ! in_array( "{$wpdb->prefix}pod_types", $this->tables, true ) ) {
28 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
29 }
30
31 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_types`', false );
32
33 if ( ! empty( $count ) ) {
34 $count = (int) $count[0]->count;
35 } else {
36 $count = 0;
37 }
38
39 return $count;
40 }
41
42 /**
43 * @return array|bool|int|mixed|null|void
44 */
45 public function prepare_fields() {
46 /**
47 * @var $wpdb WPDB
48 */
49 global $wpdb;
50
51 if ( ! in_array( "{$wpdb->prefix}pod_fields", $this->tables, true ) ) {
52 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
53 }
54
55 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_fields`', false );
56
57 if ( ! empty( $count ) ) {
58 $count = (int) $count[0]->count;
59 } else {
60 $count = 0;
61 }
62
63 return $count;
64 }
65
66 /**
67 * @return array|bool|int|mixed|null|void
68 */
69 public function prepare_relationships() {
70 /**
71 * @var $wpdb WPDB
72 */
73 global $wpdb;
74
75 if ( ! in_array( "{$wpdb->prefix}pod_rel", $this->tables, true ) ) {
76 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
77 }
78
79 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_rel`', false );
80
81 if ( ! empty( $count ) ) {
82 $count = (int) $count[0]->count;
83 } else {
84 $count = 0;
85 }
86
87 return $count;
88 }
89
90 /**
91 * @return array|bool|int|mixed|null|void
92 */
93 public function prepare_index() {
94 /**
95 * @var $wpdb WPDB
96 */
97 global $wpdb;
98
99 if ( ! in_array( "{$wpdb->prefix}pod", $this->tables, true ) ) {
100 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
101 }
102
103 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod`', false );
104
105 if ( ! empty( $count ) ) {
106 $count = (int) $count[0]->count;
107 } else {
108 $count = 0;
109 }
110
111 return $count;
112 }
113
114 /**
115 * @return array|bool|int|mixed|null|void
116 */
117 public function prepare_templates() {
118 /**
119 * @var $wpdb WPDB
120 */
121 global $wpdb;
122
123 if ( ! in_array( "{$wpdb->prefix}pod_templates", $this->tables, true ) ) {
124 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
125 }
126
127 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_templates`', false );
128
129 if ( ! empty( $count ) ) {
130 $count = (int) $count[0]->count;
131 } else {
132 $count = 0;
133 }
134
135 return $count;
136 }
137
138 /**
139 * @return array|bool|int|mixed|null|void
140 */
141 public function prepare_pages() {
142 /**
143 * @var $wpdb WPDB
144 */
145 global $wpdb;
146
147 if ( ! in_array( "{$wpdb->prefix}pod_pages", $this->tables, true ) ) {
148 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
149 }
150
151 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_pages`', false );
152
153 if ( ! empty( $count ) ) {
154 $count = (int) $count[0]->count;
155 } else {
156 $count = 0;
157 }
158
159 return $count;
160 }
161
162 /**
163 * @return array|bool|int|mixed|null|void
164 */
165 public function prepare_helpers() {
166 /**
167 * @var $wpdb WPDB
168 */
169 global $wpdb;
170
171 if ( ! in_array( "{$wpdb->prefix}pod_helpers", $this->tables, true ) ) {
172 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
173 }
174
175 $count = pods_query( 'SELECT COUNT(*) AS `count` FROM `@wp_pod_helpers`', false );
176
177 if ( ! empty( $count ) ) {
178 $count = (int) $count[0]->count;
179 } else {
180 $count = 0;
181 }
182
183 return $count;
184 }
185
186 /**
187 * @param $params
188 *
189 * @return array|bool|int|mixed|null|void
190 */
191 public function prepare_pod( $params ) {
192 /**
193 * @var $wpdb WPDB
194 */
195 global $wpdb;
196
197 if ( ! isset( $params->pod ) ) {
198 return pods_error( __( 'Invalid Pod.', 'pods' ) );
199 }
200
201 $pod = pods_sanitize( pods_clean_name( $params->pod ) );
202
203 if ( ! in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables, true ) ) {
204 return pods_error( __( 'Table not found, it cannot be migrated', 'pods' ) );
205 }
206
207 $count = pods_query( "SELECT COUNT(*) AS `count` FROM `@wp_pod_tbl_{$pod}`", false );
208
209 if ( ! empty( $count ) ) {
210 $count = (int) $count[0]->count;
211 } else {
212 $count = 0;
213 }
214
215 $pod_type = pods_query( "SELECT `id` FROM `@wp_pod_types` WHERE `name` = '{$pod}'", false );
216
217 if ( ! empty( $pod_type ) ) {
218 $pod_type = (int) $pod_type[0]->id;
219 } else {
220 return pods_error( __( 'Pod not found, it cannot be migrated', 'pods' ) );
221 }
222
223 $fields = [ 'id' ];
224
225 $field_rows = pods_query( "SELECT `id`, `name`, `coltype` FROM `@wp_pod_fields` WHERE `datatype` = {$pod_type} ORDER BY `weight`, `name`" );
226
227 if ( ! empty( $field_rows ) ) {
228 foreach ( $field_rows as $field ) {
229 if ( ! in_array( $field->coltype, [ 'pick', 'file' ], true ) ) {
230 $fields[] = $field->name;
231 }
232 }
233 }
234
235 $columns = PodsData::get_table_columns( "{$wpdb->prefix}pod_tbl_{$pod}" );
236
237 $errors = [];
238
239 foreach ( $columns as $column => $info ) {
240 if ( ! in_array( $column, $fields, true ) ) {
241 $errors[] = "<strong>{$column}</strong> " . __( 'is a field in the table, but was not found in this pod - the field data will not be migrated.', 'pods' );
242 }
243 }
244
245 foreach ( $fields as $field ) {
246 if ( ! isset( $columns[ $field ] ) ) {
247 $errors[] = "<strong>{$field}</strong> " . __( 'is a field in this pod, but was not found in the table - the field data will not be migrated.', 'pods' );
248 }
249 }
250
251 if ( ! empty( $errors ) ) {
252 return pods_error( implode( '<br />', $errors ) );
253 }
254
255 return $count;
256 }
257
258 /**
259 *
260 */
261 public function migrate_1_x() {
262 $old_version = (string) get_option( 'pods_version' );
263
264 if ( 0 < strlen( $old_version ) ) {
265 if ( false === strpos( $old_version, '.' ) ) {
266 $old_version = pods_version_to_point( $old_version );
267 }
268
269 // Last DB change was 1.11
270 if ( version_compare( $old_version, '1.11', '<' ) ) {
271 do_action( 'pods_update', PODS_VERSION, $old_version );
272
273 if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $old_version ) ) {
274 include_once PODS_DIR . 'sql/update-1.x.php';
275 }
276
277 do_action( 'pods_update_post', PODS_VERSION, $old_version );
278 }
279 }
280
281 return '1';
282 }
283
284 /**
285 * @return array|string
286 */
287 public function migrate_pods() {
288 if ( true === $this->check_progress( __FUNCTION__ ) ) {
289 return '1';
290 }
291
292 $sister_ids = (array) get_option( 'pods_framework_upgrade_2_0_sister_ids', [] );
293
294 $migration_limit = (int) apply_filters( 'pods_upgrade_pod_limit', 1 );
295 $migration_limit = max( $migration_limit, 1 );
296
297 $last_id = (int) $this->check_progress( __FUNCTION__ );
298
299 $sql = "
300 SELECT *
301 FROM `@wp_pod_types`
302 WHERE {$last_id} < `id`
303 ORDER BY `id`
304 LIMIT 0, {$migration_limit}
305 ";
306
307 $pod_types = pods_query( $sql );
308
309 $last_id = true;
310
311 if ( ! empty( $pod_types ) ) {
312 foreach ( $pod_types as $pod_type ) {
313 $field_rows = pods_query( "SELECT * FROM `@wp_pod_fields` WHERE `datatype` = {$pod_type->id} ORDER BY `weight`, `name`" );
314
315 $fields = [
316 [
317 'name' => 'name',
318 'label' => 'Name',
319 'type' => 'text',
320 'weight' => 0,
321 'options' => [
322 'required' => 1,
323 'text_max_length' => 128,
324 ],
325 ],
326 [
327 'name' => 'created',
328 'label' => 'Date Created',
329 'type' => 'datetime',
330 'options' => [
331 'datetime_format' => 'ymd_slash',
332 'datetime_time_type' => '12',
333 'datetime_time_format' => 'h_mm_ss_A',
334 ],
335 'weight' => 1,
336 ],
337 [
338 'name' => 'modified',
339 'label' => 'Date Modified',
340 'type' => 'datetime',
341 'options' => [
342 'datetime_format' => 'ymd_slash',
343 'datetime_time_type' => '12',
344 'datetime_time_format' => 'h_mm_ss_A',
345 ],
346 'weight' => 2,
347 ],
348 [
349 'name' => 'author',
350 'label' => 'Author',
351 'type' => 'pick',
352 'pick_object' => 'user',
353 'options' => [
354 'pick_format_type' => 'single',
355 'pick_format_single' => 'autocomplete',
356 'default_value' => '{@user.ID}',
357 'default_evaluate_tags' => 1,
358 ],
359 'weight' => 3,
360 ],
361 ];
362
363 $weight = 4;
364
365 $found_fields = [];
366
367 foreach ( $field_rows as $row ) {
368 if ( 'name' === $row->name ) {
369 continue;
370 }
371
372 $old_name = $row->name;
373
374 $row->name = pods_clean_name( $row->name );
375
376 if ( in_array( $row->name, [ 'created', 'modified', 'author' ], true ) ) {
377 $row->name .= '2';
378 }
379
380 $field_type = $row->coltype;
381
382 if ( 'txt' === $field_type ) {
383 $field_type = 'text';
384 } elseif ( 'desc' === $field_type ) {
385 $field_type = 'wysiwyg';
386 } elseif ( 'code' === $field_type ) {
387 $field_type = 'paragraph';
388 } elseif ( 'bool' === $field_type ) {
389 $field_type = 'boolean';
390 } elseif ( 'num' === $field_type ) {
391 $field_type = 'number';
392 } elseif ( 'date' === $field_type ) {
393 $field_type = 'datetime';
394 }
395
396 $field_params = [
397 'name' => trim( $row->name ),
398 'label' => trim( $row->label ),
399 'description' => trim( $row->comment ),
400 'type' => $field_type,
401 'weight' => $weight,
402 'options' => [
403 'required' => $row->required,
404 'unique' => $row->unique,
405 'input_helper' => $row->input_helper,
406 '_pods_1x_field_name' => $old_name,
407 '_pods_1x_field_id' => $row->id,
408 ],
409 ];
410
411 if ( in_array( $field_params['name'], $found_fields, true ) ) {
412 continue;
413 }
414
415 $found_fields[] = $field_params['name'];
416
417 if ( 'pick' === $field_type ) {
418 $field_params['pick_object'] = 'pod-' . $row->pickval;
419
420 if ( 'wp_user' === $row->pickval ) {
421 $field_params['pick_object'] = 'user';
422 } elseif ( 'wp_post' === $row->pickval ) {
423 $field_params['pick_object'] = 'post_type-post';
424 } elseif ( 'wp_page' === $row->pickval ) {
425 $field_params['pick_object'] = 'post_type-page';
426 } elseif ( 'wp_taxonomy' === $row->pickval ) {
427 $field_params['pick_object'] = 'taxonomy-category';
428 }
429
430 $field_params['sister_id'] = $row->sister_field_id;
431
432 $sister_ids[ $row->id ] = $row->sister_field_id;
433 // Old Sister Field ID
434 $field_params['options']['_pods_1x_sister_id'] = $row->sister_field_id;
435
436 $field_params['options']['pick_filter'] = $row->pick_filter;
437 $field_params['options']['pick_orderby'] = $row->pick_orderby;
438 $field_params['options']['pick_display'] = '';
439 $field_params['options']['pick_size'] = 'medium';
440
441 if ( 1 === (int) $row->multiple ) {
442 $field_params['options']['pick_format_type'] = 'multi';
443 $field_params['options']['pick_format_multi'] = 'checkbox';
444 $field_params['options']['pick_limit'] = 0;
445 } else {
446 $field_params['options']['pick_format_type'] = 'single';
447 $field_params['options']['pick_format_single'] = 'dropdown';
448 $field_params['options']['pick_limit'] = 1;
449 }
450 } elseif ( 'file' === $field_type ) {
451 $field_params['options']['file_format_type'] = 'multi';
452 $field_params['options']['file_type'] = 'any';
453 } elseif ( 'number' === $field_type ) {
454 $field_params['options']['number_decimals'] = 2;
455 } elseif ( 'desc' === $row->coltype ) {
456 $field_params['options']['wysiwyg_editor'] = 'tinymce';
457 } elseif ( 'text' === $field_type ) {
458 $field_params['options']['text_max_length'] = 128;
459 }//end if
460
461 $fields[] = $field_params;
462
463 $weight ++;
464 }//end foreach
465
466 $pod_type->name = pods_sanitize( pods_clean_name( $pod_type->name ) );
467
468 $pod_params = [
469 'name' => $pod_type->name,
470 'label' => $pod_type->label,
471 'type' => 'pod',
472 'storage' => 'table',
473 'fields' => $fields,
474 'options' => [
475 'pre_save_helpers' => $pod_type->pre_save_helpers,
476 'post_save_helpers' => $pod_type->post_save_helpers,
477 'pre_delete_helpers' => $pod_type->pre_drop_helpers,
478 'post_delete_helpers' => $pod_type->post_drop_helpers,
479 'show_in_menu' => $pod_type->is_toplevel,
480 'detail_url' => $pod_type->detail_page,
481 'pod_index' => 'name',
482 '_pods_1x_pod_id' => $pod_type->id,
483 ],
484 ];
485
486 if ( empty( $pod_params['label'] ) ) {
487 $pod_params['label'] = ucwords( str_replace( '_', ' ', $pod_params['name'] ) );
488 }
489
490 $pod_id = $this->api->save_pod( $pod_params );
491
492 if ( 0 < $pod_id ) {
493 $last_id = $pod_type->id;
494 } else {
495 pods_error( 'Error: ' . $pod_id );
496 }
497 }//end foreach
498 }//end if
499
500 update_option( 'pods_framework_upgrade_2_0_sister_ids', $sister_ids );
501
502 $this->update_progress( __FUNCTION__, $last_id );
503
504 if ( count( $pod_types ) === $migration_limit ) {
505 return '-2';
506 } else {
507 return '1';
508 }
509 }
510
511 /**
512 * @return string
513 */
514 public function migrate_fields() {
515 if ( true === $this->check_progress( __FUNCTION__ ) ) {
516 return '1';
517 }
518
519 $sister_ids = (array) get_option( 'pods_framework_upgrade_2_0_sister_ids', [] );
520
521 foreach ( $sister_ids as $old_field_id => $old_sister_id ) {
522 $old_field_id = (int) $old_field_id;
523 $old_sister_id = (int) $old_sister_id;
524
525 $new_field_id = pods_query( "SELECT `post_id` FROM `@wp_postmeta` WHERE `meta_key` = '_pods_1x_field_id' AND `meta_value` = '{$old_field_id}' LIMIT 1" );
526
527 if ( ! empty( $new_field_id ) ) {
528 $new_field_id = (int) $new_field_id[0]->post_id;
529
530 $new_sister_id = pods_query( "SELECT `post_id` FROM `@wp_postmeta` WHERE `meta_key` = '_pods_1x_field_id' AND `meta_value` = '{$old_sister_id}' LIMIT 1" );
531
532 if ( ! empty( $new_sister_id ) ) {
533 $new_sister_id = (int) $new_sister_id[0]->post_id;
534
535 update_post_meta( $new_field_id, 'sister_id', $new_sister_id );
536 } else {
537 delete_post_meta( $new_field_id, 'sister_id' );
538 }
539 }
540 }
541
542 // We were off the grid, so let's flush and allow for resync
543 $this->api->cache_flush_pods();
544
545 $this->update_progress( __FUNCTION__, true );
546
547 return '1';
548 }
549
550 /**
551 * @return string
552 */
553 public function migrate_relationships() {
554 if ( true === $this->check_progress( __FUNCTION__ ) ) {
555 return '1';
556 }
557
558 $migration_limit = (int) apply_filters( 'pods_upgrade_item_limit', 1500 );
559 $migration_limit = max( $migration_limit, 100 );
560
561 $last_id = (int) $this->check_progress( __FUNCTION__ );
562
563 $sql = "
564 SELECT `r`.*, `p`.`tbl_row_id` AS `real_id`, `p`.`datatype`
565 FROM `@wp_pod_rel` AS `r`
566 LEFT JOIN `@wp_pod` AS `p` ON `p`.`id` = `r`.`pod_id`
567 WHERE {$last_id} < `r`.`id`
568 AND `r`.`pod_id` IS NOT NULL
569 AND `r`.`field_id` IS NOT NULL
570 AND `p`.`id` IS NOT NULL
571 ORDER BY `r`.`id`
572 LIMIT 0, {$migration_limit}
573 ";
574
575 $rel = pods_query( $sql );
576
577 $last_id = true;
578
579 $pod_types = pods_query( 'SELECT `id`, `name` FROM `@wp_pod_types` ORDER BY `id`' );
580
581 $types = [];
582
583 $x = 0;
584
585 if ( ! empty( $rel ) && ! empty( $pod_types ) ) {
586 foreach ( $pod_types as $type ) {
587 $type->name = pods_clean_name( $type->name );
588
589 $types[ $type->id ] = $this->api->load_pod( [ 'name' => $type->name ], false );
590
591 if ( empty( $types[ $type->id ] ) ) {
592 // translators: %s is the pod name.
593 return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, relationships cannot be migrated', 'pods' ), $type->name ) );
594 }
595
596 $pod_fields = pods_query( "SELECT `id`, `name` FROM `@wp_pod_fields` WHERE `datatype` = {$type->id} ORDER BY `id`" );
597
598 $types[ $type->id ]['old_fields'] = [];
599
600 foreach ( $pod_fields as $field ) {
601 // Handle name changes
602 if ( in_array( $field->name, [ 'created', 'modified', 'author' ], true ) ) {
603 $field->name .= '2';
604 }
605
606 $types[ $type->id ]['old_fields'][ $field->id ] = $field->name;
607 }
608 }//end foreach
609
610 foreach ( $rel as $r ) {
611 $r->pod_id = (int) $r->pod_id;
612
613 if ( ! isset( $types[ $r->datatype ] ) || ! isset( $types[ $r->datatype ]['old_fields'][ $r->field_id ] ) ) {
614 continue;
615 }
616
617 if ( ! isset( $types[ $r->datatype ]['fields'][ $types[ $r->datatype ]['old_fields'][ $r->field_id ] ] ) ) {
618 continue;
619 }
620
621 $field = $types[ $r->datatype ]['fields'][ $types[ $r->datatype ]['old_fields'][ $r->field_id ] ];
622
623 if ( ! in_array( $field['type'], [ 'pick', 'file' ], true ) ) {
624 continue;
625 }
626
627 $pod_id = $types[ $r->datatype ]['id'];
628 $field_id = $field['id'];
629 $item_id = $r->real_id;
630
631 $related_pod_id = 0;
632 $related_field_id = 0;
633 $related_item_id = $r->tbl_row_id;
634
635 if ( 'pick' === $field['type'] ) {
636 $old_sister_id = (int) pods_v( '_pods_1x_sister_id', $field['options'], 0 );
637
638 if ( 0 < $old_sister_id ) {
639 $sql = '
640 SELECT `f`.`id`, `f`.`name`, `t`.`name` AS `pod`
641 FROM `@wp_pod_fields` AS `f`
642 LEFT JOIN `@wp_pod_types` AS `t` ON `t`.`id` = `f`.`datatype`
643 WHERE `f`.`id` = ' . $old_sister_id . ' AND `t`.`id` IS NOT NULL
644 ORDER BY `f`.`id`
645 LIMIT 1
646 ';
647
648 $old_field = pods_query( $sql );
649
650 if ( empty( $old_field ) ) {
651 continue;
652 }
653
654 $old_field = $old_field[0];
655
656 $related_field = $this->api->load_field( [
657 'name' => $old_field->name,
658 'pod' => $old_field->pod,
659 ] );
660
661 if ( empty( $related_field ) ) {
662 continue;
663 }
664
665 $related_pod_id = $related_field['pod_id'];
666 $related_field_id = $related_field['id'];
667 } elseif ( 'pod' === $field['pick_object'] && 0 < strlen( $field['pick_val'] ) ) {
668 $related_pod = $this->api->load_pod( [ 'name' => $field['pick_val'] ], false );
669
670 if ( empty( $related_pod ) ) {
671 continue;
672 }
673
674 $related_pod_id = $related_pod['id'];
675 }//end if
676 }//end if
677
678 $r->id = (int) $r->id;
679 $pod_id = (int) $pod_id;
680 $field_id = (int) $field_id;
681 $item_id = (int) $item_id;
682 $related_pod_id = (int) $related_pod_id;
683 $related_field_id = (int) $related_field_id;
684 $related_item_id = (int) $related_item_id;
685 $r->weight = (int) $r->weight;
686
687 $table_data = [
688 'id' => $r->id,
689 'pod_id' => $pod_id,
690 'field_id' => $field_id,
691 'item_id' => $item_id,
692 'related_pod_id' => $related_pod_id,
693 'related_field_id' => $related_field_id,
694 'related_item_id' => $related_item_id,
695 'weight' => $r->weight,
696 ];
697
698 $table_formats = array_fill( 0, count( $table_data ), '%d' );
699
700 $sql = PodsData::insert_on_duplicate( '@wp_podsrel', $table_data, $table_formats );
701
702 pods_query( $sql );
703
704 $last_id = $r->id;
705
706 $x ++;
707
708 if ( 10 < $x ) {
709 $this->update_progress( __FUNCTION__, $last_id );
710
711 $x = 0;
712 }
713 }//end foreach
714 }//end if
715
716 $this->update_progress( __FUNCTION__, $last_id );
717
718 if ( count( $rel ) === $migration_limit ) {
719 return '-2';
720 } else {
721 return '1';
722 }
723 }
724
725 /**
726 * @return string
727 */
728 public function migrate_settings() {
729 return $this->migrate_roles();
730 }
731
732 /**
733 * @return string
734 */
735 public function migrate_roles() {
736 if ( true === $this->check_progress( __FUNCTION__ ) ) {
737 return '1';
738 }
739
740 /**
741 * @var $wpdb WPDB
742 */
743 global $wpdb;
744
745 $wp_roles = get_option( "{$wpdb->prefix}user_roles" );
746
747 $old_roles = get_option( 'pods_roles' );
748
749 if ( ! is_array( $old_roles ) && ! empty( $old_roles ) ) {
750 $old_roles = pods_maybe_safely_unserialize( $old_roles );
751 }
752
753 if ( ! is_array( $old_roles ) ) {
754 $old_roles = [];
755 }
756
757 if ( ! empty( $old_roles ) ) {
758 foreach ( $old_roles as $role => $data ) {
759 if ( '_wpnonce' === $role ) {
760 continue;
761 }
762
763 if ( ! isset( $wp_roles[ $role ] ) ) {
764 continue;
765 }
766
767 $caps = $wp_roles[ $role ]['capabilities'];
768
769 foreach ( $data as $cap ) {
770 $cap = (string) $cap;
771
772 if ( 0 === strpos( 'manage_', $cap ) ) {
773 if ( 'manage_roles' === $cap ) {
774 continue;
775 }
776
777 $cap = pods_str_replace( 'manage_', 'pods_', $cap, 1 );
778 $cap = pods_str_replace( 'pod_pages', 'pages', $cap, 1 );
779
780 $caps[ $cap ] = true;
781 } elseif ( 0 === strpos( 'pod_', $cap ) ) {
782 $keys = [
783 pods_str_replace( 'pod_', 'pods_new_', $cap, 1 ),
784 pods_str_replace( 'pod_', 'pods_edit_', $cap, 1 ),
785 pods_str_replace( 'pod_', 'pods_delete_', $cap, 1 ),
786 ];
787
788 foreach ( $keys as $key ) {
789 $caps[ $key ] = true;
790 }
791 }
792 }//end foreach
793
794 $wp_roles[ $role ]['capabilities'] = $caps;
795 }//end foreach
796 }//end if
797
798 update_option( "{$wpdb->prefix}user_roles", $wp_roles );
799
800 $this->update_progress( __FUNCTION__, true );
801
802 return '1';
803 }
804
805 /**
806 * @return array|string
807 */
808 public function migrate_templates() {
809 if ( true === $this->check_progress( __FUNCTION__ ) ) {
810 return '1';
811 }
812
813 $templates = pods_query( 'SELECT * FROM `@wp_pod_templates`', false );
814
815 $results = [];
816
817 if ( ! empty( $templates ) ) {
818 foreach ( $templates as $template ) {
819 unset( $template->id );
820
821 $results[] = $this->api->save_template( $template );
822 }
823 }
824
825 $this->update_progress( __FUNCTION__, true );
826
827 return '1';
828 }
829
830 /**
831 * @return array|string
832 */
833 public function migrate_pages() {
834 if ( true === $this->check_progress( __FUNCTION__ ) ) {
835 return '1';
836 }
837
838 $pages = pods_query( 'SELECT * FROM `@wp_pod_pages`', false );
839
840 if ( ! empty( $pages ) ) {
841 foreach ( $pages as $page ) {
842 unset( $page->id );
843
844 $this->api->save_page( $page );
845 }
846 }
847
848 $this->update_progress( __FUNCTION__, true );
849
850 return '1';
851 }
852
853 /**
854 * @return array|string
855 */
856 public function migrate_helpers() {
857 return '1';
858 }
859
860 /**
861 * @param $params
862 *
863 * @return mixed|string|void
864 */
865 public function migrate_pod( $params ) {
866 /**
867 * @var $wpdb WPDB
868 */
869 global $wpdb;
870
871 if ( ! isset( $params->pod ) ) {
872 return pods_error( __( 'Invalid Pod.', 'pods' ) );
873 }
874
875 $pod = pods_sanitize( pods_clean_name( $params->pod ) );
876
877 if ( ! in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables, true ) ) {
878 return pods_error( __( 'Table not found, items cannot be migrated', 'pods' ) );
879 }
880
881 if ( ! in_array( "{$wpdb->prefix}pods_{$pod}", $this->tables, true ) ) {
882 return pods_error( __( 'New table not found, items cannot be migrated', 'pods' ) );
883 }
884
885 if ( ! in_array( "{$wpdb->prefix}pod_types", $this->tables, true ) ) {
886 return pods_error( __( 'Pod Types table not found, items cannot be migrated', 'pods' ) );
887 }
888
889 if ( ! in_array( "{$wpdb->prefix}pod", $this->tables, true ) ) {
890 return pods_error( __( 'Pod table not found, items cannot be migrated', 'pods' ) );
891 }
892
893 if ( true === $this->check_progress( __FUNCTION__, $pod ) ) {
894 return '1';
895 }
896
897 $pod_data = $this->api->load_pod( [ 'name' => $pod ], false );
898
899 if ( empty( $pod_data ) ) {
900 // translators: %s is the pod name.
901 return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, items cannot be migrated', 'pods' ), $pod ) );
902 }
903
904 $columns = [];
905 $old_columns = [];
906
907 foreach ( $pod_data['fields'] as $field ) {
908 if ( ! in_array( $field['name'], [
909 'created',
910 'modified',
911 'author',
912 ], true ) && ! in_array( $field['type'], [ 'file', 'pick' ], true ) ) {
913 $columns[] = pods_sanitize( $field['name'] );
914 $old_columns[] = pods_v( '_pods_1x_field_name', $field['options'], $field['name'], false );
915 }
916 }
917
918 $into = '`id`';
919 $select = '`t`.`id`';
920
921 if ( ! empty( $columns ) ) {
922 $into .= ', `' . implode( '`, `', $columns ) . '`';
923 $select .= ', `t`.`' . implode( '`, `t`.`', $old_columns ) . '`';
924 }
925
926 // Copy content from the old table into the new
927 $sql = "
928 REPLACE INTO `@wp_pods_{$pod}`
929 ( {$into} )
930 ( SELECT {$select}
931 FROM `@wp_pod_tbl_{$pod}` AS `t` )
932 ";
933
934 pods_query( $sql );
935
936 // Copy index data from the old index table into the new individual table
937 $sql = "
938 UPDATE `@wp_pods_{$pod}` AS `t`
939 LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}'
940 LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id`
941 SET `t`.`created` = `p`.`created`, `t`.`modified` = `p`.`modified`
942 WHERE `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL
943 ";
944
945 pods_query( $sql );
946
947 // Copy name data from the old index table into the new individual table (if name empty in indiv table)
948 $sql = "
949 UPDATE `@wp_pods_{$pod}` AS `t`
950 LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}'
951 LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id`
952 SET `t`.`name` = `p`.`name`
953 WHERE ( `t`.`name` IS NULL OR `t`.`name` = '' ) AND `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL
954 ";
955
956 pods_query( $sql );
957
958 $this->update_progress( __FUNCTION__, true, $pod );
959
960 return '1';
961 }
962
963 /**
964 * @return string
965 */
966 public function migrate_cleanup() {
967 update_option( 'pods_framework_upgraded_1_x', 1 );
968
969 PodsInit::$components->activate_component( 'templates' );
970 PodsInit::$components->activate_component( 'pages' );
971 PodsInit::$components->activate_component( 'helpers' );
972
973 $this->api->cache_flush_pods();
974
975 return '1';
976 }
977
978 /**
979 *
980 */
981 public function restart() {
982 /**
983 * @var $wpdb WPDB
984 */
985 global $wpdb;
986
987 foreach ( $this->tables as $table ) {
988 if ( false !== strpos( $table, "{$wpdb->prefix}pods" ) ) {
989 pods_query( "TRUNCATE `{$table}`", false );
990 }
991 }
992
993 delete_option( 'pods_framework_upgrade_2_0' );
994 delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
995 delete_option( 'pods_framework_upgraded_1_x' );
996 }
997
998 /**
999 *
1000 */
1001 public function cleanup() {
1002 /**
1003 * @var $wpdb WPDB
1004 */
1005 global $wpdb;
1006
1007 foreach ( $this->tables as $table ) {
1008 if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" === $table ) {
1009 pods_query( "DROP TABLE `{$table}`", false );
1010 }
1011 }
1012
1013 delete_option( 'pods_roles' );
1014 delete_option( 'pods_version' );
1015 delete_option( 'pods_framework_upgrade_2_0' );
1016 delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1017 delete_option( 'pods_framework_upgraded_1_x' );
1018
1019 delete_option( 'pods_disable_file_browser' );
1020 delete_option( 'pods_files_require_login' );
1021 delete_option( 'pods_files_require_login_cap' );
1022 delete_option( 'pods_disable_file_upload' );
1023 delete_option( 'pods_upload_require_login' );
1024 delete_option( 'pods_upload_require_login_cap' );
1025
1026 pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" );
1027 }
1028 }
1029