PluginProbe
Stream – Activity Log & Audit Trail / 3.8.0
Stream – Activity Log & Audit Trail v3.8.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.8.0, at classes/class-db-driver-wpdb.php

181 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 $this->insert_meta( $record_id, $key, $val );
85 }
86 }
87
88 return $record_id;
89 }
90
91 /**
92 * Insert record meta
93 *
94 * @param int $record_id Record ID.
95 * @param string $key Meta Key.
96 * @param string $val Meta Data.
97 *
98 * @return array
99 */
100 public function insert_meta( $record_id, $key, $val ) {
101 global $wpdb;
102
103 $result = $wpdb->insert(
104 $this->table_meta,
105 array(
106 'record_id' => $record_id,
107 'meta_key' => $key,
108 'meta_value' => $val,
109 )
110 );
111
112 return $result;
113 }
114
115 /**
116 * Retrieve records
117 *
118 * @param array $args Query arguments.
119 *
120 * @return array
121 */
122 public function get_records( $args ) {
123 return $this->query->query( $args );
124 }
125
126 /**
127 * Returns array of existing values for requested column.
128 * Used to fill search filters with only used items, instead of all items.
129 *
130 * GROUP BY allows query to find just the first occurrence of each value in the column,
131 * increasing the efficiency of the query.
132 *
133 * @param string $column Column being filtered.
134 *
135 * @return array
136 */
137 public function get_column_values( $column ) {
138 global $wpdb;
139 return (array) $wpdb->get_results(
140 "SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name
141 'ARRAY_A'
142 );
143 }
144
145 /**
146 * Public getter to return table names
147 *
148 * @return array
149 */
150 public function get_table_names() {
151 return array(
152 $this->table,
153 $this->table_meta,
154 );
155 }
156
157 /**
158 * Init storage.
159 *
160 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
161 * @return \WP_Stream\Install
162 */
163 public function setup_storage( $plugin ) {
164 return new Install( $plugin );
165 }
166
167 /**
168 * Purge storage.
169 *
170 * @param \WP_Stream\Plugin $plugin Instance of the plugin.
171 * @return \WP_Stream\Uninstall
172 */
173 public function purge_storage( $plugin ) {
174 $uninstall = new Uninstall( $plugin );
175 add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
176
177 return $uninstall;
178 }
179
180 }
181