Skip to content

Instantly share code, notes, and snippets.

@danielck
Created May 12, 2017 10:58
Show Gist options
  • Select an option

  • Save danielck/df8a4428136ddada6c6ebc9208a0ea86 to your computer and use it in GitHub Desktop.

Select an option

Save danielck/df8a4428136ddada6c6ebc9208a0ea86 to your computer and use it in GitHub Desktop.

Revisions

  1. danielck created this gist May 12, 2017.
    71 changes: 71 additions & 0 deletions migrate-comments.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    <?php
    /*
    Plugin Name: Migrate comment likes
    Version: 0.1
    Description: Migrate comment likes from WP Comment Rating Pro to Ulike
    Author: Daniel Koskinen / Zeeland Family
    Author URI: http://wordpress.zeelandfamily.fi/
    */

    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_command( 'migrate-comment-likes', 'ZF_Migrate_Comment_Likes' );
    }

    class ZF_Migrate_Comment_Likes extends WP_CLI_Command {

    /**
    *
    * @subcommand migrate
    *
    **/
    function migrate( $args, $assoc_args ) {

    WP_CLI::log( 'Starting migration...' );

    WP_CLI::log( 'Reading WP Comment Rating Pro DB table' );

    global $wpdb;

    $old_comments = $wpdb->get_results(
    "SELECT * FROM {$wpdb->prefix}comment_rating",
    'ARRAY_A'
    );

    $old_comments_count = count( $old_comments );

    WP_CLI::success( $old_comments_count . ' comments with likes found.' );

    // WP_CLI::log( print_r( $old_comments[0] ) );

    // Set up our static data. This will be the same for every entry.
    $datetime = date( 'Y-m-d H:i:s' );
    $host = gethostname();
    $ip = gethostbyname( $host );
    $fake_user_id = 1;

    WP_CLI::log( "Date: " . $datetime . " Ip: " . $ip . " User: " . $fake_user_id );

    foreach ( $old_comments as $old_comment ) {

    $comment_id = (int) $old_comment['ck_comment_id'];
    $comment_likes = (int) $old_comment['ck_rating_up'];

    if ( $comment_likes > 0 ) {
    WP_CLI::log( "Processing comment ". $comment_id . ". " . $comment_likes . " likes found." );

    update_comment_meta( $comment_id, '_commentliked', $comment_likes );

    for ( $i = 1 ; $i <= $comment_likes; $i++ ) {
    // Insert a new row to the Ulike table for each like
    $insert_row = $wpdb->query(
    $wpdb->prepare( "INSERT INTO {$wpdb->prefix}ulike_comments ( `comment_id`, `date_time`, `ip`, `user_id`, `status` ) VALUES ('%d', '%s', '%s', '%d', '%s')", $comment_id, $datetime, $ip, $fake_user_id, 'like' )
    );
    }
    }
    }

    WP_CLI::success( 'OK' );

    }

    }