Skip to content

Instantly share code, notes, and snippets.

@andrezrv
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrezrv/11553983 to your computer and use it in GitHub Desktop.
Save andrezrv/11553983 to your computer and use it in GitHub Desktop.
<?php
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
add_action( 'wp_ajax_wpsc_migrate_anonymous_user', '_wpsc_meta_migrate_anonymous_user_worker' );
add_action( 'wp_ajax_nopriv_wpsc_migrate_anonymous_user', '_wpsc_meta_migrate_anonymous_user_worker' );
function _wpsc_meta_migrate_anonymous_user_worker() {
global $wpdb;
$blog_prefix = is_multisite() ? $wpdb->get_blog_prefix() : '';
$key_pattern = "{$blog_prefix}_wpsc_";
wp_suspend_cache_addition( true );
$sql = 'SELECT ID FROM '. $wpdb->users . ' WHERE user_login LIKE "\_%" AND user_email = "" AND user_login = user_nicename AND user_login = display_name LIMIT 100';
$user_ids = $wpdb->get_col( $sql, 0 );
// Create an array to store users to be removed.
$bin = array();
foreach ( $user_ids as $user_id ) {
$wpdb->query( 'INSERT INTO ' . $wpdb->wpsc_visitors . '(`id`) VALUES ( ' . $user_id . ' )' );
wpsc_set_visitor_expiration( $user_id, DAY_IN_SECONDS );
$meta = get_user_meta( $user_id );
foreach ( $meta as $key => $value ) {
if ( strpos( $key, $key_pattern ) === FALSE )
continue;
$short_key = str_replace( $key_pattern, '', $key );
if ( $short_key !== 'cart' ) {
wpsc_add_visitor_meta( $user_id , $short_key, $value[0] );
} else {
$wpsc_user_cart = maybe_unserialize( base64_decode( $value[0] ) );
if ( ! ($wpsc_user_cart instanceof wpsc_cart) ) {
$wpsc_user_cart = new wpsc_cart();
} else {
continue;
}
}
}
$comment_count = $wpdb->get_var( 'SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE user_id = ' . $user_id );
if ( ! count_user_posts( $user_id ) && ! $comment_count ) {
//wp_delete_user( $user_id );
// Add user to bin.
$bin[] = $user_id;
}
}
// Remove users.
if ( ! empty( $bin ) ) {
// Convert $bin to string.
$bin = implode( ',', $bin );
$wpdb->query( 'DELETE FROM ' . $wpdb->users . ' WHERE ID IN (' . $bin . ')' );
$wpdb->query( 'DELETE FROM ' . $wpdb->usermeta . ' WHERE user_id IN (' . $bin . ')' );
}
wp_suspend_cache_addition( false );
exit( 0 );
}
}
add_action( 'wpsc_migrate_anonymous_user_cron', '_wpsc_meta_migrate_anonymous_user_cron' );
function _wpsc_meta_migrate_anonymous_user_cron() {
global $wpdb;
set_time_limit( 10 * 60 ); // 10 minutes maximum for the cron
// WPEC created user records with a funky format, no email is a dead giveaway, as is login, user name and display name being idnentical with the '_'
$sql = 'SELECT count( ID ) FROM '. $wpdb->users . ' WHERE user_login LIKE "\_%" AND user_email = "" AND user_login = user_nicename AND user_login = display_name LIMIT 1';
$ids_to_migrate = $user_ids = $wpdb->get_var( $sql );
if ( $ids_to_migrate ) {
$response = wp_remote_post( admin_url( 'admin-ajax.php' ) . '?action=wpsc_migrate_anonymous_user' , array( 'blocking' => true, ) );
wp_schedule_single_event( time() + 30 , 'wpsc_migrate_anonymous_user_cron' );
} else {
wpsc_core_flush_temporary_data();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment