-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathclass-db-driver-wpdb.php
executable file
·180 lines (154 loc) · 3.37 KB
/
class-db-driver-wpdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Database Driver class for "stream" table responsible for holding records.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - DB_Driver_WPDB
*/
class DB_Driver_WPDB implements DB_Driver {
/**
* Holds Query class
*
* @var Query
*/
protected $query;
/**
* Hold records table name
*
* @var string
*/
public $table;
/**
* Hold meta table name
*
* @var string
*/
public $table_meta;
/**
* Class constructor.
*/
public function __construct() {
$this->query = new Query( $this );
global $wpdb;
$prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
$this->table = $prefix . 'stream';
$this->table_meta = $prefix . 'stream_meta';
$wpdb->stream = $this->table;
$wpdb->streammeta = $this->table_meta;
// Hack for get_metadata.
$wpdb->recordmeta = $this->table_meta;
}
/**
* Insert a record.
*
* @param array $data Data to insert.
*
* @return int
*/
public function insert_record( $data ) {
global $wpdb;
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return false;
}
$meta = array();
if ( array_key_exists( 'meta', $data ) ) {
$meta = $data['meta'];
unset( $data['meta'] );
}
$result = $wpdb->insert( $this->table, $data );
if ( ! $result ) {
return false;
}
$record_id = $wpdb->insert_id;
// Insert record meta.
foreach ( (array) $meta as $key => $vals ) {
foreach ( (array) $vals as $val ) {
$this->insert_meta( $record_id, $key, $val );
}
}
return $record_id;
}
/**
* Insert record meta
*
* @param int $record_id Record ID.
* @param string $key Meta Key.
* @param string $val Meta Data.
*
* @return array
*/
public function insert_meta( $record_id, $key, $val ) {
global $wpdb;
$result = $wpdb->insert(
$this->table_meta,
array(
'record_id' => $record_id,
'meta_key' => $key,
'meta_value' => $val,
)
);
return $result;
}
/**
* Retrieve records
*
* @param array $args Query arguments.
*
* @return array
*/
public function get_records( $args ) {
return $this->query->query( $args );
}
/**
* Returns array of existing values for requested column.
* Used to fill search filters with only used items, instead of all items.
*
* GROUP BY allows query to find just the first occurrence of each value in the column,
* increasing the efficiency of the query.
*
* @param string $column Column being filtered.
*
* @return array
*/
public function get_column_values( $column ) {
global $wpdb;
return (array) $wpdb->get_results(
"SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
'ARRAY_A'
);
}
/**
* Public getter to return table names
*
* @return array
*/
public function get_table_names() {
return array(
$this->table,
$this->table_meta,
);
}
/**
* Init storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
* @return \WP_Stream\Install
*/
public function setup_storage( $plugin ) {
return new Install( $plugin );
}
/**
* Purge storage.
*
* @param \WP_Stream\Plugin $plugin Instance of the plugin.
* @return \WP_Stream\Uninstall
*/
public function purge_storage( $plugin ) {
$uninstall = new Uninstall( $plugin );
add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
return $uninstall;
}
}