| 1 |
<?php |
| 2 |
|
| 3 |
namespace YTP\Database; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound |
| 4 |
|
| 5 |
class Table |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Create a database table |
| 9 |
* |
| 10 |
* @param string $name |
| 11 |
* @param string $columns |
| 12 |
* @param integer $version |
| 13 |
* @param array $opts |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function create($name, $columns, $version = 1, $opts = []) |
| 17 |
{ |
| 18 |
$current_version = get_option("{$name}_database_version", 0); |
| 19 |
|
| 20 |
if ($version == $current_version) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
$full_table_name = $wpdb->prefix . sanitize_key($name); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 27 |
|
| 28 |
$opts = wp_parse_args($opts, [ |
| 29 |
'upgrade_method' => 'dbDelta', |
| 30 |
'table_options' => '', |
| 31 |
]); |
| 32 |
|
| 33 |
$charset_collate = ''; |
| 34 |
if ($wpdb->has_cap('collation')) { |
| 35 |
if (!empty($wpdb->charset)) { |
| 36 |
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 37 |
} |
| 38 |
if (!empty($wpdb->collate)) { |
| 39 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
$table_options = $charset_collate . ' ' . $opts['table_options']; |
| 44 |
|
| 45 |
// use dbDelta by default |
| 46 |
if ('dbDelta' == $opts['upgrade_method']) { |
| 47 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 48 |
dbDelta("CREATE TABLE $full_table_name ( $columns ) $table_options"); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 49 |
update_option("{$name}_database_version", $version); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ('delete_first' == $opts['upgrade_method']) { |
| 54 |
$wpdb->query("DROP TABLE IF EXISTS $full_table_name;"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 55 |
} |
| 56 |
|
| 57 |
$wpdb->query("CREATE TABLE IF NOT EXISTS $full_table_name ( $columns ) $table_options;"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 58 |
|
| 59 |
|
| 60 |
update_option("{$name}_database_version", $version); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Drops the table and database option |
| 65 |
* |
| 66 |
* @param string $name |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function drop($name) |
| 70 |
{ |
| 71 |
global $wpdb; |
| 72 |
$wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . sanitize_key($name)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 73 |
delete_option("{$name}_database_version"); |
| 74 |
} |
| 75 |
} |
| 76 |
|