class-learndash-admin-groups-listing.php
14 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* LearnDash Groups (groups) Posts Listing.
*
* @since 3.2.0
* @package LearnDash\Group\Listing
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Posts_Listing' ) ) && ( ! class_exists( 'Learndash_Admin_Groups_Listing' ) ) ) {
/**
* Class LearnDash Groups (groups) Posts Listing.
*
* @since 3.2.0
* @uses Learndash_Admin_Posts_Listing
*/
class Learndash_Admin_Groups_Listing extends Learndash_Admin_Posts_Listing {
/**
* Public constructor for class
*
* @since 3.2.0
*/
public function __construct() {
$this->post_type = learndash_get_post_type_slug( 'group' );
parent::__construct();
}
/**
* Called via the WordPress init action hook.
*
* @since 3.2.3
*/
public function listing_init() {
if ( $this->listing_init_done ) {
return;
}
$this->selectors = array(
'user_id' => array(
'type' => 'user',
'show_all_value' => '',
'show_all_label' => esc_html__( 'All Users', 'learndash' ),
'selector_filter_function' => array( $this, 'selector_filter_for_author' ),
'selector_value_function' => array( $this, 'selector_value_for_author' ),
),
'price_type' => array(
'type' => 'early',
'display' => array( $this, 'show_selector_price_type' ),
'show_all_value' => '',
'show_all_label' => esc_html__( 'All Price Types', 'learndash' ),
'options' => array(
'free' => esc_html__( 'Free', 'learndash' ),
'paynow' => esc_html__( 'Buy now', 'learndash' ),
'subscribe' => esc_html__( 'Recurring', 'learndash' ),
'closed' => esc_html__( 'Closed', 'learndash' ),
),
'listing_query_function' => array( $this, 'filter_listing_by_price_type' ),
'select2' => true,
'select2_fetch' => false,
),
'course_id' => array(
'type' => 'post_type',
'post_type' => learndash_get_post_type_slug( 'course' ),
'show_all_value' => '',
'show_all_label' => sprintf(
// translators: placeholder: Courses.
esc_html_x( 'All %s', 'placeholder: Courses', 'learndash' ),
LearnDash_Custom_Label::get_label( 'courses' )
),
'listing_query_function' => array( $this, 'selector_filter_for_group_course' ),
'selector_filter_function' => array( $this, 'selector_filter_for_course' ),
'selector_filters' => array( 'group_id' ),
),
'certificate_id' => array(
'type' => 'post_type',
'post_type' => learndash_get_post_type_slug( 'certificate' ),
'show_all_value' => '',
'show_all_label' => esc_html__( 'All Certificates', 'learndash' ),
'show_empty_value' => 'empty',
'show_empty_label' => esc_html__( '-- No Certificate --', 'learndash' ),
'listing_query_function' => array( $this, 'listing_filter_by_certificate' ),
),
);
$this->columns = array(
'groups_courses_users' => array(
'label' => sprintf(
// translators: placeholder: Courses.
esc_html_x( '%s / Users', 'placeholder: Courses', 'learndash' ),
LearnDash_Custom_Label::get_label( 'courses' )
),
'display' => array( $this, 'show_column_course_users' ),
'after' => 'title',
),
'price_type' => array(
'label' => esc_html__( 'Price Type', 'learndash' ),
'after' => 'title',
'display' => array( $this, 'show_column_price_type' ),
'required' => true,
),
);
add_action( 'admin_notices', array( $this, 'nonpublic_groups_warning' ) );
parent::listing_init();
$this->listing_init_done = true;
}
/**
* Call via the WordPress load sequence for admin pages.
*
* @since 3.2.3
*/
public function on_load_listing() {
if ( $this->post_type_check() ) {
parent::on_load_listing();
add_filter( 'learndash_listing_table_query_vars_filter', array( $this, 'listing_table_query_vars_filter_groups' ), 30, 3 );
/**
* Convert the Group Post Meta items.
*
* @since 3.4.1
*/
$ld_data_upgrade_group_post_meta = Learndash_Admin_Data_Upgrades::get_instance( 'Learndash_Admin_Data_Upgrades_Group_Post_Meta' );
if ( ( $ld_data_upgrade_group_post_meta ) && ( is_a( $ld_data_upgrade_group_post_meta, 'Learndash_Admin_Data_Upgrades_Group_Post_Meta' ) ) ) {
$ld_data_upgrade_group_post_meta->process_post_meta( false );
}
}
}
/**
* Listing table query vars
*
* @since 3.2.3
*
* @param array $q_vars Array of query vars.
* @param string $post_type Post Type being displayed.
* @param array $query Main Query.
*/
public function listing_table_query_vars_filter_groups( $q_vars, $post_type, $query ) {
if ( $post_type === $this->post_type ) {
if ( ! learndash_is_admin_user() ) {
if ( ( learndash_is_group_leader_user( get_current_user_id() ) ) && ( 'basic' === learndash_get_group_leader_manage_groups() ) ) {
$group_ids = learndash_get_administrators_group_ids( get_current_user_id() );
$group_ids = array_map( 'absint', $group_ids );
if ( ! empty( $group_ids ) ) {
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = $group_ids;
} else {
$q_vars['post__in'] = array_intersect( $q_vars['post__in'], $group_ids );
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
} else {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
}
$user_selector = $this->get_selector( 'user_id' );
if ( ( $user_selector ) && ( isset( $user_selector['selected'] ) ) && ( ! empty( $user_selector['selected'] ) ) ) {
$group_ids = learndash_get_users_group_ids( $user_selector['selected'], true );
$group_ids = array_map( 'absint', $group_ids );
if ( ! empty( $group_ids ) ) {
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = $group_ids;
} else {
$q_vars['post__in'] = array_intersect( $q_vars['post__in'], $group_ids );
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
} else {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
// Filter the Groups listing for the Group Membership for the Post.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ( isset( $_GET['ld-group-membership-post-id'] ) ) && ( ! empty( $_GET['ld-group-membership-post-id'] ) ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$group_membership_settings = learndash_get_post_group_membership_settings( absint( $_GET['ld-group-membership-post-id'] ) );
if ( ! empty( $group_membership_settings['groups_membership_groups'] ) ) {
$group_ids = $group_membership_settings['groups_membership_groups'];
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = $group_ids;
} else {
$q_vars['post__in'] = array_intersect( $q_vars['post__in'], $group_ids );
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
} else {
$q_vars['post__in'] = array( 0 );
return $q_vars;
}
}
}
return $q_vars;
}
/**
* Show Group Course Users column.
*
* @since 3.2.3
*
* @param int $post_id The Step post ID shown.
* @param string $column_name Column name/slug.
*/
protected function show_column_course_users( $post_id = 0, $column_name = '' ) {
if ( ( ! empty( $post_id ) ) && ( 'groups_courses_users' === $column_name ) ) {
$hidden = (array) get_hidden_columns( get_current_screen()->id );
if ( in_array( $column_name, $hidden, true ) ) {
echo esc_html__( 'reload', 'learndash' );
return;
}
$group_users = learndash_get_groups_user_ids( $post_id );
if ( ( empty( $group_users ) ) || ( ! is_array( $group_users ) ) ) {
$group_users = array();
}
echo sprintf(
// translators: placeholder: Group Users Count.
esc_html_x( 'Users: %d', 'placeholder: Group Users Count', 'learndash' ),
count( $group_users )
);
echo '<br />';
// Group Courses.
$group_courses = learndash_group_enrolled_courses( $post_id );
if ( ( empty( $group_courses ) ) || ( ! is_array( $group_courses ) ) ) {
$group_courses = array();
}
echo sprintf(
// translators: placeholder: Group Courses Count.
esc_html_x( '%1$s: %2$d', 'placeholders: Courses, Group Courses Count', 'learndash' ),
learndash_get_custom_label( 'courses' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method escapes output
count( $group_courses )
);
echo '<br />';
// Group Leaders.
$group_leaders = learndash_get_groups_administrator_ids( $post_id );
if ( ( empty( $group_leaders ) ) || ( ! is_array( $group_leaders ) ) ) {
$group_leaders = array();
}
printf(
// translators: placeholder: Group Leaders Count.
esc_html_x( 'Leaders %d', 'placeholder: Group Leaders Count', 'learndash' ),
count( $group_leaders )
);
}
}
/**
* Filter the main query listing by the course_id
*
* @since 3.2.3
*
* @param array $q_vars Query vars used for the table listing.
* @param array $selector Array of attributes used to display the filter selector.
* @return array $q_vars.
*/
protected function selector_filter_for_group_course( $q_vars = array(), $selector = array() ) {
if ( ( isset( $selector['selected'] ) ) && ( ! empty( $selector['selected'] ) ) ) {
$course_group_ids = learndash_get_course_groups( absint( $selector['selected'] ), true );
if ( ! empty( $course_group_ids ) ) {
if ( ! isset( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = array();
}
if ( empty( $q_vars['post__in'] ) ) {
$q_vars['post__in'] = $course_group_ids;
} else {
$q_vars['post__in'] = array_intersect( $q_vars['post__in'], $course_group_ids );
}
} else {
$q_vars['post__in'] = array( 0 );
}
}
return $q_vars;
}
/**
* Show selector for price type.
*
* @since 3.4.1
*
* @param array $selector Selector args.
*/
protected function show_selector_price_type( $selector = array() ) {
if ( ( isset( $selector['options'] ) ) && ( ! empty( $selector['options'] ) ) ) {
$this->show_selector_start( $selector );
$this->show_selector_all_option( $selector );
if ( ( isset( $_GET['price_type'] ) ) && ( ! empty( $_GET['price_type'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$selected_price_type = sanitize_text_field( wp_unslash( $_GET['price_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
} else {
$selected_price_type = '';
}
foreach ( $selector['options'] as $price_type_key => $price_type_label ) {
echo '<option value="' . esc_attr( $price_type_key ) . '" ' . selected( $price_type_key, $selected_price_type, false ) . '>' . esc_attr( $price_type_label ) . '</option>';
}
$this->show_selector_end( $selector );
}
}
/**
* This function fill filter the table listing items by the selected Price Type.
* Called via 'parse_query' filter from WP.
*
* @since 3.4.2
*
* @param object $q_vars Query vars used for the table listing.
* @param array $selector Array of attributes used to display the filter selector.
*
* @return object $q_vars.
*/
protected function filter_listing_by_price_type( $q_vars, $selector ) {
if ( ( isset( $selector['selected'] ) ) && ( ! empty( $selector['selected'] ) ) ) {
if ( learndash_post_meta_processed( $this->post_type ) ) {
if ( ! isset( $q_vars['meta_query'] ) ) {
$q_vars['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
$q_vars['meta_query'][] = array(
'key' => '_ld_price_type',
'value' => $selector['selected'],
);
} else {
$post_ids = learndash_get_posts_by_price_type( $this->post_type, $selector['selected'] );
if ( ! empty( $post_ids ) ) {
$q_vars['post__in'] = $post_ids;
} else {
$q_vars['post__in'] = array( 0 );
}
}
}
return $q_vars;
}
/**
* Show Price Type column.
*
* @since 3.4.1
*
* @param int $post_id The Step post ID shown.
* @param string $column_name Column name/slug being processed.
*/
protected function show_column_price_type( $post_id = 0, $column_name = '' ) {
if ( ( ! empty( $post_id ) ) && ( 'price_type' === $column_name ) ) {
$price_type_key = learndash_get_setting( $post_id, 'group_price_type' );
if ( ! empty( $price_type_key ) ) {
if ( isset( $this->selectors[ $column_name ]['options'][ $price_type_key ] ) ) {
$price_type_label = $this->selectors[ $column_name ]['options'][ $price_type_key ];
$row_actions = array();
$filter_url = add_query_arg( 'price_type', $price_type_key, $this->get_clean_filter_url() );
$link_aria_label = esc_html__( 'Filter listing by Price type', 'learndash' );
echo '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $link_aria_label ) . '">' . esc_attr( $price_type_label ) . '</a>';
$row_actions['ld-post-filter'] = '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $link_aria_label ) . '">' . esc_html__( 'filter', 'learndash' ) . '</a>';
echo $this->list_table_row_actions( $row_actions ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML
}
}
}
}
/**
* Returns message if groups are not set to Public
*
* @since 3.4.2
*/
public function nonpublic_groups_warning() {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Groups_CPT', 'public' ) !== 'yes' ) {
$message = learndash_groups_get_not_public_message();
if ( ! empty( $message ) ) {
echo wp_kses_post( $message );
}
}
}
// End of functions.
}
}
new Learndash_Admin_Groups_Listing();