Convert UserSearch to using mysqli
Showing
1 changed file
with
141 additions
and
46 deletions
| 1 | <?php | 1 | <?php |
| 2 | namespace Tz\WordPress\Tools; | 2 | namespace Tz\WordPress\Tools; |
| 3 | use ArrayAccess, Iterator, Countable; | 3 | |
| 4 | use ArrayAccess; | ||
| 5 | use Countable; | ||
| 6 | use Iterator; | ||
| 4 | 7 | ||
| 5 | /* | 8 | /* |
| 6 | Get all active CBV users who are subscribers | 9 | Get all active CBV users who are subscribers |
| ... | @@ -25,24 +28,46 @@ use ArrayAccess, Iterator, Countable; | ... | @@ -25,24 +28,46 @@ use ArrayAccess, Iterator, Countable; |
| 25 | // Note, although the example is tailored for CBV, this will work with all WordPress installs | 28 | // Note, although the example is tailored for CBV, this will work with all WordPress installs |
| 26 | */ | 29 | */ |
| 27 | 30 | ||
| 28 | class UserSearch implements ArrayAccess, Iterator, Countable { | 31 | class UserSearch implements ArrayAccess, Iterator, Countable |
| 32 | { | ||
| 29 | protected $result; | 33 | protected $result; |
| 30 | protected $pos = 0; | 34 | protected $pos = 0; |
| 31 | 35 | ||
| 32 | protected $count = 0; | 36 | protected $count = 0; |
| 33 | protected $num_rows = 0; | 37 | protected $num_rows = 0; |
| 34 | 38 | ||
| 35 | protected $fields = Array(); | 39 | protected $fields = []; |
| 36 | 40 | ||
| 37 | protected $userclass = 'User'; | 41 | protected $userclass = 'User'; |
| 38 | 42 | ||
| 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, $search_users_table = null) { | 43 | /** |
| 40 | $this->userclass = __NAMESPACE__ . '\User'; | 44 | * @param array $fields |
| 45 | * @param int $num_results | ||
| 46 | * @param int $page | ||
| 47 | * @param null $sortby | ||
| 48 | * @param string $sortorder | ||
| 49 | * @param array $field_match | ||
| 50 | * @param null $text_search | ||
| 51 | * @param array $in_groups | ||
| 52 | * @param null $search_users_table | ||
| 53 | */ | ||
| 54 | public function __construct( | ||
| 55 | Array $fields, | ||
| 56 | $num_results = -1, | ||
| 57 | $page = 1, | ||
| 58 | $sortby = null, | ||
| 59 | $sortorder = 'ASC', | ||
| 60 | array $field_match = null, | ||
| 61 | $text_search = null, | ||
| 62 | array $in_groups = null, | ||
| 63 | $search_users_table = null | ||
| 64 | ) { | ||
| 65 | $this->userclass = __NAMESPACE__.'\User'; | ||
| 41 | 66 | ||
| 42 | global $wpdb; | 67 | global $wpdb; |
| 43 | 68 | ||
| 44 | $this->fields = $fields; | 69 | $this->fields = $fields; |
| 45 | $search_clause = $limit_clause = $order_clause = $order_sort = $having_clause = $uam_join = $uam_clause = ''; | 70 | $search_clause = $limit_clause = $order_clause = $order_sort = $having_clause = $uam_join = $uam_clause = ''; |
| 46 | 71 | ||
| 47 | // Field matches | 72 | // Field matches |
| 48 | $having = " HAVING `json` LIKE "; | 73 | $having = " HAVING `json` LIKE "; |
| ... | @@ -70,18 +95,21 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -70,18 +95,21 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 70 | // Order | 95 | // Order |
| 71 | if (!is_null($sortby)) { | 96 | if (!is_null($sortby)) { |
| 72 | $order_clause = " , GROUP_CONCAT(IF(`meta_key` = '{$sortby}', `meta_value`, '') SEPARATOR '') AS `fake_order` "; | 97 | $order_clause = " , GROUP_CONCAT(IF(`meta_key` = '{$sortby}', `meta_value`, '') SEPARATOR '') AS `fake_order` "; |
| 73 | $order_sort = " ORDER BY `fake_order` " . ($sortorder == 'DESC' ? 'DESC' : 'ASC'); | 98 | $order_sort = " ORDER BY `fake_order` ".($sortorder == 'DESC' ? 'DESC' : 'ASC'); |
| 74 | } | 99 | } |
| 75 | 100 | ||
| 76 | // Pagination | 101 | // Pagination |
| 77 | if ($num_results > 0) { | 102 | if ($num_results > 0) { |
| 78 | $limit_clause = " LIMIT " . (($page - 1) * $num_results ) . ", {$num_results}"; | 103 | $limit_clause = " LIMIT ".(($page - 1) * $num_results).", {$num_results}"; |
| 79 | } | 104 | } |
| 80 | 105 | ||
| 81 | // Groups | 106 | // Groups |
| 82 | if (is_array($in_groups) && count($in_groups) > 0 && is_plugin_active('tz-user-access-manager/user-access-manager.php')) { | 107 | if (is_array($in_groups) && count($in_groups) > 0 && is_plugin_active( |
| 108 | 'tz-user-access-manager/user-access-manager.php' | ||
| 109 | ) | ||
| 110 | ) { | ||
| 83 | $uam_join = " LEFT JOIN `wp_uam_accessgroup_to_object` AS `uam` ON `wp_usermeta`.`user_id` = `uam`.`object_id` "; | 111 | $uam_join = " LEFT JOIN `wp_uam_accessgroup_to_object` AS `uam` ON `wp_usermeta`.`user_id` = `uam`.`object_id` "; |
| 84 | $uam_clause = " AND `uam`.`object_type` = 'user' AND `uam`.`group_id` IN (" . implode(',', $in_groups) . ") "; | 112 | $uam_clause = " AND `uam`.`object_type` = 'user' AND `uam`.`group_id` IN (".implode(',', $in_groups).") "; |
| 85 | } | 113 | } |
| 86 | 114 | ||
| 87 | // Users table | 115 | // Users table |
| ... | @@ -91,7 +119,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -91,7 +119,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 91 | } | 119 | } |
| 92 | } | 120 | } |
| 93 | 121 | ||
| 94 | mysql_query("SET SESSION group_concat_max_len = 40960", $wpdb->dbh); | 122 | mysqli_query($wpdb->dbh, "SET SESSION group_concat_max_len = 40960"); |
| 95 | 123 | ||
| 96 | $query = " | 124 | $query = " |
| 97 | SELECT SQL_CALC_FOUND_ROWS | 125 | SELECT SQL_CALC_FOUND_ROWS |
| ... | @@ -102,7 +130,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -102,7 +130,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 102 | '\"' | 130 | '\"' |
| 103 | , `meta_key` | 131 | , `meta_key` |
| 104 | , '\":\"' | 132 | , '\":\"' |
| 105 | , REPLACE(REPLACE(REPLACE(REPLACE(`meta_value`, '" . '"' . "', '" . '\\\"' . "'), '\\r', '\\\\r'), '\\n', '\\\\n'), '\\t', '\\\\t') | 133 | , REPLACE(REPLACE(REPLACE(REPLACE(`meta_value`, '".'"'."', '".'\\\"'."'), '\\r', '\\\\r'), '\\n', '\\\\n'), '\\t', '\\\\t') |
| 106 | , '\"' | 134 | , '\"' |
| 107 | ) | 135 | ) |
| 108 | , '}' | 136 | , '}' |
| ... | @@ -111,7 +139,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -111,7 +139,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 111 | FROM `wp_usermeta` | 139 | FROM `wp_usermeta` |
| 112 | {$uam_join} | 140 | {$uam_join} |
| 113 | WHERE | 141 | WHERE |
| 114 | `meta_key` IN ('" . implode("','", $this->fields) . "') | 142 | `meta_key` IN ('".implode("','", $this->fields)."') |
| 115 | {$uam_clause} | 143 | {$uam_clause} |
| 116 | GROUP BY `user_id` | 144 | GROUP BY `user_id` |
| 117 | {$having_clause} | 145 | {$having_clause} |
| ... | @@ -119,50 +147,88 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -119,50 +147,88 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 119 | {$limit_clause} | 147 | {$limit_clause} |
| 120 | "; | 148 | "; |
| 121 | 149 | ||
| 122 | $this->result = mysql_query($query, $wpdb->dbh); | 150 | $this->result = mysqli_query($wpdb->dbh, $query); |
| 123 | $this->num_rows = mysql_num_rows($this->result); | 151 | $this->num_rows = mysqli_num_rows($this->result); |
| 124 | 152 | ||
| 125 | $count_result = mysql_query("SELECT FOUND_ROWS()", $wpdb->dbh); | 153 | $count_result = mysqli_query($wpdb->dbh, "SELECT FOUND_ROWS()"); |
| 126 | list($this->count) = mysql_fetch_row($count_result); | 154 | list($this->count) = mysqli_fetch_row($count_result); |
| 127 | } | 155 | } |
| 128 | 156 | ||
| 129 | public function setUserClass($classname) { | 157 | /** |
| 158 | * @param $classname | ||
| 159 | */ | ||
| 160 | public function setUserClass($classname) | ||
| 161 | { | ||
| 130 | $this->userclass = $classname; | 162 | $this->userclass = $classname; |
| 131 | } | 163 | } |
| 132 | 164 | ||
| 133 | public function countTotal() { | 165 | /** |
| 166 | * @return int | ||
| 167 | */ | ||
| 168 | public function countTotal() | ||
| 169 | { | ||
| 134 | return $this->count; | 170 | return $this->count; |
| 135 | } | 171 | } |
| 136 | 172 | ||
| 137 | public function count() { | 173 | /** |
| 174 | * @return int | ||
| 175 | */ | ||
| 176 | public function count() | ||
| 177 | { | ||
| 138 | return $this->num_rows; | 178 | return $this->num_rows; |
| 139 | } | 179 | } |
| 140 | 180 | ||
| 141 | public function current() { | 181 | /** |
| 182 | * @return bool|mixed | ||
| 183 | */ | ||
| 184 | public function current() | ||
| 185 | { | ||
| 142 | return $this->offsetGet($this->pos); | 186 | return $this->offsetGet($this->pos); |
| 143 | } | 187 | } |
| 144 | 188 | ||
| 145 | public function key() { | 189 | /** |
| 190 | * @return int | ||
| 191 | */ | ||
| 192 | public function key() | ||
| 193 | { | ||
| 146 | return $this->pos; | 194 | return $this->pos; |
| 147 | } | 195 | } |
| 148 | 196 | ||
| 149 | public function next() { | 197 | /** |
| 198 | * | ||
| 199 | */ | ||
| 200 | public function next() | ||
| 201 | { | ||
| 150 | $this->pos++; | 202 | $this->pos++; |
| 151 | } | 203 | } |
| 152 | 204 | ||
| 153 | public function rewind() { | 205 | /** |
| 206 | * | ||
| 207 | */ | ||
| 208 | public function rewind() | ||
| 209 | { | ||
| 154 | $this->current = 0; | 210 | $this->current = 0; |
| 155 | 211 | ||
| 156 | if ($this->num_rows > 0) { | 212 | if ($this->num_rows > 0) { |
| 157 | mysql_data_seek($this->result, 0); | 213 | mysqli_data_seek($this->result, 0); |
| 158 | } | 214 | } |
| 159 | } | 215 | } |
| 160 | 216 | ||
| 161 | public function valid() { | 217 | /** |
| 218 | * @return bool | ||
| 219 | */ | ||
| 220 | public function valid() | ||
| 221 | { | ||
| 162 | return $this->offsetExists($this->pos); | 222 | return $this->offsetExists($this->pos); |
| 163 | } | 223 | } |
| 164 | 224 | ||
| 165 | public function offsetExists($offset) { | 225 | /** |
| 226 | * @param mixed $offset | ||
| 227 | * | ||
| 228 | * @return bool | ||
| 229 | */ | ||
| 230 | public function offsetExists($offset) | ||
| 231 | { | ||
| 166 | if ($this->num_rows == 0) { | 232 | if ($this->num_rows == 0) { |
| 167 | return false; | 233 | return false; |
| 168 | } | 234 | } |
| ... | @@ -170,41 +236,70 @@ class UserSearch implements ArrayAccess, Iterator, Countable { | ... | @@ -170,41 +236,70 @@ class UserSearch implements ArrayAccess, Iterator, Countable { |
| 170 | return ($offset < 0 || $offset > ($this->num_rows - 1) ? false : true); | 236 | return ($offset < 0 || $offset > ($this->num_rows - 1) ? false : true); |
| 171 | } | 237 | } |
| 172 | 238 | ||
| 173 | public function offsetGet($offset) { | 239 | /** |
| 240 | * @param mixed $offset | ||
| 241 | * | ||
| 242 | * @return bool|mixed | ||
| 243 | */ | ||
| 244 | public function offsetGet($offset) | ||
| 245 | { | ||
| 174 | if (!$this->offsetExists($offset)) { | 246 | if (!$this->offsetExists($offset)) { |
| 175 | return false; | 247 | return false; |
| 176 | } | 248 | } |
| 177 | 249 | ||
| 178 | if ($offset != $this->pos) { | 250 | if ($offset != $this->pos) { |
| 179 | mysql_data_seek($this->result, $offset); | 251 | mysqli_data_seek($this->result, $offset); |
| 180 | } | 252 | } |
| 181 | 253 | ||
| 182 | list($user_id, $user_string) = mysql_fetch_row($this->result); | 254 | list($user_id, $user_string) = mysqli_fetch_row($this->result); |
| 183 | $user_string = str_ireplace("\\'", "\\\\'", $user_string); | 255 | $user_string = str_ireplace("\\'", "\\\\'", $user_string); |
| 184 | $json_data = json_decode($user_string, true); | 256 | $json_data = json_decode($user_string, true); |
| 185 | if (json_last_error() != JSON_ERROR_NONE) { | 257 | if (json_last_error() != JSON_ERROR_NONE) { |
| 186 | return false; | 258 | return false; |
| 187 | } | 259 | } |
| 188 | $data = array_merge(array_map(function() { return ''; }, array_flip($this->fields)), $json_data); | 260 | $data = array_merge( |
| 261 | array_map( | ||
| 262 | function () { | ||
| 263 | return ''; | ||
| 264 | }, | ||
| 265 | array_flip($this->fields) | ||
| 266 | ), | ||
| 267 | $json_data | ||
| 268 | ); | ||
| 189 | foreach ($data as $key => &$val) { | 269 | foreach ($data as $key => &$val) { |
| 190 | $val = maybe_unserialize($val); | 270 | $val = maybe_unserialize($val); |
| 191 | } | 271 | } |
| 192 | 272 | ||
| 193 | return unserialize(sprintf( | 273 | return unserialize( |
| 194 | '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";}' | 274 | sprintf( |
| 195 | , $user_id | 275 | '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";}', |
| 196 | , strlen($user_id) | 276 | $user_id, |
| 197 | , serialize($data) | 277 | strlen($user_id), |
| 198 | , $this->userclass | 278 | serialize($data), |
| 199 | , strlen($this->userclass) | 279 | $this->userclass, |
| 200 | )); | 280 | strlen($this->userclass) |
| 281 | ) | ||
| 282 | ); | ||
| 201 | } | 283 | } |
| 202 | 284 | ||
| 203 | public function offsetSet($offset, $value) { | 285 | /** |
| 204 | throw new Exception("You're...you're crazy man. I like you, but you're crazy."); | 286 | * @param mixed $offset |
| 287 | * @param mixed $value | ||
| 288 | * | ||
| 289 | * @throws \Exception | ||
| 290 | */ | ||
| 291 | public function offsetSet($offset, $value) | ||
| 292 | { | ||
| 293 | throw new \Exception("You're...you're crazy man. I like you, but you're crazy."); | ||
| 205 | } | 294 | } |
| 206 | 295 | ||
| 207 | public function offsetUnset($offset) { | 296 | /** |
| 208 | throw new Exception("You're...you're crazy man. I like you, but you're crazy."); | 297 | * @param mixed $offset |
| 298 | * | ||
| 299 | * @throws \Exception | ||
| 300 | */ | ||
| 301 | public function offsetUnset($offset) | ||
| 302 | { | ||
| 303 | throw new \Exception("You're...you're crazy man. I like you, but you're crazy."); | ||
| 209 | } | 304 | } |
| 210 | } | 305 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment