PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.2
GiveWP – Donation Plugin and Fundraising Platform v2.0.2
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
441 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 * Set Minimum Index Length
38 *
39 * @since 2.0.1
40 * @access public
41 *
42 * @var int
43 */
44 public $min_index_length = 191;
45
46 /**
47 * The version of our database table
48 *
49 * @since 1.0
50 * @access public
51 *
52 * @var string
53 */
54 public $version;
55
56 /**
57 * The name of the primary column
58 *
59 * @since 1.0
60 * @access public
61 *
62 * @var string
63 */
64 public $primary_key;
65
66 /**
67 * Class Constructor
68 *
69 * Set up the Give DB Class.
70 *
71 * @since 1.0
72 * @access public
73 */
74 public function __construct() {
75 }
76
77 /**
78 * Whitelist of columns
79 *
80 * @since 1.0
81 * @access public
82 *
83 * @return array Columns and formats.
84 */
85 public function get_columns() {
86 return array();
87 }
88
89 /**
90 * Default column values
91 *
92 * @since 1.0
93 * @access public
94 *
95 * @return array Default column values.
96 */
97 public function get_column_defaults() {
98 return array();
99 }
100
101 /**
102 * Retrieve a row by the primary key
103 *
104 * @since 1.0
105 * @access public
106 *
107 * @param int $row_id Row ID.
108 *
109 * @return object
110 */
111 public function get( $row_id ) {
112 /* @var WPDB $wpdb */
113 global $wpdb;
114
115 // Bailout.
116 if ( empty( $row_id ) ) {
117 return null;
118 }
119
120 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
121 }
122
123 /**
124 * Retrieve a row by a specific column / value
125 *
126 * @since 1.0
127 * @access public
128 *
129 * @param int $column Column ID.
130 * @param int $row_id Row ID.
131 *
132 * @return object
133 */
134 public function get_by( $column, $row_id ) {
135 /* @var WPDB $wpdb */
136 global $wpdb;
137
138 // Bailout.
139 if ( empty( $column ) || empty( $row_id ) ) {
140 return null;
141 }
142
143 $column = esc_sql( $column );
144
145 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
146 }
147
148 /**
149 * Retrieve a specific column's value by the primary key
150 *
151 * @since 1.0
152 * @access public
153 *
154 * @param int $column Column ID.
155 * @param int $row_id Row ID.
156 *
157 * @return string Column value.
158 */
159 public function get_column( $column, $row_id ) {
160 /* @var WPDB $wpdb */
161 global $wpdb;
162
163 // Bailout.
164 if ( empty( $column ) || empty( $row_id ) ) {
165 return null;
166 }
167
168 $column = esc_sql( $column );
169
170 return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
171 }
172
173 /**
174 * Retrieve a specific column's value by the the specified column / value
175 *
176 * @since 1.0
177 * @access public
178 *
179 * @param int $column Column ID.
180 * @param string $column_where Column name.
181 * @param string $column_value Column value.
182 *
183 * @return string
184 */
185 public function get_column_by( $column, $column_where, $column_value ) {
186 /* @var WPDB $wpdb */
187 global $wpdb;
188
189 // Bailout.
190 if ( empty( $column ) || empty( $column_where ) || empty( $column_value ) ) {
191 return null;
192 }
193
194 $column_where = esc_sql( $column_where );
195 $column = esc_sql( $column );
196
197 return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
198 }
199
200 /**
201 * Insert a new row
202 *
203 * @since 1.0
204 * @access public
205 *
206 * @param array $data
207 * @param string $type
208 *
209 * @return int
210 */
211 public function insert( $data, $type = '' ) {
212 /* @var WPDB $wpdb */
213 global $wpdb;
214
215 // Set default values.
216 $data = wp_parse_args( $data, $this->get_column_defaults() );
217
218 /**
219 * Fires before inserting data to the database.
220 *
221 * @since 1.0
222 *
223 * @param array $data
224 */
225 do_action( "give_pre_insert_{$type}", $data );
226
227 // Initialise column format array
228 $column_formats = $this->get_columns();
229
230 // Force fields to lower case
231 // $data = array_change_key_case( $data );
232
233 // White list columns
234 $data = array_intersect_key( $data, $column_formats );
235
236 // Reorder $column_formats to match the order of columns given in $data
237 $data_keys = array_keys( $data );
238 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
239
240 $wpdb->insert( $this->table_name, $data, $column_formats );
241
242 /**
243 * Fires after inserting data to the database.
244 *
245 * @since 1.0
246 *
247 * @param int $insert_id
248 * @param array $data
249 */
250 do_action( "give_post_insert_{$type}", $wpdb->insert_id, $data );
251
252 return $wpdb->insert_id;
253 }
254
255 /**
256 * Update a row
257 *
258 * @since 1.0
259 * @access public
260 *
261 * @param int $row_id Column ID
262 * @param array $data
263 * @param string $where Column value
264 *
265 * @return bool
266 */
267 public function update( $row_id, $data = array(), $where = '' ) {
268 /* @var WPDB $wpdb */
269 global $wpdb;
270
271 // Row ID must be positive integer
272 $row_id = absint( $row_id );
273
274 if ( empty( $row_id ) ) {
275 return false;
276 }
277
278 if ( empty( $where ) ) {
279 $where = $this->primary_key;
280 }
281
282 // Initialise column format array
283 $column_formats = $this->get_columns();
284
285 // Force fields to lower case
286 $data = array_change_key_case( $data );
287
288 // White list columns
289 $data = array_intersect_key( $data, $column_formats );
290
291 // Reorder $column_formats to match the order of columns given in $data
292 $data_keys = array_keys( $data );
293 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
294
295 if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
296 return false;
297 }
298
299 return true;
300 }
301
302 /**
303 * Delete a row identified by the primary key
304 *
305 * @since 1.0
306 * @access public
307 *
308 * @param int $row_id Column ID.
309 *
310 * @return bool
311 */
312 public function delete( $row_id = 0 ) {
313 /* @var WPDB $wpdb */
314 global $wpdb;
315
316 // Row ID must be positive integer
317 $row_id = absint( $row_id );
318
319 if ( empty( $row_id ) ) {
320 return false;
321 }
322
323 if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
324 return false;
325 }
326
327 return true;
328 }
329
330 /**
331 * Check if the given table exists
332 *
333 * @since 1.3.2
334 * @access public
335 *
336 * @param string $table The table name.
337 *
338 * @return bool If the table name exists.
339 */
340 public function table_exists( $table ) {
341 /* @var WPDB $wpdb */
342 global $wpdb;
343
344 $table = sanitize_text_field( $table );
345
346 return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
347 }
348
349 /**
350 * Checks whether column exists in a table or not.
351 *
352 * @param string $column_name Name of the Column in Database Table.
353 *
354 * @since 1.8.18
355 *
356 * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe
357 *
358 * @return bool
359 */
360 public function does_column_exist( $column_name ) {
361
362 global $wpdb;
363
364 $column = $wpdb->get_results( $wpdb->prepare(
365 "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s ",
366 DB_NAME, $this->table_name, $column_name
367 ) );
368
369 if ( ! empty( $column ) ) {
370 return true;
371 }
372
373 return false;
374 }
375
376 /**
377 * Check if the table was ever installed
378 *
379 * @since 1.6
380 * @access public
381 *
382 * @return bool Returns if the customers table was installed and upgrade routine run.
383 */
384 public function installed() {
385 return $this->table_exists( $this->table_name );
386 }
387
388 /**
389 * Register tables
390 *
391 * @since 1.8.9
392 * @access public
393 */
394 public function register_table() {
395 $current_version = get_option( $this->table_name . '_db_version' );
396 if ( ! $current_version || version_compare( $current_version, $this->version, '<' ) ) {
397 $this->create_table();
398 }
399 }
400
401 /**
402 * Create table
403 *
404 * @since 1.8.9
405 * @access public
406 */
407 public function create_table() {
408 }
409
410
411 /**
412 * Given a ID, make sure it's a positive number, greater than zero before inserting or adding.
413 *
414 * @access private
415 * @since 2.0
416 *
417 * @param int $id A passed ID.
418 *
419 * @return int|bool The normalized log ID or false if it's found to not be valid.
420 */
421 public function sanitize_id( $id ) {
422 if ( ! is_numeric( $id ) ) {
423 return false;
424 }
425
426 $id = (int) $id;
427
428 // We were given a non positive number.
429 if ( absint( $id ) !== $id ) {
430 return false;
431 }
432
433 if ( empty( $id ) ) {
434 return false;
435 }
436
437 return absint( $id );
438
439 }
440 }
441