PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
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 4.2.1.1 All 137 releases
learnpress / inc / Databases / DataBase.php

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

891 lines 20.8 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\FilterBase;
7 use wpdb;
8
9 defined( 'ABSPATH' ) || exit();
10
11 /**
12 * Class DataBase
13 *
14 * @since 4.2.9.3
15 * @version 1.0.0
16 */
17 class DataBase {
18 private static $_instance;
19 public $wpdb, $tb_users;
20 public $tb_lp_courses;
21 public $tb_lp_user_items, $tb_lp_user_itemmeta;
22 public $tb_posts, $tb_postmeta, $tb_options;
23 public $tb_terms, $tb_term_relationships, $tb_term_taxonomy;
24 public $tb_lp_order_items, $tb_lp_order_itemmeta;
25 public $tb_lp_sections, $tb_lp_section_items;
26 public $tb_lp_quiz_questions;
27 public $tb_lp_user_item_results;
28 public $tb_lp_question_answers;
29 public $tb_lp_question_answermeta;
30 public $tb_lp_upgrade_db;
31 public $tb_lp_sessions;
32 public $tb_lp_files;
33 public $tb_lp_webhooks;
34 public $tb_thim_cache;
35 private $collate = '';
36 public $max_index_length = '191';
37
38 protected function __construct() {
39 /**
40 * @var wpdb $wpdb
41 */
42 global $wpdb;
43 $prefix = $wpdb->prefix;
44
45 $this->wpdb = $wpdb;
46 $this->tb_users = $wpdb->users;
47 $this->tb_posts = $wpdb->posts;
48 $this->tb_postmeta = $wpdb->postmeta;
49 $this->tb_options = $wpdb->options;
50 $this->tb_terms = $wpdb->terms;
51 $this->tb_term_relationships = $wpdb->term_relationships;
52 $this->tb_term_taxonomy = $wpdb->term_taxonomy;
53 $this->tb_lp_courses = $prefix . 'learnpress_courses';
54 $this->tb_lp_user_items = $prefix . 'learnpress_user_items';
55 $this->tb_lp_user_itemmeta = $prefix . 'learnpress_user_itemmeta';
56 $this->tb_lp_order_items = $prefix . 'learnpress_order_items';
57 $this->tb_lp_order_itemmeta = $prefix . 'learnpress_order_itemmeta';
58 $this->tb_lp_section_items = $prefix . 'learnpress_section_items';
59 $this->tb_lp_sections = $prefix . 'learnpress_sections';
60 $this->tb_lp_quiz_questions = $prefix . 'learnpress_quiz_questions';
61 $this->tb_lp_user_item_results = $prefix . 'learnpress_user_item_results';
62 $this->tb_lp_question_answers = $prefix . 'learnpress_question_answers';
63 $this->tb_lp_question_answermeta = $prefix . 'learnpress_question_answermeta';
64 $this->tb_lp_upgrade_db = $prefix . 'learnpress_upgrade_db';
65 $this->tb_lp_sessions = $prefix . 'learnpress_sessions';
66 $this->tb_lp_files = $prefix . 'learnpress_files';
67 $this->tb_lp_webhooks = $prefix . 'learnpress_webhooks';
68 $this->tb_thim_cache = $prefix . 'thim_cache';
69 $this->wpdb->hide_errors();
70 $this->set_collate();
71 }
72
73 /**
74 * Get Instance
75 *
76 * @return DataBase
77 */
78 public static function getInstance() {
79 if ( is_null( self::$_instance ) ) {
80 self::$_instance = new self();
81 }
82
83 return self::$_instance;
84 }
85
86 public function set_collate() {
87 $collate = '';
88
89 if ( $this->wpdb->has_cap( 'collation' ) ) {
90 if ( ! empty( $this->wpdb->charset ) ) {
91 $collate .= 'DEFAULT CHARACTER SET ' . $this->wpdb->charset;
92 }
93
94 if ( ! empty( $this->wpdb->collate ) ) {
95 $collate .= ' COLLATE ' . $this->wpdb->collate;
96 }
97 }
98
99 $this->collate = $collate;
100 }
101
102 public function get_collate(): string {
103 return $this->collate;
104 }
105
106 /**
107 * Check table exists.
108 *
109 * @param string $name_table
110 *
111 * @return bool|int
112 */
113 public function check_table_exists( string $name_table ) {
114 return $this->wpdb->query( $this->wpdb->prepare( "SHOW TABLES LIKE '%s'", $name_table ) );
115 }
116
117 /**
118 * Clone table
119 *
120 * @param string $name_table .
121 *
122 * @throws Exception
123 */
124 public function clone_table( string $name_table ): bool {
125 if ( ! current_user_can( ADMIN_ROLE ) ) {
126 throw new Exception( 'You don\'t have permission' );
127 }
128
129 $table_bk = $name_table . '_bk';
130
131 // Drop table bk if exists.
132 $this->drop_table( $table_bk );
133
134 // Clone table
135 $this->wpdb->query( "CREATE TABLE $table_bk LIKE $name_table" );
136 $this->wpdb->query( "INSERT INTO $table_bk SELECT * FROM $name_table" );
137
138 /*dbDelta(
139 "CREATE TABLE $table_bk LIKE $name_table;
140 INSERT INTO $table_bk SELECT * FROM $name_table;"
141 );*/
142
143 $this->check_execute_has_error();
144
145 return true;
146 }
147
148 /**
149 * Check column table
150 *
151 * @param string $name_table .
152 * @param string $name_col .
153 *
154 * @return bool|int
155 */
156 public function check_col_table( string $name_table = '', string $name_col = '' ) {
157 $query = $this->wpdb->prepare( "SHOW COLUMNS FROM $name_table LIKE '%s'", $name_col );
158
159 return $this->wpdb->query( $query );
160 }
161
162 /**
163 * Drop Column of Table
164 *
165 * @param string $name_table .
166 * @param string $name_col .
167 *
168 * @return bool|int
169 * @throws Exception
170 */
171 public function drop_col_table( string $name_table = '', string $name_col = '' ) {
172 if ( ! current_user_can( 'administrator' ) ) {
173 return false;
174 }
175
176 $check_table = $this->check_col_table( $name_table, $name_col );
177
178 if ( $check_table ) {
179 $execute = $this->wpdb->query( "ALTER TABLE $name_table DROP COLUMN $name_col" );
180
181 $this->check_execute_has_error();
182
183 return $execute;
184 }
185
186 return true;
187 }
188
189 /**
190 * Add Column of Table
191 *
192 * @param string $name_table .
193 * @param string $name_col .
194 * @param string $type .
195 * @param string $after_col .
196 *
197 * @return bool|int
198 * @throws Exception
199 */
200 public function add_col_table( string $name_table, string $name_col, string $type, string $after_col = '' ) {
201 if ( ! current_user_can( ADMIN_ROLE ) ) {
202 return false;
203 }
204
205 $query_add = '';
206
207 $col_exists = $this->check_col_table( $name_table, $name_col );
208
209 if ( ! empty( $after_col ) ) {
210 $query_add .= "AFTER $after_col";
211 }
212
213 if ( ! $col_exists ) {
214 $execute = $this->wpdb->query( "ALTER TABLE $name_table ADD COLUMN $name_col $type $query_add" );
215
216 $this->check_execute_has_error();
217
218 return $execute;
219 }
220
221 return true;
222 }
223
224 /**
225 * Drop Index of Table
226 *
227 * @param string $name_table .
228 *
229 * @return void
230 * @throws Exception
231 */
232 public function drop_indexs_table( string $name_table ) {
233 $show_index = "SHOW INDEX FROM $name_table";
234 $indexs = $this->wpdb->get_results( $show_index );
235
236 foreach ( $indexs as $index ) {
237 if ( 'PRIMARY' === $index->Key_name || '1' !== $index->Seq_in_index ) {
238 continue;
239 }
240
241 $query = "ALTER TABLE $name_table DROP INDEX $index->Key_name";
242
243 $this->wpdb->query( $query );
244 $this->check_execute_has_error();
245 }
246 }
247
248 /**
249 * Add Index of Table
250 *
251 * @param string $name_table .
252 * @param array $indexs .
253 *
254 * @return bool|int
255 * @throws Exception
256 */
257 public function add_indexs_table( string $name_table, array $indexs ) {
258 $add_index = '';
259 $count_indexs = count( $indexs ) - 1;
260
261 // Drop indexs .
262 $this->drop_indexs_table( $name_table );
263
264 foreach ( $indexs as $index ) {
265 if ( $count_indexs === array_search( $index, $indexs ) ) {
266 $add_index .= ' ADD INDEX ' . $index . ' (' . $index . ')';
267 } else {
268 $add_index .= ' ADD INDEX ' . $index . ' (' . $index . '),';
269 }
270 }
271
272 $execute = $this->wpdb->query(
273 "ALTER TABLE $name_table
274 $add_index"
275 );
276
277 $this->check_execute_has_error();
278
279 return $execute;
280 }
281
282 /**
283 * Drop table
284 *
285 * @param string $name_table .
286 *
287 * @return bool|int
288 * @throws Exception
289 */
290 public function drop_table( string $name_table = '' ) {
291 if ( ! current_user_can( ADMIN_ROLE ) ) {
292 throw new Exception( 'You don\'t have permission' );
293 }
294
295 // Check table exists.
296 $tb_exists = $this->check_table_exists( $name_table );
297 if ( $tb_exists ) {
298 $execute = $this->wpdb->query( "DROP TABLE $name_table" );
299
300 $this->check_execute_has_error();
301
302 return $execute;
303 }
304
305 return true;
306 }
307
308 /**
309 * Get list columns name of table
310 *
311 * @param string $name_table
312 *
313 * @return array
314 * @throws Exception
315 * @version 1.0.0
316 * @since 4.1.6
317 * @author tungnx
318 */
319 public function get_cols_of_table( string $name_table ): array {
320 $query = "SHOW COLUMNS FROM $name_table";
321
322 $result = $this->wpdb->get_col( $query );
323
324 $this->check_execute_has_error();
325
326 return $result;
327 }
328
329 /**
330 * Create table learnpress_user_item_results
331 *
332 * @return bool|int
333 * @throws Exception
334 */
335 public function create_tb_lp_user_item_results() {
336 $collate = $this->get_collate();
337
338 $execute = $this->wpdb->query(
339 "
340 CREATE TABLE IF NOT EXISTS $this->tb_lp_user_item_results(
341 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
342 user_item_id bigint(20) unsigned NOT NULL,
343 result longtext,
344 PRIMARY KEY (id),
345 KEY user_item_id (user_item_id)
346 ) $collate
347 "
348 );
349
350 $this->check_execute_has_error();
351
352 return $execute;
353 }
354
355 /**
356 * Create table learnpress_upgrade_db
357 *
358 * @return bool|int
359 * @throws Exception
360 */
361 public function create_tb_lp_upgrade_db() {
362 $collate = $this->get_collate();
363
364 $execute = $this->wpdb->query(
365 "
366 CREATE TABLE IF NOT EXISTS {$this->tb_lp_upgrade_db}(
367 step varchar(50) PRIMARY KEY UNIQUE,
368 status varchar(10),
369 KEY status (status)
370 ) $collate
371 "
372 );
373
374 $this->check_execute_has_error();
375
376 return $execute;
377 }
378
379 /**
380 * Set step completed.
381 *
382 * @param string $step .
383 * @param string $status .
384 *
385 * @return int|bool
386 */
387 public function set_step_complete( string $step, string $status ) {
388 if ( ! current_user_can( 'administrator' ) ) {
389 return false;
390 }
391
392 return $this->wpdb->insert(
393 $this->tb_lp_upgrade_db,
394 array(
395 'step' => $step,
396 'status' => $status,
397 ),
398 array( '%s', '%s' )
399 );
400 }
401
402 /**
403 * Get steps completed.
404 *
405 * @return array|object|null
406 */
407 public function get_steps_completed() {
408 return $this->wpdb->get_results( "SELECT step, status FROM {$this->tb_lp_upgrade_db}", OBJECT_K );
409 }
410
411 /**
412 * Check execute current has any errors.
413 *
414 * @throws Exception
415 */
416 public function check_execute_has_error() {
417 if ( $this->wpdb->last_error ) {
418 throw new Exception( $this->wpdb->last_error );
419 }
420 }
421
422 /**
423 * Important: Reason need set again indexes for table options of WP
424 * because if want change value of "option_name" will error "database error Duplicate entry"
425 * So before set must drop and add when done all
426 *
427 * @throws Exception
428 * @version 1.0.0
429 * @since 4.0.3
430 * @author tungnx
431 */
432 public function create_indexes_tb_options() {
433 $this->drop_indexs_table( $this->tb_options );
434 $result = $this->wpdb->query(
435 "
436 ALTER TABLE $this->tb_options
437 ADD UNIQUE option_name (option_name),
438 ADD INDEX autoload (autoload)
439 "
440 );
441
442 $this->check_execute_has_error();
443
444 return $result;
445 }
446
447 /**
448 * Rename table
449 *
450 * @throws Exception
451 * @version 1.0.0
452 * @since 4.0.3
453 * @author tungnx
454 */
455 public function rename_table( string $name_table = '', string $new_name = '' ) {
456 if ( ! current_user_can( ADMIN_ROLE ) ) {
457 throw new Exception( 'You don\'t have permission' );
458 }
459
460 $tb_exists = $this->check_table_exists( $name_table );
461
462 if ( ! $tb_exists ) {
463 throw new Exception( 'Table not exists' );
464 }
465
466 $result = $this->wpdb->query(
467 "
468 ALTER TABLE $name_table
469 RENAME $new_name
470 "
471 );
472 $this->check_execute_has_error();
473
474 return $result;
475 }
476
477 /**
478 * Check key postmeta exist on Database
479 *
480 * @param int $post_id
481 * @param string $key
482 *
483 * @return bool|int
484 */
485 public function check_key_postmeta_exists( int $post_id = 0, string $key = '' ) {
486 return $this->wpdb->query(
487 $this->wpdb->prepare(
488 "
489 SELECT meta_id FROM $this->tb_postmeta
490 WHERE meta_key = %s
491 AND post_id = %d
492 ",
493 $key,
494 $post_id
495 )
496 );
497 }
498
499 /**
500 * Get total pages
501 *
502 * @param int $limit
503 * @param int $total_rows
504 *
505 * @return int
506 */
507 public static function get_total_pages( int $limit = 0, int $total_rows = 0 ): int {
508 if ( $limit == 0 ) {
509 return 0;
510 }
511
512 $total_pages = (int) ceil( $total_rows / $limit );
513
514 return (int) $total_pages;
515 }
516
517 /**
518 * Get query string single row
519 *
520 * @param FilterBase $filter
521 *
522 * @since 4.2.5
523 * @version 1.0.1
524 */
525 public function get_query_single_row( &$filter ) {
526 $filter->limit = 1;
527 $filter->return_string_query = true;
528 $filter->run_query_count = false;
529 }
530
531 /**
532 * Get result query
533 *
534 * @param FilterBase $filter
535 * @param int $total_rows
536 *
537 * @return array|object|null|int|string
538 * @throws Exception
539 * @author tungnx
540 * @version 1.0.2
541 * @since 4.1.6
542 */
543 public function execute( $filter, int &$total_rows = 0 ) {
544 $result = null;
545
546 // Where
547 $WHERE = array( 'WHERE 1=1' );
548
549 // Fields select
550 $FIELDS = '*';
551 if ( ! empty( $filter->only_fields ) ) {
552 $FIELDS = implode( ',', array_unique( $filter->only_fields ) );
553 } elseif ( ! empty( $filter->fields ) ) {
554 // exclude more fields
555 if ( ! empty( $filter->exclude_fields ) ) {
556 foreach ( $filter->exclude_fields as $field ) {
557 $index_field = array_search( $field, $filter->fields );
558 if ( $index_field ) {
559 unset( $filter->fields[ $index_field ] );
560 }
561 }
562 }
563
564 foreach ( $filter->fields as $key => $field ) {
565 if ( $field === 'order' ) {
566 // Replace order with `order` to avoid conflict with SQL reserved word.
567 $filter->fields[ $key ] = '`order`';
568 break;
569 }
570 }
571
572 $FIELDS = implode( ',', array_unique( $filter->fields ) );
573 }
574 $FIELDS = apply_filters( 'lp/query/fields', $FIELDS, $filter );
575
576 $INNER_JOIN = array();
577 $INNER_JOIN = array_merge( $INNER_JOIN, $filter->join );
578 $INNER_JOIN = apply_filters( 'lp/query/inner_join', $INNER_JOIN, $filter );
579 $INNER_JOIN = implode( ' ', array_unique( $INNER_JOIN ) );
580
581 $WHERE = array_merge( $WHERE, $filter->where );
582 $WHERE = apply_filters( 'lp/query/where', $WHERE, $filter );
583 $WHERE = implode( ' ', array_unique( $WHERE ) );
584
585 // Group by
586 $GROUP_BY = '';
587 if ( $filter->group_by ) {
588 $GROUP_BY .= 'GROUP BY ' . $filter->group_by;
589 $GROUP_BY = apply_filters( 'lp/query/group_by', $GROUP_BY, $filter );
590 }
591
592 // Order by
593 $ORDER_BY = '';
594 if ( $filter->order_by ) {
595 $filter->order = strtoupper( $filter->order );
596 if ( ! in_array( $filter->order, [ 'DESC', 'ASC' ] ) ) {
597 $filter->order = 'DESC';
598 }
599
600 $ORDER_BY .= 'ORDER BY ' . $filter->order_by . ' ' . $filter->order . ' ';
601 $ORDER_BY = apply_filters( 'lp/query/order_by', $ORDER_BY, $filter );
602 }
603
604 // Limit
605 $LIMIT = '';
606 if ( $filter->limit != - 1 ) {
607 $filter->limit = absint( $filter->limit );
608 /*if ( $filter->limit > $filter->max_limit ) {
609 $filter->limit = $filter->max_limit;
610 }*/
611 $offset = $filter->limit * ( $filter->page - 1 );
612 $LIMIT = $this->wpdb->prepare( 'LIMIT %d, %d', $offset, $filter->limit );
613 }
614
615 // For nest query
616 if ( $filter->return_string_query ) {
617 $LIMIT = '';
618 }
619
620 // From table or group select
621 $COLLECTION = '';
622 if ( ! empty( $filter->collection ) ) {
623 $COLLECTION = $filter->collection;
624 }
625
626 // Alias table
627 $ALIAS_COLLECTION = 'X';
628 if ( ! empty( $filter->collection_alias ) ) {
629 $ALIAS_COLLECTION = $filter->collection_alias;
630 }
631
632 // Query
633 $query = "SELECT $FIELDS FROM $COLLECTION AS $ALIAS_COLLECTION
634 $INNER_JOIN
635 $WHERE
636 $GROUP_BY
637 $ORDER_BY
638 $LIMIT
639 ";
640
641 if ( $filter->return_string_query ) {
642 return $query;
643 } elseif ( ! empty( $filter->union ) ) {
644 $query = implode( ' UNION ', array_unique( $filter->union ) );
645 $query .= $GROUP_BY;
646 $query .= $ORDER_BY;
647 $query .= $LIMIT;
648 }
649
650 if ( ! $filter->query_count ) {
651 // Debug string query
652 if ( $filter->debug_string_query ) {
653 return $query;
654 }
655
656 $result = $this->wpdb->get_results( $query );
657 }
658
659 // Query total rows
660 if ( $filter->run_query_count ) {
661 $query = str_replace( array( $LIMIT, $ORDER_BY ), '', $query );
662 $query_total = "SELECT COUNT($filter->field_count) FROM ($query) AS $ALIAS_COLLECTION";
663 $total_rows = (int) $this->wpdb->get_var( $query_total );
664
665 $this->check_execute_has_error();
666
667 if ( $filter->query_count ) {
668 // Debug string query
669 if ( $filter->debug_string_query ) {
670 return $query_total;
671 }
672
673 return $total_rows;
674 }
675 }
676
677 $this->check_execute_has_error();
678
679 return $result;
680 }
681
682 /**
683 * Query update
684 *
685 * @param FilterBase $filter
686 *
687 * @throws Exception
688 * @since 4.1.7
689 * @version 1.0.1
690 */
691 public function update_execute( $filter ) {
692
693 $COLLECTION = $filter->collection;
694
695 // SET value
696 $SET = apply_filters( 'lp/query/update/set', $filter->set, $filter );
697 $SET = implode( ',', array_unique( $SET ) );
698
699 // Where
700 $WHERE = array( 'WHERE 1=1' );
701 $WHERE = array_merge( $WHERE, $filter->where );
702 $WHERE = apply_filters( 'lp/query/update/where', $WHERE, $filter );
703 $WHERE = implode( ' ', array_unique( $WHERE ) );
704
705 $query = "
706 UPDATE $COLLECTION
707 SET $SET
708 $WHERE
709 ";
710
711 $result = $this->wpdb->query( $query );
712
713 $this->check_execute_has_error();
714
715 return $result;
716 }
717
718 /**
719 * Query delete
720 *
721 * @param FilterBase $filter
722 * @param string $table
723 *
724 * @return bool|int|\mysqli_result|string|null
725 * @throws Exception
726 * @since 4.1.7
727 * @version 1.0.1
728 */
729 public function delete_execute( $filter, string $table = '' ) {
730 $COLLECTION = $filter->collection;
731
732 // Where
733 $WHERE = array( 'WHERE 1=1' );
734 $WHERE = array_merge( $WHERE, $filter->where );
735 $WHERE = apply_filters( 'lp/query/delete/where', $WHERE, $filter );
736 $WHERE = implode( ' ', array_unique( $WHERE ) );
737
738 // Join
739 $INNER_JOIN = array();
740 $INNER_JOIN = array_merge( $INNER_JOIN, $filter->join );
741 $INNER_JOIN = apply_filters( 'lp/query/delete/inner_join', $INNER_JOIN, $filter );
742 $INNER_JOIN = implode( ' ', array_unique( $INNER_JOIN ) );
743
744 $query = "
745 DELETE $table FROM $COLLECTION
746 $INNER_JOIN
747 $WHERE
748 ";
749
750 if ( $filter->return_string_query ) {
751 return $query;
752 }
753
754 $result = $this->wpdb->query( $query );
755
756 $this->check_execute_has_error();
757
758 return $result;
759 }
760
761 /**
762 * Get values of list object by key
763 *
764 * @param array $arr_object
765 * @param string $key
766 *
767 * @return array
768 */
769 public static function get_values_by_key( array $arr_object, string $key = 'ID' ): array {
770 $arr_object_ids = array();
771 foreach ( $arr_object as $object ) {
772 $arr_object_ids[] = $object->{$key};
773 }
774
775 return $arr_object_ids;
776 }
777
778 /**
779 * Insert data
780 *
781 * @param array $args [ 'data' => [], 'filter' => FilterBase, 'table_name' => '', 'key_auto_increment' => '' ]
782 *
783 * @return int
784 * @throws Exception
785 * @version 1.0.1
786 * @since 4.2.9
787 */
788 public function insert_data( array $args ): int {
789 $data = $args['data'] ?? [];
790 $filter = $args['filter'] ?? null;
791 $table_name = $args['table_name'] ?? '';
792 $key_auto_increment = $args['key_auto_increment'] ?? '';
793
794 if ( empty( $data ) || ! is_array( $data ) ) {
795 throw new Exception( __( 'Data must be an array!', 'learnpress' ) . ' | ' . __FUNCTION__ );
796 }
797
798 /*if ( ! $filter instanceof FilterBase ) {
799 throw new Exception( __( 'Invalid filter!', 'learnpress' ) . ' | ' . __FUNCTION__ );
800 }*/
801
802 if ( empty( $filter->all_fields ) ) {
803 throw new Exception( __( 'Filter must have property all_fields!', 'learnpress' ) . ' | ' . __FUNCTION__ );
804 }
805
806 if ( empty( $table_name ) ) {
807 throw new Exception( __( 'Table name is required!', 'learnpress' ) . ' | ' . __FUNCTION__ );
808 }
809
810 if ( empty( $key_auto_increment ) || ! is_string( $key_auto_increment ) ) {
811 throw new Exception( __( 'Key auto increment must be a string!', 'learnpress' ) . ' | ' . __FUNCTION__ );
812 }
813
814 foreach ( $data as $col_name => $value ) {
815 if ( ! in_array( $col_name, $filter->all_fields ) ) {
816 unset( $data[ $col_name ] );
817 }
818 }
819
820 // unset key is auto increment.
821 unset( $data[ $key_auto_increment ] );
822
823 $this->wpdb->insert( $table_name, $data );
824
825 $this->check_execute_has_error();
826
827 return $this->wpdb->insert_id;
828 }
829
830 /**
831 * Update data
832 *
833 * @param array $args
834 *
835 * @return bool
836 *
837 * @throws Exception
838 * @since 4.2.9
839 * @version 1.0.2
840 */
841 public function update_data( array $args ): bool {
842 $data = $args['data'] ?? [];
843 $filter = $args['filter'] ?? null;
844 $table_name = $args['table_name'] ?? '';
845 $where_key = $args['where_key'] ?? '';
846
847 /*if ( ! $filter instanceof FilterBase ) {
848 throw new Exception( __( 'Invalid filter!', 'learnpress' ) . ' | ' . __FUNCTION__ );
849 }*/
850
851 if ( empty( $filter->all_fields ) ) {
852 throw new Exception( __( 'Filter must have property all_fields!', 'learnpress' ) . ' | ' . __FUNCTION__ );
853 }
854
855 if ( empty( $data ) || ! is_array( $data ) ) {
856 throw new Exception( __( 'Data must be an array!', 'learnpress' ) . ' | ' . __FUNCTION__ );
857 }
858
859 if ( empty( $where_key ) ) {
860 throw new Exception( __( 'Invalid where key!', 'learnpress' ) . ' | ' . __FUNCTION__ );
861 }
862
863 if ( empty( $table_name ) ) {
864 throw new Exception( __( 'Table name is required!', 'learnpress' ) . ' | ' . __FUNCTION__ );
865 }
866
867 $filter->collection = $table_name;
868 foreach ( $data as $col_name => $value ) {
869 if ( ! in_array( $col_name, $filter->all_fields ) ) {
870 continue;
871 }
872
873 // Key `order` is reserved keyword in MySQL
874 if ( $col_name === 'order' ) {
875 $col_name = '`order`';
876 }
877
878 if ( is_null( $value ) ) {
879 $filter->set[] = $col_name . ' = null';
880 } else {
881 $filter->set[] = $this->wpdb->prepare( $col_name . ' = %s', $value );
882 }
883 }
884
885 $filter->where[] = $this->wpdb->prepare( "AND $where_key = %d", $data[ $where_key ] );
886 $this->update_execute( $filter );
887
888 return true;
889 }
890 }
891