c59732cb by Chris Boden

Updated UserSearch to return WP_User object, updated UserManager to reflect

1 parent 094cd5c1
......@@ -98,7 +98,7 @@ function get_users($role = null, $pagenum=1, $records_per_page=0, $return_count_
$fs = Array('wp_capabilities' => "a:1:{s:{$len}:\\\\\\\\\"{$role}\\\\\\\\\"");
}
return new Tools\UserSearch(
$users = new Tools\UserSearch(
Array('member_id', 'first_name', 'last_name', 'home_email', 'work_email', 'email_address_preference', 'status', 'wp_capabilities')
, $records_per_page
, $pagenum
......@@ -107,6 +107,9 @@ function get_users($role = null, $pagenum=1, $records_per_page=0, $return_count_
, $fs
, $search
);
$users->setUserClass('Tz\WordPress\CBV\User\Account');
return $users;
}
function create_user() {
......
......@@ -185,19 +185,19 @@ if ($last > 1) {
<tbody>
<?php
foreach ($site_users as $user):
$pref = strtolower($user['email_address_preference']);
$pref = strtolower($user->email_address_preference);
if (!in_array($pref, Array('home', 'work'))) {
$pref = (empty($data['home_email']) ? 'work' : 'home');
$pref = (empty($user->home_email) ? 'work' : 'home');
}
?>
<tr>
<td><a href="/wp-admin/admin.php?page=cbv_users&action=edit&uid=<?php echo $user['user_id']?>"><?php echo $user['last_name'].', '.$user['first_name']?></a></td>
<td><a href="/wp-admin/admin.php?page=cbv_users&action=edit&uid=<?php echo $user['user_id']?>"><?php echo $user['member_id']?></a></td>
<td><?php echo $user[$pref . '_email']?></td>
<td><?php echo current(array_keys($user['wp_capabilities'])); ?></td>
<td><?php echo ucwords($user['status']);?></td>
<td><a href="/wp-admin/admin.php?page=cbv_users&action=edit&uid=<?php echo $user->id; ?>"><?php echo $user->last_name . ', ' . $user->first_name; ?></a></td>
<td><a href="/wp-admin/admin.php?page=cbv_users&action=edit&uid=<?php echo $user->id; ?>"><?php echo $user->member_id; ?></a></td>
<td><?php echo $user->{$pref . '_email'}; ?></td>
<td><?php echo current(array_keys($user->wp_capabilities)); ?></td>
<td><?php echo ucwords($user->status);?></td>
<td><a href="/wp-admin/admin-ajax.php?ajax=yes&action=build_user_remove&uid=<?php echo $user['user_id'];?>" class="remove-user" rel="<?php echo $user['user_id'];?>">Remove User</a></td>
<td><a href="/wp-admin/admin-ajax.php?ajax=yes&action=build_user_remove&uid=<?php echo $user->user_id;?>" class="remove-user" rel="<?php echo $user->user_id;?>">Remove User</a></td>
</tr>
<?php endforeach; ?>
......
......@@ -32,6 +32,15 @@ class User {
public function __wakeup() {
$this->_wpuser = new WP_User(0);
// For some (stupid) reason, WP_User has these 2 meta_key fields in its global parameters...
if (isset($this->_metacache['first_name'])) {
$this->_wpuser->first_name = $this->_metacache['first_name'];
}
if (isset($this->_metacache['last_name'])) {
$this->_wpuser->last_name = $this->_metacache['last_name'];
}
}
public function __call($method, $params) {
......
......@@ -15,10 +15,14 @@ use ArrayAccess, Iterator, Countable;
, null
, Array(16)
);
$user->setUserClass('Tz\WordPress\CBV\User\Account');
foreach ($users as $i => $data) {
echo "{$data['first_name']} {$data['last_name']}";
// Each $user is an Account (extension of) Tools\User (extension of) WP_User object
foreach ($users as $i => $user) {
echo "{$user->first_name} {$user->last_name} ($user->getRole()}";
}
// Note, although the example is tailored for CBV, this will work with all WordPress installs
*/
class UserSearch implements ArrayAccess, Iterator, Countable {
......@@ -30,7 +34,11 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
protected $fields = Array();
protected $userclass = 'User';
public function __construct(Array $fields, $num_results = -1, $page = 1, $sortby = null, $sortorder = 'ASC', Array $field_match = null, $text_search = null, Array $in_groups = null) {
$this->userclass = __NAMESPACE__ . '\User';
global $wpdb;
$this->fields = $fields;
......@@ -125,6 +133,10 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
list($this->count) = mysql_fetch_row($count_result);
}
public function setUserClass($classname) {
$this->userclass = $classname;
}
public function countTotal() {
return $this->count;
}
......@@ -180,9 +192,14 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
$val = maybe_unserialize($val);
}
// $sobj = unserialize(sprintf('O:23:"Tz\WordPress\Tools\User":3:{s:10:"_metacache";%3$ss:2:"id";s:%2$d:"%1$d";s:2:"ID";s:%2$d:"%1$d";}', $user_id, strlen($user_id), serialize($data)));
return $data;
return unserialize(sprintf(
'O:%5$d:"%4$s":3:{s:10:"_metacache";%3$ss:2:"id";s:%2$d:"%1$d";s:2:"ID";s:%2$d:"%1$d";}'
, $user_id
, strlen($user_id)
, serialize($data)
, $this->userclass
, strlen($this->userclass)
));
}
public function offsetSet($offset, $value) {
......@@ -193,3 +210,11 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
throw new Exception("You're...you're crazy man. I like you, but you're crazy.");
}
}
/*
class UserInjector extends Pimple {
public function __construct(Array $config = null) {
$this += $config;
}
}
*/
\ No newline at end of file
......