PluginProbe
Stream – Activity Log & Audit Trail / 3.9.2
Stream – Activity Log & Audit Trail v3.9.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-db.php +164 -115 3.13.9.2 View file →
@@ -1,80 +1,44 @@
1 1 <?php
2 +/**
3 + * Manage DB connections using a provided DB driver.
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
10 +/**
11 + * Class - DB
12 + */
4 13 class DB {
5 14 /**
6 - * Hold Plugin class
7 - * @var Plugin
8 - */
9 - public $plugin;
10 -
11 - /**
12 - * Hold Query class
13 - * @var Query
14 - */
15 - public $query;
16 -
17 - /**
18 - * Hold records table name
15 + * Holds the driver instance
19 16 *
20 - * @var string
17 + * @var DB_Driver
21 18 */
22 - public $table;
19 + public $driver;
23 20
24 21 /**
25 - * Hold meta table name
22 + * Number of records in last request
26 23 *
27 - * @var string
24 + * @var int
28 25 */
29 - public $table_meta;
26 + protected $found_records_count = 0;
30 27
31 28 /**
32 29 * Class constructor.
33 30 *
34 - * @param Plugin $plugin The main Plugin class.
31 + * @param DB_Driver $driver Driver we want to use.
35 32 */
36 - public function __construct( $plugin ) {
37 - $this->plugin = $plugin;
38 - $this->query = new Query( $this );
39 -
40 - global $wpdb;
41 -
42 - /**
43 - * Allows devs to alter the tables prefix, default to base_prefix
44 - *
45 - * @param string $prefix
46 - *
47 - * @return string
48 - */
49 - $prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
50 -
51 - $this->table = $prefix . 'stream';
52 - $this->table_meta = $prefix . 'stream_meta';
53 -
54 - $wpdb->stream = $this->table;
55 - $wpdb->streammeta = $this->table_meta;
56 -
57 - // Hack for get_metadata
58 - $wpdb->recordmeta = $this->table_meta;
33 + public function __construct( $driver ) {
34 + $this->driver = $driver;
59 35 }
60 36
61 37 /**
62 - * Public getter to return table names
63 - *
64 - * @return array
65 - */
66 - public function get_table_names() {
67 - return array(
68 - $this->table,
69 - $this->table_meta,
70 - );
71 - }
72 -
73 - /**
74 38 * Insert a record
75 39 *
76 - * @param array $record
40 + * @param array $record New record.
77 41 *
78 42 * @return int
79 43 */
80 44 public function insert( $record ) {
@@ -90,26 +54,16 @@
90 54 * @return array
91 55 */
92 56 $record = apply_filters( 'wp_stream_record_array', $record );
93 57
94 - array_walk( $record, function( &$value, &$key ) {
95 - if ( ! is_array( $value ) ) {
96 - $value = strip_tags( $value );
97 - }
98 - });
99 -
100 - if ( empty( $record ) ) {
58 + $data = $this->sanitize_record( $record );
59 + if ( empty( $data ) ) {
101 60 return false;
102 61 }
103 62
104 - global $wpdb;
63 + $record_id = $this->driver->insert_record( $data );
105 64
106 - $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action' );
107 - $data = array_intersect_key( $record, array_flip( $fields ) );
108 -
109 - $result = $wpdb->insert( $this->table, $data );
110 -
111 - if ( 1 !== $result ) {
65 + if ( ! $record_id ) {
112 66 /**
113 67 * Fires on a record insertion error
114 68 *
115 69 * @param array $record
@@ -114,27 +68,13 @@
114 68 *
115 69 * @param array $record
116 70 * @param mixed $result
117 71 */
118 - do_action( 'wp_stream_record_insert_error', $record, $result );
72 + do_action( 'wp_stream_record_insert_error', $record, false );
119 73
120 - return $result;
74 + return false;
121 75 }
122 76
123 - $record_id = $wpdb->insert_id;
124 -
125 - // Insert record meta
126 - foreach ( (array) $record['meta'] as $key => $vals ) {
127 - // If associative array, serialize it, otherwise loop on its members
128 - $vals = ( is_array( $vals ) && 0 !== key( $vals ) ) ? array( $vals ) : $vals;
129 -
130 - foreach ( (array) $vals as $val ) {
131 - $val = maybe_serialize( $val );
132 -
133 - $this->insert_meta( $record_id, $key, $val );
134 - }
135 - }
136 -
137 77 /**
138 78 * Fires after a record has been inserted
139 79 *
140 80 * @param int $record_id
@@ -145,29 +85,48 @@
145 85 return absint( $record_id );
146 86 }
147 87
148 88 /**
149 - * Insert record meta
89 + * Ensure the record matches our schema.
150 90 *
151 - * @param int $record_id
152 - * @param string $key
153 - * @param string $val
91 + * @param array $record Record to store.
154 92 *
155 93 * @return array
156 94 */
157 - public function insert_meta( $record_id, $key, $val ) {
158 - global $wpdb;
95 + protected function sanitize_record( $record ) {
96 + if ( ! is_array( $record ) ) {
97 + return array();
98 + }
159 99
160 - $result = $wpdb->insert(
161 - $this->table_meta,
162 - array(
163 - 'record_id' => $record_id,
164 - 'meta_key' => $key,
165 - 'meta_value' => $val,
166 - )
100 + $record_defaults = array(
101 + 'object_id' => null,
102 + 'site_id' => null,
103 + 'blog_id' => null,
104 + 'user_id' => null,
105 + 'user_role' => null,
106 + 'created' => null,
107 + 'summary' => null,
108 + 'ip' => null,
109 + 'connector' => null,
110 + 'context' => null,
111 + 'action' => null,
112 + 'meta' => array(),
167 113 );
168 114
169 - return $result;
115 + // Records can have only these fields.
116 + $record = array_intersect_key( $record, $record_defaults );
117 +
118 + // Sanitize all record values.
119 + return array_map(
120 + function( $value ) {
121 + if ( ! is_array( $value ) ) {
122 + return wp_strip_all_tags( $value );
123 + }
124 +
125 + return $value;
126 + },
127 + $record
128 + );
170 129 }
171 130
172 131 /**
173 132 * Returns array of existing values for requested column.
@@ -172,31 +131,26 @@
172 131 /**
173 132 * Returns array of existing values for requested column.
174 133 * Used to fill search filters with only used items, instead of all items.
175 134 *
176 - * GROUP BY allows query to find just the first occurance of each value in the column,
135 + * GROUP BY allows query to find just the first occurrence of each value in the column,
177 136 * increasing the efficiency of the query.
178 137 *
179 138 * @see assemble_records
180 139 * @since 1.0.4
181 140 *
182 - * @param string $column
141 + * @param string $column Table column to pull data from.
183 142 *
184 143 * @return array
185 144 */
186 - function existing_records( $column ) {
187 - global $wpdb;
188 -
189 - // Sanitize column
145 + public function existing_records( $column ) {
146 + // Sanitize column.
190 147 $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
191 148 if ( ! in_array( $column, $allowed_columns, true ) ) {
192 149 return array();
193 150 }
194 151
195 - $rows = $wpdb->get_results(
196 - "SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
197 - 'ARRAY_A'
198 - );
152 + $rows = $this->driver->get_column_values( $column );
199 153
200 154 if ( is_array( $rows ) && ! empty( $rows ) ) {
201 155 $output_array = array();
202 156
@@ -210,20 +164,115 @@
210 164 }
211 165
212 166 $column = sprintf( 'stream_%s', $column );
213 167
214 - return isset( $this->plugin->connectors->term_labels[ $column ] ) ? $this->plugin->connectors->term_labels[ $column ] : array();
168 + $term_labels = wp_stream_get_instance()->connectors->term_labels;
169 + return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array();
215 170 }
216 171
217 172 /**
218 - * Helper function for calling $this->query->query()
173 + * Get stream records
219 174 *
220 - * @see Query->query()
175 + * @param array $args Arguments to filter result by.
221 176 *
222 - * @param array Query args
177 + * @return array Stream Records
178 + */
179 + public function get_records( $args ) {
180 + $defaults = array(
181 + // Search param.
182 + 'search' => null,
183 + 'search_field' => 'summary',
184 + 'record_after' => null, // Deprecated, use date_after instead
185 + // Date-based filters.
186 + 'date' => null, // Ex: 2015-07-01.
187 + 'date_from' => null, // Ex: 2015-07-01.
188 + 'date_to' => null, // Ex: 2015-07-01.
189 + 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00.
190 + 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00.
191 + // Record ID filters.
192 + 'record' => null,
193 + 'record__in' => array(),
194 + 'record__not_in' => array(),
195 + // Pagination params.
196 + 'records_per_page' => get_option( 'posts_per_page', 20 ),
197 + 'paged' => 1,
198 + // Order.
199 + 'order' => 'desc',
200 + 'orderby' => 'date',
201 + // Fields selection.
202 + 'fields' => array(),
203 + );
204 +
205 + // Additional property fields.
206 + $properties = array(
207 + 'user_id' => null,
208 + 'user_role' => null,
209 + 'ip' => null,
210 + 'object_id' => null,
211 + 'site_id' => null,
212 + 'blog_id' => null,
213 + 'connector' => null,
214 + 'context' => null,
215 + 'action' => null,
216 + );
217 +
218 + /**
219 + * Filter allows additional query properties to be added
220 + *
221 + * @return array Array of query properties
222 + */
223 + $properties = apply_filters( 'wp_stream_query_properties', $properties );
224 +
225 + // Add property fields to defaults, including their __in/__not_in variations.
226 + foreach ( $properties as $property => $default ) {
227 + if ( ! isset( $defaults[ $property ] ) ) {
228 + $defaults[ $property ] = $default;
229 + }
230 +
231 + $defaults[ "{$property}__in" ] = array();
232 + $defaults[ "{$property}__not_in" ] = array();
233 + }
234 +
235 + $args = wp_parse_args( $args, $defaults );
236 +
237 + /**
238 + * Filter allows additional arguments to query $args
239 + *
240 + * @return array Array of query arguments
241 + */
242 + $args = apply_filters( 'wp_stream_query_args', $args );
243 +
244 + $result = (array) $this->driver->get_records( $args );
245 + $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
246 +
247 + return empty( $result['items'] ) ? array() : $result['items'];
248 + }
249 +
250 + /**
251 + * Helper function, backwards compatibility
223 252 *
253 + * @param array $args Argument to filter result by.
254 + *
224 255 * @return array Stream Records
225 256 */
226 - function query( $args ) {
227 - return $this->query->query( $args );
257 + public function query( $args ) {
258 + return $this->get_records( $args );
259 + }
260 +
261 + /**
262 + * Return the number of records found in last request
263 + *
264 + * @return int
265 + */
266 + public function get_found_records_count() {
267 + return $this->found_records_count;
268 + }
269 +
270 + /**
271 + * Public getter to return table names
272 + *
273 + * @return array
274 + */
275 + public function get_table_names() {
276 + return $this->driver->get_table_names();
228 277 }
229 278 }