PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.0
GiveWP – Donation Plugin and Fundraising Platform v2.0.0
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / class-give-db.php
give / includes Last commit date
admin 8 years ago api 8 years ago deprecated 8 years ago donors 8 years ago emails 8 years ago forms 8 years ago gateways 8 years ago libraries 8 years ago payments 8 years ago actions.php 8 years ago ajax-functions.php 8 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 8 years ago class-give-cache.php 8 years ago class-give-cli-commands.php 8 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 8 years ago class-give-db-form-meta.php 8 years ago class-give-db-logs-meta.php 8 years ago class-give-db-logs.php 8 years ago class-give-db-meta.php 8 years ago class-give-db-payment-meta.php 8 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor.php 8 years ago class-give-email-access.php 8 years ago class-give-gravatars.php 8 years ago class-give-html-elements.php 8 years ago class-give-license-handler.php 8 years ago class-give-logging.php 8 years ago class-give-roles.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 9 years ago class-give-tooltips.php 8 years ago class-give-translation.php 8 years ago class-notices.php 8 years ago country-functions.php 8 years ago currency-functions.php 8 years ago error-tracking.php 8 years ago filters.php 8 years ago formatting.php 8 years ago import-functions.php 8 years ago install.php 8 years ago login-register.php 8 years ago misc-functions.php 8 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 8 years ago process-donation.php 8 years ago scripts.php 8 years ago shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
class-give-db.php
431 lines
1 <?php
2 /**
3 * Give DB
4 *
5 * @package Give
6 * @subpackage Classes/Give_DB
7 * @copyright Copyright (c) 2016, WordImpress
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_DB Class
19 *
20 * This class is for interacting with the database table.
21 *
22 * @since 1.0
23 */
24 abstract class Give_DB {
25
26 /**
27 * The name of our database table
28 *
29 * @since 1.0
30 * @access public
31 *
32 * @var string
33 */
34 public $table_name;
35
36 /**
37 * The version of our database table
38 *
39 * @since 1.0
40 * @access public
41 *
42 * @var string
43 */
44 public $version;
45
46 /**
47 * The name of the primary column
48 *
49 * @since 1.0
50 * @access public
51 *
52 * @var string
53 */
54 public $primary_key;
55
56 /**
57 * Class Constructor
58 *
59 * Set up the Give DB Class.
60 *
61 * @since 1.0
62 * @access public
63 */
64 public function __construct() {
65 }
66
67 /**
68 * Whitelist of columns
69 *
70 * @since 1.0
71 * @access public
72 *
73 * @return array Columns and formats.
74 */
75 public function get_columns() {
76 return array();
77 }
78
79 /**
80 * Default column values
81 *
82 * @since 1.0
83 * @access public
84 *
85 * @return array Default column values.
86 */
87 public function get_column_defaults() {
88 return array();
89 }
90
91 /**
92 * Retrieve a row by the primary key
93 *
94 * @since 1.0
95 * @access public
96 *
97 * @param int $row_id Row ID.
98 *
99 * @return object
100 */
101 public function get( $row_id ) {
102 /* @var WPDB $wpdb */
103 global $wpdb;
104
105 // Bailout.
106 if ( empty( $row_id ) ) {
107 return null;
108 }
109
110 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
111 }
112
113 /**
114 * Retrieve a row by a specific column / value
115 *
116 * @since 1.0
117 * @access public
118 *
119 * @param int $column Column ID.
120 * @param int $row_id Row ID.
121 *
122 * @return object
123 */
124 public function get_by( $column, $row_id ) {
125 /* @var WPDB $wpdb */
126 global $wpdb;
127
128 // Bailout.
129 if ( empty( $column ) || empty( $row_id ) ) {
130 return null;
131 }
132
133 $column = esc_sql( $column );
134
135 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
136 }
137
138 /**
139 * Retrieve a specific column's value by the primary key
140 *
141 * @since 1.0
142 * @access public
143 *
144 * @param int $column Column ID.
145 * @param int $row_id Row ID.
146 *
147 * @return string Column value.
148 */
149 public function get_column( $column, $row_id ) {
150 /* @var WPDB $wpdb */
151 global $wpdb;
152
153 // Bailout.
154 if ( empty( $column ) || empty( $row_id ) ) {
155 return null;
156 }
157
158 $column = esc_sql( $column );
159
160 return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
161 }
162
163 /**
164 * Retrieve a specific column's value by the the specified column / value
165 *
166 * @since 1.0
167 * @access public
168 *
169 * @param int $column Column ID.
170 * @param string $column_where Column name.
171 * @param string $column_value Column value.
172 *
173 * @return string
174 */
175 public function get_column_by( $column, $column_where, $column_value ) {
176 /* @var WPDB $wpdb */
177 global $wpdb;
178
179 // Bailout.
180 if ( empty( $column ) || empty( $column_where ) || empty( $column_value ) ) {
181 return null;
182 }
183
184 $column_where = esc_sql( $column_where );
185 $column = esc_sql( $column );
186
187 return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
188 }
189
190 /**
191 * Insert a new row
192 *
193 * @since 1.0
194 * @access public
195 *
196 * @param array $data
197 * @param string $type
198 *
199 * @return int
200 */
201 public function insert( $data, $type = '' ) {
202 /* @var WPDB $wpdb */
203 global $wpdb;
204
205 // Set default values.
206 $data = wp_parse_args( $data, $this->get_column_defaults() );
207
208 /**
209 * Fires before inserting data to the database.
210 *
211 * @since 1.0
212 *
213 * @param array $data
214 */
215 do_action( "give_pre_insert_{$type}", $data );
216
217 // Initialise column format array
218 $column_formats = $this->get_columns();
219
220 // Force fields to lower case
221 // $data = array_change_key_case( $data );
222
223 // White list columns
224 $data = array_intersect_key( $data, $column_formats );
225
226 // Reorder $column_formats to match the order of columns given in $data
227 $data_keys = array_keys( $data );
228 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
229
230 $wpdb->insert( $this->table_name, $data, $column_formats );
231
232 /**
233 * Fires after inserting data to the database.
234 *
235 * @since 1.0
236 *
237 * @param int $insert_id
238 * @param array $data
239 */
240 do_action( "give_post_insert_{$type}", $wpdb->insert_id, $data );
241
242 return $wpdb->insert_id;
243 }
244
245 /**
246 * Update a row
247 *
248 * @since 1.0
249 * @access public
250 *
251 * @param int $row_id Column ID
252 * @param array $data
253 * @param string $where Column value
254 *
255 * @return bool
256 */
257 public function update( $row_id, $data = array(), $where = '' ) {
258 /* @var WPDB $wpdb */
259 global $wpdb;
260
261 // Row ID must be positive integer
262 $row_id = absint( $row_id );
263
264 if ( empty( $row_id ) ) {
265 return false;
266 }
267
268 if ( empty( $where ) ) {
269 $where = $this->primary_key;
270 }
271
272 // Initialise column format array
273 $column_formats = $this->get_columns();
274
275 // Force fields to lower case
276 $data = array_change_key_case( $data );
277
278 // White list columns
279 $data = array_intersect_key( $data, $column_formats );
280
281 // Reorder $column_formats to match the order of columns given in $data
282 $data_keys = array_keys( $data );
283 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
284
285 if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
286 return false;
287 }
288
289 return true;
290 }
291
292 /**
293 * Delete a row identified by the primary key
294 *
295 * @since 1.0
296 * @access public
297 *
298 * @param int $row_id Column ID.
299 *
300 * @return bool
301 */
302 public function delete( $row_id = 0 ) {
303 /* @var WPDB $wpdb */
304 global $wpdb;
305
306 // Row ID must be positive integer
307 $row_id = absint( $row_id );
308
309 if ( empty( $row_id ) ) {
310 return false;
311 }
312
313 if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
314 return false;
315 }
316
317 return true;
318 }
319
320 /**
321 * Check if the given table exists
322 *
323 * @since 1.3.2
324 * @access public
325 *
326 * @param string $table The table name.
327 *
328 * @return bool If the table name exists.
329 */
330 public function table_exists( $table ) {
331 /* @var WPDB $wpdb */
332 global $wpdb;
333
334 $table = sanitize_text_field( $table );
335
336 return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
337 }
338
339 /**
340 * Checks whether column exists in a table or not.
341 *
342 * @param string $column_name Name of the Column in Database Table.
343 *
344 * @since 1.8.18
345 *
346 * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe
347 *
348 * @return bool
349 */
350 public function does_column_exist( $column_name ) {
351
352 global $wpdb;
353
354 $column = $wpdb->get_results( $wpdb->prepare(
355 "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s ",
356 DB_NAME, $this->table_name, $column_name
357 ) );
358
359 if ( ! empty( $column ) ) {
360 return true;
361 }
362
363 return false;
364 }
365
366 /**
367 * Check if the table was ever installed
368 *
369 * @since 1.6
370 * @access public
371 *
372 * @return bool Returns if the customers table was installed and upgrade routine run.
373 */
374 public function installed() {
375 return $this->table_exists( $this->table_name );
376 }
377
378 /**
379 * Register tables
380 *
381 * @since 1.8.9
382 * @access public
383 */
384 public function register_table() {
385 $current_version = get_option( $this->table_name . '_db_version' );
386 if ( ! $current_version || version_compare( $current_version, $this->version, '<' ) ) {
387 $this->create_table();
388 }
389 }
390
391 /**
392 * Create table
393 *
394 * @since 1.8.9
395 * @access public
396 */
397 public function create_table() {
398 }
399
400
401 /**
402 * Given a ID, make sure it's a positive number, greater than zero before inserting or adding.
403 *
404 * @access private
405 * @since 2.0
406 *
407 * @param int $id A passed ID.
408 *
409 * @return int|bool The normalized log ID or false if it's found to not be valid.
410 */
411 public function sanitize_id( $id ) {
412 if ( ! is_numeric( $id ) ) {
413 return false;
414 }
415
416 $id = (int) $id;
417
418 // We were given a non positive number.
419 if ( absint( $id ) !== $id ) {
420 return false;
421 }
422
423 if ( empty( $id ) ) {
424 return false;
425 }
426
427 return absint( $id );
428
429 }
430 }
431