Skip to content

Commit

Permalink
fix: add/remove options in each multisite instance
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 2, 2023
1 parent 03d664c commit 10265e7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
45 changes: 24 additions & 21 deletions includes/class-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,27 @@ public static function update_options() {
* @return void
*/
public static function install() {
$db_version = get_option( 'wp_notifications_db_version' );
global $wpdb;

if ( ! $db_version ) {
self::create_tables();
update_option( 'wp_notifications_db_version', '1' );
// Engage multisite if in the middle of turning it on from network.php.
$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );

if ( $is_multisite ) {
// Get all blogs in the network and uninstall the plugin on each one.
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
// Loop over the individual sites and create tables for each.

foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );

self::create_tables();

restore_current_blog();
}
}

// If the options do not exist then create them
self::update_options();
// Always create the main site database tables and options.
self::create_tables();
}

/**
Expand All @@ -97,24 +109,15 @@ public static function activate() {
* @return void
*/
protected static function create_tables() {
global $wpdb;

// Engage multisite if in the middle of turning it on from network.php.
$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK );
$db_version = get_option( 'wp_notifications_db_version' );

if ( $is_multisite ) {
// Get all blogs in the network and uninstall the plugin on each one.
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
// Loop over the individual sites and create tables for each.
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::create_tables_v1();
restore_current_blog();
}
if ( ! $db_version ) {
self::create_tables_v1();
update_option( 'wp_notifications_db_version', WP_NOTIFICATION_CENTER_DB_VERSION );
}

// Always create a main tables for the main site.
self::create_tables_v1();
// If the options do not exist then create them
self::update_options();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions includes/class-uninstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ public static function uninstall() {
if ( $is_multisite ) {
// Get all blogs in the network and uninstall the plugin on each one.
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );

foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );

self::drop_tables();
self::delete_options();

restore_current_blog();
}
}

// Always remove the main site database tables and options.
self::drop_tables();
self::delete_options();
}
Expand Down
4 changes: 4 additions & 0 deletions wp-feature-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
define( 'WP_NOTIFICATION_CENTER_PLUGIN_VERSION', '0.0.1' );
}

if ( ! defined( 'WP_NOTIFICATION_CENTER_DB_VERSION' ) ) {
define( 'WP_NOTIFICATION_CENTER_DB_VERSION', '1' );
}

if ( ! defined( 'WP_NOTIFICATIONS_DEBUG' ) ) {
define( 'WP_NOTIFICATIONS_DEBUG', false );
}
Expand Down

0 comments on commit 10265e7

Please sign in to comment.