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
stream / classes / class-db.php

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

238 lines 5.5 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(
50 $record,
51 function( &$value, &$key ) {
52 if ( ! is_array( $value ) ) {
53 $value = strip_tags( $value );
54 }
55 }
56 );
57
58 if ( empty( $record ) ) {
59 return false;
60 }
61
62 $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' );
63 $data = array_intersect_key( $record, array_flip( $fields ) );
64
65 $record_id = $this->driver->insert_record( $data );
66
67 if ( ! $record_id ) {
68 /**
69 * Fires on a record insertion error
70 *
71 * @param array $record
72 * @param mixed $result
73 */
74 do_action( 'wp_stream_record_insert_error', $record, false );
75
76 return false;
77 }
78
79 /**
80 * Fires after a record has been inserted
81 *
82 * @param int $record_id
83 * @param array $record
84 */
85 do_action( 'wp_stream_record_inserted', $record_id, $record );
86
87 return absint( $record_id );
88 }
89
90 /**
91 * Returns array of existing values for requested column.
92 * Used to fill search filters with only used items, instead of all items.
93 *
94 * GROUP BY allows query to find just the first occurrence of each value in the column,
95 * increasing the efficiency of the query.
96 *
97 * @see assemble_records
98 * @since 1.0.4
99 *
100 * @param string $column
101 *
102 * @return array
103 */
104 public function existing_records( $column ) {
105 // Sanitize column
106 $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
107 if ( ! in_array( $column, $allowed_columns, true ) ) {
108 return array();
109 }
110
111 $rows = $this->driver->get_column_values( $column );
112
113 if ( is_array( $rows ) && ! empty( $rows ) ) {
114 $output_array = array();
115
116 foreach ( $rows as $row ) {
117 foreach ( $row as $cell => $value ) {
118 $output_array[ $value ] = $value;
119 }
120 }
121
122 return (array) $output_array;
123 }
124
125 $column = sprintf( 'stream_%s', $column );
126
127 $term_labels = wp_stream_get_instance()->connectors->term_labels;
128 return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array();
129 }
130
131 /**
132 * Get stream records
133 *
134 * @param array Query args
135 *
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
211 *
212 * @param array $args Query args
213 *
214 * @return array Stream Records
215 */
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();
236 }
237 }
238