| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\TemplateLibrary; |
| 4 |
|
| 5 |
use YayMail\Models\LibraryTemplateModel; |
| 6 |
|
| 7 |
/** |
| 8 |
* Ensures the library templates custom table exists (e.g. before migration runs in dev). |
| 9 |
*/ |
| 10 |
class LibraryTemplateSchema { |
| 11 |
|
| 12 |
/** |
| 13 |
* Create table via dbDelta if missing. |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public static function maybe_create_table() { |
| 18 |
global $wpdb; |
| 19 |
|
| 20 |
$table_name = LibraryTemplateModel::get_table_name(); |
| 21 |
|
| 22 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 23 |
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) === $table_name ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
self::create_table(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Run dbDelta for yaymail_library_templates. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public static function create_table() { |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$table_name = LibraryTemplateModel::get_table_name(); |
| 39 |
$charset_collate = $wpdb->get_charset_collate(); |
| 40 |
|
| 41 |
$sql = "CREATE TABLE {$table_name} ( |
| 42 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 43 |
public_id char(36) NOT NULL, |
| 44 |
email_type varchar(64) NOT NULL, |
| 45 |
name varchar(255) NOT NULL, |
| 46 |
description text NULL, |
| 47 |
elements longtext NOT NULL, |
| 48 |
email_settings text NOT NULL, |
| 49 |
global_header_settings text NULL, |
| 50 |
global_footer_settings text NULL, |
| 51 |
position smallint(5) unsigned NOT NULL DEFAULT 100, |
| 52 |
created_by bigint(20) unsigned NOT NULL DEFAULT 0, |
| 53 |
created_at datetime NOT NULL, |
| 54 |
updated_at datetime NOT NULL, |
| 55 |
PRIMARY KEY (id), |
| 56 |
UNIQUE KEY public_id (public_id), |
| 57 |
KEY idx_email_type (email_type) |
| 58 |
) {$charset_collate};"; |
| 59 |
|
| 60 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 61 |
dbDelta( $sql ); |
| 62 |
} |
| 63 |
} |
| 64 |
|