DB.php
87 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GDPR DB base class. |
| 4 | * |
| 5 | * @package Tutor\GDPR\DB |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\GDPR\DB; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Abstract GDPR DB table class. |
| 17 | */ |
| 18 | abstract class DB { |
| 19 | |
| 20 | /** |
| 21 | * Create all GDPR tables. |
| 22 | * |
| 23 | * @since 4.0.0 |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public static function create_tables() { |
| 28 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 29 | |
| 30 | foreach ( static::tables() as $table_class ) { |
| 31 | $sql = $table_class::get_schema(); |
| 32 | if ( ! empty( $sql ) ) { |
| 33 | dbDelta( $sql ); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Drop all GDPR tables. |
| 40 | * |
| 41 | * @since 4.0.0 |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public static function drop_tables() { |
| 46 | global $wpdb; |
| 47 | |
| 48 | foreach ( static::tables() as $table_class ) { |
| 49 | $table_name = $table_class::get_table_name(); |
| 50 | $wpdb->query( "DROP TABLE IF EXISTS {$table_name}" ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Registered GDPR table classes. |
| 56 | * |
| 57 | * @since 4.0.0 |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | protected static function tables() { |
| 62 | return array( |
| 63 | LegalConsents::class, |
| 64 | UserConsents::class, |
| 65 | Logs::class, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get table name. |
| 71 | * |
| 72 | * @since 4.0.0 |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | abstract public static function get_table_name(); |
| 77 | |
| 78 | /** |
| 79 | * Get create table SQL. |
| 80 | * |
| 81 | * @since 4.0.0 |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | abstract public static function get_schema(); |
| 86 | } |
| 87 |