| @@ -3,13 +3,16 @@ | ||
| 3 | 3 | * Class LP_Course_Post_Type |
| 4 | 4 | * |
| 5 | 5 | * @author ThimPress |
| 6 | 6 | * @package LearnPress/Classes |
| 7 | - * @version 3.0.2 | |
| 7 | + * @version 3.0.3 | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | +use LearnPress\Databases\PostDB; | |
| 11 | +use LearnPress\Filters\CoursePostFilter; | |
| 10 | 12 | use LearnPress\Models\CourseModel; |
| 11 | 13 | use LearnPress\Models\CoursePostModel; |
| 14 | +use LearnPress\Models\PostModel; | |
| 12 | 15 | use LearnPress\Models\WPTables\CoursesTable; |
| 13 | 16 | |
| 14 | 17 | defined( 'ABSPATH' ) || exit(); |
| 15 | 18 | |
| @@ -32,10 +35,11 @@ | ||
| 32 | 35 | public function __construct() { |
| 33 | 36 | parent::__construct(); |
| 34 | 37 | |
| 35 | 38 | add_action( 'init', array( $this, 'register_taxonomy' ) ); |
| 36 | - add_filter( 'posts_where_paged', array( $this, '_posts_where_paged_course_items' ), 10 ); | |
| 37 | - add_filter( 'posts_join_paged', array( $this, '_posts_join_paged_course_items' ), 10 ); | |
| 39 | + add_action( 'posts_pre_query', array( $this, 'posts_pre_query' ), 999, 2 ); | |
| 40 | + // add_filter( 'posts_where_paged', array( $this, '_posts_where_paged_course_items' ), 10 ); | |
| 41 | + // add_filter( 'posts_join_paged', array( $this, '_posts_join_paged_course_items' ), 10 ); | |
| 38 | 42 | } |
| 39 | 43 | |
| 40 | 44 | /** |
| 41 | 45 | * Register course post type. |
| @@ -206,13 +210,129 @@ | ||
| 206 | 210 | $course->delete_relate_data_when_delete_course(); |
| 207 | 211 | } |
| 208 | 212 | |
| 209 | 213 | /** |
| 210 | - * @param string $fields | |
| 214 | + * Query lp courses in admin via Post DB | |
| 211 | 215 | * |
| 212 | - * @return string | |
| 216 | + * @param array $posts | |
| 217 | + * @param WP_Query $wp_query | |
| 218 | + * | |
| 219 | + * @return array|WP_Query | |
| 220 | + * @since 4.4.8 | |
| 221 | + * @version 1.0.0 | |
| 213 | 222 | */ |
| 214 | - public function posts_fields( $fields ): string { | |
| 223 | + public function posts_pre_query( $posts, $wp_query ) { | |
| 224 | + try { | |
| 225 | + if ( ! is_admin() ) { | |
| 226 | + return $posts; | |
| 227 | + } | |
| 228 | + | |
| 229 | + // Check screen | |
| 230 | + $curren_screen = get_current_screen(); | |
| 231 | + if ( ! $curren_screen || $curren_screen->id !== 'edit-' . LP_COURSE_CPT ) { | |
| 232 | + return $posts; | |
| 233 | + } | |
| 234 | + | |
| 235 | + $post_type = $wp_query->get( 'post_type' ); | |
| 236 | + | |
| 237 | + if ( empty( $post_type ) || $post_type !== LP_COURSE_CPT ) { | |
| 238 | + return $posts; | |
| 239 | + } | |
| 240 | + | |
| 241 | + // Convert params from WP_Query to CoursePostFilter | |
| 242 | + $posts_per_page = get_user_option( "edit_{$post_type}_per_page", get_current_user_id() ); | |
| 243 | + if ( empty( $posts_per_page ) ) { | |
| 244 | + $posts_per_page = 20; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $paged = max( 1, get_query_var( 'paged' ) ); | |
| 248 | + $author = $wp_query->get( 'author' ); | |
| 249 | + $status = $wp_query->get( 'post_status' ); | |
| 250 | + $search = $wp_query->get( 's' ); | |
| 251 | + $month = $wp_query->get( 'm' ); | |
| 252 | + $orderby = LP_Request::get_param( 'orderby', '', 'key' ); | |
| 253 | + $order = LP_Request::get_param( 'order', '', 'key' ); | |
| 254 | + $filter_price = LP_Helper::sanitize_params_submitted( $_REQUEST['filter_price'] ?? '' ); | |
| 255 | + | |
| 256 | + $filter = new CoursePostFilter(); | |
| 257 | + $filter->page = $paged; | |
| 258 | + $filter->limit = $posts_per_page; | |
| 259 | + $post_db = PostDB::getInstance(); | |
| 260 | + | |
| 261 | + if ( ! empty( $status ) ) { | |
| 262 | + $filter->post_status = array( $status ); | |
| 263 | + } | |
| 264 | + | |
| 265 | + if ( ! empty( $author ) ) { | |
| 266 | + $filter->post_author = absint( $author ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + if ( ! empty( $search ) ) { | |
| 270 | + $filter->post_title = sanitize_text_field( wp_unslash( $search ) ); | |
| 271 | + } | |
| 272 | + | |
| 273 | + if ( ! empty( $month ) && is_numeric( $month ) && strlen( (string) $month ) === 6 ) { | |
| 274 | + $filter->where[] = $post_db->wpdb->prepare( | |
| 275 | + 'AND YEAR(p.post_date) = %d AND MONTH(p.post_date) = %d', | |
| 276 | + substr( $month, 0, 4 ), | |
| 277 | + substr( $month, 4, 2 ) | |
| 278 | + ); | |
| 279 | + } | |
| 280 | + | |
| 281 | + /*$has_price_filter = false; | |
| 282 | + if ( array_key_exists( 'filter_price', $_REQUEST ) && $filter_price !== '' ) { | |
| 283 | + $filter_price = floatval( $filter_price ); | |
| 284 | + $has_price_filter = true; | |
| 285 | + } | |
| 286 | + | |
| 287 | + if ( $orderby === 'price' || $has_price_filter ) { | |
| 288 | + $filter->join[] = "LEFT JOIN {$post_db->wpdb->postmeta} pm_price ON pm_price.post_id = p.ID AND pm_price.meta_key = '_lp_price'"; | |
| 289 | + } | |
| 290 | + | |
| 291 | + if ( $has_price_filter ) { | |
| 292 | + if ( $filter_price == 0 ) { | |
| 293 | + $filter->where[] = 'AND ( pm_price.meta_value IS NULL || pm_price.meta_value = 0 )'; | |
| 294 | + } else { | |
| 295 | + $filter->where[] = $post_db->wpdb->prepare( ' AND ( pm_price.meta_value = %s )', $filter_price ); | |
| 296 | + } | |
| 297 | + }*/ | |
| 298 | + | |
| 299 | + // Exclude status auto-draft | |
| 300 | + $filter->where[] = $post_db->wpdb->prepare( 'AND p.post_status != %s', PostModel::STATUS_AUTO_DRAFT ); | |
| 301 | + | |
| 302 | + if ( $orderby === 'price' ) { | |
| 303 | + $filter->join[] = "LEFT JOIN {$post_db->wpdb->postmeta} pm_price | |
| 304 | + ON pm_price.post_id = p.ID | |
| 305 | + AND pm_price.meta_key = '_lp_price'"; | |
| 306 | + $filter->order_by = 'CAST(pm_price.meta_value AS UNSIGNED)'; | |
| 307 | + } elseif ( $orderby === 'title' ) { | |
| 308 | + $filter->order_by = 'p.post_title'; | |
| 309 | + } elseif ( $orderby === 'author' ) { | |
| 310 | + $filter->order_by = 'p.post_author'; | |
| 311 | + } elseif ( $orderby === 'date' ) { | |
| 312 | + $filter->order_by = 'p.post_date'; | |
| 313 | + } else { | |
| 314 | + $filter->order_by = 'p.menu_order'; | |
| 315 | + } | |
| 316 | + | |
| 317 | + $filter->order = strtolower( $order ) === 'asc' ? 'ASC' : 'DESC'; | |
| 318 | + | |
| 319 | + // Get lp courses | |
| 320 | + $total_rows = 0; | |
| 321 | + $lp_courses = $post_db->get_posts( $filter, $total_rows ); | |
| 322 | + | |
| 323 | + $wp_query->post_count = count( $lp_courses ); | |
| 324 | + $wp_query->found_posts = $total_rows; | |
| 325 | + $wp_query->max_num_pages = (int) ceil( $total_rows / $posts_per_page ); | |
| 326 | + $posts = $lp_courses; | |
| 327 | + } catch ( Throwable $e ) { | |
| 328 | + LP_Debug::error_log( $e ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + return $posts; | |
| 332 | + } | |
| 333 | + | |
| 334 | + /*public function posts_fields( $fields ): string { | |
| 215 | 335 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| 216 | 336 | return $fields; |
| 217 | 337 | } |
| 218 | 338 | |
| @@ -248,13 +368,8 @@ | ||
| 248 | 368 | |
| 249 | 369 | return $where; |
| 250 | 370 | } |
| 251 | 371 | |
| 252 | - /** | |
| 253 | - * @param $join | |
| 254 | - * | |
| 255 | - * @return string | |
| 256 | - */ | |
| 257 | 372 | public function posts_join_paged( $join ) { |
| 258 | 373 | global $wpdb; |
| 259 | 374 | |
| 260 | 375 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| @@ -269,13 +384,8 @@ | ||
| 269 | 384 | |
| 270 | 385 | return $join; |
| 271 | 386 | } |
| 272 | 387 | |
| 273 | - /** | |
| 274 | - * @param $where | |
| 275 | - * | |
| 276 | - * @return mixed|string | |
| 277 | - */ | |
| 278 | 388 | public function posts_where_paged( $where ) { |
| 279 | 389 | global $wpdb; |
| 280 | 390 | |
| 281 | 391 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| @@ -291,29 +401,11 @@ | ||
| 291 | 401 | $where .= $wpdb->prepare( ' AND ( pm_price.meta_value = %s )', $filter_price ); |
| 292 | 402 | } |
| 293 | 403 | } |
| 294 | 404 | |
| 295 | - /*$not_in = $wpdb->prepare( | |
| 296 | - " | |
| 297 | - SELECT ID | |
| 298 | - FROM {$wpdb->posts} p | |
| 299 | - INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key = %s | |
| 300 | - WHERE pm.meta_value = %s | |
| 301 | - ", | |
| 302 | - '_lp_preview_course', | |
| 303 | - 'yes' | |
| 304 | - ); | |
| 305 | - | |
| 306 | - $where .= " AND {$wpdb->posts}.ID NOT IN( {$not_in} )";*/ | |
| 307 | - | |
| 308 | 405 | return $where; |
| 309 | 406 | } |
| 310 | 407 | |
| 311 | - /** | |
| 312 | - * @param $orderby | |
| 313 | - * | |
| 314 | - * @return string | |
| 315 | - */ | |
| 316 | 408 | public function posts_orderby( $orderby ) { |
| 317 | 409 | if ( ! $this->is_page_list_posts_on_backend() ) { |
| 318 | 410 | return $orderby; |
| 319 | 411 | } |
| @@ -324,9 +416,9 @@ | ||
| 324 | 416 | $orderby = "CAST(pm_price.meta_value AS UNSIGNED) {$order}"; |
| 325 | 417 | } |
| 326 | 418 | |
| 327 | 419 | return $orderby; |
| 328 | - } | |
| 420 | + }*/ | |
| 329 | 421 | |
| 330 | 422 | /** |
| 331 | 423 | * Save course post |
| 332 | 424 | * |