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