BBP_Converter_Base::convert_table( $to_type,  $start )

Convert Table


Description Description


Parameters Parameters

(Required) Start row


Top ↑

Source Source

File: includes/admin/classes/class-bbp-converter-base.php

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
public function convert_table( $to_type, $start ) {
 
    // Set some defaults
    $has_insert     = false;
    $from_tablename = '';
    $field_list     = $from_tables = $tablefield_array = array();
 
    // Toggle Table Name based on $to_type (destination)
    switch ( $to_type ) {
        case 'user' :
            $tablename = $this->wpdb->users;
            break;
 
        case 'tags' :
            $tablename = '';
            break;
 
        case 'forum_subscriptions' :
            $tablename = $this->wpdb->postmeta;
            break;
 
        case 'topic_subscriptions' :
            $tablename = $this->wpdb->postmeta;
            break;
 
        case 'favorites' :
            $tablename = $this->wpdb->postmeta;
            break;
 
        default :
            $tablename = $this->wpdb->posts;
    }
 
    // Get the fields from the destination table
    if ( ! empty( $tablename ) ) {
        $tablefield_array = $this->get_fields( $tablename );
    }
 
    /** Step 1 ************************************************************/
 
    // Loop through the field maps, and look for to_type matches
    foreach ( $this->field_map as $item ) {
 
        // Yay a match, and we have a from table, too
        if ( ( $item['to_type'] === $to_type ) && ! empty( $item['from_tablename'] ) ) {
 
            // $from_tablename was set from a previous loop iteration
            if ( ! empty( $from_tablename ) ) {
 
                // Doing some joining
                if ( ! in_array( $item['from_tablename'], $from_tables, true ) && in_array( $item['join_tablename'], $from_tables, true ) ) {
                    $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
                }
 
            // $from_tablename needs to be set
            } else {
                $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
            }
 
            // Specific FROM expression data used
            if ( ! empty( $item['from_expression'] ) ) {
 
                // No 'WHERE' in expression
                if ( stripos( $from_tablename, "WHERE" ) === false ) {
                    $from_tablename .= ' ' . $item['from_expression'];
 
                // 'WHERE' in expression, so replace with 'AND'
                } else {
                    $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] );
                }
            }
 
            // Add tablename and fieldname to arrays, formatted for querying
            $from_tables[] = $item['from_tablename'];
            $field_list[]  = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
        }
    }
 
    /** Step 2 ************************************************************/
 
    // We have a $from_tablename, so we want to get some data to convert
    if ( ! empty( $from_tablename ) ) {
 
        // Update rows
        $this->count_rows_by_table( "{$this->opdb->prefix}{$from_tablename}" );
 
        // Get some data from the old forums
        $field_list  = array_unique( $field_list );
        $fields      = implode( ',', $field_list );
        $forum_query = "SELECT {$fields} FROM {$this->opdb->prefix}{$from_tablename} LIMIT {$start}, {$this->max_rows}";
 
        // Set this query as the last one ran
        $this->update_query( $forum_query );
 
        // Get results as an array
        $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
 
        // Query returned some results
        if ( ! empty( $forum_array ) ) {
 
            // Loop through results
            foreach ( (array) $forum_array as $forum ) {
 
                // Reset some defaults
                $insert_post = $insert_postmeta = $insert_data = array();
 
                // Loop through field map, again...
                foreach ( $this->field_map as $row ) {
 
                    // Types match and to_fieldname is present. This means
                    // we have some work to do here.
                    if ( ( $row['to_type'] === $to_type ) && isset( $row['to_fieldname'] ) ) {
 
                        // This row has a destination that matches one of the
                        // columns in this table.
                        if ( in_array( $row['to_fieldname'], $tablefield_array, true ) ) {
 
                            // Allows us to set default fields.
                            if ( isset( $row['default'] ) ) {
                                $insert_post[ $row['to_fieldname'] ] = $row['default'];
 
                            // Translates a field from the old forum.
                            } elseif ( isset( $row['callback_method'] ) ) {
                                if ( ( 'callback_userid' === $row['callback_method'] ) && ( false === $this->convert_users ) ) {
                                    $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
                                } else {
                                    $insert_post[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
                                }
 
                            // Maps the field from the old forum.
                            } else {
                                $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
                            }
 
                        // Destination field is not empty, so we might need
                        // to do some extra work or set a default.
                        } elseif ( ! empty( $row['to_fieldname'] ) ) {
 
                            // Allows us to set default fields.
                            if ( isset( $row['default'] ) ) {
                                $insert_postmeta[ $row['to_fieldname'] ] = $row['default'];
 
                            // Translates a field from the old forum.
                            } elseif ( isset( $row['callback_method'] ) ) {
                                if ( ( $row['callback_method'] === 'callback_userid' ) && ( false === $this->convert_users ) ) {
                                    $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
                                } else {
                                    $insert_postmeta[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
                                }
 
                            // Maps the field from the old forum.
                            } else {
                                $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
                            }
                        }
                    }
                }
 
                /** Step 3 ************************************************/
 
                // Something to insert into the destination field
                if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) {
 
                    switch ( $to_type ) {
 
                        /** New user **************************************/
 
                        case 'user' :
                            if ( username_exists( $insert_post['user_login'] ) ) {
                                $insert_post['user_login'] = "imported_{$insert_post['user_login']}";
                            }
 
                            if ( email_exists( $insert_post['user_email'] ) ) {
                                $insert_post['user_email'] = "imported_{$insert_post['user_email']}";
                            }
 
                            if ( empty( $insert_post['user_pass'] ) ) {
                                $insert_post['user_pass'] = '';
                            }
 
                            // Internally re-calls _exists() checks above.
                            // Also checks for existing nicename.
                            $post_id = wp_insert_user( $insert_post );
 
                            if ( is_numeric( $post_id ) ) {
                                foreach ( $insert_postmeta as $key => $value ) {
                                    add_user_meta( $post_id, $key, $value, true );
 
                                    if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
                                        $this->wpdb->insert( $this->sync_table_name, array(
                                            'value_type' => 'user',
                                            'value_id'   => $post_id,
                                            'meta_key'   => $key,
                                            'meta_value' => $value
                                        ) );
                                    }
                                }
                            }
                            break;
 
                        /** New Topic-Tag *********************************/
 
                        case 'tags' :
                            $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
                            $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag');
                            if ( false !== $term ) {
                                wp_update_term( $term->term_id, 'topic-tag', array(
                                    'description' => $insert_postmeta['description'],
                                    'slug'        => $insert_postmeta['slug']
                                ) );
                            }
                            break;
 
                        /** Forum Subscriptions ***************************/
 
                        case 'forum_subscriptions' :
                            $user_id = $insert_post['user_id'];
                            $items   = wp_list_pluck( $insert_postmeta, '_bbp_forum_subscriptions' );
                            if ( is_numeric( $user_id ) && ! empty( $items ) ) {
                                foreach ( $items as $value ) {
 
                                    // Maybe string with commas
                                    $value = is_string( $value )
                                        ? explode( ',', $value )
                                        : (array) $value;
 
                                    // Add user ID to forums subscribed users
                                    foreach ( $value as $fav ) {
                                        bbp_add_user_subscription( $user_id, $this->callback_forumid( $fav ) );
                                    }
                                }
                            }
                            break;
 
                        /** Subscriptions *********************************/
 
                        case 'topic_subscriptions' :
                            $user_id = $insert_post['user_id'];
                            $items   = wp_list_pluck( $insert_postmeta, '_bbp_subscriptions' );
                            if ( is_numeric( $user_id ) && ! empty( $items ) ) {
                                foreach ( $items as $value ) {
 
                                    // Maybe string with commas
                                    $value = is_string( $value )
                                        ? explode( ',', $value )
                                        : (array) $value;
 
                                    // Add user ID to topics subscribed users
                                    foreach ( $value as $fav ) {
                                        bbp_add_user_subscription( $user_id, $this->callback_topicid( $fav ) );
                                    }
                                }
                            }
                            break;
 
                        /** Favorites *************************************/
 
                        case 'favorites' :
                            $user_id = $insert_post['user_id'];
                            $items   = wp_list_pluck( $insert_postmeta, '_bbp_favorites' );
                            if ( is_numeric( $user_id ) && ! empty( $items ) ) {
                                foreach ( $items as $value ) {
 
                                    // Maybe string with commas
                                    $value = is_string( $value )
                                        ? explode( ',', $value )
                                        : (array) $value;
 
                                    // Add user ID to topics favorited users
                                    foreach ( $value as $fav ) {
                                        bbp_add_user_favorite( $user_id, $this->callback_topicid( $fav ) );
                                    }
                                }
                            }
                            break;
 
                        /** Forum, Topic, Reply ***************************/
 
                        default :
                            $post_id = wp_insert_post( $insert_post, true );
 
                            if ( is_numeric( $post_id ) ) {
                                foreach ( $insert_postmeta as $key => $value ) {
                                    add_post_meta( $post_id, $key, $value, true );
 
                                    /**
                                     * If we are using the sync_table add
                                     * the meta '_id' keys to the table
                                     *
                                     * Forums:  _bbp_old_forum_id         // The old forum ID
                                     *          _bbp_old_forum_parent_id  // The old forum parent ID
                                     *
                                     * Topics:  _bbp_forum_id             // The new forum ID
                                     *          _bbp_old_topic_id         // The old topic ID
                                     *          _bbp_old_closed_status_id // The old topic open/closed status
                                     *          _bbp_old_sticky_status_id // The old topic sticky status
                                     *
                                     * Replies: _bbp_forum_id             // The new forum ID
                                     *          _bbp_topic_id             // The new topic ID
                                     *          _bbp_old_reply_id         // The old reply ID
                                     *          _bbp_old_reply_to_id      // The old reply to ID
                                     */
                                    if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) {
                                        $this->wpdb->insert( $this->sync_table_name, array(
                                            'value_type' => 'post',
                                            'value_id'   => $post_id,
                                            'meta_key'   => $key,
                                            'meta_value' => $value
                                        ) );
                                    }
 
                                    /**
                                     * Replies need to save their old reply_to ID for
                                     * hierarchical replies association. Later we update
                                     * the _bbp_reply_to value with the new bbPress
                                     * value using convert_reply_to_parents()
                                     */
                                    if ( ( 'reply' === $to_type ) && ( '_bbp_old_reply_to_id' === $key ) ) {
                                        add_post_meta( $post_id, '_bbp_reply_to', $value );
                                    }
                                }
                            }
                            break;
                    }
                    $has_insert = true;
                }
            }
        }
    }
 
    return ! $has_insert;
}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.