PluginProbe
Stream – Activity Log & Audit Trail / trunk
Stream – Activity Log & Audit Trail vtrunk
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-driver-wpdb.php

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

182 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Database Driver class for "stream" table responsible for holding records.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - DB_Driver_WPDB
12 */
13 class DB_Driver_WPDB implements DB_Driver {
14 /**
15 * Holds Query class
16 *
17 * @var Query
18 */
19 protected $query;
20
21 /**
22 * Hold records table name
23 *
24 * @var string
25 */
26 public $table;
27
28 /**
29 * Hold meta table name
30 *
31 * @var string
32 */
33 public $table_meta;
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 $this->query = new Query( $this );
40
41 global $wpdb;
42 $prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
43
44 $this->table = $prefix . 'stream';
45 $this->table_meta = $prefix . 'stream_meta';
46
47 $wpdb->stream = $this->table;
48 $wpdb->streammeta = $this->table_meta;
49
50 // Hack for get_metadata.
51 $wpdb->recordmeta = $this->table_meta;
52 }
53
54 /**
55 * Insert a record.
56 *
57 * @param array $data Data to insert.
58 *
59 * @return int
60 */
61 public function insert_record( $data ) {
62 global $wpdb;
63
64 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
65 return false;
66 }
67
68 $meta = array();
69 if ( array_key_exists( 'meta', $data ) ) {
70 $meta = $data['meta'];
71 unset( $data['meta'] );
72 }
73
74 $result = $wpdb->insert( $this->table, $data );
75 if ( ! $result ) {
76 return false;
77 }
78
79 $record_id = $wpdb->insert_id;
80
81 // Insert record meta.
82 foreach ( (array) $meta as $key => $vals ) {
83 foreach ( (array) $vals as $val ) {
84 if ( is_scalar( $val ) && '' !== $val ) {
85 $this->insert_meta( $record_id, $key, $val );
86 }
87 }
88 }
89
90 return $record_id;
91 }
92
93 /**
94 * Insert record meta
95 *
96 * @param int $record_id Record ID.
97 * @param string $key Meta Key.
98 * @param string $val Meta Data.
99 *
100 * @return array
101 */
102 public function insert_meta( $record_id, $key, $val ) {
103 global $wpdb;
104
105 $result = $wpdb->insert(
106 $this->table_meta,
107 array(
108 'record_id' => $record_id,
109 'meta_key' => $key,
110 'meta_value' => $val,
111 )
112 );
113
114 return $result;
115 }
116
117 /**
118 * Retrieve records
119 *
120 * @param array $args Query arguments.
121 *
122 * @return array
123 */
124 public function get_records( $args ) {
125 return $this->query->query( $args );
126 }
127
128 /**
129 * Returns array of existing values for requested column.
130 * Used to fill search filters with only used items, instead of all items.
131 *
132 * GROUP BY allows query to find just the first occurrence of each value in the column,
133 * increasing the efficiency of the query.
134 *
135 * @param string $column Column being filtered.
136 *
137 * @return array
138 */
139 public function get_column_values( $column ) {
140 global $wpdb;
141 if ( ! in_array( $column, Query::ALLOWED_FIELDS, true ) ) {
142 return array();
143 }
144
145 return (array) $wpdb->get_results(
146 "SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
147 'ARRAY_A'
148 );
149 }
150
151 /**
152 * Public getter to return table names
153 *
154 * @return array
155 */
156 public function get_table_names() {
157 return array(
158 $this->table,
159 $this->table_meta,
160 );
161 }
162
163 /**
164 * Init storage.
165 *
166 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
167 * @return \WP_Stream\Install
168 */
169 public function setup_storage( $plugin ) {
170 return new Install( $plugin );
171 }
172
173 /**
174 * Purge storage.
175 *
176 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
177 */
178 public function purge_storage( $plugin ) {
179 // @TODO: Not doing anything here until the deactivation/uninstall flow has been rethought.
180 }
181 }
182