class-learndash-admin-exams-listing.php
9.97 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
<?php
/**
* LearnDash Exams (ld-exam) Posts Listing Class.
*
* @package LearnDash\Admin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Posts_Listing' ) ) && ( ! class_exists( 'Learndash_Admin_Exams_Listing' ) ) ) {
/**
* Class for LearnDash Exams Listing Pages.
*/
class Learndash_Admin_Exams_Listing extends Learndash_Admin_Posts_Listing {
/**
* Public constructor for class
*/
public function __construct() {
$this->post_type = learndash_get_post_type_slug( 'exam' );
parent::__construct();
}
/**
* Called via the WordPress init action hook.
*/
public function listing_init() {
if ( $this->listing_init_done ) {
return;
}
$this->selectors = array(
'exam_challenge_course_show' => 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 Show', 'placeholder: Courses', 'learndash' ),
LearnDash_Custom_Label::get_label( 'courses' )
),
'show_empty_value' => 'empty',
'show_empty_label' => sprintf(
// translators: placeholder: Course.
esc_html_x( '-- No %s --', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
),
'listing_query_function' => array( $this, 'listing_filter_by_course_show' ),
),
'exam_challenge_course_passed' => 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 Pass', 'placeholder: Courses', 'learndash' ),
LearnDash_Custom_Label::get_label( 'courses' )
),
'show_empty_value' => 'empty',
'show_empty_label' => sprintf(
// translators: placeholder: Course.
esc_html_x( '-- No %s --', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
),
'listing_query_function' => array( $this, 'listing_filter_by_course_passed' ),
),
);
$this->columns = array(
'exam_challenge_course_show' => array(
'label' => sprintf(
// translators: placeholder: Course.
esc_html_x( '%s Show', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
),
'after' => 'title',
'display' => array( $this, 'show_column_step_course_show' ),
'required' => false,
),
'exam_challenge_course_passed' => array(
'label' => sprintf(
// translators: placeholder: Course.
esc_html_x( '%s Passed', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
),
'after' => 'exam_challenge_course_show',
'display' => array( $this, 'show_column_step_course_passed' ),
'required' => false,
),
);
add_action( 'admin_notices', array( $this, 'learndash_exam_legacy_theme_warning' ) );
parent::listing_init();
$this->listing_init_done = true;
}
/**
* Show Exam Course "show" column.
*
* @since 4.0.0
*
* @param int $post_id The Step post ID shown.
* @param array $column_meta Array of column meta information.
*/
protected function show_column_step_course_show( $post_id = 0, $column_meta = array() ) {
if ( ! empty( $post_id ) ) {
$course_id = (int) learndash_get_setting( $post_id, 'exam_challenge_course_show' );
if ( ! empty( $course_id ) ) {
$course_post = get_post( $course_id );
if ( ( $course_post ) && ( is_a( $course_post, 'WP_Post' ) ) ) {
$course_title = learndash_format_step_post_title_with_status_label( $course_post );
$row_actions = array();
$filter_url = add_query_arg( 'exam_challenge_course_show', $course_id, $this->get_clean_filter_url() );
echo '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'filter' ) ) . '">' . wp_kses_post( $course_title ) . '</a>';
$row_actions['ld-post-filter'] = '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'filter' ) ) . '">' . esc_html__( 'filter', 'learndash' ) . '</a>';
if ( current_user_can( 'edit_post', $course_id ) ) {
$row_actions['ld-post-edit'] = '<a href="' . esc_url( get_edit_post_link( $course_id ) ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'edit' ) ) . '">' . esc_html__( 'edit', 'learndash' ) . '</a>';
}
if ( is_post_type_viewable( get_post_type( $course_id ) ) ) {
$row_actions['ld-post-view'] = '<a href="' . esc_url( get_permalink( $course_id ) ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'view' ) ) . '">' . esc_html__( 'view', 'learndash' ) . '</a>';
}
echo $this->list_table_row_actions( $row_actions ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML
}
}
}
}
/**
* Show Exam Course "passed" column.
*
* @since 4.0.0
*
* @param int $post_id The Step post ID shown.
* @param array $column_meta Array of column meta information.
*/
protected function show_column_step_course_passed( $post_id = 0, $column_meta = array() ) {
if ( ! empty( $post_id ) ) {
$course_id = (int) learndash_get_setting( $post_id, 'exam_challenge_course_passed' );
if ( ! empty( $course_id ) ) {
$course_post = get_post( $course_id );
if ( ( $course_post ) && ( is_a( $course_post, 'WP_Post' ) ) ) {
$course_title = learndash_format_step_post_title_with_status_label( $course_post );
$row_actions = array();
$filter_url = add_query_arg( 'exam_challenge_course_passed', $course_id, $this->get_clean_filter_url() );
echo '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'filter' ) ) . '">' . wp_kses_post( $course_title ) . '</a>';
$row_actions['ld-post-filter'] = '<a href="' . esc_url( $filter_url ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'filter' ) ) . '">' . esc_html__( 'filter', 'learndash' ) . '</a>';
if ( current_user_can( 'edit_post', $course_id ) ) {
$row_actions['ld-post-edit'] = '<a href="' . esc_url( get_edit_post_link( $course_id ) ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'edit' ) ) . '">' . esc_html__( 'edit', 'learndash' ) . '</a>';
}
if ( is_post_type_viewable( get_post_type( $course_id ) ) ) {
$row_actions['ld-post-view'] = '<a href="' . esc_url( get_permalink( $course_id ) ) . '" aria-label="' . esc_attr( $this->get_aria_label_for_post( $course_id, 'view' ) ) . '">' . esc_html__( 'view', 'learndash' ) . '</a>';
}
echo $this->list_table_row_actions( $row_actions ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML
}
}
}
}
/**
* Filter Exam listing by Course Show
*
* @since 4.0.0
*
* @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 listing_filter_by_course_show( $q_vars, $selector = array() ) {
if ( ( isset( $selector['selected'] ) ) && ( ! empty( $selector['selected'] ) ) ) {
if ( ( isset( $selector['show_empty_value'] ) ) && ( $selector['show_empty_value'] === $selector['selected'] ) ) {
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(
array(
'key' => 'exam_challenge_course_show',
'compare' => 'NOT EXISTS',
),
);
} else {
if ( ! empty( $selector['selected'] ) ) {
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' => 'exam_challenge_course_show',
'value' => absint( $selector['selected'] ),
);
}
}
}
return $q_vars;
}
/**
* Filter Exam listing by Course Pass
*
* @since 4.0.0
*
* @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 listing_filter_by_course_passed( $q_vars, $selector = array() ) {
if ( ( isset( $selector['selected'] ) ) && ( ! empty( $selector['selected'] ) ) ) {
if ( ( isset( $selector['show_empty_value'] ) ) && ( $selector['show_empty_value'] === $selector['selected'] ) ) {
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(
array(
'key' => 'exam_challenge_course_passed',
'compare' => 'NOT EXISTS',
),
);
} else {
if ( ! empty( $selector['selected'] ) ) {
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' => 'exam_challenge_course_passed',
'value' => absint( $selector['selected'] ),
);
}
}
}
return $q_vars;
}
/**
* Returns message if current active theme is set to Legacy
*
* @since 4.0.0
*/
public function learndash_exam_legacy_theme_warning() {
if ( learndash_is_active_theme( 'legacy' ) ) {
$message = learndash_exam_legacy_theme_warning_message();
if ( ! empty( $message ) ) {
echo wp_kses_post( $message );
}
}
}
// End of functions.
}
}
new Learndash_Admin_Exams_Listing();