PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.5
Pods – Custom Content Types and Fields v3.3.5
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
1027 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 = 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 if ( 0 === strpos( 'manage_', $cap ) ) {
771 if ( 'manage_roles' === $cap ) {
772 continue;
773 }
774
775 $cap = pods_str_replace( 'manage_', 'pods_', $cap, 1 );
776 $cap = pods_str_replace( 'pod_pages', 'pages', $cap, 1 );
777
778 $caps[ $cap ] = true;
779 } elseif ( 0 === strpos( 'pod_', $cap ) ) {
780 $keys = [
781 pods_str_replace( 'pod_', 'pods_new_', $cap, 1 ),
782 pods_str_replace( 'pod_', 'pods_edit_', $cap, 1 ),
783 pods_str_replace( 'pod_', 'pods_delete_', $cap, 1 ),
784 ];
785
786 foreach ( $keys as $key ) {
787 $caps[ $key ] = true;
788 }
789 }
790 }//end foreach
791
792 $wp_roles[ $role ]['capabilities'] = $caps;
793 }//end foreach
794 }//end if
795
796 update_option( "{$wpdb->prefix}user_roles", $wp_roles );
797
798 $this->update_progress( __FUNCTION__, true );
799
800 return '1';
801 }
802
803 /**
804 * @return array|string
805 */
806 public function migrate_templates() {
807 if ( true === $this->check_progress( __FUNCTION__ ) ) {
808 return '1';
809 }
810
811 $templates = pods_query( 'SELECT * FROM `@wp_pod_templates`', false );
812
813 $results = [];
814
815 if ( ! empty( $templates ) ) {
816 foreach ( $templates as $template ) {
817 unset( $template->id );
818
819 $results[] = $this->api->save_template( $template );
820 }
821 }
822
823 $this->update_progress( __FUNCTION__, true );
824
825 return '1';
826 }
827
828 /**
829 * @return array|string
830 */
831 public function migrate_pages() {
832 if ( true === $this->check_progress( __FUNCTION__ ) ) {
833 return '1';
834 }
835
836 $pages = pods_query( 'SELECT * FROM `@wp_pod_pages`', false );
837
838 if ( ! empty( $pages ) ) {
839 foreach ( $pages as $page ) {
840 unset( $page->id );
841
842 $this->api->save_page( $page );
843 }
844 }
845
846 $this->update_progress( __FUNCTION__, true );
847
848 return '1';
849 }
850
851 /**
852 * @return array|string
853 */
854 public function migrate_helpers() {
855 return '1';
856 }
857
858 /**
859 * @param $params
860 *
861 * @return mixed|string|void
862 */
863 public function migrate_pod( $params ) {
864 /**
865 * @var $wpdb WPDB
866 */
867 global $wpdb;
868
869 if ( ! isset( $params->pod ) ) {
870 return pods_error( __( 'Invalid Pod.', 'pods' ) );
871 }
872
873 $pod = pods_sanitize( pods_clean_name( $params->pod ) );
874
875 if ( ! in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables, true ) ) {
876 return pods_error( __( 'Table not found, items cannot be migrated', 'pods' ) );
877 }
878
879 if ( ! in_array( "{$wpdb->prefix}pods_{$pod}", $this->tables, true ) ) {
880 return pods_error( __( 'New table not found, items cannot be migrated', 'pods' ) );
881 }
882
883 if ( ! in_array( "{$wpdb->prefix}pod_types", $this->tables, true ) ) {
884 return pods_error( __( 'Pod Types table not found, items cannot be migrated', 'pods' ) );
885 }
886
887 if ( ! in_array( "{$wpdb->prefix}pod", $this->tables, true ) ) {
888 return pods_error( __( 'Pod table not found, items cannot be migrated', 'pods' ) );
889 }
890
891 if ( true === $this->check_progress( __FUNCTION__, $pod ) ) {
892 return '1';
893 }
894
895 $pod_data = $this->api->load_pod( [ 'name' => $pod ], false );
896
897 if ( empty( $pod_data ) ) {
898 // translators: %s is the pod name.
899 return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, items cannot be migrated', 'pods' ), $pod ) );
900 }
901
902 $columns = [];
903 $old_columns = [];
904
905 foreach ( $pod_data['fields'] as $field ) {
906 if ( ! in_array( $field['name'], [
907 'created',
908 'modified',
909 'author',
910 ], true ) && ! in_array( $field['type'], [ 'file', 'pick' ], true ) ) {
911 $columns[] = pods_sanitize( $field['name'] );
912 $old_columns[] = pods_v( '_pods_1x_field_name', $field['options'], $field['name'], false );
913 }
914 }
915
916 $into = '`id`';
917 $select = '`t`.`id`';
918
919 if ( ! empty( $columns ) ) {
920 $into .= ', `' . implode( '`, `', $columns ) . '`';
921 $select .= ', `t`.`' . implode( '`, `t`.`', $old_columns ) . '`';
922 }
923
924 // Copy content from the old table into the new
925 $sql = "
926 REPLACE INTO `@wp_pods_{$pod}`
927 ( {$into} )
928 ( SELECT {$select}
929 FROM `@wp_pod_tbl_{$pod}` AS `t` )
930 ";
931
932 pods_query( $sql );
933
934 // Copy index data from the old index table into the new individual table
935 $sql = "
936 UPDATE `@wp_pods_{$pod}` AS `t`
937 LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}'
938 LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id`
939 SET `t`.`created` = `p`.`created`, `t`.`modified` = `p`.`modified`
940 WHERE `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL
941 ";
942
943 pods_query( $sql );
944
945 // Copy name data from the old index table into the new individual table (if name empty in indiv table)
946 $sql = "
947 UPDATE `@wp_pods_{$pod}` AS `t`
948 LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}'
949 LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id`
950 SET `t`.`name` = `p`.`name`
951 WHERE ( `t`.`name` IS NULL OR `t`.`name` = '' ) AND `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL
952 ";
953
954 pods_query( $sql );
955
956 $this->update_progress( __FUNCTION__, true, $pod );
957
958 return '1';
959 }
960
961 /**
962 * @return string
963 */
964 public function migrate_cleanup() {
965 update_option( 'pods_framework_upgraded_1_x', 1 );
966
967 PodsInit::$components->activate_component( 'templates' );
968 PodsInit::$components->activate_component( 'pages' );
969 PodsInit::$components->activate_component( 'helpers' );
970
971 $this->api->cache_flush_pods();
972
973 return '1';
974 }
975
976 /**
977 *
978 */
979 public function restart() {
980 /**
981 * @var $wpdb WPDB
982 */
983 global $wpdb;
984
985 foreach ( $this->tables as $table ) {
986 if ( false !== strpos( $table, "{$wpdb->prefix}pods" ) ) {
987 pods_query( "TRUNCATE `{$table}`", false );
988 }
989 }
990
991 delete_option( 'pods_framework_upgrade_2_0' );
992 delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
993 delete_option( 'pods_framework_upgraded_1_x' );
994 }
995
996 /**
997 *
998 */
999 public function cleanup() {
1000 /**
1001 * @var $wpdb WPDB
1002 */
1003 global $wpdb;
1004
1005 foreach ( $this->tables as $table ) {
1006 if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" === $table ) {
1007 pods_query( "DROP TABLE `{$table}`", false );
1008 }
1009 }
1010
1011 delete_option( 'pods_roles' );
1012 delete_option( 'pods_version' );
1013 delete_option( 'pods_framework_upgrade_2_0' );
1014 delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1015 delete_option( 'pods_framework_upgraded_1_x' );
1016
1017 delete_option( 'pods_disable_file_browser' );
1018 delete_option( 'pods_files_require_login' );
1019 delete_option( 'pods_files_require_login_cap' );
1020 delete_option( 'pods_disable_file_upload' );
1021 delete_option( 'pods_upload_require_login' );
1022 delete_option( 'pods_upload_require_login_cap' );
1023
1024 pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" );
1025 }
1026 }
1027