PluginProbe
The GDPR Framework By Data443 / 2.1.0
The GDPR Framework By Data443 v2.1.0
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Database / WordpressDatabase.php

WordpressDatabase.php in The GDPR Framework By Data443 2.1.0, at src/Database/WordpressDatabase.php

266 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\Database;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 /**
10 * DB base class
11 * https://pippinsplugins.com/custom-database-api-the-basic-api-class/
12 */
13 abstract class WordpressDatabase
14 {
15
16 /**
17 * The name of our database table
18 *
19 * @access public
20 * @since 2.1
21 */
22 public $tableName;
23
24 /**
25 * The version of our database table
26 *
27 * @access public
28 * @since 2.1
29 */
30 public $version;
31
32 /**
33 * The name of the primary column
34 *
35 * @access public
36 * @since 2.1
37 */
38 public $primaryKey;
39
40 /**
41 * Get things started
42 *
43 * @access public
44 * @since 2.1
45 */
46 public function __construct()
47 {
48 }
49
50 /**
51 * Whitelist of columns
52 *
53 * @access public
54 * @since 2.1
55 * @return array
56 */
57 public function getColumns()
58 {
59 return [];
60 }
61
62 /**
63 * Default column values
64 *
65 * @access public
66 * @since 2.1
67 * @return array
68 */
69 public function getColumnDefaults()
70 {
71 return [];
72 }
73
74 /**
75 * Retrieve a row by the primary key
76 *
77 * @access public
78 * @since 2.1
79 * @return object
80 */
81 public function get($row_id)
82 {
83 global $wpdb;
84
85 return $wpdb->get_row($wpdb->prepare(
86 "SELECT * FROM $this->tableName WHERE $this->primary_key = %s LIMIT 1;", $row_id
87 ));
88 }
89
90 /**
91 * Retrieve a row by a specific column / value
92 *
93 * @access public
94 * @since 2.1
95 * @return object
96 */
97 public function getBy($column, $row_id)
98 {
99 global $wpdb;
100 $column = esc_sql($column);
101
102 return $wpdb->get_row($wpdb->prepare(
103 "SELECT * FROM $this->tableName WHERE $column = %s LIMIT 1;", $row_id
104 ));
105 }
106
107 /**
108 * Retrieve a specific column's value by the primary key
109 *
110 * @access public
111 * @since 2.1
112 * @return string
113 */
114 public function getColumn($column, $row_id)
115 {
116 global $wpdb;
117 $column = esc_sql($column);
118
119 return $wpdb->get_var($wpdb->prepare(
120 "SELECT $column FROM $this->tableName WHERE $this->primary_key = %s LIMIT 1;", $row_id
121 ));
122 }
123
124 /**
125 * Retrieve a specific column's value by the the specified column / value
126 *
127 * @access public
128 * @since 2.1
129 * @return string
130 */
131 public function getColumnBy($column, $column_where, $column_value)
132 {
133 global $wpdb;
134 $column_where = esc_sql($column_where);
135 $column = esc_sql($column);
136
137 return $wpdb->get_var($wpdb->prepare(
138 "SELECT $column FROM $this->tableName WHERE $column_where = %s LIMIT 1;", $column_value
139 ));
140 }
141
142 /**
143 * Insert a new row
144 *
145 * @access public
146 * @since 2.1
147 * @return int
148 */
149 public function insert($data, $type = '')
150 {
151 global $wpdb;
152
153 // Set default values
154 $data = wp_parse_args($data, $this->getColumnDefaults());
155
156 do_action('bs_db_pre_insert_' . $type, $data);
157
158 // Initialise column format array
159 $columnFormats = $this->getColumns();
160
161 // Force fields to lower case
162 $data = array_change_key_case($data);
163
164 // White list columns
165 $data = array_intersect_key($data, $columnFormats);
166
167 // Reorder $columnFormats to match the order of columns given in $data
168 $data_keys = array_keys($data);
169 $columnFormats = array_merge(array_flip($data_keys), $columnFormats);
170
171 $wpdb->insert($this->tableName, $data, $columnFormats);
172
173 do_action('bs_db_post_insert_' . $type, $wpdb->insert_id, $data);
174
175 return $wpdb->insert_id;
176 }
177
178 /**
179 * Update a row
180 *
181 * @access public
182 * @since 2.1
183 * @return bool
184 */
185 public function update($row_id, $data = [], $where = '')
186 {
187 global $wpdb;
188
189 // Row ID must be positive integer
190 $row_id = absint($row_id);
191
192 if (empty($row_id)) {
193 return false;
194 }
195
196 if (empty($where)) {
197 $where = $this->primaryKey;
198 }
199
200 // Initialise column format array
201 $columnFormats = $this->getColumns();
202
203 // Force fields to lower case
204 $data = array_change_key_case($data);
205
206 // White list columns
207 $data = array_intersect_key($data, $columnFormats);
208
209 // Reorder $columnFormats to match the order of columns given in $data
210 $data_keys = array_keys($data);
211 $columnFormats = array_merge(array_flip($data_keys), $columnFormats);
212
213 if (false === $wpdb->update($this->tableName, $data, [$where => $row_id], $columnFormats)) {
214 return false;
215 }
216
217 return true;
218 }
219
220
221 /**
222 * Delete a row identified by the primary key
223 *
224 * @access public
225 * @since 2.1
226 * @return bool
227 */
228 public function delete($row_id = 0)
229 {
230 global $wpdb;
231
232 // Row ID must be positive integer
233 $row_id = absint($row_id);
234
235 if (empty($row_id)) {
236 return false;
237 }
238
239 if (false === $wpdb->query($wpdb->prepare(
240 "DELETE FROM $this->tableName WHERE $this->primary_key = %d", $row_id
241 ))) {
242 return false;
243 }
244
245 return true;
246 }
247
248 /**
249 * Check if the given table exists
250 *
251 * @since 2.4
252 * @param string $table The table name
253 * @return bool If the table name exists
254 */
255 public function tableExists($table)
256 {
257 global $wpdb;
258 $table = sanitize_text_field($table);
259
260 return $wpdb->get_var($wpdb->prepare(
261 "SHOW TABLES LIKE '%s'", $table
262 )) === $table;
263 }
264
265
266 }