PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.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 +74 -32 3.2.33.9.0 View file →
@@ -1,10 +1,19 @@
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 the Driver class
15 + * Holds the driver instance
7 16 *
8 17 * @var DB_Driver
9 18 */
10 19 public $driver;
@@ -27,9 +36,9 @@
27 36
28 37 /**
29 38 * Insert a record
30 39 *
31 - * @param array $record
40 + * @param array $record New record.
32 41 *
33 42 * @return int
34 43 */
35 44 public function insert( $record ) {
@@ -45,23 +54,13 @@
45 54 * @return array
46 55 */
47 56 $record = apply_filters( 'wp_stream_record_array', $record );
48 57
49 - array_walk(
50 - $record, function( &$value, &$key ) {
51 - if ( ! is_array( $value ) ) {
52 - $value = strip_tags( $value );
53 - }
54 - }
55 - );
56 -
57 - if ( empty( $record ) ) {
58 + $data = $this->sanitize_record( $record );
59 + if ( empty( $data ) ) {
58 60 return false;
59 61 }
60 62
61 - $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' );
62 - $data = array_intersect_key( $record, array_flip( $fields ) );
63 -
64 63 $record_id = $this->driver->insert_record( $data );
65 64
66 65 if ( ! $record_id ) {
67 66 /**
@@ -86,8 +85,51 @@
86 85 return absint( $record_id );
87 86 }
88 87
89 88 /**
89 + * Ensure the record matches our schema.
90 + *
91 + * @param array $record Record to store.
92 + *
93 + * @return array
94 + */
95 + protected function sanitize_record( $record ) {
96 + if ( ! is_array( $record ) ) {
97 + return array();
98 + }
99 +
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(),
113 + );
114 +
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 + );
129 + }
130 +
131 + /**
90 132 * Returns array of existing values for requested column.
91 133 * Used to fill search filters with only used items, instead of all items.
92 134 *
93 135 * GROUP BY allows query to find just the first occurrence of each value in the column,
@@ -95,14 +137,14 @@
95 137 *
96 138 * @see assemble_records
97 139 * @since 1.0.4
98 140 *
99 - * @param string $column
141 + * @param string $column Table column to pull data from.
100 142 *
101 143 * @return array
102 144 */
103 145 public function existing_records( $column ) {
104 - // Sanitize column
146 + // Sanitize column.
105 147 $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
106 148 if ( ! in_array( $column, $allowed_columns, true ) ) {
107 149 return array();
108 150 }
@@ -129,39 +171,39 @@
129 171
130 172 /**
131 173 * Get stream records
132 174 *
133 - * @param array Query args
175 + * @param array $args Arguments to filter result by.
134 176 *
135 177 * @return array Stream Records
136 178 */
137 179 public function get_records( $args ) {
138 180 $defaults = array(
139 - // Search param
181 + // Search param.
140 182 'search' => null,
141 183 'search_field' => 'summary',
142 184 'record_after' => null, // Deprecated, use date_after instead
143 - // Date-based filters
144 - 'date' => null, // Ex: 2015-07-01
145 - 'date_from' => null, // Ex: 2015-07-01
146 - 'date_to' => null, // Ex: 2015-07-01
147 - 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00
148 - 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00
149 - // Record ID filters
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.
150 192 'record' => null,
151 193 'record__in' => array(),
152 194 'record__not_in' => array(),
153 - // Pagination params
195 + // Pagination params.
154 196 'records_per_page' => get_option( 'posts_per_page', 20 ),
155 197 'paged' => 1,
156 - // Order
198 + // Order.
157 199 'order' => 'desc',
158 200 'orderby' => 'date',
159 - // Fields selection
201 + // Fields selection.
160 202 'fields' => array(),
161 203 );
162 204
163 - // Additional property fields
205 + // Additional property fields.
164 206 $properties = array(
165 207 'user_id' => null,
166 208 'user_role' => null,
167 209 'ip' => null,
@@ -179,9 +221,9 @@
179 221 * @return array Array of query properties
180 222 */
181 223 $properties = apply_filters( 'wp_stream_query_properties', $properties );
182 224
183 - // Add property fields to defaults, including their __in/__not_in variations
225 + // Add property fields to defaults, including their __in/__not_in variations.
184 226 foreach ( $properties as $property => $default ) {
185 227 if ( ! isset( $defaults[ $property ] ) ) {
186 228 $defaults[ $property ] = $default;
187 229 }
@@ -207,9 +249,9 @@
207 249
208 250 /**
209 251 * Helper function, backwards compatibility
210 252 *
211 - * @param array $args Query args
253 + * @param array $args Argument to filter result by.
212 254 *
213 255 * @return array Stream Records
214 256 */
215 257 public function query( $args ) {
@@ -218,9 +260,9 @@
218 260
219 261 /**
220 262 * Return the number of records found in last request
221 263 *
222 - * return int
264 + * @return int
223 265 */
224 266 public function get_found_records_count() {
225 267 return $this->found_records_count;
226 268 }