PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
← All changes | inc/custom-post-types/quiz.php +147 -121 4.4.3 → 4.4.9 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,136 @@
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 )
274 + // Check screen
275 + if ( ! function_exists( 'get_current_screen' ) ) {
276 + return $posts;
277 + }
278 +
279 + $curren_screen = get_current_screen();
280 + if ( ! $curren_screen || $curren_screen->id !== 'edit-' . LP_QUIZ_CPT ) {
281 + return $posts;
282 + }
283 +
284 + $post_type = $wp_query->get( 'post_type' );
285 +
286 + if ( empty( $post_type ) || $post_type !== LP_QUIZ_CPT ) {
287 + return $posts;
288 + }
289 +
290 + $posts_per_page = get_user_option( "edit_{$post_type}_per_page", get_current_user_id() );
291 + if ( empty( $posts_per_page ) ) {
292 + $posts_per_page = 20;
293 + }
294 +
295 + $paged = max( 1, get_query_var( 'paged' ) );
296 + $author = $wp_query->get( 'author' );
297 + $status = $wp_query->get( 'post_status' );
298 + $search = $wp_query->get( 's' );
299 + $month = $wp_query->get( 'm' );
300 + $orderby = LP_Request::get_param( 'orderby', '', 'key' );
301 + $order = LP_Request::get_param( 'order', '', 'key' );
302 +
303 + $filter = new QuizPostFilter();
304 + $filter->only_fields = array_map(
305 + function ( $field ) {
306 + return "p.{$field}";
307 + },
308 + $filter->all_fields
251 309 );
252 - }
310 + $filter->field_count = 'p.ID';
311 + $post_db = PostDB::getInstance();
253 312
254 - unset( $columns['taxonomy-lesson-tag'] );
255 - $user = wp_get_current_user();
313 + if ( ! empty( $status ) ) {
314 + $filter->post_status = array( $status );
315 + }
256 316
257 - if ( in_array( 'lp_teacher', $user->roles ) ) {
258 - unset( $columns['instructor'] );
259 - }
317 + if ( ! empty( $author ) ) {
318 + $filter->post_author = absint( $author );
319 + }
260 320
261 - if ( ! empty( $columns['author'] ) ) {
262 - unset( $columns['author'] );
263 - }
321 + if ( ! empty( $search ) ) {
322 + $filter->post_title = sanitize_text_field( wp_unslash( $search ) );
323 + }
264 324
265 - return $columns;
266 - }
325 + if ( ! empty( $month ) && is_numeric( $month ) && strlen( (string) $month ) === 6 ) {
326 + $filter->where[] = $post_db->wpdb->prepare(
327 + 'AND YEAR(p.post_date) = %d AND MONTH(p.post_date) = %d',
328 + substr( $month, 0, 4 ),
329 + substr( $month, 4, 2 )
330 + );
331 + }
267 332
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 - }
333 + // Exclude status auto-draft
334 + $filter->where[] = $post_db->wpdb->prepare( 'AND p.post_status != %s', PostModel::STATUS_AUTO_DRAFT );
279 335
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();
336 + // Add question count field
337 + $filter->only_fields[] = "(SELECT COUNT(*) FROM {$post_db->tb_lp_quiz_questions} qq_count
338 + WHERE qq_count.quiz_id = p.ID) AS question_count";
289 339
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
297 - );
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];
340 + // Join sections/courses when sorting by course-name
341 + if ( $orderby === 'course-name' ) {
342 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_section_items si ON p.ID = si.item_id";
343 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_sections s ON s.section_id = si.section_id";
344 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->posts} c ON c.ID = s.section_course_id";
345 + }
304 346
305 - if ( $duration > 0 ) {
306 - $duration_str = LP_Datetime::get_string_plural_duration( $duration, $duration_type );
307 - } else {
308 - $duration_str = __( 'Unlimited', 'learnpress' );
309 - }
347 + // Filter unassigned
348 + if ( 'yes' === LP_Request::get_param( 'unassigned', '', 'key' ) ) {
349 + $filter->where[] = "AND p.ID NOT IN(
350 + SELECT si.item_id
351 + FROM {$post_db->wpdb->learnpress_section_items} si
352 + )";
353 + }
310 354
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;
355 + if ( $orderby === 'course-name' ) {
356 + $filter->order_by = 'c.post_title';
357 + } elseif ( $orderby === 'question-count' ) {
358 + $filter->order_by = 'question_count';
359 + } elseif ( $orderby === 'title' ) {
360 + $filter->order_by = 'p.post_title';
361 + } elseif ( $orderby === 'author' ) {
362 + $filter->order_by = 'p.post_author';
363 + } elseif ( $orderby === 'date' ) {
364 + $filter->order_by = 'p.post_date';
365 + } else {
366 + $filter->order_by = 'p.menu_order';
367 + }
323 368
369 + $filter->order = strtolower( $order ) === 'asc' ? 'ASC' : 'DESC';
370 +
371 + // Get lp quizzes
372 + $total_rows = 0;
373 + $lp_quizzes = $post_db->get_posts( $filter, $total_rows );
374 +
375 + $wp_query->post_count = count( $lp_quizzes );
376 + $wp_query->found_posts = $total_rows;
377 + $wp_query->max_num_pages = (int) ceil( $total_rows / $posts_per_page );
378 + $posts = $lp_quizzes;
379 + } catch ( Throwable $e ) {
380 + LP_Debug::error_log( $e );
324 381 }
382 +
383 + return $posts;
325 384 }
326 385
327 - /**
328 - * @param $fields
329 - *
330 - * @return string
331 - */
332 - public function posts_fields( $fields ): string {
386 + /*public function posts_fields( $fields ): string {
333 387 global $wpdb;
334 388
335 389 if ( ! $this->is_page_list_posts_on_backend() ) {
336 390 return $fields;
@@ -344,13 +398,8 @@
344 398
345 399 return $fields;
346 400 }
347 401
348 - /**
349 - * @param $join
350 - *
351 - * @return string
352 - */
353 402 public function posts_join_paged( $join ): string {
354 403 if ( ! $this->is_page_list_posts_on_backend() ) {
355 404 return $join;
356 405 }
@@ -357,13 +406,8 @@
357 406
358 407 return $join;
359 408 }
360 409
361 - /**
362 - * @param $where
363 - *
364 - * @return mixed|string
365 - */
366 410 public function posts_where_paged( $where ) {
367 411 if ( ! $this->is_page_list_posts_on_backend() ) {
368 412 return $where;
369 413 }
@@ -372,15 +416,15 @@
372 416
373 417 if ( 'yes' === LP_Request::get( 'unassigned' ) ) {
374 418 $where .= $wpdb->prepare(
375 419 "
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 - ",
420 + AND {$wpdb->posts}.ID NOT IN(
421 + SELECT si.item_id
422 + FROM {$wpdb->learnpress_section_items} si
423 + INNER JOIN {$wpdb->posts} p ON p.ID = si.item_id
424 + WHERE p.post_type = %s
425 + )
426 + ",
383 427 LP_QUIZ_CPT
384 428 );
385 429 }
386 430
@@ -386,13 +430,8 @@
386 430
387 431 return $where;
388 432 }
389 433
390 - /**
391 - * @param $order_by_statement
392 - *
393 - * @return string
394 - */
395 434 public function posts_orderby( $order_by_statement ) {
396 435 global $wpdb;
397 436
398 437 if ( ! $this->is_page_list_posts_on_backend() ) {
@@ -415,22 +454,9 @@
415 454 }
416 455 }
417 456
418 457 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 - }
458 + }*/
433 459
434 460 /**
435 461 * Quiz assigned view.
436 462 *