PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.7
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 4.2.1 All 138 releases
learnpress / inc / Databases / PostDB.php

PostDB.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.7, at inc/Databases/PostDB.php

114 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Databases;
4
5 use Exception;
6 use LearnPress\Filters\PostFilter;
7 use LP_Helper;
8
9 /**
10 * Class PostDB
11 *
12 * @since 4.2.9.3
13 * @version 1.0.1
14 */
15 class PostDB extends DataBase {
16
17 private static $_instance;
18
19 protected function __construct() {
20 parent::__construct();
21 }
22
23 public static function getInstance() {
24 if ( is_null( self::$_instance ) ) {
25 self::$_instance = new self();
26 }
27
28 return self::$_instance;
29 }
30
31 /**
32 * Get posts by filter
33 *
34 * @param PostFilter|\LP_Post_Type_Filter $filter
35 *
36 * @return array|null|int|string
37 * @throws Exception
38 * @since 4.2.9.3
39 * @version 1.0.1
40 */
41 public function get_posts( $filter, int &$total_rows = 0 ) {
42 $filter->fields = array_merge( $filter->all_fields, $filter->fields );
43
44 if ( empty( $filter->collection ) ) {
45 $filter->collection = $this->tb_posts;
46 }
47
48 if ( empty( $filter->collection_alias ) ) {
49 $filter->collection_alias = 'p';
50 }
51
52 $ca = $filter->collection_alias;
53
54 // Where
55 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_type = %s", $filter->post_type );
56
57 // Find ID
58 if ( isset( $filter->ID ) ) {
59 $filter->where[] = $this->wpdb->prepare( "AND $ca.ID = %d", $filter->ID );
60 }
61
62 // Status
63 $filter->post_status = (array) $filter->post_status;
64 if ( ! empty( $filter->post_status ) ) {
65 $post_status_format = LP_Helper::db_format_array( $filter->post_status, '%s' );
66 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_status IN (" . $post_status_format . ')', $filter->post_status );
67 }
68
69 // Term ids
70 if ( ! empty( $filter->term_ids ) ) {
71 // Sanitize term ids
72 $filter->term_ids = array_map( 'absint', $filter->term_ids );
73 $term_ids_format = join( ',', $filter->term_ids );
74 $filter->join[] = "INNER JOIN $this->tb_term_relationships AS r_term_p ON $ca.ID = r_term_p.object_id";
75 $filter->join[] = "INNER JOIN $this->tb_term_taxonomy AS tx_p ON r_term_p.term_taxonomy_id = tx_p.term_taxonomy_id";
76 $filter->where[] = "AND r_term_p.term_taxonomy_id IN ($term_ids_format)";
77 $filter->where[] = $this->wpdb->prepare( 'AND tx_p.taxonomy = %s', $filter->taxonomy );
78 }
79
80 // Post ids
81 if ( ! empty( $filter->post_ids ) ) {
82 $post_ids = array_map( 'absint', $filter->post_ids );
83 $post_ids_format = join( ',', $post_ids );
84 $filter->where[] = "AND $ca.ID IN ($post_ids_format)";
85 }
86
87 // Title
88 if ( $filter->post_title ) {
89 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_title LIKE %s", '%' . $filter->post_title . '%' );
90 }
91
92 // Name(slug)
93 if ( $filter->post_name ) {
94 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_name = %s", $filter->post_name );
95 }
96
97 // Author
98 if ( isset( $filter->post_author ) ) {
99 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_author = %d", $filter->post_author );
100 }
101
102 // Authors
103 if ( ! empty( $filter->post_authors ) ) {
104 $post_authors = array_map( 'absint', $filter->post_authors );
105 $post_authors_format = join( ',', $post_authors );
106 $filter->where[] = "AND $ca.post_author IN ($post_authors_format)";
107 }
108
109 $filter = apply_filters( 'lp/post/query/filter', $filter );
110
111 return $this->execute( $filter, $total_rows );
112 }
113 }
114