PluginProbe
Stream – Activity Log & Audit Trail / 3.7.0
Stream – Activity Log & Audit Trail v3.7.0
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 +148 -125 3.0.33.7.0 View file →
@@ -1,84 +1,48 @@
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 $recordarr
40 + * @param array $record New record.
77 41 *
78 42 * @return int
79 43 */
80 - public function insert( $recordarr ) {
44 + public function insert( $record ) {
81 45 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
82 46 return false;
83 47 }
84 48
@@ -84,113 +48,77 @@
84 48
85 49 /**
86 50 * Filter allows modification of record information
87 51 *
88 - * @param array $recordarr
52 + * @param array $record
89 53 *
90 54 * @return array
91 55 */
92 - $recordarr = apply_filters( 'wp_stream_record_array', $recordarr );
56 + $record = apply_filters( 'wp_stream_record_array', $record );
93 57
94 - if ( empty( $recordarr ) ) {
58 + array_walk(
59 + $record,
60 + function( &$value, &$key ) {
61 + if ( ! is_array( $value ) ) {
62 + $value = wp_strip_all_tags( $value );
63 + }
64 + }
65 + );
66 +
67 + if ( empty( $record ) ) {
95 68 return false;
96 69 }
97 70
98 - global $wpdb;
71 + $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' );
72 + $data = array_intersect_key( $record, array_flip( $fields ) );
99 73
100 - $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action' );
101 - $data = array_intersect_key( $recordarr, array_flip( $fields ) );
74 + $record_id = $this->driver->insert_record( $data );
102 75
103 - $result = $wpdb->insert( $this->table, $data );
104 -
105 - if ( 1 !== $result ) {
76 + if ( ! $record_id ) {
106 77 /**
107 78 * Fires on a record insertion error
108 79 *
109 - * @param array $recordarr
80 + * @param array $record
110 81 * @param mixed $result
111 82 */
112 - do_action( 'wp_stream_record_insert_error', $recordarr, $result );
83 + do_action( 'wp_stream_record_insert_error', $record, false );
113 84
114 - return $result;
85 + return false;
115 86 }
116 87
117 - $record_id = $wpdb->insert_id;
118 -
119 - // Insert record meta
120 - foreach ( (array) $recordarr['meta'] as $key => $vals ) {
121 - // If associative array, serialize it, otherwise loop on its members
122 - $vals = ( is_array( $vals ) && 0 !== key( $vals ) ) ? array( $vals ) : $vals;
123 -
124 - foreach ( (array) $vals as $val ) {
125 - $val = maybe_serialize( $val );
126 -
127 - $this->insert_meta( $record_id, $key, $val );
128 - }
129 - }
130 -
131 88 /**
132 89 * Fires after a record has been inserted
133 90 *
134 91 * @param int $record_id
135 - * @param array $recordarr
92 + * @param array $record
136 93 */
137 - do_action( 'wp_stream_record_inserted', $record_id, $recordarr );
94 + do_action( 'wp_stream_record_inserted', $record_id, $record );
138 95
139 96 return absint( $record_id );
140 97 }
141 98
142 99 /**
143 - * Insert record meta
144 - *
145 - * @param int $record_id
146 - * @param string $key
147 - * @param string $val
148 - *
149 - * @return array
150 - */
151 - public function insert_meta( $record_id, $key, $val ) {
152 - global $wpdb;
153 -
154 - $result = $wpdb->insert(
155 - $this->table_meta,
156 - array(
157 - 'record_id' => $record_id,
158 - 'meta_key' => $key,
159 - 'meta_value' => $val,
160 - )
161 - );
162 -
163 - return $result;
164 - }
165 -
166 - /**
167 100 * Returns array of existing values for requested column.
168 101 * Used to fill search filters with only used items, instead of all items.
169 102 *
170 - * GROUP BY allows query to find just the first occurance of each value in the column,
103 + * GROUP BY allows query to find just the first occurrence of each value in the column,
171 104 * increasing the efficiency of the query.
172 105 *
173 106 * @see assemble_records
174 107 * @since 1.0.4
175 108 *
176 - * @param string $column
109 + * @param string $column Table column to pull data from.
177 110 *
178 111 * @return array
179 112 */
180 - function existing_records( $column ) {
181 - global $wpdb;
182 -
183 - // Sanitize column
113 + public function existing_records( $column ) {
114 + // Sanitize column.
184 115 $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
185 - if ( ! in_array( $column, $allowed_columns ) ) {
116 + if ( ! in_array( $column, $allowed_columns, true ) ) {
186 117 return array();
187 118 }
188 119
189 - $rows = $wpdb->get_results(
190 - $wpdb->prepare( "SELECT $column FROM $wpdb->stream GROUP BY %s", $column ), // @codingStandardsIgnoreLine can't prepare column name
191 - 'ARRAY_A'
192 - );
120 + $rows = $this->driver->get_column_values( $column );
193 121
194 122 if ( is_array( $rows ) && ! empty( $rows ) ) {
195 123 $output_array = array();
196 124
@@ -204,20 +132,115 @@
204 132 }
205 133
206 134 $column = sprintf( 'stream_%s', $column );
207 135
208 - return isset( $this->plugin->connectors->term_labels[ $column ] ) ? $this->plugin->connectors->term_labels[ $column ] : array();
136 + $term_labels = wp_stream_get_instance()->connectors->term_labels;
137 + return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array();
209 138 }
210 139
211 140 /**
212 - * Helper function for calling $this->query->query()
141 + * Get stream records
213 142 *
214 - * @see Query->query()
143 + * @param array $args Arguments to filter result by.
215 144 *
216 - * @param array Query args
145 + * @return array Stream Records
146 + */
147 + public function get_records( $args ) {
148 + $defaults = array(
149 + // Search param.
150 + 'search' => null,
151 + 'search_field' => 'summary',
152 + 'record_after' => null, // Deprecated, use date_after instead
153 + // Date-based filters.
154 + 'date' => null, // Ex: 2015-07-01.
155 + 'date_from' => null, // Ex: 2015-07-01.
156 + 'date_to' => null, // Ex: 2015-07-01.
157 + 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00.
158 + 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00.
159 + // Record ID filters.
160 + 'record' => null,
161 + 'record__in' => array(),
162 + 'record__not_in' => array(),
163 + // Pagination params.
164 + 'records_per_page' => get_option( 'posts_per_page', 20 ),
165 + 'paged' => 1,
166 + // Order.
167 + 'order' => 'desc',
168 + 'orderby' => 'date',
169 + // Fields selection.
170 + 'fields' => array(),
171 + );
172 +
173 + // Additional property fields.
174 + $properties = array(
175 + 'user_id' => null,
176 + 'user_role' => null,
177 + 'ip' => null,
178 + 'object_id' => null,
179 + 'site_id' => null,
180 + 'blog_id' => null,
181 + 'connector' => null,
182 + 'context' => null,
183 + 'action' => null,
184 + );
185 +
186 + /**
187 + * Filter allows additional query properties to be added
188 + *
189 + * @return array Array of query properties
190 + */
191 + $properties = apply_filters( 'wp_stream_query_properties', $properties );
192 +
193 + // Add property fields to defaults, including their __in/__not_in variations.
194 + foreach ( $properties as $property => $default ) {
195 + if ( ! isset( $defaults[ $property ] ) ) {
196 + $defaults[ $property ] = $default;
197 + }
198 +
199 + $defaults[ "{$property}__in" ] = array();
200 + $defaults[ "{$property}__not_in" ] = array();
201 + }
202 +
203 + $args = wp_parse_args( $args, $defaults );
204 +
205 + /**
206 + * Filter allows additional arguments to query $args
207 + *
208 + * @return array Array of query arguments
209 + */
210 + $args = apply_filters( 'wp_stream_query_args', $args );
211 +
212 + $result = (array) $this->driver->get_records( $args );
213 + $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0;
214 +
215 + return empty( $result['items'] ) ? array() : $result['items'];
216 + }
217 +
218 + /**
219 + * Helper function, backwards compatibility
217 220 *
221 + * @param array $args Argument to filter result by.
222 + *
218 223 * @return array Stream Records
219 224 */
220 - function query( $args ) {
221 - return $this->query->query( $args );
225 + public function query( $args ) {
226 + return $this->get_records( $args );
227 + }
228 +
229 + /**
230 + * Return the number of records found in last request
231 + *
232 + * @return int
233 + */
234 + public function get_found_records_count() {
235 + return $this->found_records_count;
236 + }
237 +
238 + /**
239 + * Public getter to return table names
240 + *
241 + * @return array
242 + */
243 + public function get_table_names() {
244 + return $this->driver->get_table_names();
222 245 }
223 246 }