PluginProbe
Stream – Activity Log & Audit Trail / 3.2.0
Stream – Activity Log & Audit Trail v3.2.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
stream / classes / class-db.php

class-db.php in Stream – Activity Log & Audit Trail 3.2.0, at classes/class-db.php

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