class-wpml-translation-roles-records.php
7.02 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
use WPML\LIB\WP\Cache;
abstract class WPML_Translation_Roles_Records {
const USERS_WITH_CAPABILITY = 'LIKE';
const USERS_WITHOUT_CAPABILITY = 'NOT LIKE';
const MIN_SEARCH_LENGTH = 3;
const CACHE_GROUP = __CLASS__;
/** @var wpdb */
protected $wpdb;
/** @var WPML_WP_User_Query_Factory */
private $user_query_factory;
/** @var \WP_Roles */
protected $wp_roles;
/**
* WPML_Translation_Roles_Records constructor.
*
* @param \wpdb $wpdb
* @param \WPML_WP_User_Query_Factory $user_query_factory
* @param \WP_Roles $wp_roles
*/
public function __construct( wpdb $wpdb, WPML_WP_User_Query_Factory $user_query_factory, WP_Roles $wp_roles ) {
$this->wpdb = $wpdb;
$this->user_query_factory = $user_query_factory;
$this->wp_roles = $wp_roles;
}
public function has_users_with_capability() {
$sql = "
SELECT EXISTS(
SELECT user_id
FROM {$this->wpdb->usermeta}
WHERE meta_key = '{$this->wpdb->prefix}capabilities' AND meta_value LIKE %s
)
";
$sql = $this->wpdb->prepare( $sql, '%' . $this->get_capability() . '%' );
return (bool) $this->wpdb->get_var( $sql );
}
/**
* @return array
*/
public function get_users_with_capability() {
return $this->get_records( self::USERS_WITH_CAPABILITY );
}
/**
* @return int
*/
public function get_number_of_users_with_capability() {
return count( $this->get_users_with_capability() );
}
/**
* @param string $search
* @param int $limit
*
* @return array
*/
public function search_for_users_without_capability( $search = '', $limit = -1 ) {
return $this->get_records( self::USERS_WITHOUT_CAPABILITY, $search, $limit );
}
/**
* @param int $user_id
*
* @return bool
*/
public function does_user_have_capability( $user_id ) {
$fn = Cache::memorize(
self::CACHE_GROUP . '_does_user_have_capability',
3600,
function ( $user_id ) {
return $this->fetch_user_capability( $user_id );
}
);
return $fn( $user_id );
}
/**
* Delete records for all users
*/
public function delete_all() {
$users = $this->get_users_with_capability();
foreach ( $users as $user ) {
$this->delete( $user->ID );
}
}
/**
* Delete the record for the user
*
* @param int $user_id
*/
public function delete( $user_id ) {
$user = new WP_User( $user_id );
$user->remove_cap( $this->get_capability() );
}
/**
* @param string $compare
* @param string $search
* @param int $limit
*
* @return array
*/
private function get_records( $compare, $search = '', $limit = -1 ) {
$search = trim( $search );
$cache_key = md5( (string) wp_json_encode( [ get_class( $this ), $compare, $search, $limit ] ) );
$cache = wpml_get_cache( self::CACHE_GROUP );
$found = false;
$results = $cache->get( $cache_key, $found );
if ( $found ) {
return $results;
}
$preparedUserQuery = $this->wpdb->prepare(
"SELECT u.id FROM {$this->wpdb->users} u INNER JOIN {$this->wpdb->usermeta} c ON c.user_id=u.ID AND CAST(c.meta_key AS BINARY)=%s AND c.meta_value {$compare} %s",
"{$this->wpdb->prefix}capabilities",
"%" . $this->get_capability() . "%"
);
if ( self::USERS_WITHOUT_CAPABILITY === $compare ) {
$required_wp_roles = $this->get_required_wp_roles();
foreach( $required_wp_roles as $required_wp_role ) {
$preparedUserQuery .= $this->wpdb->prepare( " AND c.meta_value LIKE %s", "%{$required_wp_role}%" );
}
}
if ( $search ) {
$preparedUserQuery .= $this->wpdb->prepare( " AND (u.user_login LIKE %s OR u.user_nicename LIKE %s OR u.user_email LIKE %s)", "%{$search}%", "%{$search}%", "%{$search}%" );
}
$preparedUserQuery .= ' ORDER BY user_login ASC';
if ( $limit > 0 ) {
$preparedUserQuery .= $this->wpdb->prepare(" LIMIT 0,%d", $limit );
}
$users = $this->wpdb->get_col( $preparedUserQuery );
if ( $search && strlen( $search ) > self::MIN_SEARCH_LENGTH && ( $limit <= 0 || count( $users ) < $limit ) ) {
$users_from_metas = $this->get_records_from_users_metas( $compare, $search, $limit );
$users_with_dupes = array_merge( $users, $users_from_metas );
$users = wpml_array_unique( $users_with_dupes, SORT_REGULAR );
}
$results = array();
foreach ( $users as $user_id ) {
$user_data = get_userdata( $user_id );
if ( $user_data ) {
$language_pair_records = new WPML_Language_Pair_Records( $this->wpdb, new WPML_Language_Records( $this->wpdb ) );
$language_pairs = $language_pair_records->get( $user_id );
$result = (object) array(
'ID' => $user_data->ID,
'full_name' => trim( $user_data->first_name . ' ' . $user_data->last_name ),
'user_login' => $user_data->user_login,
'user_email' => $user_data->user_email,
'display_name' => $user_data->display_name,
'language_pairs' => $language_pairs,
'roles' => $user_data->roles
);
$results[] = $result;
}
}
$cache->set( $cache_key, $results );
return $results;
}
/**
* @param string $compare
* @param string $search
* @param int $limit
*
* @return array
*/
private function get_records_from_users_metas( $compare, $search, $limit = -1 ) {
$search = trim( $search );
if ( ! $search ) {
return array();
}
$sanitized_search = preg_replace( '!\s+!', ' ', $search );
$words = explode( ' ', $sanitized_search );
if ( ! $words ) {
return array();
}
$search_by_names = array( 'relation' => 'OR' );
foreach ( $words as $word ) {
$search_by_names[] = array(
'key' => 'first_name',
'value' => $word,
'compare' => 'LIKE',
);
$search_by_names[] = array(
'key' => 'last_name',
'value' => $word,
'compare' => 'LIKE',
);
$search_by_names[] = array(
'key' => 'last_name',
'value' => $word,
'compare' => 'LIKE',
);
}
$query_args = array(
'fields' => 'ID',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => "{$this->wpdb->prefix}capabilities",
'value' => $this->get_capability(),
'compare' => $compare,
),
$search_by_names,
),
'number' => $limit,
);
if ( 'NOT LIKE' === $compare ) {
$required_wp_roles = $this->get_required_wp_roles();
if ( $required_wp_roles ) {
$query_args['role__in'] = $required_wp_roles;
}
}
$user_query = $this->user_query_factory->create( $query_args );
return $user_query->get_results();
}
/**
* Fetches the capability for the user from DB
*
* @param int $user_id
*/
private function fetch_user_capability( $user_id ) {
$sql = "
SELECT user_id
FROM {$this->wpdb->usermeta}
WHERE user_id = %d AND meta_key = %s AND meta_value LIKE %s
LIMIT 1
";
$sql = $this->wpdb->prepare( $sql, $user_id, $this->wpdb->prefix . 'capabilities', '%' . $this->get_capability() . '%' );
return (bool) $this->wpdb->get_var( $sql );
}
/**
* @return string
*/
abstract protected function get_capability();
/**
* @return array
*/
abstract protected function get_required_wp_roles();
}