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

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

183 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 return (array) $wpdb->get_results(
142 "SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
143 'ARRAY_A'
144 );
145 }
146
147 /**
148 * Public getter to return table names
149 *
150 * @return array
151 */
152 public function get_table_names() {
153 return array(
154 $this->table,
155 $this->table_meta,
156 );
157 }
158
159 /**
160 * Init storage.
161 *
162 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
163 * @return \WP_Stream\Install
164 */
165 public function setup_storage( $plugin ) {
166 return new Install( $plugin );
167 }
168
169 /**
170 * Purge storage.
171 *
172 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
173 * @return \WP_Stream\Uninstall
174 */
175 public function purge_storage( $plugin ) {
176 $uninstall = new Uninstall( $plugin );
177 add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
178
179 return $uninstall;
180 }
181
182 }
183