Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Tenzing
/
Tz Tools
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
8006404d
authored
2014-11-13 15:14:01 -0500
by
Marty Penner
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Revert "Convert UserSearch to using mysqli"
This reverts commit
af513cab
.
1 parent
dca2a3bc
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
137 deletions
lib/UserSearch.php
lib/UserSearch.php
View file @
8006404
<?php
namespace
Tz\WordPress\Tools
;
use
ArrayAccess
;
use
Countable
;
use
Iterator
;
use
ArrayAccess
,
Iterator
,
Countable
;
/*
Get all active CBV users who are subscribers
...
...
@@ -28,41 +25,19 @@ 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
$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
;
...
...
@@ -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
}
}
mysql
i_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
=
mysql
i_query
(
$wpdb
->
dbh
,
$query
);
$this
->
num_rows
=
mysql
i
_num_rows
(
$this
->
result
);
$this
->
result
=
mysql
_query
(
$query
,
$wpdb
->
dbh
);
$this
->
num_rows
=
mysql_num_rows
(
$this
->
result
);
$count_result
=
mysql
i_query
(
$wpdb
->
dbh
,
"SELECT FOUND_ROWS()"
);
list
(
$this
->
count
)
=
mysql
i
_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
)
{
mysql
i
_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
)
{
mysql
i
_data_seek
(
$this
->
result
,
$offset
);
mysql_data_seek
(
$this
->
result
,
$offset
);
}
list
(
$user_id
,
$user_string
)
=
mysql
i
_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
);
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
...
...
Please
register
or
sign in
to post a comment