Skip to content

Commit 232894f

Browse files
committed
Make SQLite integration network-only
Require network activation on multisite and place SQLite management UI, notices, and links in Network Admin while retaining the existing single-site locations.
1 parent e69aad7 commit 232894f

6 files changed

Lines changed: 412 additions & 25 deletions

File tree

‎packages/plugin-sqlite-database-integration/activate.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
function sqlite_plugin_activation_redirect( $plugin ) {
1717
if ( plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) {
18-
if ( wp_safe_redirect( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) {
18+
if ( wp_safe_redirect( sqlite_plugin_get_admin_page_url() ) ) {
1919
exit;
2020
}
2121
}

‎packages/plugin-sqlite-database-integration/admin-notices.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ function sqlite_plugin_admin_notice() {
6666
/* translators: 1: db.php drop-in path, 2: Admin URL to deactivate the module */
6767
__( 'The SQLite Integration plugin is active, but the %1$s file is missing. Please <a href="%2$s">deactivate the plugin</a> and re-activate it to try again.', 'sqlite-database-integration' ),
6868
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>',
69-
esc_url( admin_url( 'plugins.php' ) )
69+
esc_url( self_admin_url( 'plugins.php' ) )
7070
)
7171
);
7272
}
73-
add_action( 'admin_notices', 'sqlite_plugin_admin_notice' ); // Add the admin notices.
73+
add_action( is_multisite() ? 'network_admin_notices' : 'admin_notices', 'sqlite_plugin_admin_notice' ); // Add the admin notices.
7474

7575
// Remove the PL-plugin admin notices for SQLite.
7676
remove_action( 'admin_notices', 'perflab_sqlite_plugin_admin_notice' );

‎packages/plugin-sqlite-database-integration/admin-page.php‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
* @since 1.0.0
1313
*/
1414
function sqlite_add_admin_menu() {
15-
add_options_page(
15+
$parent_slug = is_multisite() ? 'settings.php' : 'options-general.php';
16+
$capability = is_multisite() ? 'manage_network_options' : 'manage_options';
17+
18+
add_submenu_page(
19+
$parent_slug,
1620
__( 'SQLite integration', 'sqlite-database-integration' ),
1721
__( 'SQLite integration', 'sqlite-database-integration' ),
18-
'manage_options',
22+
$capability,
1923
'sqlite-integration',
2024
'sqlite_integration_admin_screen'
2125
);
2226
}
23-
add_action( 'admin_menu', 'sqlite_add_admin_menu' );
27+
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', 'sqlite_add_admin_menu' );
2428

2529
/**
2630
* The admin page contents.
@@ -56,7 +60,7 @@ function sqlite_integration_admin_screen() {
5660
printf(
5761
/* translators: 1: Admin URL to deactivate the module, 2: db.php drop-in path, */
5862
__( 'The SQLite drop-in is enabled. To disable it and get back to your previous, MySQL database, you can <a href="%1$s">deactivate the plugin</a>. Alternatively, you can manually delete the %2$s file from your server.', 'sqlite-database-integration' ),
59-
esc_url( admin_url( 'plugins.php' ) ),
63+
esc_url( self_admin_url( 'plugins.php' ) ),
6064
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
6165
);
6266
?>
@@ -78,7 +82,16 @@ function sqlite_integration_admin_screen() {
7882
?>
7983
</p>
8084
</div>
81-
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install&upgrade-from-pl' ), 'sqlite-install' ) ); ?>">
85+
<?php
86+
$upgrade_url = add_query_arg(
87+
array(
88+
'confirm-install' => '1',
89+
'upgrade-from-pl' => '1',
90+
),
91+
sqlite_plugin_get_admin_page_url()
92+
);
93+
?>
94+
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( $upgrade_url, 'sqlite-install' ) ); ?>">
8295
<?php
8396
printf(
8497
/* translators: %s: db.php drop-in path */
@@ -136,7 +149,7 @@ function sqlite_integration_admin_screen() {
136149

137150
<p><?php esc_html_e( 'By clicking the button below, you will be redirected to the WordPress installation screen to setup your new database', 'sqlite-database-integration' ); ?></p>
138151

139-
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install' ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a>
152+
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'confirm-install', '1', sqlite_plugin_get_admin_page_url() ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a>
140153
<?php endif; ?>
141154
</div>
142155
<?php
@@ -166,9 +179,23 @@ function sqlite_plugin_adminbar_item( $admin_bar ) {
166179
'id' => 'sqlite-db-integration',
167180
'parent' => 'top-secondary',
168181
'title' => $title,
169-
'href' => esc_url( admin_url( 'options-general.php?page=sqlite-integration' ) ),
182+
'href' => esc_url( sqlite_plugin_get_admin_page_url() ),
170183
'meta' => false,
171184
);
172185
$admin_bar->add_node( $args );
173186
}
174187
add_action( 'admin_bar_menu', 'sqlite_plugin_adminbar_item', 999 );
188+
189+
/**
190+
* Get the SQLite integration admin page URL.
191+
*
192+
* @access private
193+
*
194+
* @return string Admin page URL.
195+
*/
196+
function sqlite_plugin_get_admin_page_url() {
197+
if ( is_multisite() ) {
198+
return network_admin_url( 'settings.php?page=sqlite-integration' );
199+
}
200+
return admin_url( 'options-general.php?page=sqlite-integration' );
201+
}

‎packages/plugin-sqlite-database-integration/deactivate.php‎

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* Delete the db.php file in wp-content.
1111
*
1212
* When the plugin gets merged in wp-core, this is not to be ported.
13+
*
14+
* @param bool $network_deactivating Whether the plugin is being deactivated network-wide.
1315
*/
14-
function sqlite_plugin_remove_db_file() {
16+
function sqlite_plugin_remove_db_file( $network_deactivating = false ) {
1517
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) {
1618
return;
1719
}
@@ -33,7 +35,7 @@ function sqlite_plugin_remove_db_file() {
3335
// Run an action on `shutdown`, to deactivate the option in the MySQL database.
3436
add_action(
3537
'shutdown',
36-
function () {
38+
function () use ( $network_deactivating ) {
3739
global $table_prefix;
3840

3941
// Get credentials for the MySQL database.
@@ -46,23 +48,60 @@ function () {
4648
$wpdb_mysql = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
4749
$wpdb_mysql->set_prefix( $table_prefix );
4850

49-
// Get the perflab options, remove the database/sqlite module and update the option.
50-
$row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", 'active_plugins' ) );
51-
if ( is_object( $row ) ) {
52-
$value = maybe_unserialize( $row->option_value );
53-
if ( is_array( $value ) ) {
54-
$value_flipped = array_flip( $value );
55-
$items = array_reverse( explode( DIRECTORY_SEPARATOR, SQLITE_MAIN_FILE ) );
56-
$item = $items[1] . DIRECTORY_SEPARATOR . $items[0];
57-
unset( $value_flipped[ $item ] );
58-
$value = array_flip( $value_flipped );
59-
$wpdb_mysql->update( $wpdb_mysql->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => 'active_plugins' ) );
60-
}
61-
}
51+
sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating );
6252
},
6353
PHP_INT_MAX
6454
);
6555
// Flush any persistent cache.
6656
wp_cache_flush();
6757
}
6858
register_deactivation_hook( SQLITE_MAIN_FILE, 'sqlite_plugin_remove_db_file' ); // Remove db.php file on plugin deactivation.
59+
60+
/**
61+
* Deactivate the plugin in the original MySQL database.
62+
*
63+
* @access private
64+
*
65+
* @param wpdb $wpdb_mysql MySQL database connection.
66+
* @param bool $network_deactivating Whether the plugin is being deactivated network-wide.
67+
*/
68+
function sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating ) {
69+
if ( $network_deactivating ) {
70+
$network_id = get_current_network_id();
71+
$row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT meta_value AS active_plugins FROM $wpdb_mysql->sitemeta WHERE site_id = %d AND meta_key = %s LIMIT 1", $network_id, 'active_sitewide_plugins' ) );
72+
$table = $wpdb_mysql->sitemeta;
73+
$value_column = 'meta_value';
74+
$where = array(
75+
'site_id' => $network_id,
76+
'meta_key' => 'active_sitewide_plugins',
77+
);
78+
} else {
79+
$row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value AS active_plugins FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", 'active_plugins' ) );
80+
$table = $wpdb_mysql->options;
81+
$value_column = 'option_value';
82+
$where = array( 'option_name' => 'active_plugins' );
83+
}
84+
85+
if ( ! is_object( $row ) ) {
86+
return;
87+
}
88+
89+
$active_plugins = maybe_unserialize( $row->active_plugins );
90+
if ( ! is_array( $active_plugins ) ) {
91+
return;
92+
}
93+
94+
$item = plugin_basename( SQLITE_MAIN_FILE );
95+
if ( $network_deactivating ) {
96+
$key = $item;
97+
} else {
98+
$key = array_search( $item, $active_plugins, true );
99+
}
100+
101+
if ( false === $key || ! array_key_exists( $key, $active_plugins ) ) {
102+
return;
103+
}
104+
105+
unset( $active_plugins[ $key ] );
106+
$wpdb_mysql->update( $table, array( $value_column => maybe_serialize( $active_plugins ) ), $where );
107+
}

‎packages/plugin-sqlite-database-integration/load.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Author: The WordPress Team
66
* Version: 3.0.0-rc.7
77
* Requires PHP: 7.2
8+
* Network: true
89
* Textdomain: sqlite-database-integration
910
*
1011
* This feature plugin allows WordPress to use SQLite instead of MySQL as its database.

0 commit comments

Comments
 (0)