class-learndash-admin-binary-selector-users.php
9.32 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
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* LearnDash Binary Selector Users.
*
* @since 2.2.1
* @package LearnDash\Settings
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'Learndash_Binary_Selector_Users' ) ) && ( class_exists( 'Learndash_Binary_Selector' ) ) ) {
/**
* Class LearnDash Binary Selector Users.
*
* @since 2.2.1
* @uses Learndash_Binary_Selector
*/
class Learndash_Binary_Selector_Users extends Learndash_Binary_Selector {
/**
* Public constructor for class.
*
* @since 2.2.1
*
* @param array $args Array of arguments for class.
*/
public function __construct( $args = array() ) {
// Set up the default query args for the Users.
$defaults = array(
'paged' => 1,
'number' => LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Per_Page', 'per_page' ),
'fields' => array( 'ID', 'display_name', 'user_login' ),
'orderby' => 'display_name',
'order' => 'ASC',
'search' => '',
);
if ( ( ! isset( $args['number'] ) ) && ( isset( $args['per_page'] ) ) && ( ! empty( $args['per_page'] ) ) ) {
$args['number'] = $args['per_page'];
}
$args = wp_parse_args( $args, $defaults );
parent::__construct( $args );
if ( ( isset( $this->args['included_ids'] ) ) && ( ! empty( $this->args['included_ids'] ) ) ) {
$this->args['include'] = $this->args['included_ids'];
}
if ( ( isset( $this->args['excluded_ids'] ) ) && ( ! empty( $this->args['excluded_ids'] ) ) ) {
$this->args['exclude'] = $this->args['excluded_ids'];
}
}
/**
* Get selector section pager data.
*
* @since 2.2.1
*
* @param string $position Value for 'left' or 'right' position.
*/
protected function get_pager_data( $position = '' ) {
$pager = array();
if ( $this->is_valid_position( $position ) ) {
if ( isset( $this->element_queries[ $position ] ) ) {
if ( isset( $this->element_queries[ $position ]->query_vars['paged'] ) ) {
$pager['current_page'] = intval( $this->element_queries[ $position ]->query_vars['paged'] );
} else {
$pager['current_page'] = 0;
}
if ( isset( $this->element_queries[ $position ]->query_vars['number'] ) ) {
$pager['per_page'] = intval( $this->element_queries[ $position ]->query_vars['number'] );
} else {
$pager['per_page'] = 0;
}
if ( isset( $this->element_queries[ $position ]->total_users ) ) {
$pager['total_items'] = intval( $this->element_queries[ $position ]->total_users );
} else {
$pager['total_items'] = 0;
}
if ( ( ! empty( $pager['per_page'] ) ) && ( ! empty( $pager['total_items'] ) ) ) {
$pager['total_pages'] = ceil( intval( $this->element_queries[ $position ]->total_users ) / intval( $this->element_queries[ $position ]->query_vars['number'] ) );
} else {
$pager['total_pages'] = 0;
}
}
}
return $pager;
}
/**
* Build selector section options HTML.
*
* @since 2.2.1
*
* @param string $position Value for 'left' or 'right' position.
*/
protected function build_options_html( $position = '' ) {
$options_html = '';
if ( $this->is_valid_position( $position ) ) {
if ( ! empty( $this->element_items[ $position ] ) ) {
foreach ( $this->element_items[ $position ] as $user ) {
/**
* Filters binary selector item.
*
* @since 2.6.0
*
* @param string $selector_item Binary selector item title.
* @param WP_User $user WP_User object.
* @param string $position Value for 'left' or 'right' position.
* @param string $selector_class Class reference to selector.
*/
$user_name = apply_filters( 'learndash_binary_selector_item', $user->display_name . ' (' . $user->user_login . ')', $user, $position, $this->selector_class );
if ( ! empty( $user_name ) ) {
$user_name = wp_strip_all_tags( $user_name );
} else {
$user_name = $user->display_name . ' (' . $user->user_login . ')';
}
$disabled_class = '';
$disabled_state = '';
if ( ( is_array( $this->args['selected_ids'] ) ) && ( ! empty( $this->args['selected_ids'] ) ) ) {
if ( in_array( absint( $user->ID ), $this->args['selected_ids'], true ) ) {
$disabled_class = 'learndash-binary-selector-item-disabled';
if ( 'left' === $position ) {
$disabled_state = ' disabled="disabled" ';
}
}
}
$options_html .= '<option class="learndash-binary-selector-item ' . $disabled_class . '" ' . $disabled_state . ' value="' . $user->ID . '" data-value="' . $user->ID . '">' . $user_name . '</option>';
}
}
}
return $options_html;
}
/**
* Query selector section items.
*
* @since 2.2.1
*
* @param string $position Value for 'left' or 'right' position.
*/
protected function query_selection_section_items( $position = '' ) {
global $wpdb;
if ( $this->is_valid_position( $position ) ) {
if ( 'left' === $position ) {
if ( ! empty( $this->args['included_ids'] ) ) {
$this->args['include'] = $this->args['included_ids'];
}
if ( ( isset( $this->args['excluded_ids'] ) ) && ( ! empty( $this->args['excluded_ids'] ) ) ) {
$this->args['exclude'] = $this->args['excluded_ids'];
}
if ( true === $this->args['is_search'] ) {
if ( ( isset( $this->args['selected_ids'] ) ) && ( ! empty( $this->args['selected_ids'] ) ) ) {
if ( ! isset( $this->args['exclude'] ) ) {
$this->args['exclude'] = array();
}
$this->args['exclude'] = array_merge( $this->args['exclude'], $this->args['selected_ids'] );
}
}
} elseif ( 'right' === $position ) {
if ( ( isset( $this->args['selected_meta_query'] ) ) && ( ! empty( $this->args['selected_meta_query'] ) ) ) {
$this->args['meta_query'] = $this->args['selected_meta_query']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
} elseif ( ! empty( $this->args['selected_ids'] ) ) {
$this->args['include'] = $this->args['selected_ids'];
} else {
$this->args['include'] = array( 0 );
}
if ( ( isset( $this->args['excluded_ids'] ) ) && ( ! empty( $this->args['excluded_ids'] ) ) ) {
$this->args['exclude'] = $this->args['excluded_ids'];
}
}
/**
* Filter to exclude users with no role.
*
* @since 3.2.3
*
* @param bool $true Boolean to exclude no_role users.
* @param array $query_args Array of current query args.
* @param string $position Position (left/right) for queried items.
* @param string $selector_class Class for Binary selector.
*/
if ( apply_filters( 'learndash_exclude_user_no_role', true, $this->args, $position, $this->selector_class ) ) {
$blog_prefix = $wpdb->get_blog_prefix( $wpdb->blogid );
if ( ! isset( $this->args['meta_query'] ) ) {
$this->args['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
$this->args['meta_query'][] = array(
'key' => "{$blog_prefix}capabilities",
'compare' => 'EXISTS',
);
$meta_query_relation_present = false;
foreach ( $this->args['meta_query'] as $idx => $query_item ) {
if ( 'relation' === $idx ) {
$meta_query_relation_present = true;
break;
}
}
if ( ! $meta_query_relation_present ) {
$this->args['meta_query']['relation'] = 'AND';
}
}
$this->process_query( $this->args, $position );
if ( isset( $this->args['include'] ) ) {
unset( $this->args['include'] );
}
}
}
/**
* Process selector section query.
*
* @since 2.2.1
*
* @param array $query_args Array of query args.
* @param string $position Value for 'left' or 'right' position.
*/
public function process_query( $query_args = array(), $position = '' ) {
if ( $this->is_valid_position( $position ) ) {
$query = new WP_User_Query( $query_args );
$items = $query->get_results();
if ( ! empty( $items ) ) {
$this->element_queries[ $position ] = $query;
$this->element_items[ $position ] = $items;
// We only need to store one reference to the query as the left and right will share this. Plus
// the query on the right side may/will have the 'include' elements and we store this as 'selected_ids' key.
if ( 'left' === $position ) {
$this->element_data['query_vars'] = $query_args;
}
$this->element_data['selector_class'] = $this->selector_class;
$this->element_data['selector_nonce'] = $this->get_nonce_data();
$this->element_data[ $position ]['position'] = $position;
$this->element_data[ $position ]['pager'] = $this->get_pager_data( $position );
}
}
}
/**
* Load selector section search AJAX.
*
* @since 2.2.1
*
* @param string $position Value for 'left' or 'right' position.
*/
public function load_search_ajax( $position = '' ) {
$reply_data = array();
if ( $this->is_valid_position( $position ) ) {
if ( ( isset( $this->args['search'] ) ) && ( ! empty( $this->args['search'] ) ) ) {
// For user searching Users we must include the beginning and ending '*' for wildcard matches.
$this->args['search'] = '*' . $this->args['search'] . '*';
// Now call the parent function to perform the actual search.
$reply_data = parent::load_search_ajax( $position );
}
}
return $reply_data;
}
}
}