-
Notifications
You must be signed in to change notification settings - Fork 116
/
class-db.php
executable file
·278 lines (242 loc) · 6.24 KB
/
class-db.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Manage DB connections using a provided DB driver.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - DB
*/
class DB {
/**
* Holds the driver instance
*
* @var DB_Driver
*/
public $driver;
/**
* Number of records in last request
*
* @var int
*/
protected $found_records_count = 0;
/**
* Class constructor.
*
* @param DB_Driver $driver Driver we want to use.
*/
public function __construct( $driver ) {
$this->driver = $driver;
}
/**
* Insert a record
*
* @param array $record New record.
*
* @return int
*/
public function insert( $record ) {
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
return false;
}
/**
* Filter allows modification of record information
*
* @param array $record
*
* @return array
*/
$record = apply_filters( 'wp_stream_record_array', $record );
$data = $this->sanitize_record( $record );
if ( empty( $data ) ) {
return false;
}
$record_id = $this->driver->insert_record( $data );
if ( ! $record_id ) {
/**
* Fires on a record insertion error
*
* @param array $record
* @param mixed $result
*/
do_action( 'wp_stream_record_insert_error', $record, false );
return false;
}
/**
* Fires after a record has been inserted
*
* @param int $record_id
* @param array $record
*/
do_action( 'wp_stream_record_inserted', $record_id, $record );
return absint( $record_id );
}
/**
* Ensure the record matches our schema.
*
* @param array $record Record to store.
*
* @return array
*/
protected function sanitize_record( $record ) {
if ( ! is_array( $record ) ) {
return array();
}
$record_defaults = array(
'object_id' => null,
'site_id' => null,
'blog_id' => null,
'user_id' => null,
'user_role' => null,
'created' => null,
'summary' => null,
'ip' => null,
'connector' => null,
'context' => null,
'action' => null,
'meta' => array(),
);
// Records can have only these fields.
$record = array_intersect_key( $record, $record_defaults );
// Sanitize all record values.
return array_map(
function ( $value ) {
if ( ! is_array( $value ) ) {
return wp_strip_all_tags( $value );
}
return $value;
},
$record
);
}
/**
* 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.
*
* @see assemble_records
* @since 1.0.4
*
* @param string $column Table column to pull data from.
*
* @return array
*/
public function existing_records( $column ) {
// Sanitize column.
$allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
if ( ! in_array( $column, $allowed_columns, true ) ) {
return array();
}
$rows = $this->driver->get_column_values( $column );
if ( is_array( $rows ) && ! empty( $rows ) ) {
$output_array = array();
foreach ( $rows as $row ) {
foreach ( $row as $cell => $value ) {
$output_array[ $value ] = $value;
}
}
return (array) $output_array;
}
$column = sprintf( 'stream_%s', $column );
$term_labels = wp_stream_get_instance()->connectors->term_labels;
return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array();
}
/**
* Get stream records
*
* @param array $args Arguments to filter result by.
*
* @return array Stream Records
*/
public function get_records( $args ) {
$defaults = array(
// Search param.
'search' => null,
'search_field' => 'summary',
'record_after' => null, // Deprecated, use date_after instead
// Date-based filters.
'date' => null, // Ex: 2015-07-01.
'date_from' => null, // Ex: 2015-07-01.
'date_to' => null, // Ex: 2015-07-01.
'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00.
'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00.
// Record ID filters.
'record' => null,
'record__in' => array(),
'record__not_in' => array(),
// Pagination params.
'records_per_page' => get_option( 'posts_per_page', 20 ),
'paged' => 1,
// Order.
'order' => 'desc',
'orderby' => 'date',
// Fields selection.
'fields' => array(),
);
// Additional property fields.
$properties = array(
'user_id' => null,
'user_role' => null,
'ip' => null,
'object_id' => null,
'site_id' => null,
'blog_id' => null,
'connector' => null,
'context' => null,
'action' => null,
);
/**
* Filter allows additional query properties to be added
*
* @return array Array of query properties
*/
$properties = apply_filters( 'wp_stream_query_properties', $properties );
// Add property fields to defaults, including their __in/__not_in variations.
foreach ( $properties as $property => $default ) {
if ( ! isset( $defaults[ $property ] ) ) {
$defaults[ $property ] = $default;
}
$defaults[ "{$property}__in" ] = array();
$defaults[ "{$property}__not_in" ] = array();
}
$args = wp_parse_args( $args, $defaults );
/**
* Filter allows additional arguments to query $args
*
* @return array Array of query arguments
*/
$args = apply_filters( 'wp_stream_query_args', $args );
$result = (array) $this->driver->get_records( $args );
$this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
return empty( $result['items'] ) ? array() : $result['items'];
}
/**
* Helper function, backwards compatibility
*
* @param array $args Argument to filter result by.
*
* @return array Stream Records
*/
public function query( $args ) {
return $this->get_records( $args );
}
/**
* Return the number of records found in last request
*
* @return int
*/
public function get_found_records_count() {
return $this->found_records_count;
}
/**
* Public getter to return table names
*
* @return array
*/
public function get_table_names() {
return $this->driver->get_table_names();
}
}