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