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