Skip to content

Commit

Permalink
refactor: extract db table sql to Schema class
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 21, 2023
1 parent 3fc8156 commit 77e4429
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
46 changes: 11 additions & 35 deletions includes/class-activator.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php
/**
* Notifications API:Activator class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications;

use WP\Notifications\Database;

/**
* Class Activator.
*
* Defines the plugin's activation procedure.
*
* @package wordpress/wp-feature-notifications
*/
class Activator {

Expand Down Expand Up @@ -129,54 +134,25 @@ public static function create_tables_v1() {
global $wpdb;

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

$charset_collate = $wpdb->get_charset_collate();
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/database/class-schema.php';

$tables = $wpdb->get_results( 'SHOW TABLES' );

// Create the messages table
if ( ! in_array( $wpdb->prefix . 'notifications_messages', $tables, true ) ) {
$messages_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_messages` (
`id` BIGINT(20) NOT NULL,
`channel_name` VARCHAR(50) NOT NULL,
`channel_title` TINYTEXT NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`expires_at` DATETIME NULL,
`severity` VARCHAR(16) NULL,
`title` TINYTEXT NULL,
`message` TINYTEXT NULL,
`meta` TEXT NULL,
PRIMARY KEY (`id`),
KEY `channel_name` (`channel_name`)
) $charset_collate;\n";

$messages_sql = Database\Schema::messages_table_v1();
dbDelta( $messages_sql );
}

// Create the subscriptions table
if ( ! in_array( $wpdb->prefix . 'notifications_subscriptions', $tables, true ) ) {
$subscriptions_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_subscriptions` (
`user_id` BIGINT(20) NOT NULL,
`channel_name` VARCHAR(50) NOT NULL,
`snoozed_until` DATETIME NULL,
KEY `user_id` (`user_id`),
KEY `channel_name` (`channel_name`)
) $charset_collate;\n";

$subscriptions_sql = Database\Schema::subscriptions_table_v1();
dbDelta( $subscriptions_sql );
}

// Create the queue table
if ( ! in_array( $wpdb->prefix . 'notifications_queue', $tables, true ) ) {
$queue_sql = 'CREATE TABLE `' . $wpdb->prefix . "notifications_queue` (
`message_id` BIGINT(20) NOT NULL,
`user_id` BIGINT(20) NOT NULL,
`dismissed_at` DATETIME NULL,
`displayed_at` DATETIME NULL,
KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`)
) $charset_collate;\n";

$queue_sql = Database\Schema::queue_table_v1();
dbDelta( $queue_sql );
}
}
Expand Down
7 changes: 6 additions & 1 deletion includes/class-uninstaller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Notifications API:Uninstaller class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications;

Expand All @@ -19,7 +24,7 @@ class Uninstaller {
public static function full_reset() {
self::uninstall();

require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-activator.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/class-activator.php';

Activator::install();
}
Expand Down
66 changes: 66 additions & 0 deletions includes/database/class-schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Notifications API:Schema class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications\Database;

/**
* Class Schema
*
* Static class representing the database schema.
*/
final class Schema {

public static function messages_table_v1() {
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

return 'CREATE TABLE `' . $wpdb->prefix . "notifications_messages` (
`id` BIGINT(20) NOT NULL,
`channel_name` VARCHAR(50) NOT NULL,
`channel_title` TINYTEXT NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`expires_at` DATETIME NULL,
`severity` VARCHAR(16) NULL,
`title` TINYTEXT NULL,
`message` TINYTEXT NULL,
`meta` TEXT NULL,
PRIMARY KEY (`id`),
KEY `channel_name` (`channel_name`)
) $charset_collate;\n";
}


public static function subscriptions_table_v1() {
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

return 'CREATE TABLE `' . $wpdb->prefix . "notifications_subscriptions` (
`user_id` BIGINT(20) NOT NULL,
`channel_name` VARCHAR(50) NOT NULL,
`snoozed_until` DATETIME NULL,
KEY `user_id` (`user_id`),
KEY `channel_name` (`channel_name`)
) $charset_collate;\n";
}

public static function queue_table_v1() {
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

return 'CREATE TABLE `' . $wpdb->prefix . "notifications_queue` (
`message_id` BIGINT(20) NOT NULL,
`user_id` BIGINT(20) NOT NULL,
`dismissed_at` DATETIME NULL,
`displayed_at` DATETIME NULL,
KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`)
) $charset_collate;\n";
}
}

0 comments on commit 77e4429

Please sign in to comment.