Created
April 4, 2018 19:31
-
-
Save spacedmonkey/ef0254e9dca9505d257dfe746989bd28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function migrate1( $args, $assoc_args ) { | |
global $wpdb; | |
$user_list = $wpdb->get_col( "SELECT ID FROM $wpdb->users;" ); | |
// Number of users returned by query | |
$found_users = count( $user_list ); | |
// Generate progess bar | |
$progress = new \cli\progress\Bar( 'Progress', $found_users ); | |
$this->start_bulk_operation(); | |
// Counter for number of user successfully processed | |
$counter_success = 0; | |
// Counter for number of user processed | |
$counter_processed = 0; | |
$counter_skipped = 0; | |
// The Loop | |
if ( ! empty( $user_list ) ) { | |
foreach ( $user_list as $user_id ) { | |
$list = get_blogs_of_user( $user_id ); | |
$blogs = wp_list_pluck( $list, 'userblog_id' ); | |
if ( ! empty( $blogs ) ) { | |
foreach ( $blogs as $blog_id ) { | |
$user = new WP_User( $user_id, '', $blog_id ); | |
$prefix = $wpdb->get_blog_prefix( $blog_id ); | |
foreach ( $user->roles as $role ) { | |
$key = $prefix . 'role_' . $role; | |
update_user_meta( $user_id, $key, 1 ); | |
} | |
} | |
$counter_success ++; | |
} else { | |
$counter_skipped ++; | |
} | |
$counter_processed ++; | |
$progress->tick(); | |
} | |
} | |
$progress->finish(); | |
$this->end_bulk_operation(); | |
if ( 0 === $found_users ) { | |
WP_CLI::error( __( 'No users found', 'th-user-storage' ) ); | |
} elseif ( 0 === $counter_processed ) { | |
WP_CLI::error( __( 'No users processed', 'th-user-storage' ) ); | |
} else { | |
$items = [ | |
[ | |
'title' => 'success', | |
'total' => $counter_success, | |
], | |
[ | |
'title' => 'skipped', | |
'total' => $counter_skipped, | |
], | |
[ | |
'title' => 'total', | |
'total' => $counter_processed, | |
], | |
]; | |
WP_CLI\Utils\format_items( 'table', $items, [ 'title', 'total' ] ); | |
WP_CLI::success( __( 'Processing compelete.', 'th-user-storage' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment