| 1 |
<?php |
| 2 |
namespace ABlocksCookieConsent; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class Database { |
| 9 |
|
| 10 |
public static function table_name() { |
| 11 |
global $wpdb; |
| 12 |
return $wpdb->prefix . ABLOCKS_PLUGIN_SLUG . '_consent_records'; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Created when the addon is switched on rather than at plugin install, so |
| 17 |
* a site that never enables consent never grows the table. |
| 18 |
*/ |
| 19 |
public static function create_table() { |
| 20 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
$table = self::table_name(); |
| 24 |
$charset_collate = $wpdb->get_charset_collate(); |
| 25 |
|
| 26 |
// `consent_id` is not unique: a visitor who changes their mind produces |
| 27 |
// a second row, and the history is the point — a record that only ever |
| 28 |
// shows the latest state cannot demonstrate what was agreed and when. |
| 29 |
$sql = "CREATE TABLE $table ( |
| 30 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 31 |
consent_id VARCHAR(64) NOT NULL, |
| 32 |
user_id BIGINT(20) UNSIGNED NULL, |
| 33 |
policy_version INT(11) UNSIGNED NOT NULL DEFAULT 1, |
| 34 |
categories VARCHAR(255) NOT NULL DEFAULT '', |
| 35 |
decision VARCHAR(20) NOT NULL DEFAULT 'save', |
| 36 |
banner_hash VARCHAR(32) NOT NULL DEFAULT '', |
| 37 |
page_url VARCHAR(190) NULL, |
| 38 |
ip VARCHAR(100) NULL, |
| 39 |
user_agent VARCHAR(190) NULL, |
| 40 |
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, |
| 41 |
PRIMARY KEY (id), |
| 42 |
KEY consent_id (consent_id), |
| 43 |
KEY user_id (user_id), |
| 44 |
KEY created_at (created_at) |
| 45 |
) $charset_collate;"; |
| 46 |
|
| 47 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 48 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table}'" ) !== $table ) { |
| 49 |
dbDelta( $sql ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public static function table_exists() { |
| 54 |
global $wpdb; |
| 55 |
$table = self::table_name(); |
| 56 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 57 |
return $wpdb->get_var( "SHOW TABLES LIKE '{$table}'" ) === $table; |
| 58 |
} |
| 59 |
} |
| 60 |
|