get_results( $wpdb->prepare( 'SELECT id, email FROM %i WHERE id > %d AND orders_count > 0 AND email IS NOT NULL ORDER BY id ASC LIMIT %d', $t, $cursor, self::BATCH ), ARRAY_A ); foreach ( (array) $rows as $r ) { $cursor = (int) $r['id']; if ( Nodebook_Activities::count_type( $cursor, 'order' ) < (int) Nodebook_Contacts::get( $cursor )['orders_count'] ) { Nodebook_Woocommerce::history( $cursor, $r['email'] ); } } if ( count( (array) $rows ) === self::BATCH ) { update_option( self::CURSOR, $cursor, false ); wp_schedule_single_event( time() + 10, self::HOOK ); } else { delete_option( self::CURSOR ); } } /** Remove links to staff accounts whose email isn't the contact's own. */ public static function unlink_staff() { global $wpdb; $t = Nodebook_Contacts::table(); $users = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT wp_user_id FROM %i WHERE wp_user_id > 0', $t ) ); $fixed = 0; foreach ( (array) $users as $uid ) { $uid = (int) $uid; $u = get_userdata( $uid ); if ( $u && ! self::is_staff( $uid ) ) { continue; } $email = $u ? strtolower( (string) $u->user_email ) : ''; $fixed += (int) $wpdb->query( $wpdb->prepare( 'UPDATE %i SET wp_user_id = 0 WHERE wp_user_id = %d AND ( email IS NULL OR LOWER(email) <> %s )', $t, $uid, $email ) ); } if ( $fixed ) { delete_transient( 'nodebook_dupe_count' ); } return $fixed; } }