PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 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 All 140 releases
learnpress / inc / Databases / DataBase.php

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

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