PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/custom-post-types/quiz.php +140 -121 4.4.34.4.8 View file →
@@ -6,9 +6,13 @@
6 6 * @package LearnPress/Classes
7 7 * @version 4.0.0
8 8 */
9 9
10 +use LearnPress\Databases\PostDB;
11 +use LearnPress\Filters\QuizPostFilter;
12 +use LearnPress\Models\PostModel;
10 13 use LearnPress\Models\QuizPostModel;
14 +use LearnPress\Models\WPTables\QuizzesTable;
11 15
12 16 defined( 'ABSPATH' ) || exit();
13 17
14 18 if ( ! class_exists( 'LP_Quiz_Post_Type' ) ) {
@@ -33,8 +37,13 @@
33 37 */
34 38 protected $_post_type = LP_QUIZ_CPT;
35 39
36 40 /**
41 + * @var string
42 + */
43 + protected $_screen_list = 'edit-' . LP_QUIZ_CPT;
44 +
45 + /**
37 46 * LP_Quiz_Post_Type constructor.
38 47 *
39 48 * @param $post_type
40 49 * @param mixed
@@ -45,14 +54,32 @@
45 54
46 55 add_action( 'learn-press/admin/after-enqueue-scripts', array( $this, 'data_quiz_editor' ) );
47 56
48 57 add_filter( 'views_edit-' . LP_QUIZ_CPT, array( $this, 'views_pages' ), 10 );
49 - add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ), 10 );
58 + add_action( 'posts_pre_query', array( $this, 'posts_pre_query' ), 999, 2 );
59 + // add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ), 10 );
50 60
51 61 parent::__construct();
52 62 }
53 63
54 64 /**
65 + * Declare class name of table list quizzes.
66 + *
67 + * @param string $class_name
68 + * @param array $args
69 + *
70 + * @return string
71 + * @since 4.2.9.5
72 + */
73 + public function wp_list_table_class_name( $class_name, $args ) {
74 + if ( $this->check_class_name_handle_table( $args['screen'] ) ) {
75 + $class_name = QuizzesTable::class;
76 + }
77 +
78 + return $class_name;
79 + }
80 +
81 + /**
55 82 * Add filters to lesson view.
56 83 *
57 84 * @since 3.0.0
58 85 *
@@ -111,9 +138,9 @@
111 138 'title',
112 139 'editor',
113 140 'revisions',
114 141 ),
115 - 'hierarchical' => true,
142 + 'hierarchical' => false, // Quizzes have no parent-child hierarchy.
116 143 'rewrite' => array(
117 144 'slug' => 'quizzes',
118 145 'hierarchical' => true,
119 146 'with_front' => false,
@@ -228,109 +255,129 @@
228 255 do_action( 'learn-press/admin/edit-quiz/layout', $quizPostModel );
229 256 }
230 257
231 258 /**
232 - * Add columns to admin manage quiz page
259 + * Query lp quizzes in admin via Post DB
233 260 *
234 - * @param array $columns
261 + * @param array $posts
262 + * @param WP_Query $wp_query
235 263 *
236 - * @return array
264 + * @return array|WP_Query
265 + * @since 4.4.8
266 + * @version 1.0.0
237 267 */
238 - public function columns_head( $columns ) {
239 - $pos = array_search( 'title', array_keys( $columns ) );
268 + public function posts_pre_query( $posts, $wp_query ) {
269 + try {
270 + if ( ! is_admin() ) {
271 + return $posts;
272 + }
240 273
241 - if ( false !== $pos && ! array_key_exists( LP_COURSE_CPT, $columns ) ) {
242 - $columns = array_merge(
243 - array_slice( $columns, 0, $pos + 1 ),
244 - array(
245 - 'instructor' => esc_html__( 'Author', 'learnpress' ),
246 - LP_COURSE_CPT => esc_html__( 'Course', 'learnpress' ),
247 - 'num_of_question' => esc_html__( 'Questions', 'learnpress' ),
248 - 'duration' => esc_html__( 'Duration', 'learnpress' ),
249 - ),
250 - array_slice( $columns, $pos + 1 )
251 - );
252 - }
274 + // Check screen
275 + $curren_screen = get_current_screen();
276 + if ( ! $curren_screen || $curren_screen->id !== 'edit-' . LP_QUIZ_CPT ) {
277 + return $posts;
278 + }
253 279
254 - unset( $columns['taxonomy-lesson-tag'] );
255 - $user = wp_get_current_user();
280 + $post_type = $wp_query->get( 'post_type' );
256 281
257 - if ( in_array( 'lp_teacher', $user->roles ) ) {
258 - unset( $columns['instructor'] );
259 - }
282 + if ( empty( $post_type ) || $post_type !== LP_QUIZ_CPT ) {
283 + return $posts;
284 + }
260 285
261 - if ( ! empty( $columns['author'] ) ) {
262 - unset( $columns['author'] );
263 - }
286 + $posts_per_page = get_user_option( "edit_{$post_type}_per_page", get_current_user_id() );
287 + if ( empty( $posts_per_page ) ) {
288 + $posts_per_page = 20;
289 + }
264 290
265 - return $columns;
266 - }
291 + $paged = max( 1, get_query_var( 'paged' ) );
292 + $author = $wp_query->get( 'author' );
293 + $status = $wp_query->get( 'post_status' );
294 + $search = $wp_query->get( 's' );
295 + $month = $wp_query->get( 'm' );
296 + $orderby = LP_Request::get_param( 'orderby', '', 'key' );
297 + $order = LP_Request::get_param( 'order', '', 'key' );
267 298
268 - /**
269 - * Display content for custom column
270 - *
271 - * @param string $name
272 - * @param int $post_id
273 - */
274 - public function columns_content( $name, $post_id = 0 ) {
275 - $quizPostModel = QuizPostModel::find( $post_id, true );
276 - if ( ! $quizPostModel ) {
277 - return;
278 - }
299 + $filter = new QuizPostFilter();
300 + $filter->only_fields = [ 'p.ID', 'p.post_title', 'p.post_author', 'p.post_date', 'p.post_date_gmt' ];
301 + $filter->field_count = 'p.ID';
302 + $filter->page = $paged;
303 + $filter->limit = $posts_per_page;
304 + $post_db = PostDB::getInstance();
279 305
280 - switch ( $name ) {
281 - case 'instructor':
282 - $this->column_instructor( $post_id );
283 - break;
284 - case 'lp_course':
285 - $this->_get_item_course( $post_id );
286 - break;
287 - case 'num_of_question':
288 - $count = $quizPostModel->count_questions();
306 + if ( ! empty( $status ) ) {
307 + $filter->post_status = array( $status );
308 + }
289 309
290 - printf(
291 - '<span class="lp-label-counter %s" title="%s">%s</span>',
292 - ! $count ? 'disabled' : '',
293 - $count ?
294 - sprintf( _n( '%d question', '%d questions', $count, 'learnpress' ), $count ) :
295 - __( 'This quiz has no questions', 'learnpress' ),
296 - $count
310 + if ( ! empty( $author ) ) {
311 + $filter->post_author = absint( $author );
312 + }
313 +
314 + if ( ! empty( $search ) ) {
315 + $filter->post_title = sanitize_text_field( wp_unslash( $search ) );
316 + }
317 +
318 + if ( ! empty( $month ) && is_numeric( $month ) && strlen( (string) $month ) === 6 ) {
319 + $filter->where[] = $post_db->wpdb->prepare(
320 + 'AND YEAR(p.post_date) = %d AND MONTH(p.post_date) = %d',
321 + substr( $month, 0, 4 ),
322 + substr( $month, 4, 2 )
297 323 );
298 - break;
299 - case 'duration':
300 - $duration_str = $quizPostModel->get_duration();
301 - $duration_arr = explode( ' ', $duration_str );
302 - $duration = $duration_arr[0];
303 - $duration_type = $duration_arr[1];
324 + }
304 325
305 - if ( $duration > 0 ) {
306 - $duration_str = LP_Datetime::get_string_plural_duration( $duration, $duration_type );
307 - } else {
308 - $duration_str = __( 'Unlimited', 'learnpress' );
309 - }
326 + // Exclude status auto-draft
327 + $filter->where[] = $post_db->wpdb->prepare( 'AND p.post_status != %s', PostModel::STATUS_AUTO_DRAFT );
310 328
311 - echo esc_html( $duration_str );
312 - break;
313 - case 'preview':
314 - printf(
315 - '<input type="checkbox" class="learn-press-checkbox learn-press-toggle-item-preview" %s value="%s" data-nonce="%s" />',
316 - get_post_meta( $post_id, '_lp_preview', true ) == 'yes' ? ' checked="checked"' : '',
317 - $post_id,
318 - wp_create_nonce( 'learn-press-toggle-item-preview' )
319 - );
320 - break;
321 - default:
322 - break;
329 + // Add question count field
330 + $filter->only_fields[] = "(SELECT COUNT(*) FROM {$post_db->tb_lp_quiz_questions} qq_count
331 + WHERE qq_count.quiz_id = p.ID) AS question_count";
323 332
333 + // Join sections/courses when sorting by course-name
334 + if ( $orderby === 'course-name' ) {
335 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_section_items si ON p.ID = si.item_id";
336 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_sections s ON s.section_id = si.section_id";
337 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->posts} c ON c.ID = s.section_course_id";
338 + }
339 +
340 + // Filter unassigned
341 + if ( 'yes' === LP_Request::get_param( 'unassigned', '', 'key' ) ) {
342 + $filter->where[] = "AND p.ID NOT IN(
343 + SELECT si.item_id
344 + FROM {$post_db->wpdb->learnpress_section_items} si
345 + )";
346 + }
347 +
348 + if ( $orderby === 'course-name' ) {
349 + $filter->order_by = 'c.post_title';
350 + } elseif ( $orderby === 'question-count' ) {
351 + $filter->order_by = 'question_count';
352 + } elseif ( $orderby === 'title' ) {
353 + $filter->order_by = 'p.post_title';
354 + } elseif ( $orderby === 'author' ) {
355 + $filter->order_by = 'p.post_author';
356 + } elseif ( $orderby === 'date' ) {
357 + $filter->order_by = 'p.post_date';
358 + } else {
359 + $filter->order_by = 'p.menu_order';
360 + }
361 +
362 + $filter->order = strtolower( $order ) === 'asc' ? 'ASC' : 'DESC';
363 +
364 + // Get lp quizzes
365 + $total_rows = 0;
366 + $lp_quizzes = $post_db->get_posts( $filter, $total_rows );
367 +
368 + $wp_query->post_count = count( $lp_quizzes );
369 + $wp_query->found_posts = $total_rows;
370 + $wp_query->max_num_pages = (int) ceil( $total_rows / $posts_per_page );
371 + $posts = $lp_quizzes;
372 + } catch ( Throwable $e ) {
373 + LP_Debug::error_log( $e );
324 374 }
375 +
376 + return $posts;
325 377 }
326 378
327 - /**
328 - * @param $fields
329 - *
330 - * @return string
331 - */
332 - public function posts_fields( $fields ): string {
379 + /*public function posts_fields( $fields ): string {
333 380 global $wpdb;
334 381
335 382 if ( ! $this->is_page_list_posts_on_backend() ) {
336 383 return $fields;
@@ -344,13 +391,8 @@
344 391
345 392 return $fields;
346 393 }
347 394
348 - /**
349 - * @param $join
350 - *
351 - * @return string
352 - */
353 395 public function posts_join_paged( $join ): string {
354 396 if ( ! $this->is_page_list_posts_on_backend() ) {
355 397 return $join;
356 398 }
@@ -357,13 +399,8 @@
357 399
358 400 return $join;
359 401 }
360 402
361 - /**
362 - * @param $where
363 - *
364 - * @return mixed|string
365 - */
366 403 public function posts_where_paged( $where ) {
367 404 if ( ! $this->is_page_list_posts_on_backend() ) {
368 405 return $where;
369 406 }
@@ -372,15 +409,15 @@
372 409
373 410 if ( 'yes' === LP_Request::get( 'unassigned' ) ) {
374 411 $where .= $wpdb->prepare(
375 412 "
376 - AND {$wpdb->posts}.ID NOT IN(
377 - SELECT si.item_id
378 - FROM {$wpdb->learnpress_section_items} si
379 - INNER JOIN {$wpdb->posts} p ON p.ID = si.item_id
380 - WHERE p.post_type = %s
381 - )
382 - ",
413 + AND {$wpdb->posts}.ID NOT IN(
414 + SELECT si.item_id
415 + FROM {$wpdb->learnpress_section_items} si
416 + INNER JOIN {$wpdb->posts} p ON p.ID = si.item_id
417 + WHERE p.post_type = %s
418 + )
419 + ",
383 420 LP_QUIZ_CPT
384 421 );
385 422 }
386 423
@@ -386,13 +423,8 @@
386 423
387 424 return $where;
388 425 }
389 426
390 - /**
391 - * @param $order_by_statement
392 - *
393 - * @return string
394 - */
395 427 public function posts_orderby( $order_by_statement ) {
396 428 global $wpdb;
397 429
398 430 if ( ! $this->is_page_list_posts_on_backend() ) {
@@ -415,22 +447,9 @@
415 447 }
416 448 }
417 449
418 450 return $order_by_statement;
419 - }
420 -
421 - /**
422 - * @param $columns
423 - *
424 - * @return mixed
425 - */
426 - public function sortable_columns( $columns ) {
427 - $columns['instructor'] = 'author';
428 - $columns[ LP_COURSE_CPT ] = 'course-name';
429 - $columns['num_of_question'] = 'question-count';
430 -
431 - return $columns;
432 - }
451 + }*/
433 452
434 453 /**
435 454 * Quiz assigned view.
436 455 *