PluginProbe
Stream – Activity Log & Audit Trail / 3.1
Stream – Activity Log & Audit Trail v3.1
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.1, at classes/class-db.php

230 lines 4.6 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 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
19 *
20 * @var string
21 */
22 public $table;
23
24 /**
25 * Hold meta table name
26 *
27 * @var string
28 */
29 public $table_meta;
30
31 /**
32 * Class constructor.
33 *
34 * @param Plugin $plugin The main Plugin class.
35 */
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;
59 }
60
61 /**
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 * Insert a record
75 *
76 * @param array $record
77 *
78 * @return int
79 */
80 public function insert( $record ) {
81 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
82 return false;
83 }
84
85 /**
86 * Filter allows modification of record information
87 *
88 * @param array $record
89 *
90 * @return array
91 */
92 $record = apply_filters( 'wp_stream_record_array', $record );
93
94 array_walk( $record, function( &$value, &$key ) {
95 if ( ! is_array( $value ) ) {
96 $value = strip_tags( $value );
97 }
98 });
99
100 if ( empty( $record ) ) {
101 return false;
102 }
103
104 global $wpdb;
105
106 $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action' );
107 $data = array_intersect_key( $record, array_flip( $fields ) );
108
109 $result = $wpdb->insert( $this->table, $data );
110
111 if ( 1 !== $result ) {
112 /**
113 * Fires on a record insertion error
114 *
115 * @param array $record
116 * @param mixed $result
117 */
118 do_action( 'wp_stream_record_insert_error', $record, $result );
119
120 return $result;
121 }
122
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 /**
138 * Fires after a record has been inserted
139 *
140 * @param int $record_id
141 * @param array $record
142 */
143 do_action( 'wp_stream_record_inserted', $record_id, $record );
144
145 return absint( $record_id );
146 }
147
148 /**
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 * Returns array of existing values for requested column.
174 * Used to fill search filters with only used items, instead of all items.
175 *
176 * GROUP BY allows query to find just the first occurance of each value in the column,
177 * increasing the efficiency of the query.
178 *
179 * @see assemble_records
180 * @since 1.0.4
181 *
182 * @param string $column
183 *
184 * @return array
185 */
186 function existing_records( $column ) {
187 global $wpdb;
188
189 // Sanitize column
190 $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
191 if ( ! in_array( $column, $allowed_columns, true ) ) {
192 return array();
193 }
194
195 $rows = $wpdb->get_results(
196 "SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
197 'ARRAY_A'
198 );
199
200 if ( is_array( $rows ) && ! empty( $rows ) ) {
201 $output_array = array();
202
203 foreach ( $rows as $row ) {
204 foreach ( $row as $cell => $value ) {
205 $output_array[ $value ] = $value;
206 }
207 }
208
209 return (array) $output_array;
210 }
211
212 $column = sprintf( 'stream_%s', $column );
213
214 return isset( $this->plugin->connectors->term_labels[ $column ] ) ? $this->plugin->connectors->term_labels[ $column ] : array();
215 }
216
217 /**
218 * Helper function for calling $this->query->query()
219 *
220 * @see Query->query()
221 *
222 * @param array Query args
223 *
224 * @return array Stream Records
225 */
226 function query( $args ) {
227 return $this->query->query( $args );
228 }
229 }
230