UserSearch.php
5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Tz\WordPress\Tools;
use ArrayAccess, Iterator, Countable;
/*
Get all active CBV users who are subscribers
$users = new UserSearch(
Array('first_name', 'last_name')
, -1
, 1
, 'last_name'
, 'ASC'
, Array('status' => 'active')
, null
, Array(16)
);
foreach ($users as $i => $data) {
echo "{$data['first_name']} {$data['last_name']}";
}
*/
class UserSearch implements ArrayAccess, Iterator, Countable {
protected $result;
protected $pos = 0;
protected $count = 0;
protected $num_rows = 0;
protected $fields = Array();
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) {
global $wpdb;
$this->fields = $fields;
$search_clause = $limit_clause = $order_clause = $order_sort = $having_clause = $uam_join = $uam_clause = '';
// Field matches
$having = " HAVING `json` LIKE ";
if (is_array($field_match) && count($field_match) > 0) {
foreach ($field_match as $key => $val) {
if (!in_array($key, $this->fields)) {
$this->fields[] = $key;
}
$having_clause .= (empty($having_clause) ? $having : "\n AND `json` LIKE ");
$having_clause .= " '%\"{$key}\":\"{$val}%' ";
}
}
// Open ended search
if (!is_null($text_search)) {
$search_strings = explode(' ', $text_search);
foreach ($search_strings as $search) {
$having_clause .= (empty($having_clause) ? $having : "\n AND `json` LIKE ");
$having_clause .= " '%{$search}%' ";
}
}
// Order
if (!is_null($sortby)) {
$order_clause = " , GROUP_CONCAT(IF(`meta_key` = '{$sortby}', `meta_value`, '') SEPARATOR '') AS `fake_order` ";
$order_sort = " ORDER BY `fake_order` " . ($sortorder == 'DESC' ? 'DESC' : 'ASC');
}
// Pagination
if ($num_results > 0) {
$limit_clause = " LIMIT " . (($page - 1) * $num_results ) . ", {$num_results}";
}
// Groups
if (is_array($in_groups) && count($in_groups) > 0 && is_plugin_active('tz-user-access-manager/user-access-manager.php')) {
$uam_join = " LEFT JOIN `wp_uam_accessgroup_to_object` AS `uam` ON `wp_usermeta`.`user_id` = `uam`.`object_id` ";
$uam_clause = " AND `uam`.`object_type` = 'user' AND `uam`.`group_id` IN (" . implode(',', $in_groups) . ") ";
}
$query = "
SELECT SQL_CALC_FOUND_ROWS
`user_id`
, CONCAT(
'{'
, GROUP_CONCAT(
'\"'
, `meta_key`
, '\":\"'
, REPLACE(REPLACE(REPLACE(`meta_value`, '" . '"' . "', '" . '\\\"' . "'), '\\r', '\\\\r'), '\\n', '\\\\n')
, '\"'
)
, '}'
) AS `json`
{$order_clause}
FROM `wp_usermeta`
{$uam_join}
WHERE
`meta_key` IN ('" . implode("','", $this->fields) . "')
{$uam_clause}
GROUP BY `user_id`
{$having_clause}
{$order_sort}
{$limit_clause}
";
/*
?>
<textarea style="width: 95%; height: 35em; font-size: 11px;" autofocus="autofocus">
<?php
$me = new User(1);
echo serialize($me) . "\n";
echo serialize(Array('hello' => 'world'));
?>
<?php echo serialize(new User(1)); ?>
<?php echo serialize(new User(4)); ?>
<?php echo trim($query); ?>;
</textarea>
<?php
/**/
$this->result = mysql_query($query, $wpdb->dbh);
$this->num_rows = mysql_num_rows($this->result);
$count_result = mysql_query("SELECT FOUND_ROWS()", $wpdb->dbh);
list($this->count) = mysql_fetch_row($count_result);
}
public function countTotal() {
return $this->count;
}
public function count() {
return $this->num_rows;
}
public function current() {
return $this->offsetGet($this->pos);
}
public function key() {
return $this->pos;
}
public function next() {
$this->pos++;
}
public function rewind() {
$this->current = 0;
if ($this->num_rows > 0) {
mysql_data_seek($this->result, 0);
}
}
public function valid() {
return $this->offsetExists($this->pos);
}
public function offsetExists($offset) {
if ($this->num_rows == 0) {
return false;
}
return ($offset < 0 || $offset > ($this->num_rows - 1) ? false : true);
}
public function offsetGet($offset) {
if (!$this->offsetExists($offset)) {
return false;
}
if ($offset != $this->pos) {
mysql_data_seek($this->result, $offset);
}
list($user_id, $user_string) = mysql_fetch_row($this->result);
$data = array_merge(array_map(function() { return ''; }, array_flip($this->fields)), json_decode($user_string, true));
foreach ($data as $key => &$val) {
$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;
}
public function offsetSet($offset, $value) {
throw new Exception("You're...you're crazy man. I like you, but you're crazy.");
}
public function offsetUnset($offset) {
throw new Exception("You're...you're crazy man. I like you, but you're crazy.");
}
}