PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / abstracts / abstract-class-charitable-db.php

abstract-class-charitable-db.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.11, at includes/abstracts/abstract-class-charitable-db.php

376 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable DB base class.
4 *
5 * Credit: This is based on Easy Digital Downloads' EDD_DB class. Major props to Pippin Williamson.
6 * https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/class-edd-db.php
7 *
8 * @package Charitable/Classes/Charitable_DB
9 * @author David Bisset
10 * @copyright Copyright (c) 2022, WP Charitable LLC
11 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
12 * @since 1.0.0
13 * @version 1.5.11
14 */
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 if ( ! class_exists( 'Charitable_DB' ) ) :
22
23 /**
24 * Charitable_DB
25 *
26 * @abstract
27 * @since 1.0.0
28 */
29 abstract class Charitable_DB {
30
31 /**
32 * The name of our database table
33 *
34 * @since 1.0.0
35 *
36 * @var string
37 */
38 public $table_name;
39
40 /**
41 * The version of our database table
42 *
43 * @since 1.0.0
44 *
45 * @var string
46 */
47 public $version;
48
49 /**
50 * The name of the primary column
51 *
52 * @since 1.0.0
53 *
54 * @var string
55 */
56 public $primary_key;
57
58 /**
59 * Get things started
60 *
61 * @since 1.0.0
62 */
63 public function __construct() {}
64
65 /**
66 * Whitelist of columns
67 *
68 * @since 1.0.0
69 *
70 * @return array
71 */
72 abstract public function get_columns();
73
74 /**
75 * Default column values
76 *
77 * @since 1.0.0
78 *
79 * @return array
80 */
81 abstract public function get_column_defaults();
82
83 /**
84 * Return the format for the given column.
85 *
86 * @since 1.0.0
87 *
88 * @param string $column The column to get the format for.
89 * @return %s, %d or %f
90 */
91 public function get_column_format( $column ) {
92 $columns = $this->get_columns();
93 $format = isset( $columns[ $column ] ) ? $columns[ $column ] : false;
94
95 // If the column isn't found, throw an exception.
96 if ( false === $format ) {
97 throw new Exception( sprintf( 'Invalid column passed %s', $column ) );
98 }
99
100 // If the column format isn't valid, throw an exception.
101 if ( ! in_array( $format, array( '%s', '%d', '%f' ) ) ) {
102 throw new Exception( sprintf( 'Invalid column format for column %s. Format returned %s', $column, $format ) );
103 }
104
105 return $format;
106 }
107
108 /**
109 * Retrieve a row by the primary key
110 *
111 * @since 1.0.0
112 *
113 * @return object
114 */
115 public function get( $row_id ) {
116 global $wpdb;
117
118 return $wpdb->get_row(
119 $wpdb->prepare(
120 "SELECT * FROM $this->table_name WHERE $this->primary_key = %d LIMIT 1;",
121 $row_id
122 )
123 );
124 }
125
126 /**
127 * Retrieve a row by a specific column / value
128 *
129 * @since 1.0.0
130 * @return object
131 */
132 public function get_by( $column, $row_id ) {
133 global $wpdb;
134
135 return $wpdb->get_row(
136 $wpdb->prepare(
137 "SELECT * FROM $this->table_name WHERE $column = {$this->get_column_format($column)} LIMIT 1;",
138 $row_id
139 )
140 );
141 }
142
143 /**
144 * Retrieve a specific column's value by the primary key.
145 *
146 * @since 1.0.0
147 *
148 * @return string
149 */
150 public function get_column( $column, $row_id ) {
151 global $wpdb;
152
153 return $wpdb->get_var(
154 $wpdb->prepare(
155 "SELECT $column FROM $this->table_name WHERE $this->primary_key = %d LIMIT 1;",
156 $row_id
157 )
158 );
159 }
160
161 /**
162 * Retrieve a specific column's value by the the specified column / value
163 *
164 * @since 1.0.0
165 *
166 * @param string $column The column with the value we are retrieving.
167 * @param string $column_where The column that we will be querying by.
168 * @param mixed $column_value The value we are searching for in the column set by $column_where.
169 * @return mixed
170 */
171 public function get_column_by( $column, $column_where, $column_value ) {
172 global $wpdb;
173
174 return $wpdb->get_var(
175 $wpdb->prepare(
176 "SELECT $column FROM $this->table_name WHERE $column_where = {$this->get_column_format($column_where)} LIMIT 1;",
177 $column_value
178 )
179 );
180 }
181
182 /**
183 * Retrieve all values in a specific column by the specified column / value.
184 *
185 * @since 1.5.11
186 *
187 * @param string $column The column with the value we are retrieving.
188 * @param string $column_where The column that we will be querying by.
189 * @param mixed $column_value The value we are searching for in the column set by $column_where.
190 * @return array
191 */
192 public function get_column_all_by( $column, $column_where, $column_value ) {
193 global $wpdb;
194
195 return $wpdb->get_col(
196 $wpdb->prepare(
197 "SELECT $column FROM $this->table_name WHERE $column_where = {$this->get_column_format($column_where)};",
198 $column_value
199 )
200 );
201 }
202
203 /**
204 * Count all rows.
205 *
206 * @since 1.0.0
207 *
208 * @return int
209 */
210 public function count_all() {
211 global $wpdb;
212
213 return $wpdb->get_var(
214 "SELECT COUNT( * ) FROM $this->table_name;"
215 );
216 }
217
218 /**
219 * Count all rows that certain criteria.
220 *
221 * @since 1.0.0
222 *
223 * @return int
224 */
225 public function count_by( $column, $column_value ) {
226 global $wpdb;
227
228 return $wpdb->get_var(
229 $wpdb->prepare(
230 "SELECT COUNT( * ) FROM $this->table_name WHERE $column = {$this->get_column_format($column)};",
231 $column_value
232 )
233 );
234 }
235
236 /**
237 * Insert a new row
238 *
239 * @since 1.0.0
240 *
241 * @return int
242 */
243 public function insert( $data, $type = '' ) {
244 global $wpdb;
245
246 /* Set default values */
247 $data = wp_parse_args( $data, $this->get_column_defaults() );
248
249 do_action( 'charitable_pre_insert_' . $type, $data );
250
251 /* Initialise column format array */
252 $column_formats = $this->get_columns();
253
254 /* Force fields to lower case */
255 $data = array_change_key_case( $data );
256
257 /* White list columns */
258 $data = array_intersect_key( $data, $column_formats );
259
260 /* Reorder $column_formats to match the order of columns given in $data */
261 $data_keys = array_keys( $data );
262 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
263
264 $inserted = $wpdb->insert( $this->table_name, $data, $column_formats );
265
266 /* If the insert failed, return 0 */
267 if ( false === $inserted ) {
268 return 0;
269 }
270
271 do_action( 'charitable_post_insert_' . $type, $wpdb->insert_id, $data );
272
273 return $wpdb->insert_id;
274 }
275
276 /**
277 * Update a row.
278 *
279 * @since 1.0.0
280 *
281 * @global WPDB $wpdb
282 * @param int $row_id The record to update.
283 * @param array $data Data to update.
284 * @param string $where Column used in where argument.
285 * @return boolean
286 */
287 public function update( $row_id, $data = array(), $where = '' ) {
288 global $wpdb;
289
290 // Row ID must be positive integer.
291 $row_id = absint( $row_id );
292
293 if ( empty( $row_id ) ) {
294 return false;
295 }
296
297 if ( empty( $where ) ) {
298 $where = $this->primary_key;
299 }
300
301 /* Initialise column format array. */
302 $column_formats = $this->get_columns();
303
304 /* Force fields to lower case */
305 $data = array_change_key_case( $data );
306
307 /* White list columns. */
308 $data = array_intersect_key( $data, $column_formats );
309
310 /* Reorder $column_formats to match the order of columns given in $data. */
311 $data_keys = array_keys( $data );
312 $column_formats = array_merge( array_flip( $data_keys ), $column_formats );
313
314 if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
315 return false;
316 }
317
318 return true;
319 }
320
321 /**
322 * Delete a row identified by the primary key
323 *
324 * @since 1.0.0
325 *
326 * @param int $row_id
327 * @return boolean
328 */
329 public function delete( $row_id = 0 ) {
330 // Row ID must be positive integer
331 $row_id = absint( $row_id );
332
333 if ( empty( $row_id ) ) {
334 return false;
335 }
336
337 return $this->delete_by( $this->primary_key, $row_id );
338 }
339
340 /**
341 * Delete a row identified by a specific column.
342 *
343 * @since 1.2.0
344 *
345 * @global WPDB $wpdb
346 * @param string $column The column to search for the row ID.
347 * @param mixed $row_id The row ID to be removed.
348 * @return boolean
349 */
350 public function delete_by( $column, $row_id = 0 ) {
351 global $wpdb;
352
353 $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $column = {$this->get_column_format($column)}", $row_id ) );
354
355 return false !== $result;
356 }
357
358 /**
359 * Create the table.
360 *
361 * @since 1.0.0
362 *
363 * @param string $sql The SQL statement to execute.
364 * @return void
365 */
366 protected function _create_table( $sql ) {
367 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
368
369 dbDelta( $sql );
370
371 update_option( $this->table_name . '_db_version', $this->version );
372 }
373 }
374
375 endif;
376