PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.3
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 1.7.0.9 All 211 releases
charitable / includes / logger / class-charitable-log-db.php

class-charitable-log-db.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.3, at includes/logger/class-charitable-log-db.php

286 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Log DB class.
4 *
5 * @package Charitable/Classes/Charitable_Log_DB
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8.11
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Log_DB' ) ) :
18
19 /**
20 * Charitable_Log_DB
21 *
22 * @since 1.8.11
23 */
24 class Charitable_Log_DB extends Charitable_DB {
25
26 /**
27 * The version of our database table.
28 *
29 * @since 1.8.11
30 *
31 * @var string
32 */
33 public $version = '1.8.11';
34
35 /**
36 * The name of the primary column.
37 *
38 * @since 1.8.11
39 *
40 * @var string
41 */
42 public $primary_key = 'id';
43
44 /**
45 * Columns found in the database.
46 *
47 * @since 1.8.11
48 *
49 * @var array
50 */
51 protected $db_columns;
52
53 /**
54 * Set up the database table name.
55 *
56 * @since 1.8.11
57 *
58 * @global WPDB $wpdb
59 */
60 public function __construct() {
61 global $wpdb;
62
63 $this->table_name = $wpdb->prefix . 'charitable_logs';
64 }
65
66 /**
67 * Create the table.
68 *
69 * @since 1.8.11
70 *
71 * @global WPDB $wpdb
72 */
73 public function create_table() {
74 global $wpdb;
75
76 $charset_collate = $wpdb->get_charset_collate();
77
78 $sql = "CREATE TABLE {$this->table_name} (
79 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
80 title varchar(255) NOT NULL DEFAULT '',
81 message longtext DEFAULT NULL,
82 types varchar(255) NOT NULL DEFAULT 'log',
83 level varchar(20) NOT NULL DEFAULT 'info',
84 source varchar(100) NOT NULL DEFAULT 'core',
85 create_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
86 campaign_id bigint(20) unsigned DEFAULT 0,
87 donation_id bigint(20) unsigned DEFAULT 0,
88 donor_id bigint(20) unsigned DEFAULT 0,
89 user_id bigint(20) unsigned DEFAULT 0,
90 object_id bigint(20) unsigned DEFAULT 0,
91 object_type varchar(100) DEFAULT '',
92 PRIMARY KEY (id),
93 KEY source (source),
94 KEY level (level),
95 KEY types (types),
96 KEY object_id (object_id),
97 KEY object_type (object_type)
98 ) $charset_collate;";
99
100 $this->_create_table( $sql );
101 }
102
103 /**
104 * Return the list of columns that currently exist in the database.
105 *
106 * @since 1.8.11
107 *
108 * @global WPDB $wpdb
109 * @return array
110 */
111 public function get_db_columns() {
112 if ( ! isset( $this->db_columns ) ) {
113 global $wpdb;
114
115 $columns = $wpdb->get_results( "SHOW COLUMNS FROM $this->table_name" ); // phpcs:ignore
116 $this->db_columns = $columns ? wp_list_pluck( $columns, 'Field' ) : array();
117 }
118
119 return $this->db_columns;
120 }
121
122 /**
123 * Whitelist of columns.
124 *
125 * @since 1.8.11
126 *
127 * @return array
128 */
129 public function get_columns() {
130 return array(
131 'id' => '%d',
132 'title' => '%s',
133 'message' => '%s',
134 'types' => '%s',
135 'level' => '%s',
136 'source' => '%s',
137 'create_at' => '%s',
138 'campaign_id' => '%d',
139 'donation_id' => '%d',
140 'donor_id' => '%d',
141 'user_id' => '%d',
142 'object_id' => '%d',
143 'object_type' => '%s',
144 );
145 }
146
147 /**
148 * Default column values.
149 *
150 * @since 1.8.11
151 *
152 * @return array
153 */
154 public function get_column_defaults() {
155 return array(
156 'title' => '',
157 'message' => '',
158 'types' => 'log',
159 'level' => 'info',
160 'source' => 'core',
161 'create_at' => gmdate( 'Y-m-d H:i:s' ),
162 'campaign_id' => 0,
163 'donation_id' => 0,
164 'donor_id' => 0,
165 'user_id' => 0,
166 'object_id' => 0,
167 'object_type' => '',
168 );
169 }
170
171 /**
172 * Truncate the table.
173 *
174 * @since 1.8.11
175 *
176 * @global WPDB $wpdb
177 * @return bool
178 */
179 public function truncate_table() {
180 global $wpdb;
181
182 return false !== $wpdb->query( "TRUNCATE TABLE {$this->table_name}" ); // phpcs:ignore
183 }
184
185 /**
186 * Delete records older than a given number of days.
187 *
188 * @since 1.8.11
189 *
190 * @param int $days Number of days to retain.
191 *
192 * @global WPDB $wpdb
193 * @return int Number of rows deleted.
194 */
195 public function delete_older_than( $days ) {
196 global $wpdb;
197
198 $days = absint( $days );
199 $cutoff = gmdate( 'Y-m-d H:i:s', strtotime( "-{$days} days" ) );
200
201 return (int) $wpdb->query(
202 $wpdb->prepare( "DELETE FROM {$this->table_name} WHERE create_at < %s", $cutoff ) // phpcs:ignore
203 );
204 }
205
206 /**
207 * Check if the table exists.
208 *
209 * @since 1.8.11
210 *
211 * @global WPDB $wpdb
212 * @return bool
213 */
214 public function table_exists() {
215 global $wpdb;
216
217 $table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->table_name ) ); // phpcs:ignore
218
219 return $table === $this->table_name;
220 }
221
222 /**
223 * Return the total count of log records.
224 *
225 * @since 1.8.11
226 *
227 * @global WPDB $wpdb
228 * @return int
229 */
230 public function count() {
231 global $wpdb;
232
233 return (int) $wpdb->get_var( "SELECT COUNT(id) FROM {$this->table_name}" ); // phpcs:ignore
234 }
235
236 /**
237 * Batch insert multiple records.
238 *
239 * @since 1.8.11
240 *
241 * @param array $records Array of record arrays.
242 *
243 * @global WPDB $wpdb
244 * @return int|false Number of rows inserted or false on failure.
245 */
246 public function insert_batch( $records ) {
247 global $wpdb;
248
249 if ( empty( $records ) ) {
250 return 0;
251 }
252
253 $columns = array( 'title', 'message', 'types', 'level', 'source', 'create_at', 'campaign_id', 'donation_id', 'donor_id', 'user_id', 'object_id', 'object_type' );
254 $placeholders = array();
255 $values = array();
256
257 foreach ( $records as $record ) {
258 $placeholders[] = '(%s, %s, %s, %s, %s, %s, %d, %d, %d, %d, %d, %s)';
259 $values[] = $record['title'];
260 $values[] = $record['message'];
261 $values[] = $record['types'];
262 $values[] = $record['level'];
263 $values[] = $record['source'];
264 $values[] = $record['create_at'];
265 $values[] = $record['campaign_id'];
266 $values[] = $record['donation_id'];
267 $values[] = $record['donor_id'];
268 $values[] = $record['user_id'];
269 $values[] = $record['object_id'];
270 $values[] = $record['object_type'];
271 }
272
273 $column_list = implode( ', ', $columns );
274 $placeholder_str = implode( ', ', $placeholders );
275
276 return $wpdb->query(
277 $wpdb->prepare(
278 "INSERT INTO {$this->table_name} ({$column_list}) VALUES {$placeholder_str}", // phpcs:ignore
279 ...$values
280 )
281 );
282 }
283 }
284
285 endif;
286