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

202 lines 4.1 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 $recordarr
77 *
78 * @return int
79 */
80 public function insert( $recordarr ) {
81 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
82 return false;
83 }
84
85 /**
86 * Filter allows modification of record information
87 *
88 * @param array $recordarr
89 *
90 * @return array
91 */
92 $recordarr = apply_filters( 'wp_stream_record_array', $recordarr );
93
94 if ( empty( $recordarr ) ) {
95 return false;
96 }
97
98 global $wpdb;
99
100 $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action' );
101 $data = array_intersect_key( $recordarr, array_flip( $fields ) );
102 $data = array_filter( $data );
103 $result = $wpdb->insert( $this->table, $data );
104
105 if ( 1 !== $result ) {
106 /**
107 * Fires on a record insertion error
108 *
109 * @param array $recordarr
110 * @param mixed $result
111 */
112 do_action( 'wp_stream_record_insert_error', $recordarr, $result );
113
114 return $result;
115 }
116
117 $record_id = $wpdb->insert_id;
118
119 // Insert record meta
120 foreach ( (array) $recordarr['meta'] as $key => $vals ) {
121 // If associative array, serialize it, otherwise loop on its members
122 $vals = ( is_array( $vals ) && 0 !== key( $vals ) ) ? array( $vals ) : $vals;
123
124 foreach ( (array) $vals as $val ) {
125 $val = maybe_serialize( $val );
126
127 $this->insert_meta( $record_id, $key, $val );
128 }
129 }
130
131 /**
132 * Fires after a record has been inserted
133 *
134 * @param int $record_id
135 * @param array $recordarr
136 */
137 do_action( 'wp_stream_record_inserted', $record_id, $recordarr );
138
139 return absint( $record_id );
140 }
141
142 /**
143 * Insert record meta
144 *
145 * @param int $record_id
146 * @param string $key
147 * @param string $val
148 *
149 * @return array
150 */
151 public function insert_meta( $record_id, $key, $val ) {
152 global $wpdb;
153
154 $result = $wpdb->insert(
155 $this->table_meta,
156 array(
157 'record_id' => $record_id,
158 'meta_key' => $key,
159 'meta_value' => $val,
160 )
161 );
162
163 return $result;
164 }
165
166 /**
167 * Returns array of existing values for requested column.
168 * Used to fill search filters with only used items, instead of all items.
169 *
170 * GROUP BY allows query to find just the first occurance of each value in the column,
171 * increasing the efficiency of the query.
172 *
173 * @see assemble_records
174 * @since 1.0.4
175 *
176 * @param string $column
177 *
178 * @return array
179 */
180 function existing_records( $column ) {
181 global $wpdb;
182
183 $rows = $wpdb->get_results( "SELECT {$column} FROM $wpdb->stream GROUP BY {$column}", 'ARRAY_A' );
184
185 if ( is_array( $rows ) && ! empty( $rows ) ) {
186 $output_array = array();
187
188 foreach ( $rows as $row ) {
189 foreach ( $row as $cell => $value ) {
190 $output_array[ $value ] = $value;
191 }
192 }
193
194 return (array) $output_array;
195 }
196
197 $column = sprintf( 'stream_%s', $column );
198
199 return isset( $this->plugin->connectors->term_labels[ $column ] ) ? $this->plugin->connectors->term_labels[ $column ] : array();
200 }
201 }
202