| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Course_JSON_DB |
| 4 |
* |
| 5 |
* @since 4.2.6.9 |
| 6 |
* @version 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit(); |
| 10 |
|
| 11 |
class LP_Course_JSON_DB extends LP_Database { |
| 12 |
private static $_instance; |
| 13 |
|
| 14 |
protected function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
} |
| 17 |
|
| 18 |
public static function getInstance() { |
| 19 |
if ( is_null( self::$_instance ) ) { |
| 20 |
self::$_instance = new self(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$_instance; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get Courses |
| 28 |
* |
| 29 |
* @param LP_Course_JSON_Filter $filter |
| 30 |
* @param int $total_rows return total_rows |
| 31 |
* |
| 32 |
* @return array|int|string|null |
| 33 |
* @throws Exception |
| 34 |
* @version 1.0.0 |
| 35 |
* @since 4.2.6.9 |
| 36 |
*/ |
| 37 |
public function get_courses( LP_Course_JSON_Filter $filter, int &$total_rows = 0 ) { |
| 38 |
$filter->fields = array_merge( $filter->all_fields, $filter->fields ); |
| 39 |
|
| 40 |
if ( empty( $filter->collection ) ) { |
| 41 |
$filter->collection = $this->tb_lp_courses; |
| 42 |
} |
| 43 |
|
| 44 |
if ( empty( $filter->collection_alias ) ) { |
| 45 |
$filter->collection_alias = 'c'; |
| 46 |
} |
| 47 |
|
| 48 |
$ca = $filter->collection_alias; |
| 49 |
|
| 50 |
// Find ID |
| 51 |
if ( ! empty( $filter->ID ) ) { |
| 52 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.ID = %d", $filter->ID ); |
| 53 |
} |
| 54 |
|
| 55 |
// Status |
| 56 |
$filter->post_status = (array) $filter->post_status; |
| 57 |
if ( ! empty( $filter->post_status ) ) { |
| 58 |
$post_status_format = LP_Helper::db_format_array( $filter->post_status, '%s' ); |
| 59 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.post_status IN (" . $post_status_format . ')', $filter->post_status ); |
| 60 |
} |
| 61 |
|
| 62 |
// Term ids |
| 63 |
if ( ! empty( $filter->term_ids ) ) { |
| 64 |
// Sanitize term ids |
| 65 |
$filter->term_ids = array_map( 'absint', $filter->term_ids ); |
| 66 |
$term_ids_format = join( ',', $filter->term_ids ); |
| 67 |
$filter->join[] = "INNER JOIN $this->tb_term_relationships AS r_term_p ON $ca.ID = r_term_p.object_id"; |
| 68 |
$filter->join[] = "INNER JOIN $this->tb_term_taxonomy AS tx_p ON r_term_p.term_taxonomy_id = tx_p.term_taxonomy_id"; |
| 69 |
$filter->where[] = "AND r_term_p.term_taxonomy_id IN ($term_ids_format)"; |
| 70 |
$filter->where[] = $this->wpdb->prepare( 'AND tx_p.taxonomy = %s', $filter->taxonomy ); |
| 71 |
} |
| 72 |
|
| 73 |
// Course ids |
| 74 |
if ( ! empty( $filter->post_ids ) ) { |
| 75 |
$post_ids = array_map( 'absint', $filter->post_ids ); |
| 76 |
$post_ids_format = join( ',', $post_ids ); |
| 77 |
$filter->where[] = "AND $ca.ID IN ($post_ids_format)"; |
| 78 |
} |
| 79 |
|
| 80 |
// Title |
| 81 |
if ( $filter->post_title ) { |
| 82 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.post_title LIKE %s", '%' . $filter->post_title . '%' ); |
| 83 |
} |
| 84 |
|
| 85 |
// Name(slug) |
| 86 |
if ( $filter->post_name ) { |
| 87 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.post_name = %s", $filter->post_name ); |
| 88 |
} |
| 89 |
|
| 90 |
// Author |
| 91 |
if ( $filter->post_author ) { |
| 92 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.post_author = %d", $filter->post_author ); |
| 93 |
} |
| 94 |
|
| 95 |
// Authors |
| 96 |
if ( ! empty( $filter->post_authors ) ) { |
| 97 |
$post_authors_format = LP_Helper::db_format_array( $filter->post_authors, '%d' ); |
| 98 |
$filter->where[] = $this->wpdb->prepare( "AND $ca.ID IN (" . $post_authors_format . ')', $filter->post_authors ); |
| 99 |
} |
| 100 |
|
| 101 |
$filter = apply_filters( 'lp/courses-json/query/filter', $filter ); |
| 102 |
|
| 103 |
return $this->execute( $filter, $total_rows ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Insert data |
| 108 |
* |
| 109 |
* @param array $data |
| 110 |
* |
| 111 |
* @return int |
| 112 |
* @throws Exception |
| 113 |
* @version 1.0.1 |
| 114 |
* @since 4.2.6.9 |
| 115 |
*/ |
| 116 |
public function insert_data( array $data ): int { |
| 117 |
$filter = new LP_Course_JSON_Filter(); |
| 118 |
foreach ( $data as $col_name => $value ) { |
| 119 |
if ( ! in_array( $col_name, $filter->all_fields ) ) { |
| 120 |
unset( $data[ $col_name ] ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$this->wpdb->insert( $this->tb_lp_courses, $data ); |
| 125 |
|
| 126 |
$this->check_execute_has_error(); |
| 127 |
|
| 128 |
return $this->wpdb->insert_id; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Update data |
| 133 |
* |
| 134 |
* @param array $data |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
* |
| 138 |
* @throws Exception |
| 139 |
* @since 4.2.6.9 |
| 140 |
* @version 1.0.0 |
| 141 |
*/ |
| 142 |
public function update_data( array $data ): bool { |
| 143 |
if ( empty( $data['ID'] ) ) { |
| 144 |
throw new Exception( __( 'Invalid ID!', 'learnpress' ) . ' | ' . __FUNCTION__ ); |
| 145 |
} |
| 146 |
|
| 147 |
$filter = new LP_Course_JSON_Filter(); |
| 148 |
$filter->collection = $this->tb_lp_courses; |
| 149 |
foreach ( $data as $col_name => $value ) { |
| 150 |
if ( ! in_array( $col_name, $filter->all_fields ) ) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
if ( is_null( $value ) ) { |
| 155 |
$filter->set[] = $col_name . ' = null'; |
| 156 |
} else { |
| 157 |
$filter->set[] = $this->wpdb->prepare( $col_name . ' = %s', $value ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$filter->where[] = $this->wpdb->prepare( 'AND ID = %d', $data['ID'] ); |
| 162 |
$this->update_execute( $filter ); |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
} |
| 167 |
|