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/question.php +149 -92 4.4.7 → 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\QuestionPostFilter;
12 +use LearnPress\Models\PostModel;
10 13 use LearnPress\Models\Question\QuestionPostModel;
14 +use LearnPress\Models\WPTables\QuestionsTable;
11 15
12 16 defined( 'ABSPATH' ) || exit();
13 17
14 18 if ( ! class_exists( 'LP_Question_Post_Type' ) ) {
@@ -27,8 +31,13 @@
27 31 */
28 32 protected $_post_type = LP_QUESTION_CPT;
29 33
30 34 /**
35 + * @var string
36 + */
37 + protected $_screen_list = 'edit-' . LP_QUESTION_CPT;
38 +
39 + /**
31 40 * LP_Question_Post_Type constructor.
32 41 *
33 42 * @param $post_type
34 43 * @param mixed
@@ -33,14 +42,15 @@
33 42 * @param $post_type
34 43 * @param mixed
35 44 */
36 45 public function __construct() {
37 - add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
46 + //add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
38 47 add_action( 'admin_head', array( $this, 'init' ) );
39 48 add_action( 'learn-press/admin/after-enqueue-scripts', array( $this, 'data_question_editor' ) );
40 49
41 50 add_filter( 'views_edit-' . LP_QUESTION_CPT, array( $this, 'views_pages' ), 11 );
42 - add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ), 10 );
51 + add_action( 'posts_pre_query', array( $this, 'posts_pre_query' ), 999, 2 );
52 + // add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ), 10 );
43 53
44 54 // $this->add_map_method( 'before_delete', 'before_delete_question' );
45 55
46 56 parent::__construct();
@@ -46,8 +56,25 @@
46 56 parent::__construct();
47 57 }
48 58
49 59 /**
60 + * Declare class name of table list questions.
61 + *
62 + * @param string $class_name
63 + * @param array $args
64 + *
65 + * @return string
66 + * @since 4.2.9.5
67 + */
68 + public function wp_list_table_class_name( $class_name, $args ) {
69 + if ( $this->check_class_name_handle_table( $args['screen'] ) ) {
70 + $class_name = QuestionsTable::class;
71 + }
72 +
73 + return $class_name;
74 + }
75 +
76 + /**
50 77 * Add question types support answer options
51 78 *
52 79 * @since 3.3.0
53 80 */
@@ -190,9 +217,9 @@
190 217 ),
191 218 'public' => true,
192 219 'hierarchical' => false,
193 220 'show_ui' => true,
194 - 'show_admin_column' => 'true',
221 + 'show_admin_column' => false,
195 222 'show_in_nav_menus' => true,
196 223 'rewrite' => array(
197 224 'slug' => 'question-tag',
198 225 'hierarchical' => false,
@@ -301,84 +328,136 @@
301 328 do_action( 'learn-press/admin/edit-question/layout', $questionPostModel );
302 329 }
303 330
304 331 /**
305 - * Add columns to admin manage question page
332 + * Query lp questions in admin via Post DB
306 333 *
307 - * @param array $columns
334 + * @param array $posts
335 + * @param WP_Query $wp_query
308 336 *
309 - * @return array
337 + * @return array|WP_Query
338 + * @since 4.4.8
339 + * @version 1.0.0
310 340 */
311 - public function columns_head( $columns ) {
312 - $pos = array_search( 'title', array_keys( $columns ) );
313 - $new_columns = array(
314 - 'instructor' => esc_html__( 'Author', 'learnpress' ),
315 - LP_QUIZ_CPT => esc_html__( 'Quiz', 'learnpress' ),
316 - 'type' => esc_html__( 'Type', 'learnpress' ),
317 - );
341 + public function posts_pre_query( $posts, $wp_query ) {
342 + try {
343 + if ( ! is_admin() ) {
344 + return $posts;
345 + }
318 346
319 - if ( false !== $pos && ! array_key_exists( LP_QUIZ_CPT, $columns ) ) {
320 - $columns = array_merge(
321 - array_slice( $columns, 0, $pos + 1 ),
322 - $new_columns,
323 - array_slice( $columns, $pos + 1 )
347 + // Check screen
348 + if ( ! function_exists( 'get_current_screen' ) ) {
349 + return $posts;
350 + }
351 +
352 + $curren_screen = get_current_screen();
353 + if ( ! $curren_screen || $curren_screen->id !== 'edit-' . LP_QUESTION_CPT ) {
354 + return $posts;
355 + }
356 +
357 + $post_type = $wp_query->get( 'post_type' );
358 +
359 + if ( empty( $post_type ) || $post_type !== LP_QUESTION_CPT ) {
360 + return $posts;
361 + }
362 +
363 + $posts_per_page = get_user_option( "edit_{$post_type}_per_page", get_current_user_id() );
364 + if ( empty( $posts_per_page ) ) {
365 + $posts_per_page = 20;
366 + }
367 +
368 + $paged = max( 1, get_query_var( 'paged' ) );
369 + $author = $wp_query->get( 'author' );
370 + $status = $wp_query->get( 'post_status' );
371 + $search = $wp_query->get( 's' );
372 + $month = $wp_query->get( 'm' );
373 + $orderby = LP_Request::get_param( 'orderby', '', 'key' );
374 + $order = LP_Request::get_param( 'order', '', 'key' );
375 + $quiz_id = LP_Request::get_param( 'filter_quiz' );
376 +
377 + $filter = new QuestionPostFilter();
378 + $filter->page = $paged;
379 + $filter->limit = $posts_per_page;
380 + $filter->only_fields = array_map(
381 + function ( $field ) {
382 + return "p.{$field}";
383 + },
384 + $filter->all_fields
324 385 );
325 - }
386 + $filter->field_count = 'p.ID';
387 + $post_db = PostDB::getInstance();
326 388
327 - $user = wp_get_current_user();
389 + if ( ! empty( $status ) ) {
390 + $filter->post_status = array( $status );
391 + }
328 392
329 - if ( in_array( LP_TEACHER_ROLE, $user->roles ) ) {
330 - unset( $columns['instructor'] );
331 - }
393 + if ( ! empty( $author ) ) {
394 + $filter->post_author = absint( $author );
395 + }
332 396
333 - if ( ! empty( $columns['author'] ) ) {
334 - unset( $columns['author'] );
335 - }
397 + if ( ! empty( $search ) ) {
398 + $filter->post_title = sanitize_text_field( wp_unslash( $search ) );
399 + }
336 400
337 - return $columns;
338 - }
401 + if ( ! empty( $month ) && is_numeric( $month ) && strlen( (string) $month ) === 6 ) {
402 + $filter->where[] = $post_db->wpdb->prepare(
403 + 'AND YEAR(p.post_date) = %d AND MONTH(p.post_date) = %d',
404 + substr( $month, 0, 4 ),
405 + substr( $month, 4, 2 )
406 + );
407 + }
339 408
340 - /**
341 - * Displaying the content of extra columns
342 - *
343 - * @param $name
344 - * @param $post_id
345 - */
346 - public function columns_content( $name, $post_id = 0 ) {
347 - switch ( $name ) {
348 - case 'instructor':
349 - $this->column_instructor( $post_id );
350 - break;
351 - case 'lp_quiz':
352 - $curd = new LP_Question_CURD();
353 - // get quiz
354 - $quiz = $curd->get_quiz( $post_id );
409 + // Exclude status auto-draft
410 + $filter->where[] = $post_db->wpdb->prepare( 'AND p.post_status != %s', PostModel::STATUS_AUTO_DRAFT );
355 411
356 - if ( $quiz ) {
357 - echo '<div><a href="' . esc_url_raw( add_query_arg( array( 'filter_quiz' => $quiz->ID ) ) ) . '">' . get_the_title( $quiz->ID ) . '</a>';
358 - echo '<div class="row-actions">';
359 - printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $quiz->ID ) ), esc_html__( 'Edit', 'learnpress' ) );
360 - echo '&nbsp;|&nbsp;';
361 - printf( '<a href="%s">%s</a>', get_the_permalink( $quiz->ID ), esc_html__( 'View', 'learnpress' ) );
362 - echo '</div></div>';
363 - } else {
364 - esc_html_e( 'Not assigned yet', 'learnpress' );
365 - }
366 - break;
367 - case 'type':
368 - echo learn_press_question_name_from_slug( get_post_meta( $post_id, '_lp_type', true ) );
369 - break;
412 + // Join quiz questions when needed
413 + if ( $quiz_id || $orderby === 'quiz-name' ) {
414 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id";
415 + $filter->join[] = "LEFT JOIN {$post_db->wpdb->posts} qz ON qz.ID = qq.quiz_id";
416 + }
417 +
418 + // Filter by quiz
419 + if ( $quiz_id ) {
420 + $filter->where[] = $post_db->wpdb->prepare( 'AND qz.ID = %d', $quiz_id );
421 + }
422 +
423 + // Filter unassigned
424 + if ( 'yes' === LP_Request::get_param( 'unassigned', '', 'key' ) ) {
425 + $filter->where[] = "AND p.ID NOT IN(
426 + SELECT qq_un.question_id
427 + FROM {$post_db->wpdb->learnpress_quiz_questions} qq_un
428 + )";
429 + }
430 +
431 + if ( $orderby === 'quiz-name' ) {
432 + $filter->order_by = 'qz.post_title';
433 + } elseif ( $orderby === 'title' ) {
434 + $filter->order_by = 'p.post_title';
435 + } elseif ( $orderby === 'author' ) {
436 + $filter->order_by = 'p.post_author';
437 + } elseif ( $orderby === 'date' ) {
438 + $filter->order_by = 'p.post_date';
439 + } else {
440 + $filter->order_by = 'p.menu_order';
441 + }
442 +
443 + $filter->order = strtolower( $order ) === 'asc' ? 'ASC' : 'DESC';
444 +
445 + // Get lp questions
446 + $total_rows = 0;
447 + $lp_questions = $post_db->get_posts( $filter, $total_rows );
448 +
449 + $wp_query->post_count = count( $lp_questions );
450 + $wp_query->found_posts = $total_rows;
451 + $posts = $lp_questions;
452 + } catch ( Throwable $e ) {
453 + LP_Debug::error_log( $e );
370 454 }
455 +
456 + return $posts;
371 457 }
372 458
373 - /**
374 - * Posts_join_paged.
375 - *
376 - * @param $join
377 - *
378 - * @return string
379 - */
380 - public function posts_join_paged( $join ): string {
459 + /*public function posts_join_paged( $join ): string {
381 460 if ( ! $this->is_page_list_posts_on_backend() ) {
382 461 return $join;
383 462 }
384 463
@@ -392,13 +471,8 @@
392 471
393 472 return $join;
394 473 }
395 474
396 - /**
397 - * @param $where
398 - *
399 - * @return mixed|string
400 - */
401 475 public function posts_where_paged( $where ) {
402 476 static $posts_where_paged = false;
403 477
404 478 if ( $posts_where_paged || ! $this->is_page_list_posts_on_backend() ) {
@@ -414,22 +488,17 @@
414 488
415 489 if ( 'yes' === LP_Request::get( 'unassigned' ) ) {
416 490 global $wpdb;
417 491 $where .= " AND {$wpdb->posts}.ID NOT IN(
418 - SELECT qq.question_id
419 - FROM {$wpdb->learnpress_quiz_questions} qq
420 - )
421 - ";
492 + SELECT qq.question_id
493 + FROM {$wpdb->learnpress_quiz_questions} qq
494 + )
495 + ";
422 496 }
423 497
424 498 return $where;
425 499 }
426 500
427 - /**
428 - * @param $order_by_statement
429 - *
430 - * @return string
431 - */
432 501 public function posts_orderby( $order_by_statement ): string {
433 502 if ( ! $this->is_page_list_posts_on_backend() ) {
434 503 return $order_by_statement;
435 504 }
@@ -448,21 +517,9 @@
448 517 }
449 518 }
450 519
451 520 return $order_by_statement;
452 - }
453 -
454 - /**
455 - * @param $columns
456 - *
457 - * @return mixed
458 - */
459 - public function sortable_columns( $columns ) {
460 - $columns['author'] = 'author';
461 - $columns[ LP_QUIZ_CPT ] = 'quiz-name';
462 -
463 - return $columns;
464 - }
521 + }*/
465 522
466 523 /**
467 524 * @return bool|int
468 525 */