Skip to content

Commit

Permalink
Merge pull request #17 from 10up/feature/users-mapping-improvements
Browse files Browse the repository at this point in the history
Improvements to the author mapping.
  • Loading branch information
nicholasio committed Dec 21, 2016
2 parents f0db8d5 + 8687160 commit 4d85472
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 49 deletions.
52 changes: 48 additions & 4 deletions includes/commands/class-mu-migration-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,70 @@ protected function all_posts( $query_args, $callback, $verbose = true ) {
), $verbose );
}

/**
* Run through all records on a specific table
*
* @param $message
* @param $table
* @param $callback
*/
protected function all_records( $message, $table, $callback ) {
global $wpdb;

$offset = 0;
$step = 1000;

$found_posts = $wpdb->get_col( "SELECT COUNT(ID) FROM {$table}");

if ( ! $found_posts ) {
return false;
}

$found_posts = $found_posts[0];

$progress_bar = \WP_CLI\Utils\make_progress_bar( sprintf("[%d] %s", $found_posts, $message ), (int) $found_posts, 1 );
$progress_bar->display();

do {
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$table} LIMIT %d OFFSET %d", array(
$step,
$offset
) )
);

if ( $results ) {
foreach ( $results as $result ) {
$callback( $result );
$progress_bar->tick();
}
}

$offset += $step;

} while( $results );
}

public function line( $msg, $verbose ) {
protected function line( $msg, $verbose ) {
if ( $verbose ) {
WP_CLI::line( $msg );
}
}

public function log( $msg, $verbose ) {
protected function log( $msg, $verbose ) {
if ( $verbose ) {
WP_CLI::log( $msg );
}
}

public function success( $msg, $verbose ) {
protected function success( $msg, $verbose ) {
if ( $verbose ) {
WP_CLI::success( $msg );
}
}

public function warning( $msg, $verbose ) {
protected function warning( $msg, $verbose ) {
if ( $verbose ) {
WP_CLI::warning( $msg );
}
Expand Down
2 changes: 0 additions & 2 deletions includes/commands/class-mu-migration-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,6 @@ public function all( $args = array(), $assoc_args = array() ) {
$this->users( array( $users[0] ), $users_assoc_args, $verbose );

if ( file_exists( $map_file ) ) {
WP_CLI::log( __( 'Updating post_author...', 'mu-migration' ) );

$postsCommand = new PostsCommand();

$postsCommand->update_author(
Expand Down
63 changes: 22 additions & 41 deletions includes/commands/class-mu-migration-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PostsCommand extends MUMigrationBase {
* @synopsis <inputfile> --blog_id=<blog_id>
*/
public function update_author( $args = array(), $assoc_args = array(), $verbose = true ) {
global $wpdb;

$this->process_args(
array(
0 => '', // .json map file
Expand Down Expand Up @@ -60,85 +62,64 @@ public function update_author( $args = array(), $assoc_args = array(), $verbose
);
}


$equals_id = array();
$author_not_found = array();

$posts_args = array(
'post_type' => get_post_types()
);
$this->all_records(
__( 'Updating posts authors', 'mu-migration' ),
$wpdb->posts,
function( $result ) use ( &$equals_id, &$author_not_found, $ids_map, $verbose, $is_woocommerce ) {
$author = $result->post_author;

if ( $is_woocommerce ) {
$posts_args['post_type'] = array_merge( $posts_args['post_type'], array( 'shop_order', 'shop_order_refund' ) );
$posts_args['post_status'] = array(
'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed',
'wc-cancelled', 'wc-refunded','wc-failed', 'wc-pre-ordered'
);
}

$this->all_posts(
$posts_args,
function() use ( &$equals_id, &$author_not_found, $ids_map, $verbose, $is_woocommerce ) {
$author = get_the_author_meta( 'ID' );
if ( isset( $ids_map->{$author} ) ) {
if ( $author != $ids_map->{$author} ) {
global $wpdb;

$wpdb->update( $wpdb->posts,
array(
'post_author' => $ids_map->{$author}
),
array(
'ID' => get_the_ID()
),
array(
'%d'
),
array(
'%d'
)
array( 'post_author' => $ids_map->{$author} ),
array( 'ID' => $result->ID ),
array( '%d' ),
array( '%d' )
);


$this->log( sprintf(
__( 'Updated post_author for "%s" (ID #%d)', 'mu-migration' ),
get_the_title(),
absint( get_the_ID() )
$result->post_title,
absint( $result->ID )
), $verbose );

} else {
$this->log( sprintf(
__( '#%d New user ID equals to the old user ID'),
get_the_ID()
$result->ID
), $verbose );
$equals_id[] = absint( get_the_ID() );
$equals_id[] = absint( $result->ID );
}
} else {
$this->log( sprintf(
__( "#%d New user ID not found or it's already been updated", 'mu-migration'),
absint( get_the_ID() )
absint( $result->ID )
), $verbose );

$author_not_found[] = absint( get_the_ID() );
$author_not_found[] = absint( $result->ID );
}

if ( $is_woocommerce ) {
$old_customer_user = get_post_meta( get_the_ID(), '_customer_user', true );
$old_customer_user = get_post_meta( (int) $result->ID, '_customer_user', true );

if ( isset( $ids_map->{$old_customer_user} ) && $old_customer_user != $ids_map->{$old_customer_user} ) {
$new_customer_user = $ids_map->{$old_customer_user};

update_post_meta( get_the_ID(), '_customer_user', $new_customer_user );
update_post_meta( (int) $result->ID, '_customer_user', $new_customer_user );

$this->log( sprintf(
__( 'Updated customer_user for "%s" (ID #%d)', 'mu-migration' ),
get_the_title(),
absint( get_the_ID() )
$result->post_title,
absint( $result->ID )
), $verbose );
}
}
},
false
}
);

//Report
Expand Down
4 changes: 2 additions & 2 deletions mu-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: MU Migration
* Plugin URI: http://10up.com
* Description: A set of WP-CLI commands to support the migration of single WordPress instances over to multisite
* Version: 0.2.5
* Version: 0.2.6
* Author: Nícholas André, 10up
* Author URI: http://10up.com
* Text Domain: mu-migration
Expand All @@ -17,7 +17,7 @@
return;
}

define( 'TENUP_MU_MIGRATION_VERSION', '0.2.5' );
define( 'TENUP_MU_MIGRATION_VERSION', '0.2.6' );
define( 'TENUP_MU_MIGRATION_COMMANDS_PATH', 'includes/commands/' );

// we only need to require autoload if running as a plugin
Expand Down

0 comments on commit 4d85472

Please sign in to comment.