1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; // Exit if accessed directly |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
abstract class WPInv_DB { |
7
|
|
|
|
8
|
|
|
public $table_name; |
9
|
|
|
public $version; |
10
|
|
|
public $primary_key; |
11
|
|
|
|
12
|
|
|
public function __construct() { |
13
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function get_columns() { |
17
|
|
|
return array(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function get_column_defaults() { |
21
|
|
|
return array(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function get( $row_id ) { |
25
|
|
|
global $wpdb; |
26
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1", $row_id ) ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function get_by( $column, $row_id ) { |
30
|
|
|
global $wpdb; |
31
|
|
|
$column = esc_sql( $column ); |
32
|
|
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1", $row_id ) ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function get_column( $column, $row_id ) { |
36
|
|
|
global $wpdb; |
37
|
|
|
$column = esc_sql( $column ); |
38
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1", $row_id ) ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function get_column_by( $column, $column_where, $column_value ) { |
42
|
|
|
global $wpdb; |
43
|
|
|
$column_where = esc_sql( $column_where ); |
44
|
|
|
$column = esc_sql( $column ); |
45
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1", $column_value ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function insert( $data, $type = '' ) { |
49
|
|
|
global $wpdb; |
50
|
|
|
|
51
|
|
|
$data = wp_parse_args( $data, $this->get_column_defaults() ); |
52
|
|
|
|
53
|
|
|
do_action( 'wpinv_pre_insert_' . $type, $data ); |
54
|
|
|
|
55
|
|
|
$column_formats = $this->get_columns(); |
56
|
|
|
|
57
|
|
|
$data = array_change_key_case( $data ); |
58
|
|
|
|
59
|
|
|
$data = array_intersect_key( $data, $column_formats ); |
60
|
|
|
|
61
|
|
|
$data_keys = array_keys( $data ); |
62
|
|
|
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
63
|
|
|
|
64
|
|
|
$wpdb->insert( $this->table_name, $data, $column_formats ); |
65
|
|
|
$wpdb_insert_id = $wpdb->insert_id; |
66
|
|
|
|
67
|
|
|
do_action( 'wpinv_post_insert_' . $type, $wpdb_insert_id, $data ); |
68
|
|
|
|
69
|
|
|
return $wpdb_insert_id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function update( $row_id, $data = array(), $where = '' ) { |
73
|
|
|
global $wpdb; |
74
|
|
|
|
75
|
|
|
$row_id = absint( $row_id ); |
76
|
|
|
|
77
|
|
|
if( empty( $row_id ) ) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if( empty( $where ) ) { |
82
|
|
|
$where = $this->primary_key; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$column_formats = $this->get_columns(); |
86
|
|
|
|
87
|
|
|
$data = array_change_key_case( $data ); |
88
|
|
|
|
89
|
|
|
$data = array_intersect_key( $data, $column_formats ); |
90
|
|
|
|
91
|
|
|
$data_keys = array_keys( $data ); |
92
|
|
|
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
93
|
|
|
|
94
|
|
|
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function delete( $row_id = 0 ) { |
102
|
|
|
global $wpdb; |
103
|
|
|
|
104
|
|
|
$row_id = absint( $row_id ); |
105
|
|
|
|
106
|
|
|
if( empty( $row_id ) ) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function table_exists( $table ) { |
118
|
|
|
global $wpdb; |
119
|
|
|
$table = sanitize_text_field( $table ); |
120
|
|
|
|
121
|
|
|
return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function installed() { |
125
|
|
|
return $this->table_exists( $this->table_name ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|