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_ ...@@ -98,7 +98,7 @@ function get_users($role = null, $pagenum=1, $records_per_page=0, $return_count_
98 $fs = Array('wp_capabilities' => "a:1:{s:{$len}:\\\\\\\\\"{$role}\\\\\\\\\""); 98 $fs = Array('wp_capabilities' => "a:1:{s:{$len}:\\\\\\\\\"{$role}\\\\\\\\\"");
99 } 99 }
100 100
101 return new Tools\UserSearch( 101 $users = new Tools\UserSearch(
102 Array('member_id', 'first_name', 'last_name', 'home_email', 'work_email', 'email_address_preference', 'status', 'wp_capabilities') 102 Array('member_id', 'first_name', 'last_name', 'home_email', 'work_email', 'email_address_preference', 'status', 'wp_capabilities')
103 , $records_per_page 103 , $records_per_page
104 , $pagenum 104 , $pagenum
...@@ -107,6 +107,9 @@ function get_users($role = null, $pagenum=1, $records_per_page=0, $return_count_ ...@@ -107,6 +107,9 @@ function get_users($role = null, $pagenum=1, $records_per_page=0, $return_count_
107 , $fs 107 , $fs
108 , $search 108 , $search
109 ); 109 );
110
111 $users->setUserClass('Tz\WordPress\CBV\User\Account');
112 return $users;
110 } 113 }
111 114
112 function create_user() { 115 function create_user() {
......
...@@ -185,19 +185,19 @@ if ($last > 1) { ...@@ -185,19 +185,19 @@ if ($last > 1) {
185 <tbody> 185 <tbody>
186 <?php 186 <?php
187 foreach ($site_users as $user): 187 foreach ($site_users as $user):
188 $pref = strtolower($user['email_address_preference']); 188 $pref = strtolower($user->email_address_preference);
189 if (!in_array($pref, Array('home', 'work'))) { 189 if (!in_array($pref, Array('home', 'work'))) {
190 $pref = (empty($data['home_email']) ? 'work' : 'home'); 190 $pref = (empty($user->home_email) ? 'work' : 'home');
191 } 191 }
192 ?> 192 ?>
193 <tr> 193 <tr>
194 <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> 194 <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>
195 <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> 195 <td><a href="/wp-admin/admin.php?page=cbv_users&action=edit&uid=<?php echo $user->id; ?>"><?php echo $user->member_id; ?></a></td>
196 <td><?php echo $user[$pref . '_email']?></td> 196 <td><?php echo $user->{$pref . '_email'}; ?></td>
197 <td><?php echo current(array_keys($user['wp_capabilities'])); ?></td> 197 <td><?php echo current(array_keys($user->wp_capabilities)); ?></td>
198 <td><?php echo ucwords($user['status']);?></td> 198 <td><?php echo ucwords($user->status);?></td>
199 199
200 <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> 200 <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>
201 201
202 </tr> 202 </tr>
203 <?php endforeach; ?> 203 <?php endforeach; ?>
......
...@@ -32,6 +32,15 @@ class User { ...@@ -32,6 +32,15 @@ class User {
32 32
33 public function __wakeup() { 33 public function __wakeup() {
34 $this->_wpuser = new WP_User(0); 34 $this->_wpuser = new WP_User(0);
35
36 // For some (stupid) reason, WP_User has these 2 meta_key fields in its global parameters...
37 if (isset($this->_metacache['first_name'])) {
38 $this->_wpuser->first_name = $this->_metacache['first_name'];
39 }
40
41 if (isset($this->_metacache['last_name'])) {
42 $this->_wpuser->last_name = $this->_metacache['last_name'];
43 }
35 } 44 }
36 45
37 public function __call($method, $params) { 46 public function __call($method, $params) {
......
...@@ -15,10 +15,14 @@ use ArrayAccess, Iterator, Countable; ...@@ -15,10 +15,14 @@ use ArrayAccess, Iterator, Countable;
15 , null 15 , null
16 , Array(16) 16 , Array(16)
17 ); 17 );
18 $user->setUserClass('Tz\WordPress\CBV\User\Account');
18 19
19 foreach ($users as $i => $data) { 20 // Each $user is an Account (extension of) Tools\User (extension of) WP_User object
20 echo "{$data['first_name']} {$data['last_name']}"; 21 foreach ($users as $i => $user) {
22 echo "{$user->first_name} {$user->last_name} ($user->getRole()}";
21 } 23 }
24
25 // Note, although the example is tailored for CBV, this will work with all WordPress installs
22 */ 26 */
23 27
24 class UserSearch implements ArrayAccess, Iterator, Countable { 28 class UserSearch implements ArrayAccess, Iterator, Countable {
...@@ -30,7 +34,11 @@ class UserSearch implements ArrayAccess, Iterator, Countable { ...@@ -30,7 +34,11 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
30 34
31 protected $fields = Array(); 35 protected $fields = Array();
32 36
37 protected $userclass = 'User';
38
33 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) { 39 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) {
40 $this->userclass = __NAMESPACE__ . '\User';
41
34 global $wpdb; 42 global $wpdb;
35 43
36 $this->fields = $fields; 44 $this->fields = $fields;
...@@ -125,6 +133,10 @@ class UserSearch implements ArrayAccess, Iterator, Countable { ...@@ -125,6 +133,10 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
125 list($this->count) = mysql_fetch_row($count_result); 133 list($this->count) = mysql_fetch_row($count_result);
126 } 134 }
127 135
136 public function setUserClass($classname) {
137 $this->userclass = $classname;
138 }
139
128 public function countTotal() { 140 public function countTotal() {
129 return $this->count; 141 return $this->count;
130 } 142 }
...@@ -180,9 +192,14 @@ class UserSearch implements ArrayAccess, Iterator, Countable { ...@@ -180,9 +192,14 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
180 $val = maybe_unserialize($val); 192 $val = maybe_unserialize($val);
181 } 193 }
182 194
183 // $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))); 195 return unserialize(sprintf(
184 196 '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";}'
185 return $data; 197 , $user_id
198 , strlen($user_id)
199 , serialize($data)
200 , $this->userclass
201 , strlen($this->userclass)
202 ));
186 } 203 }
187 204
188 public function offsetSet($offset, $value) { 205 public function offsetSet($offset, $value) {
...@@ -192,4 +209,12 @@ class UserSearch implements ArrayAccess, Iterator, Countable { ...@@ -192,4 +209,12 @@ class UserSearch implements ArrayAccess, Iterator, Countable {
192 public function offsetUnset($offset) { 209 public function offsetUnset($offset) {
193 throw new Exception("You're...you're crazy man. I like you, but you're crazy."); 210 throw new Exception("You're...you're crazy man. I like you, but you're crazy.");
194 } 211 }
195 }
...\ No newline at end of file ...\ No newline at end of file
212 }
213 /*
214
215 class UserInjector extends Pimple {
216 public function __construct(Array $config = null) {
217 $this += $config;
218 }
219 }
220 */
...\ No newline at end of file ...\ No newline at end of file
......