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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / models / BaseModel.php
tutor / models Last commit date
BaseModel.php 9 months ago BillingModel.php 1 year ago CartItemModel.php 9 months ago CartModel.php 9 months ago CouponModel.php 11 months ago CourseModel.php 9 months ago LessonModel.php 9 months ago OrderActivitiesModel.php 1 year ago OrderItemMetaModel.php 9 months ago OrderItemModel.php 9 months ago OrderMetaModel.php 1 year ago OrderModel.php 9 months ago QuizModel.php 9 months ago UserModel.php 1 year ago WithdrawModel.php 1 year ago
BaseModel.php
361 lines
1 <?php
2 /**
3 * Base Model
4 *
5 * @package Tutor\Models
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.7.0
9 */
10
11 namespace Tutor\Models;
12
13 use Tutor\Helpers\QueryHelper;
14
15 /**
16 * BaseModel Class.
17 *
18 * @since 3.7.0
19 */
20 abstract class BaseModel {
21 /**
22 * WP db instance
23 *
24 * @var \wpdb
25 */
26 protected $db;
27
28 /**
29 * Table name
30 *
31 * @var string
32 */
33 protected $table_name;
34
35 /**
36 * Primary key of the table
37 *
38 * @var string
39 */
40 protected $primary_key = 'id';
41
42 /**
43 * Guard which fields can not be filled
44 *
45 * @var array
46 */
47 protected $guarded = array( 'id' );
48
49 /**
50 * Set which fields can be filled
51 *
52 * @var string|array star(*) means all are fillable.
53 */
54 protected $fillable = '*';
55
56 /**
57 * Constructor
58 */
59 public function __construct() {
60 global $wpdb;
61 $this->db = $wpdb;
62
63 if ( $this->is_set_table() && ! $this->has_table_prefix() ) {
64 $this->table_name = $this->db->prefix . $this->table_name;
65 }
66
67 }
68
69 /**
70 * Check model is associated with a db table.
71 *
72 * @return boolean
73 */
74 public function is_set_table() {
75 return isset( $this->table_name );
76 }
77
78 /**
79 * Check table as table prefix.
80 *
81 * @return boolean
82 */
83 public function has_table_prefix() {
84 return strpos( $this->table_name, $this->db->prefix ) === 0;
85 }
86
87 /**
88 * Get table name.
89 *
90 * @return string
91 */
92 public function get_table_name() {
93 return $this->table_name;
94 }
95
96 /**
97 * Get table fields
98 *
99 * @return array
100 */
101 public function get_fields() {
102 $fields = array();
103 if ( isset( $this->table_name ) ) {
104 foreach ( $this->db->get_col( 'DESC ' . $this->table_name, 0 ) as $column_name ) {
105 $fields[] = $column_name;
106 }
107 }
108
109 return $fields;
110 }
111
112 /**
113 * Filter inputs.
114 *
115 * @param array $inputs user inputs.
116 *
117 * @return array
118 */
119 private function filter_inputs( $inputs ) {
120 $table_fields = $this->get_fields();
121
122 // Remove unwanted user input key.
123 $inputs = array_intersect_key( $inputs, array_flip( $table_fields ) );
124
125 // Handle fillable fields.
126 if ( is_array( $this->fillable ) ) {
127 $inputs = array_intersect_key( $inputs, array_flip( $this->fillable ) );
128 }
129
130 // Handle guarded fields.
131 if ( is_array( $this->guarded ) ) {
132 $inputs = array_diff_key( $inputs, array_flip( $this->guarded ) );
133 }
134
135 return $inputs;
136 }
137
138 /**
139 * Get a record.
140 *
141 * @param array $where where clause.
142 *
143 * @return object|false object or false when not found.
144 */
145 public function get_row( $where ) {
146 $result = false;
147 if ( $this->is_set_table() ) {
148 $result = QueryHelper::get_row( $this->table_name, $where, $this->primary_key );
149 }
150
151 return $result;
152 }
153
154 /**
155 * Get all record from table.
156 *
157 * @param array $where where clause.
158 * @param string $order_by order by.
159 * @param string $order order.
160 * @param int $limit limit.
161 *
162 * @return array|false list of rows or false when table not set.
163 */
164 public function get_all( $where, $order_by = 'id', $order = 'DESC', $limit = -1 ) {
165 $results = false;
166 $order_by_column = $order_by === $this->primary_key ? $this->primary_key : $order_by;
167 if ( $this->is_set_table() ) {
168 $results = QueryHelper::get_all( $this->table_name, $where, $order_by_column, $limit, $order );
169 }
170
171 return $results;
172 }
173
174 /**
175 * Get column values
176 *
177 * @param string $column_name column name.
178 * @param array $where where.
179 *
180 * @return array
181 */
182 public function get_col( $column_name, $where ) {
183 $where_clause = count( $where ) ? 'WHERE ' . QueryHelper::prepare_where_clause( $where ) : '';
184 $query = $this->db->prepare( "SELECT {$column_name} FROM {$this->table_name} {$where_clause}" );
185
186 return $this->db->get_col( $query );
187 }
188
189 /**
190 * Get table record with pagination.
191 *
192 * @param integer $per_page per page record.
193 * @param integer $page current page number.
194 * @param array $args options like alias, select, where, search, joins, groupby, having, orderby.
195 *
196 * @return object|false object contains total_record, total_page, per_page, current_page, data.
197 */
198 public function paginate( $per_page = 10, $page = 1, $args = array() ) {
199 if ( ! $this->is_set_table() ) {
200 return false;
201 }
202
203 $alias = $args['alias'] ?? 'main';
204 $table_with_alias = "{$this->table_name} AS {$alias}";
205
206 $select_clause = '*';
207 $where_clause = '';
208
209 if ( isset( $args['select'] ) ) {
210 $select = $args['select'];
211 if ( is_array( $select ) && count( $select ) ) {
212 $select_clause = implode( ',', $args['select'] );
213 }
214 }
215
216 $join_clause = '';
217 if ( isset( $args['joins'] ) && is_array( $args['joins'] ) ) {
218 foreach ( $args['joins'] as $join ) {
219 $type = isset( $join['type'] ) ? strtoupper( $join['type'] ) : 'LEFT';
220 $table = $join['table'];
221 $on = $join['on'];
222 $join_clause .= " {$type} JOIN {$table} ON {$on} ";
223 }
224 }
225
226 $groupby_clause = '';
227 if ( isset( $args['groupby'] ) && $args['groupby'] ) {
228 $groupby_clause = 'GROUP BY ' . $args['groupby'];
229 }
230
231 $having_clause = '';
232 if ( isset( $args['having'] ) && $args['having'] ) {
233 $having_clause = 'HAVING ' . $args['having'];
234 }
235
236 if ( isset( $args['where'] ) && is_array( $args['where'] ) ) {
237 $where = $args['where'];
238 $where_clause = count( $where ) ? 'WHERE ' . QueryHelper::prepare_where_clause( $where ) : '';
239 }
240
241 if ( isset( $args['search'] ) && is_array( $args['search'] ) ) {
242 if ( count( $args['search'] ) ) {
243 $where_clause .= ( empty( $where_clause ) ? 'WHERE ' : ' AND ' ) . QueryHelper::prepare_like_clause( $args['search'], 'AND' );
244 }
245 }
246
247 $orderby = $args['orderby'] ?? $this->primary_key;
248 $order = 'DESC' === strtoupper( $args['order'] ?? 'DESC' ) ? 'DESC' : 'ASC';
249 $orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" );
250
251 $page = max( $page, 1 );
252 $offset = ( $page - 1 ) * $per_page;
253 if ( $offset < 0 ) {
254 $offset = 0;
255 }
256
257 $sql_query = $this->db->prepare(
258 "SELECT SQL_CALC_FOUND_ROWS {$select_clause}
259 FROM {$table_with_alias} {$join_clause} {$where_clause} {$groupby_clause} {$having_clause}
260 ORDER BY {$orderby_sql}
261 LIMIT %d, %d",
262 $offset,
263 $per_page
264 );
265
266 $rows = $this->db->get_results( $sql_query );
267
268 $has_records = is_array( $rows ) && count( $rows );
269 $total_record = (int) $has_records ? $this->db->get_var( 'SELECT FOUND_ROWS()' ) : 0;
270 $total_page = (int) ceil( $total_record / $per_page );
271 $pagination = array(
272 'total_record' => (int) $total_record,
273 'per_page' => $per_page,
274 'current_page' => $page,
275 'total_page' => $total_page,
276 'data' => $rows,
277 );
278
279 return (object) $pagination;
280
281 }
282
283 /**
284 * Count record.
285 *
286 * @param array $where where clause.
287 *
288 * @return int
289 */
290 public function count( $where ) {
291 if ( ! $this->is_set_table() ) {
292 return 0;
293 }
294
295 return QueryHelper::get_count( $this->table_name, $where, array(), $this->primary_key );
296 }
297
298 /**
299 * Create record
300 *
301 * @since 3.0.0
302 *
303 * @param array $data data.
304 *
305 * @return int|false id or false when creation failed.
306 */
307 public function create( $data ) {
308 $insert = false;
309 $filtered = $this->filter_inputs( $data );
310
311 if ( $this->is_set_table() && count( $filtered ) ) {
312 $insert = $this->db->insert( $this->table_name, $filtered );
313 if ( $insert ) {
314 return $this->db->insert_id;
315 }
316 }
317
318 return $insert;
319 }
320
321 /**
322 * Update record
323 *
324 * @since 3.0.0
325 *
326 * @param int $id id.
327 * @param array $data data.
328 *
329 * @return int|false id or false when failed.
330 */
331 public function update( $id, $data ) {
332 $update = false;
333 $filtered = $this->filter_inputs( $data );
334
335 if ( $this->is_set_table() && count( $filtered ) ) {
336 $this->db->update( $this->table_name, $filtered, array( $this->primary_key => $id ) );
337 $update = $this->db->last_error ? false : $id;
338 }
339
340 return $update;
341 }
342
343 /**
344 * Delete a record.
345 *
346 * @since 3.0.0
347 *
348 * @param int $id id.
349 *
350 * @return bool
351 */
352 public function delete( $id ) {
353 $deleted = false;
354 if ( $this->is_set_table() ) {
355 $deleted = (bool) $this->db->delete( $this->table_name, array( $this->primary_key => $id ) );
356 }
357
358 return $deleted;
359 }
360 }
361