From 1165caab60b7e3a896e996a2d91bd3dc5d1bbeb5 Mon Sep 17 00:00:00 2001 From: Matt Mullenweg Date: Sun, 12 Jun 2005 20:49:13 +0000 Subject: [PATCH] Beginning of user system overhaul, TODO: update all code that deals with user data git-svn-id: https://develop.svn.wordpress.org/trunk@2631 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/upgrade-functions.php | 39 ++++++++++++++++++++++++++ wp-admin/upgrade-schema.php | 12 +++++++- wp-includes/functions.php | 15 ++++++++++ wp-includes/pluggable-functions.php | 43 +++++++++++++++++++---------- wp-settings.php | 1 + 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index 290b477f7c..16501bbfab 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -9,6 +9,7 @@ function upgrade_all() { upgrade_101(); upgrade_110(); upgrade_130(); + upgrade_160(); save_mod_rewrite_rules(); } @@ -213,6 +214,44 @@ function upgrade_130() { make_site_theme(); } +function upgrade_160() { + global $wpdb, $table_prefix; + $users = $wpdb->get_results("SELECT * FROM $wpdb->users"); + foreach ( $users as $user ) : + if ( !empty( $user->user_firstname ) ) + update_usermeta( $user->ID, 'first_name', $user->user_firstname ); + if ( !empty( $user->user_lastname ) ) + update_usermeta( $user->ID, 'last_name', $user->user_lastname ); + if ( !empty( $user->user_nickname ) ) + update_usermeta( $user->ID, 'nickname', $user->user_nickname ); + if ( !empty( $user->user_level ) ) + update_usermeta( $user->ID, $table_prefix . 'user_level', $user->user_level ); + if ( !empty( $user->user_icq ) ) + update_usermeta( $user->ID, 'icq', $user->user_icq ); + if ( !empty( $user->user_aim ) ) + update_usermeta( $user->ID, 'aim', $user->user_aim ); + if ( !empty( $user->user_msn ) ) + update_usermeta( $user->ID, 'msn', $user->user_msn ); + if ( !empty( $user->user_yim ) ) + update_usermeta( $user->ID, 'yim', $user->user_icq ); + if ( !empty( $user->user_description ) ) + update_usermeta( $user->ID, 'description', $user->user_description ); + $idmode = $user->user_idmode; + if ($idmode == 'nickname') $id = $user->user_nickname; + if ($idmode == 'login') $id = $user->user_login; + if ($idmode == 'firstname') $id = $user->user_firstname; + if ($idmode == 'lastname') $id = $user->user_lastname; + if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname; + if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname; + if (!$idmode) $id = $user->user_nickname; + $id = addslashes( $id ); + $wpdb->query("UPDATE $wpdb->users SET display_name = '$id' WHERE ID = '$user->ID'"); + endforeach; + $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname' ); + foreach ( $old_user_fields as $old ) + $wpdb->query("ALTER TABLE $wpdb->users DROP $old"); +} + // The functions we use to actually do stuff // General diff --git a/wp-admin/upgrade-schema.php b/wp-admin/upgrade-schema.php index 5c2e9237f8..cc9cd8dc7e 100644 --- a/wp-admin/upgrade-schema.php +++ b/wp-admin/upgrade-schema.php @@ -91,7 +91,7 @@ CREATE TABLE $wpdb->postmeta ( meta_id bigint(20) NOT NULL auto_increment, post_id bigint(20) NOT NULL default '0', meta_key varchar(255) default NULL, - meta_value text, + meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), KEY meta_key (meta_key) @@ -144,8 +144,18 @@ CREATE TABLE $wpdb->users ( user_activation_key varchar(60) NOT NULL default '', user_status int(11) NOT NULL default '0', user_description longtext NOT NULL default '', + display_name varchar(250) NOT NULL default '', PRIMARY KEY (ID), UNIQUE KEY user_login (user_login) +); +CREATE TABLE $wpdb->usermeta ( + umeta_id bigint(20) NOT NULL auto_increment, + user_id bigint(20) NOT NULL default '0', + meta_key varchar(255) default NULL, + meta_value longtext, + PRIMARY KEY (umeta_id), + KEY user_id (user_id), + KEY meta_key (meta_key) );"; function populate_options() { diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 19f79437b7..db7f65bccf 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1886,4 +1886,19 @@ function nocache_headers() { @ header('Pragma: no-cache'); } +function update_usermeta( $user_id, $meta_key, $meta_value ) { + global $wpdb; + $user_id = (int) $user_id; + $meta_key = preg_replace('|a-z0-9_|i', '', $meta_key); + $cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); + if ( !$cur ) { + $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value ) + VALUES + ( '$user_id', '$meta_key', '$meta_value' )"); + return true; + } + if ( $cur->meta_value != $meta_value ) + $wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'"); +} + ?> \ No newline at end of file diff --git a/wp-includes/pluggable-functions.php b/wp-includes/pluggable-functions.php index 14437c7317..3220c8b440 100644 --- a/wp-includes/pluggable-functions.php +++ b/wp-includes/pluggable-functions.php @@ -31,29 +31,42 @@ function get_currentuserinfo() { endif; if ( !function_exists('get_userdata') ) : -function get_userdata($userid) { +function get_userdata( $user_id ) { global $wpdb, $cache_userdata; - $userid = (int) $userid; - if ( empty($cache_userdata[$userid]) && $userid != 0) { - $cache_userdata[$userid] = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = $userid"); - $cache_userdata[$cache_userdata[$userid]->user_login] =& $cache_userdata[$userid]; - } + $user_id = (int) $user_id; + if ( $user_id == 0 ) + return false; - return $cache_userdata[$userid]; + if ( isset( $cache_userdata[$user_id] ) ) + return $cache_userdata[$user_id]; + + if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id'") ) + return $cache_userdata[$user_id] = false; + + $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'"); + + foreach ( $metavalues as $meta ) + $user->{$meta->meta_key} = $meta->meta_value; + + die(var_dump($user)); + $cache_userdata[$cache_userdata[$userid]->user_login] =& $cache_userdata[$user_id]; + + return $cache_userdata[$user_id]; } endif; if ( !function_exists('get_userdatabylogin') ) : function get_userdatabylogin($user_login) { global $cache_userdata, $wpdb; - if ( !empty($user_login) && empty($cache_userdata[$user_login]) ) { - $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'"); /* todo: get rid of this intermediate var */ - $cache_userdata[$user->ID] = $user; - $cache_userdata[$user_login] =& $cache_userdata[$user->ID]; - } else { - $user = $cache_userdata[$user_login]; - } - return $user; + $user_login = addslashes( $user_login ); + if ( empty( $user_login ) ) + return false; + if ( isset( $cache_userdata[$user_login] ) ) + return $cache_userdata[$user_login]; + + $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$user_login'"); + + return get_userdata( $user_id ); } endif; diff --git a/wp-settings.php b/wp-settings.php index 074616c504..7ba1d07c38 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -48,6 +48,7 @@ $wpdb->links = $table_prefix . 'links'; $wpdb->linkcategories = $table_prefix . 'linkcategories'; $wpdb->options = $table_prefix . 'options'; $wpdb->postmeta = $table_prefix . 'postmeta'; +$wpdb->usermeta = $table_prefix . 'usermeta'; if ( defined('CUSTOM_USER_TABLE') ) $wpdb->users = CUSTOM_USER_TABLE;