8006404d by Marty Penner

Revert "Convert UserSearch to using mysqli"

This reverts commit af513cab.
1 parent dca2a3bc
<?php
namespace Tz\WordPress\Tools;
use ArrayAccess;
use Countable;
use Iterator;
use ArrayAccess, Iterator, Countable;
/*
Get all active CBV users who are subscribers
......@@ -28,46 +25,24 @@ use Iterator;
// Note, although the example is tailored for CBV, this will work with all WordPress installs
*/
class UserSearch implements ArrayAccess, Iterator, Countable
{
class UserSearch implements ArrayAccess, Iterator, Countable {
protected $result;
protected $pos = 0;
protected $pos = 0;
protected $count = 0;
protected $count = 0;
protected $num_rows = 0;
protected $fields = [];
protected $fields = Array();
protected $userclass = 'User';
/**
* @param array $fields
* @param int $num_results
* @param int $page
* @param null $sortby
* @param string $sortorder
* @param array $field_match
* @param null $text_search
* @param array $in_groups
* @param null $search_users_table
*/
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
) {
$this->userclass = __NAMESPACE__.'\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, $search_users_table = null) {
$this->userclass = __NAMESPACE__ . '\User';
global $wpdb;
$this->fields = $fields;
$search_clause = $limit_clause = $order_clause = $order_sort = $having_clause = $uam_join = $uam_clause = '';
$search_clause = $limit_clause = $order_clause = $order_sort = $having_clause = $uam_join = $uam_clause = '';
// Field matches
$having = " HAVING `json` LIKE ";
......@@ -95,21 +70,18 @@ class UserSearch implements ArrayAccess, Iterator, Countable
// 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');
$order_sort = " ORDER BY `fake_order` " . ($sortorder == 'DESC' ? 'DESC' : 'ASC');
}
// Pagination
if ($num_results > 0) {
$limit_clause = " LIMIT ".(($page - 1) * $num_results).", {$num_results}";
$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'
)
) {
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).") ";
$uam_clause = " AND `uam`.`object_type` = 'user' AND `uam`.`group_id` IN (" . implode(',', $in_groups) . ") ";
}
// Users table
......@@ -119,7 +91,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable
}
}
mysqli_query($wpdb->dbh, "SET SESSION group_concat_max_len = 40960");
mysql_query("SET SESSION group_concat_max_len = 40960", $wpdb->dbh);
$query = "
SELECT SQL_CALC_FOUND_ROWS
......@@ -130,7 +102,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable
'\"'
, `meta_key`
, '\":\"'
, REPLACE(REPLACE(REPLACE(REPLACE(`meta_value`, '".'"'."', '".'\\\"'."'), '\\r', '\\\\r'), '\\n', '\\\\n'), '\\t', '\\\\t')
, REPLACE(REPLACE(REPLACE(REPLACE(`meta_value`, '" . '"' . "', '" . '\\\"' . "'), '\\r', '\\\\r'), '\\n', '\\\\n'), '\\t', '\\\\t')
, '\"'
)
, '}'
......@@ -139,7 +111,7 @@ class UserSearch implements ArrayAccess, Iterator, Countable
FROM `wp_usermeta`
{$uam_join}
WHERE
`meta_key` IN ('".implode("','", $this->fields)."')
`meta_key` IN ('" . implode("','", $this->fields) . "')
{$uam_clause}
GROUP BY `user_id`
{$having_clause}
......@@ -147,88 +119,50 @@ class UserSearch implements ArrayAccess, Iterator, Countable
{$limit_clause}
";
$this->result = mysqli_query($wpdb->dbh, $query);
$this->num_rows = mysqli_num_rows($this->result);
$this->result = mysql_query($query, $wpdb->dbh);
$this->num_rows = mysql_num_rows($this->result);
$count_result = mysqli_query($wpdb->dbh, "SELECT FOUND_ROWS()");
list($this->count) = mysqli_fetch_row($count_result);
$count_result = mysql_query("SELECT FOUND_ROWS()", $wpdb->dbh);
list($this->count) = mysql_fetch_row($count_result);
}
/**
* @param $classname
*/
public function setUserClass($classname)
{
public function setUserClass($classname) {
$this->userclass = $classname;
}
/**
* @return int
*/
public function countTotal()
{
public function countTotal() {
return $this->count;
}
/**
* @return int
*/
public function count()
{
public function count() {
return $this->num_rows;
}
/**
* @return bool|mixed
*/
public function current()
{
public function current() {
return $this->offsetGet($this->pos);
}
/**
* @return int
*/
public function key()
{
public function key() {
return $this->pos;
}
/**
*
*/
public function next()
{
public function next() {
$this->pos++;
}
/**
*
*/
public function rewind()
{
public function rewind() {
$this->current = 0;
if ($this->num_rows > 0) {
mysqli_data_seek($this->result, 0);
mysql_data_seek($this->result, 0);
}
}
/**
* @return bool
*/
public function valid()
{
public function valid() {
return $this->offsetExists($this->pos);
}
/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
public function offsetExists($offset) {
if ($this->num_rows == 0) {
return false;
}
......@@ -236,70 +170,41 @@ class UserSearch implements ArrayAccess, Iterator, Countable
return ($offset < 0 || $offset > ($this->num_rows - 1) ? false : true);
}
/**
* @param mixed $offset
*
* @return bool|mixed
*/
public function offsetGet($offset)
{
public function offsetGet($offset) {
if (!$this->offsetExists($offset)) {
return false;
}
if ($offset != $this->pos) {
mysqli_data_seek($this->result, $offset);
mysql_data_seek($this->result, $offset);
}
list($user_id, $user_string) = mysqli_fetch_row($this->result);
list($user_id, $user_string) = mysql_fetch_row($this->result);
$user_string = str_ireplace("\\'", "\\\\'", $user_string);
$json_data = json_decode($user_string, true);
$json_data = json_decode($user_string, true);
if (json_last_error() != JSON_ERROR_NONE) {
return false;
}
$data = array_merge(
array_map(
function () {
return '';
},
array_flip($this->fields)
),
$json_data
);
$data = array_merge(array_map(function() { return ''; }, array_flip($this->fields)), $json_data);
foreach ($data as $key => &$val) {
$val = maybe_unserialize($val);
}
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)
)
);
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)
));
}
/**
* @param mixed $offset
* @param mixed $value
*
* @throws \Exception
*/
public function offsetSet($offset, $value)
{
throw new \Exception("You're...you're crazy man. I like you, but you're crazy.");
public function offsetSet($offset, $value) {
throw new Exception("You're...you're crazy man. I like you, but you're crazy.");
}
/**
* @param mixed $offset
*
* @throws \Exception
*/
public function offsetUnset($offset)
{
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.");
}
}
\ No newline at end of file
......