PodsUpgrade_2_0_0.php
1048 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 | 'default_evaluate_tags' => 1, |
| 353 | ), |
| 354 | 'weight' => 3, |
| 355 | ), |
| 356 | ); |
| 357 | |
| 358 | $weight = 4; |
| 359 | |
| 360 | $found_fields = array(); |
| 361 | |
| 362 | foreach ( $field_rows as $row ) { |
| 363 | if ( 'name' === $row->name ) { |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | $old_name = $row->name; |
| 368 | |
| 369 | $row->name = pods_clean_name( $row->name ); |
| 370 | |
| 371 | if ( in_array( $row->name, array( 'created', 'modified', 'author' ), true ) ) { |
| 372 | $row->name .= '2'; |
| 373 | } |
| 374 | |
| 375 | $field_type = $row->coltype; |
| 376 | |
| 377 | if ( 'txt' === $field_type ) { |
| 378 | $field_type = 'text'; |
| 379 | } elseif ( 'desc' === $field_type ) { |
| 380 | $field_type = 'wysiwyg'; |
| 381 | } elseif ( 'code' === $field_type ) { |
| 382 | $field_type = 'paragraph'; |
| 383 | } elseif ( 'bool' === $field_type ) { |
| 384 | $field_type = 'boolean'; |
| 385 | } elseif ( 'num' === $field_type ) { |
| 386 | $field_type = 'number'; |
| 387 | } elseif ( 'date' === $field_type ) { |
| 388 | $field_type = 'datetime'; |
| 389 | } |
| 390 | |
| 391 | $field_params = array( |
| 392 | 'name' => trim( $row->name ), |
| 393 | 'label' => trim( $row->label ), |
| 394 | 'description' => trim( $row->comment ), |
| 395 | 'type' => $field_type, |
| 396 | 'weight' => $weight, |
| 397 | 'options' => array( |
| 398 | 'required' => $row->required, |
| 399 | 'unique' => $row->unique, |
| 400 | 'input_helper' => $row->input_helper, |
| 401 | '_pods_1x_field_name' => $old_name, |
| 402 | '_pods_1x_field_id' => $row->id, |
| 403 | ), |
| 404 | ); |
| 405 | |
| 406 | if ( in_array( $field_params['name'], $found_fields, true ) ) { |
| 407 | continue; |
| 408 | } |
| 409 | |
| 410 | $found_fields[] = $field_params['name']; |
| 411 | |
| 412 | if ( 'pick' === $field_type ) { |
| 413 | $field_params['pick_object'] = 'pod-' . $row->pickval; |
| 414 | |
| 415 | if ( 'wp_user' === $row->pickval ) { |
| 416 | $field_params['pick_object'] = 'user'; |
| 417 | } elseif ( 'wp_post' === $row->pickval ) { |
| 418 | $field_params['pick_object'] = 'post_type-post'; |
| 419 | } elseif ( 'wp_page' === $row->pickval ) { |
| 420 | $field_params['pick_object'] = 'post_type-page'; |
| 421 | } elseif ( 'wp_taxonomy' === $row->pickval ) { |
| 422 | $field_params['pick_object'] = 'taxonomy-category'; |
| 423 | } |
| 424 | |
| 425 | $field_params['sister_id'] = $row->sister_field_id; |
| 426 | |
| 427 | $sister_ids[ $row->id ] = $row->sister_field_id; |
| 428 | // Old Sister Field ID |
| 429 | $field_params['options']['_pods_1x_sister_id'] = $row->sister_field_id; |
| 430 | |
| 431 | $field_params['options']['pick_filter'] = $row->pick_filter; |
| 432 | $field_params['options']['pick_orderby'] = $row->pick_orderby; |
| 433 | $field_params['options']['pick_display'] = ''; |
| 434 | $field_params['options']['pick_size'] = 'medium'; |
| 435 | |
| 436 | if ( 1 === (int) $row->multiple ) { |
| 437 | $field_params['options']['pick_format_type'] = 'multi'; |
| 438 | $field_params['options']['pick_format_multi'] = 'checkbox'; |
| 439 | $field_params['options']['pick_limit'] = 0; |
| 440 | } else { |
| 441 | $field_params['options']['pick_format_type'] = 'single'; |
| 442 | $field_params['options']['pick_format_single'] = 'dropdown'; |
| 443 | $field_params['options']['pick_limit'] = 1; |
| 444 | } |
| 445 | } elseif ( 'file' === $field_type ) { |
| 446 | $field_params['options']['file_format_type'] = 'multi'; |
| 447 | $field_params['options']['file_type'] = 'any'; |
| 448 | } elseif ( 'number' === $field_type ) { |
| 449 | $field_params['options']['number_decimals'] = 2; |
| 450 | } elseif ( 'desc' === $row->coltype ) { |
| 451 | $field_params['options']['wysiwyg_editor'] = 'tinymce'; |
| 452 | } elseif ( 'text' === $field_type ) { |
| 453 | $field_params['options']['text_max_length'] = 128; |
| 454 | }//end if |
| 455 | |
| 456 | $fields[] = $field_params; |
| 457 | |
| 458 | $weight ++; |
| 459 | }//end foreach |
| 460 | |
| 461 | $pod_type->name = pods_sanitize( pods_clean_name( $pod_type->name ) ); |
| 462 | |
| 463 | $pod_params = array( |
| 464 | 'name' => $pod_type->name, |
| 465 | 'label' => $pod_type->label, |
| 466 | 'type' => 'pod', |
| 467 | 'storage' => 'table', |
| 468 | 'fields' => $fields, |
| 469 | 'options' => array( |
| 470 | 'pre_save_helpers' => $pod_type->pre_save_helpers, |
| 471 | 'post_save_helpers' => $pod_type->post_save_helpers, |
| 472 | 'pre_delete_helpers' => $pod_type->pre_drop_helpers, |
| 473 | 'post_delete_helpers' => $pod_type->post_drop_helpers, |
| 474 | 'show_in_menu' => $pod_type->is_toplevel, |
| 475 | 'detail_url' => $pod_type->detail_page, |
| 476 | 'pod_index' => 'name', |
| 477 | '_pods_1x_pod_id' => $pod_type->id, |
| 478 | ), |
| 479 | ); |
| 480 | |
| 481 | if ( empty( $pod_params['label'] ) ) { |
| 482 | $pod_params['label'] = ucwords( str_replace( '_', ' ', $pod_params['name'] ) ); |
| 483 | } |
| 484 | |
| 485 | $pod_id = $this->api->save_pod( $pod_params ); |
| 486 | |
| 487 | if ( 0 < $pod_id ) { |
| 488 | $last_id = $pod_type->id; |
| 489 | } else { |
| 490 | pods_error( 'Error: ' . $pod_id ); |
| 491 | } |
| 492 | }//end foreach |
| 493 | }//end if |
| 494 | |
| 495 | update_option( 'pods_framework_upgrade_2_0_sister_ids', $sister_ids ); |
| 496 | |
| 497 | $this->update_progress( __FUNCTION__, $last_id ); |
| 498 | |
| 499 | if ( count( $pod_types ) === $migration_limit ) { |
| 500 | return '-2'; |
| 501 | } else { |
| 502 | return '1'; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * @return string |
| 508 | */ |
| 509 | public function migrate_fields() { |
| 510 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 511 | return '1'; |
| 512 | } |
| 513 | |
| 514 | $sister_ids = (array) get_option( 'pods_framework_upgrade_2_0_sister_ids', array() ); |
| 515 | |
| 516 | foreach ( $sister_ids as $old_field_id => $old_sister_id ) { |
| 517 | $old_field_id = (int) $old_field_id; |
| 518 | $old_sister_id = (int) $old_sister_id; |
| 519 | |
| 520 | $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" ); |
| 521 | |
| 522 | if ( ! empty( $new_field_id ) ) { |
| 523 | $new_field_id = (int) $new_field_id[0]->post_id; |
| 524 | |
| 525 | $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" ); |
| 526 | |
| 527 | if ( ! empty( $new_sister_id ) ) { |
| 528 | $new_sister_id = (int) $new_sister_id[0]->post_id; |
| 529 | |
| 530 | update_post_meta( $new_field_id, 'sister_id', $new_sister_id ); |
| 531 | } else { |
| 532 | delete_post_meta( $new_field_id, 'sister_id' ); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // We were off the grid, so let's flush and allow for resync |
| 538 | $this->api->cache_flush_pods(); |
| 539 | |
| 540 | $this->update_progress( __FUNCTION__, true ); |
| 541 | |
| 542 | return '1'; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @return string |
| 547 | */ |
| 548 | public function migrate_relationships() { |
| 549 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 550 | return '1'; |
| 551 | } |
| 552 | |
| 553 | $migration_limit = (int) apply_filters( 'pods_upgrade_item_limit', 1500 ); |
| 554 | $migration_limit = max( $migration_limit, 100 ); |
| 555 | |
| 556 | $last_id = (int) $this->check_progress( __FUNCTION__ ); |
| 557 | |
| 558 | $sql = " |
| 559 | SELECT `r`.*, `p`.`tbl_row_id` AS `real_id`, `p`.`datatype` |
| 560 | FROM `@wp_pod_rel` AS `r` |
| 561 | LEFT JOIN `@wp_pod` AS `p` ON `p`.`id` = `r`.`pod_id` |
| 562 | WHERE {$last_id} < `r`.`id` |
| 563 | AND `r`.`pod_id` IS NOT NULL |
| 564 | AND `r`.`field_id` IS NOT NULL |
| 565 | AND `p`.`id` IS NOT NULL |
| 566 | ORDER BY `r`.`id` |
| 567 | LIMIT 0, {$migration_limit} |
| 568 | "; |
| 569 | |
| 570 | $rel = pods_query( $sql ); |
| 571 | |
| 572 | $last_id = true; |
| 573 | |
| 574 | $pod_types = pods_query( 'SELECT `id`, `name` FROM `@wp_pod_types` ORDER BY `id`' ); |
| 575 | |
| 576 | $types = array(); |
| 577 | |
| 578 | $x = 0; |
| 579 | |
| 580 | if ( ! empty( $rel ) && ! empty( $pod_types ) ) { |
| 581 | foreach ( $pod_types as $type ) { |
| 582 | $type->name = pods_clean_name( $type->name ); |
| 583 | |
| 584 | $types[ $type->id ] = $this->api->load_pod( array( 'name' => $type->name ), false ); |
| 585 | |
| 586 | if ( empty( $types[ $type->id ] ) ) { |
| 587 | return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, relationships cannot be migrated', 'pods' ), $type->name ) ); |
| 588 | } |
| 589 | |
| 590 | $pod_fields = pods_query( "SELECT `id`, `name` FROM `@wp_pod_fields` WHERE `datatype` = {$type->id} ORDER BY `id`" ); |
| 591 | |
| 592 | $types[ $type->id ]['old_fields'] = array(); |
| 593 | |
| 594 | foreach ( $pod_fields as $field ) { |
| 595 | // Handle name changes |
| 596 | if ( in_array( $field->name, array( 'created', 'modified', 'author' ), true ) ) { |
| 597 | $field->name .= '2'; |
| 598 | } |
| 599 | |
| 600 | $types[ $type->id ]['old_fields'][ $field->id ] = $field->name; |
| 601 | } |
| 602 | }//end foreach |
| 603 | |
| 604 | foreach ( $rel as $r ) { |
| 605 | $r->pod_id = (int) $r->pod_id; |
| 606 | |
| 607 | if ( ! isset( $types[ $r->datatype ] ) || ! isset( $types[ $r->datatype ]['old_fields'][ $r->field_id ] ) ) { |
| 608 | continue; |
| 609 | } |
| 610 | |
| 611 | if ( ! isset( $types[ $r->datatype ]['fields'][ $types[ $r->datatype ]['old_fields'][ $r->field_id ] ] ) ) { |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | $field = $types[ $r->datatype ]['fields'][ $types[ $r->datatype ]['old_fields'][ $r->field_id ] ]; |
| 616 | |
| 617 | if ( ! in_array( $field['type'], array( 'pick', 'file' ), true ) ) { |
| 618 | continue; |
| 619 | } |
| 620 | |
| 621 | $pod_id = $types[ $r->datatype ]['id']; |
| 622 | $field_id = $field['id']; |
| 623 | $item_id = $r->real_id; |
| 624 | |
| 625 | $related_pod_id = 0; |
| 626 | $related_field_id = 0; |
| 627 | $related_item_id = $r->tbl_row_id; |
| 628 | |
| 629 | if ( 'pick' === $field['type'] ) { |
| 630 | $old_sister_id = (int) pods_v( '_pods_1x_sister_id', $field['options'], 0 ); |
| 631 | |
| 632 | if ( 0 < $old_sister_id ) { |
| 633 | $sql = ' |
| 634 | SELECT `f`.`id`, `f`.`name`, `t`.`name` AS `pod` |
| 635 | FROM `@wp_pod_fields` AS `f` |
| 636 | LEFT JOIN `@wp_pod_types` AS `t` ON `t`.`id` = `f`.`datatype` |
| 637 | WHERE `f`.`id` = ' . $old_sister_id . ' AND `t`.`id` IS NOT NULL |
| 638 | ORDER BY `f`.`id` |
| 639 | LIMIT 1 |
| 640 | '; |
| 641 | |
| 642 | $old_field = pods_query( $sql ); |
| 643 | |
| 644 | if ( empty( $old_field ) ) { |
| 645 | continue; |
| 646 | } |
| 647 | |
| 648 | $old_field = $old_field[0]; |
| 649 | |
| 650 | $related_field = $this->api->load_field( array( |
| 651 | 'name' => $old_field->name, |
| 652 | 'pod' => $old_field->pod, |
| 653 | ) ); |
| 654 | |
| 655 | if ( empty( $related_field ) ) { |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | $related_pod_id = $related_field['pod_id']; |
| 660 | $related_field_id = $related_field['id']; |
| 661 | } elseif ( 'pod' === $field['pick_object'] && 0 < strlen( $field['pick_val'] ) ) { |
| 662 | $related_pod = $this->api->load_pod( array( 'name' => $field['pick_val'] ), false ); |
| 663 | |
| 664 | if ( empty( $related_pod ) ) { |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | $related_pod_id = $related_pod['id']; |
| 669 | }//end if |
| 670 | }//end if |
| 671 | |
| 672 | $r->id = (int) $r->id; |
| 673 | $pod_id = (int) $pod_id; |
| 674 | $field_id = (int) $field_id; |
| 675 | $item_id = (int) $item_id; |
| 676 | $related_pod_id = (int) $related_pod_id; |
| 677 | $related_field_id = (int) $related_field_id; |
| 678 | $related_item_id = (int) $related_item_id; |
| 679 | $r->weight = (int) $r->weight; |
| 680 | |
| 681 | $table_data = array( |
| 682 | 'id' => $r->id, |
| 683 | 'pod_id' => $pod_id, |
| 684 | 'field_id' => $field_id, |
| 685 | 'item_id' => $item_id, |
| 686 | 'related_pod_id' => $related_pod_id, |
| 687 | 'related_field_id' => $related_field_id, |
| 688 | 'related_item_id' => $related_item_id, |
| 689 | 'weight' => $r->weight, |
| 690 | ); |
| 691 | |
| 692 | $table_formats = array_fill( 0, count( $table_data ), '%d' ); |
| 693 | |
| 694 | $sql = PodsData::insert_on_duplicate( '@wp_podsrel', $table_data, $table_formats ); |
| 695 | |
| 696 | pods_query( $sql ); |
| 697 | |
| 698 | $last_id = $r->id; |
| 699 | |
| 700 | $x ++; |
| 701 | |
| 702 | if ( 10 < $x ) { |
| 703 | $this->update_progress( __FUNCTION__, $last_id ); |
| 704 | |
| 705 | $x = 0; |
| 706 | } |
| 707 | }//end foreach |
| 708 | }//end if |
| 709 | |
| 710 | $this->update_progress( __FUNCTION__, $last_id ); |
| 711 | |
| 712 | if ( count( $rel ) === $migration_limit ) { |
| 713 | return '-2'; |
| 714 | } else { |
| 715 | return '1'; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * @return string |
| 721 | */ |
| 722 | public function migrate_settings() { |
| 723 | return $this->migrate_roles(); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * @return string |
| 728 | */ |
| 729 | public function migrate_roles() { |
| 730 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 731 | return '1'; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @var $wpdb WPDB |
| 736 | */ |
| 737 | global $wpdb; |
| 738 | |
| 739 | $wp_roles = get_option( "{$wpdb->prefix}user_roles" ); |
| 740 | |
| 741 | $old_roles = get_option( 'pods_roles' ); |
| 742 | |
| 743 | if ( ! is_array( $old_roles ) && ! empty( $old_roles ) ) { |
| 744 | $old_roles = pods_maybe_safely_unserialize( $old_roles ); |
| 745 | } |
| 746 | |
| 747 | if ( ! is_array( $old_roles ) ) { |
| 748 | $old_roles = array(); |
| 749 | } |
| 750 | |
| 751 | if ( ! empty( $old_roles ) ) { |
| 752 | foreach ( $old_roles as $role => $data ) { |
| 753 | if ( '_wpnonce' === $role ) { |
| 754 | continue; |
| 755 | } |
| 756 | |
| 757 | if ( ! isset( $wp_roles[ $role ] ) ) { |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | $caps = $wp_roles[ $role ]['capabilities']; |
| 762 | |
| 763 | foreach ( $data as $cap ) { |
| 764 | if ( 0 === strpos( 'manage_', $cap ) ) { |
| 765 | if ( 'manage_roles' === $cap ) { |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | $cap = pods_str_replace( 'manage_', 'pods_', $cap, 1 ); |
| 770 | $cap = pods_str_replace( 'pod_pages', 'pages', $cap, 1 ); |
| 771 | |
| 772 | $caps[ $cap ] = true; |
| 773 | } elseif ( 0 === strpos( 'pod_', $cap ) ) { |
| 774 | $keys = array( |
| 775 | pods_str_replace( 'pod_', 'pods_new_', $cap, 1 ), |
| 776 | pods_str_replace( 'pod_', 'pods_edit_', $cap, 1 ), |
| 777 | pods_str_replace( 'pod_', 'pods_delete_', $cap, 1 ), |
| 778 | ); |
| 779 | |
| 780 | foreach ( $keys as $key ) { |
| 781 | $caps[ $key ] = true; |
| 782 | } |
| 783 | } |
| 784 | }//end foreach |
| 785 | |
| 786 | $wp_roles[ $role ]['capabilities'] = $caps; |
| 787 | }//end foreach |
| 788 | }//end if |
| 789 | |
| 790 | update_option( "{$wpdb->prefix}user_roles", $wp_roles ); |
| 791 | |
| 792 | $this->update_progress( __FUNCTION__, true ); |
| 793 | |
| 794 | return '1'; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * @return array|string |
| 799 | */ |
| 800 | public function migrate_templates() { |
| 801 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 802 | return '1'; |
| 803 | } |
| 804 | |
| 805 | $templates = pods_query( 'SELECT * FROM `@wp_pod_templates`', false ); |
| 806 | |
| 807 | $results = array(); |
| 808 | |
| 809 | if ( ! empty( $templates ) ) { |
| 810 | foreach ( $templates as $template ) { |
| 811 | unset( $template->id ); |
| 812 | |
| 813 | $results[] = $this->api->save_template( $template ); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | $this->update_progress( __FUNCTION__, true ); |
| 818 | |
| 819 | return '1'; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * @return array|string |
| 824 | */ |
| 825 | public function migrate_pages() { |
| 826 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 827 | return '1'; |
| 828 | } |
| 829 | |
| 830 | $pages = pods_query( 'SELECT * FROM `@wp_pod_pages`', false ); |
| 831 | |
| 832 | if ( ! empty( $pages ) ) { |
| 833 | foreach ( $pages as $page ) { |
| 834 | unset( $page->id ); |
| 835 | |
| 836 | $this->api->save_page( $page ); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | $this->update_progress( __FUNCTION__, true ); |
| 841 | |
| 842 | return '1'; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * @return array|string |
| 847 | */ |
| 848 | public function migrate_helpers() { |
| 849 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 850 | return '1'; |
| 851 | } |
| 852 | |
| 853 | $helpers = pods_query( 'SELECT * FROM `@wp_pod_helpers`', false ); |
| 854 | |
| 855 | $notice = false; |
| 856 | |
| 857 | if ( ! empty( $helpers ) ) { |
| 858 | foreach ( $helpers as $helper ) { |
| 859 | unset( $helper->id ); |
| 860 | |
| 861 | if ( 'input' === $helper->helper_type ) { |
| 862 | $helper->status = 'draft'; |
| 863 | |
| 864 | $notice = true; |
| 865 | } |
| 866 | |
| 867 | $this->api->save_helper( $helper ); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | $this->update_progress( __FUNCTION__, true ); |
| 872 | |
| 873 | if ( $notice ) { |
| 874 | return pods_error( __( 'Input Helpers may not function in our new forms, we have imported and disabled them for your review.', 'pods' ) ); |
| 875 | } |
| 876 | |
| 877 | return '1'; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * @param $params |
| 882 | * |
| 883 | * @return mixed|string|void |
| 884 | */ |
| 885 | public function migrate_pod( $params ) { |
| 886 | /** |
| 887 | * @var $wpdb WPDB |
| 888 | */ |
| 889 | global $wpdb; |
| 890 | |
| 891 | if ( ! isset( $params->pod ) ) { |
| 892 | return pods_error( __( 'Invalid Pod.', 'pods' ) ); |
| 893 | } |
| 894 | |
| 895 | $pod = pods_sanitize( pods_clean_name( $params->pod ) ); |
| 896 | |
| 897 | if ( ! in_array( "{$wpdb->prefix}pod_tbl_{$pod}", $this->tables, true ) ) { |
| 898 | return pods_error( __( 'Table not found, items cannot be migrated', 'pods' ) ); |
| 899 | } |
| 900 | |
| 901 | if ( ! in_array( "{$wpdb->prefix}pods_{$pod}", $this->tables, true ) ) { |
| 902 | return pods_error( __( 'New table not found, items cannot be migrated', 'pods' ) ); |
| 903 | } |
| 904 | |
| 905 | if ( ! in_array( "{$wpdb->prefix}pod_types", $this->tables, true ) ) { |
| 906 | return pods_error( __( 'Pod Types table not found, items cannot be migrated', 'pods' ) ); |
| 907 | } |
| 908 | |
| 909 | if ( ! in_array( "{$wpdb->prefix}pod", $this->tables, true ) ) { |
| 910 | return pods_error( __( 'Pod table not found, items cannot be migrated', 'pods' ) ); |
| 911 | } |
| 912 | |
| 913 | if ( true === $this->check_progress( __FUNCTION__, $pod ) ) { |
| 914 | return '1'; |
| 915 | } |
| 916 | |
| 917 | $pod_data = $this->api->load_pod( array( 'name' => $pod ), false ); |
| 918 | |
| 919 | if ( empty( $pod_data ) ) { |
| 920 | return pods_error( sprintf( __( 'Pod <strong>%s</strong> not found, items cannot be migrated', 'pods' ), $pod ) ); |
| 921 | } |
| 922 | |
| 923 | $columns = array(); |
| 924 | $old_columns = array(); |
| 925 | |
| 926 | foreach ( $pod_data['fields'] as $field ) { |
| 927 | if ( ! in_array( $field['name'], array( |
| 928 | 'created', |
| 929 | 'modified', |
| 930 | 'author', |
| 931 | ), true ) && ! in_array( $field['type'], array( 'file', 'pick' ), true ) ) { |
| 932 | $columns[] = pods_sanitize( $field['name'] ); |
| 933 | $old_columns[] = pods_v( '_pods_1x_field_name', $field['options'], $field['name'], false ); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | $into = '`id`'; |
| 938 | $select = '`t`.`id`'; |
| 939 | |
| 940 | if ( ! empty( $columns ) ) { |
| 941 | $into .= ', `' . implode( '`, `', $columns ) . '`'; |
| 942 | $select .= ', `t`.`' . implode( '`, `t`.`', $old_columns ) . '`'; |
| 943 | } |
| 944 | |
| 945 | // Copy content from the old table into the new |
| 946 | $sql = " |
| 947 | REPLACE INTO `@wp_pods_{$pod}` |
| 948 | ( {$into} ) |
| 949 | ( SELECT {$select} |
| 950 | FROM `@wp_pod_tbl_{$pod}` AS `t` ) |
| 951 | "; |
| 952 | |
| 953 | pods_query( $sql ); |
| 954 | |
| 955 | // Copy index data from the old index table into the new individual table |
| 956 | $sql = " |
| 957 | UPDATE `@wp_pods_{$pod}` AS `t` |
| 958 | LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}' |
| 959 | LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id` |
| 960 | SET `t`.`created` = `p`.`created`, `t`.`modified` = `p`.`modified` |
| 961 | WHERE `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL |
| 962 | "; |
| 963 | |
| 964 | pods_query( $sql ); |
| 965 | |
| 966 | // Copy name data from the old index table into the new individual table (if name empty in indiv table) |
| 967 | $sql = " |
| 968 | UPDATE `@wp_pods_{$pod}` AS `t` |
| 969 | LEFT JOIN `@wp_pod_types` AS `x` ON `x`.`name` = '{$pod}' |
| 970 | LEFT JOIN `@wp_pod` AS `p` ON `p`.`datatype` = `x`.`id` AND `p`.`tbl_row_id` = `t`.`id` |
| 971 | SET `t`.`name` = `p`.`name` |
| 972 | WHERE ( `t`.`name` IS NULL OR `t`.`name` = '' ) AND `x`.`id` IS NOT NULL AND `p`.`id` IS NOT NULL |
| 973 | "; |
| 974 | |
| 975 | pods_query( $sql ); |
| 976 | |
| 977 | $this->update_progress( __FUNCTION__, true, $pod ); |
| 978 | |
| 979 | return '1'; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * @return string |
| 984 | */ |
| 985 | public function migrate_cleanup() { |
| 986 | update_option( 'pods_framework_upgraded_1_x', 1 ); |
| 987 | |
| 988 | PodsInit::$components->activate_component( 'templates' ); |
| 989 | PodsInit::$components->activate_component( 'pages' ); |
| 990 | PodsInit::$components->activate_component( 'helpers' ); |
| 991 | |
| 992 | $this->api->cache_flush_pods(); |
| 993 | |
| 994 | return '1'; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * |
| 999 | */ |
| 1000 | public function restart() { |
| 1001 | /** |
| 1002 | * @var $wpdb WPDB |
| 1003 | */ |
| 1004 | global $wpdb; |
| 1005 | |
| 1006 | foreach ( $this->tables as $table ) { |
| 1007 | if ( false !== strpos( $table, "{$wpdb->prefix}pods" ) ) { |
| 1008 | pods_query( "TRUNCATE `{$table}`", false ); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | delete_option( 'pods_framework_upgrade_2_0' ); |
| 1013 | delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); |
| 1014 | delete_option( 'pods_framework_upgraded_1_x' ); |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * |
| 1019 | */ |
| 1020 | public function cleanup() { |
| 1021 | /** |
| 1022 | * @var $wpdb WPDB |
| 1023 | */ |
| 1024 | global $wpdb; |
| 1025 | |
| 1026 | foreach ( $this->tables as $table ) { |
| 1027 | if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" === $table ) { |
| 1028 | pods_query( "DROP TABLE `{$table}`", false ); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | delete_option( 'pods_roles' ); |
| 1033 | delete_option( 'pods_version' ); |
| 1034 | delete_option( 'pods_framework_upgrade_2_0' ); |
| 1035 | delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); |
| 1036 | delete_option( 'pods_framework_upgraded_1_x' ); |
| 1037 | |
| 1038 | delete_option( 'pods_disable_file_browser' ); |
| 1039 | delete_option( 'pods_files_require_login' ); |
| 1040 | delete_option( 'pods_files_require_login_cap' ); |
| 1041 | delete_option( 'pods_disable_file_upload' ); |
| 1042 | delete_option( 'pods_upload_require_login' ); |
| 1043 | delete_option( 'pods_upload_require_login_cap' ); |
| 1044 | |
| 1045 | pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" ); |
| 1046 | } |
| 1047 | } |
| 1048 |