| @@ -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,126 @@ | ||
| 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 ) | |
| 324 | - ); | |
| 325 | - } | |
| 347 | + // Check screen | |
| 348 | + $curren_screen = get_current_screen(); | |
| 349 | + if ( ! $curren_screen || $curren_screen->id !== 'edit-' . LP_QUESTION_CPT ) { | |
| 350 | + return $posts; | |
| 351 | + } | |
| 326 | 352 | |
| 327 | - $user = wp_get_current_user(); | |
| 353 | + $post_type = $wp_query->get( 'post_type' ); | |
| 328 | 354 | |
| 329 | - if ( in_array( LP_TEACHER_ROLE, $user->roles ) ) { | |
| 330 | - unset( $columns['instructor'] ); | |
| 331 | - } | |
| 355 | + if ( empty( $post_type ) || $post_type !== LP_QUESTION_CPT ) { | |
| 356 | + return $posts; | |
| 357 | + } | |
| 332 | 358 | |
| 333 | - if ( ! empty( $columns['author'] ) ) { | |
| 334 | - unset( $columns['author'] ); | |
| 335 | - } | |
| 359 | + $posts_per_page = get_user_option( "edit_{$post_type}_per_page", get_current_user_id() ); | |
| 360 | + if ( empty( $posts_per_page ) ) { | |
| 361 | + $posts_per_page = 20; | |
| 362 | + } | |
| 336 | 363 | |
| 337 | - return $columns; | |
| 338 | - } | |
| 364 | + $paged = max( 1, get_query_var( 'paged' ) ); | |
| 365 | + $author = $wp_query->get( 'author' ); | |
| 366 | + $status = $wp_query->get( 'post_status' ); | |
| 367 | + $search = $wp_query->get( 's' ); | |
| 368 | + $month = $wp_query->get( 'm' ); | |
| 369 | + $orderby = LP_Request::get_param( 'orderby', '', 'key' ); | |
| 370 | + $order = LP_Request::get_param( 'order', '', 'key' ); | |
| 371 | + $quiz_id = LP_Request::get_param( 'filter_quiz' ); | |
| 339 | 372 | |
| 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 ); | |
| 373 | + $filter = new QuestionPostFilter(); | |
| 374 | + $filter->page = $paged; | |
| 375 | + $filter->limit = $posts_per_page; | |
| 376 | + $filter->only_fields = [ 'p.ID', 'p.post_title', 'p.post_author', 'p.post_date', 'p.post_date_gmt' ]; | |
| 377 | + $post_db = PostDB::getInstance(); | |
| 355 | 378 | |
| 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 ' | '; | |
| 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; | |
| 379 | + if ( ! empty( $status ) ) { | |
| 380 | + $filter->post_status = array( $status ); | |
| 381 | + } | |
| 382 | + | |
| 383 | + if ( ! empty( $author ) ) { | |
| 384 | + $filter->post_author = absint( $author ); | |
| 385 | + } | |
| 386 | + | |
| 387 | + if ( ! empty( $search ) ) { | |
| 388 | + $filter->post_title = sanitize_text_field( wp_unslash( $search ) ); | |
| 389 | + } | |
| 390 | + | |
| 391 | + if ( ! empty( $month ) && is_numeric( $month ) && strlen( (string) $month ) === 6 ) { | |
| 392 | + $filter->where[] = $post_db->wpdb->prepare( | |
| 393 | + 'AND YEAR(p.post_date) = %d AND MONTH(p.post_date) = %d', | |
| 394 | + substr( $month, 0, 4 ), | |
| 395 | + substr( $month, 4, 2 ) | |
| 396 | + ); | |
| 397 | + } | |
| 398 | + | |
| 399 | + // Exclude status auto-draft | |
| 400 | + $filter->where[] = $post_db->wpdb->prepare( 'AND p.post_status != %s', PostModel::STATUS_AUTO_DRAFT ); | |
| 401 | + | |
| 402 | + // Join quiz questions when needed | |
| 403 | + if ( $quiz_id || $orderby === 'quiz-name' ) { | |
| 404 | + $filter->join[] = "LEFT JOIN {$post_db->wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id"; | |
| 405 | + $filter->join[] = "LEFT JOIN {$post_db->wpdb->posts} qz ON qz.ID = qq.quiz_id"; | |
| 406 | + } | |
| 407 | + | |
| 408 | + // Filter by quiz | |
| 409 | + if ( $quiz_id ) { | |
| 410 | + $filter->where[] = $post_db->wpdb->prepare( 'AND qz.ID = %d', $quiz_id ); | |
| 411 | + } | |
| 412 | + | |
| 413 | + // Filter unassigned | |
| 414 | + if ( 'yes' === LP_Request::get_param( 'unassigned', '', 'key' ) ) { | |
| 415 | + $filter->where[] = "AND p.ID NOT IN( | |
| 416 | + SELECT qq_un.question_id | |
| 417 | + FROM {$post_db->wpdb->learnpress_quiz_questions} qq_un | |
| 418 | + )"; | |
| 419 | + } | |
| 420 | + | |
| 421 | + if ( $orderby === 'quiz-name' ) { | |
| 422 | + $filter->order_by = 'qz.post_title'; | |
| 423 | + } elseif ( $orderby === 'title' ) { | |
| 424 | + $filter->order_by = 'p.post_title'; | |
| 425 | + } elseif ( $orderby === 'author' ) { | |
| 426 | + $filter->order_by = 'p.post_author'; | |
| 427 | + } elseif ( $orderby === 'date' ) { | |
| 428 | + $filter->order_by = 'p.post_date'; | |
| 429 | + } else { | |
| 430 | + $filter->order_by = 'p.menu_order'; | |
| 431 | + } | |
| 432 | + | |
| 433 | + $filter->order = strtolower( $order ) === 'asc' ? 'ASC' : 'DESC'; | |
| 434 | + | |
| 435 | + // Get lp questions | |
| 436 | + $total_rows = 0; | |
| 437 | + $lp_questions = $post_db->get_posts( $filter, $total_rows ); | |
| 438 | + | |
| 439 | + $wp_query->post_count = count( $lp_questions ); | |
| 440 | + $wp_query->found_posts = $total_rows; | |
| 441 | + $posts = $lp_questions; | |
| 442 | + } catch ( Throwable $e ) { | |
| 443 | + LP_Debug::error_log( $e ); | |
| 370 | 444 | } |
| 445 | + | |
| 446 | + return $posts; | |
| 371 | 447 | } |
| 372 | 448 | |
| 373 | - /** | |
| 374 | - * Posts_join_paged. | |
| 375 | - * | |
| 376 | - * @param $join | |
| 377 | - * | |
| 378 | - * @return string | |
| 379 | - */ | |
| 380 | - public function posts_join_paged( $join ): string { | |
| 449 | + /*public function posts_join_paged( $join ): string { | |
| 381 | 450 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| 382 | 451 | return $join; |
| 383 | 452 | } |
| 384 | 453 | |
| @@ -392,13 +461,8 @@ | ||
| 392 | 461 | |
| 393 | 462 | return $join; |
| 394 | 463 | } |
| 395 | 464 | |
| 396 | - /** | |
| 397 | - * @param $where | |
| 398 | - * | |
| 399 | - * @return mixed|string | |
| 400 | - */ | |
| 401 | 465 | public function posts_where_paged( $where ) { |
| 402 | 466 | static $posts_where_paged = false; |
| 403 | 467 | |
| 404 | 468 | if ( $posts_where_paged || ! $this->is_page_list_posts_on_backend() ) { |
| @@ -414,22 +478,17 @@ | ||
| 414 | 478 | |
| 415 | 479 | if ( 'yes' === LP_Request::get( 'unassigned' ) ) { |
| 416 | 480 | global $wpdb; |
| 417 | 481 | $where .= " AND {$wpdb->posts}.ID NOT IN( |
| 418 | - SELECT qq.question_id | |
| 419 | - FROM {$wpdb->learnpress_quiz_questions} qq | |
| 420 | - ) | |
| 421 | - "; | |
| 482 | + SELECT qq.question_id | |
| 483 | + FROM {$wpdb->learnpress_quiz_questions} qq | |
| 484 | + ) | |
| 485 | + "; | |
| 422 | 486 | } |
| 423 | 487 | |
| 424 | 488 | return $where; |
| 425 | 489 | } |
| 426 | 490 | |
| 427 | - /** | |
| 428 | - * @param $order_by_statement | |
| 429 | - * | |
| 430 | - * @return string | |
| 431 | - */ | |
| 432 | 491 | public function posts_orderby( $order_by_statement ): string { |
| 433 | 492 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| 434 | 493 | return $order_by_statement; |
| 435 | 494 | } |
| @@ -448,21 +507,9 @@ | ||
| 448 | 507 | } |
| 449 | 508 | } |
| 450 | 509 | |
| 451 | 510 | 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 | - } | |
| 511 | + }*/ | |
| 465 | 512 | |
| 466 | 513 | /** |
| 467 | 514 | * @return bool|int |
| 468 | 515 | */ |