| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Donation Activities DB class. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donation_Activities_DB |
| 6 |
* @version 1.8.1 |
| 7 |
* @author David Bisset |
| 8 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Donation_Activities_DB' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Donation_Activities_DB |
| 21 |
* |
| 22 |
* @since 1.8.1 |
| 23 |
*/ |
| 24 |
class Charitable_Donation_Activities_DB extends Charitable_DB { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @var Charitable_Donation_Activities_DB|null |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* The version of our database table |
| 35 |
* |
| 36 |
* @since 1.8.1 |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $version = '1.8.1'; |
| 40 |
|
| 41 |
/** |
| 42 |
* The name of the primary column |
| 43 |
* |
| 44 |
* @since 1.8.1 |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
public $primary_key = 'activity_id'; |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* The name of the table |
| 52 |
* |
| 53 |
* @since 1.8.1 |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public $table_name = 'charitable_donation_activities'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Set up the database table name. |
| 60 |
* |
| 61 |
* @since 1.8.1 |
| 62 |
*/ |
| 63 |
public function __construct() { |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
$this->table_name = $wpdb->prefix . 'charitable_donation_activities'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create the table. |
| 71 |
* |
| 72 |
* @global $wpdb |
| 73 |
* @since 1.8.1 |
| 74 |
*/ |
| 75 |
public function create_table() { |
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
$charset_collate = $wpdb->get_charset_collate(); |
| 79 |
|
| 80 |
$sql = "CREATE TABLE IF NOT EXISTS {$this->table_name} ( |
| 81 |
`activity_id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 82 |
`donation_id` int(11) unsigned DEFAULT NULL, |
| 83 |
`campaign_id` int(11) unsigned DEFAULT NULL, |
| 84 |
`donor_id` int(11) unsigned DEFAULT NULL, |
| 85 |
`type` varchar(25) DEFAULT NULL, |
| 86 |
`primary_action` varchar(25) DEFAULT NULL, |
| 87 |
`secondary_action` varchar(25) DEFAULT NULL, |
| 88 |
`amount` decimal(13,4) DEFAULT NULL, |
| 89 |
`created_by` int(11) DEFAULT NULL, |
| 90 |
`date_recorded` datetime DEFAULT NULL, |
| 91 |
PRIMARY KEY (`activity_id`) |
| 92 |
) $charset_collate;"; |
| 93 |
|
| 94 |
$this->_create_table( $sql ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Checks if the table exists. |
| 99 |
* |
| 100 |
* @global $wpdb |
| 101 |
* @since 1.8.1 |
| 102 |
*/ |
| 103 |
public function table_exists() { |
| 104 |
global $wpdb; |
| 105 |
|
| 106 |
return $wpdb->get_var( "SHOW TABLES LIKE '{$this->table_name}'" ) === $this->table_name; // phpcs:ignore |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Remove the table. |
| 111 |
* |
| 112 |
* @global $wpdb |
| 113 |
* @since 1.8.1 |
| 114 |
*/ |
| 115 |
public function remove_table() { |
| 116 |
global $wpdb; |
| 117 |
|
| 118 |
$sql = "DROP TABLE IF EXISTS {$this->table_name};"; |
| 119 |
|
| 120 |
$wpdb->query( $wpdb->prepare( $sql ) ); // phpcs:ignore |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Wipes all data from the table. |
| 125 |
* |
| 126 |
* @global $wpdb |
| 127 |
* @since 1.8.1 |
| 128 |
*/ |
| 129 |
public function clear_table() { |
| 130 |
global $wpdb; |
| 131 |
|
| 132 |
$sql = "TRUNCATE TABLE {$this->table_name};"; |
| 133 |
|
| 134 |
$wpdb->query( $wpdb->prepare( $sql ) ); // phpcs:ignore |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Whitelist of columns. |
| 139 |
* |
| 140 |
* @since 1.8.1 |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function get_columns() { |
| 145 |
return array( |
| 146 |
'activity_id' => '%d', |
| 147 |
'donation_id' => '%d', |
| 148 |
'campaign_id' => '%d', |
| 149 |
'donor_id' => '%d', |
| 150 |
'type' => '%s', |
| 151 |
'primary_action' => '%s', |
| 152 |
'secondary_action' => '%s', |
| 153 |
'amount' => '%f', |
| 154 |
'created_by' => '%d', |
| 155 |
'date_recorded' => '%s', |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Default column values. |
| 161 |
* |
| 162 |
* @since 1.8.1 |
| 163 |
* |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
public function get_column_defaults() { |
| 167 |
return array( |
| 168 |
'date_recorded' => gmdate( 'Y-m-d H:i:s' ), |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Add a new object. Currently a placeholder function for future use. |
| 174 |
* |
| 175 |
* @since 1.8.1 |
| 176 |
* |
| 177 |
* @param array $data Data to insert. |
| 178 |
* @param string $type Optional. Type of data to insert. Default is empty string. |
| 179 |
*/ |
| 180 |
public function insert( $data, $type = '' ) { |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Update a object. Currently a placeholder function for future use. |
| 185 |
* |
| 186 |
* @since 1.8.1 |
| 187 |
* |
| 188 |
* @param int $row_id The row ID. |
| 189 |
* @param array $data Optional. |
| 190 |
* @param array $where Optional. |
| 191 |
*/ |
| 192 |
public function update( $row_id, $data = array(), $where = '' ) { |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Delete a row identified by the primary key. Currently a placeholder function for future use. |
| 197 |
* |
| 198 |
* @since 1.8.1 |
| 199 |
* |
| 200 |
* @param int $row_id The row ID. |
| 201 |
*/ |
| 202 |
public function delete( $row_id = 0 ) { |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns and/or create the single instance of this class. |
| 207 |
* |
| 208 |
* @since 1.8.1 |
| 209 |
* |
| 210 |
* @return Charitable_Donation_Activities_DB |
| 211 |
*/ |
| 212 |
public static function get_instance() { |
| 213 |
if ( is_null( self::$instance ) ) { |
| 214 |
self::$instance = new self(); |
| 215 |
} |
| 216 |
|
| 217 |
return self::$instance; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
endif; |
| 222 |
|