Skip to content

Commit fd0d282

Browse files
committed
Enable overriding Query Monitor's "wp-content/db.php" file
1 parent 0cb3bc7 commit fd0d282

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

activate.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ function sqlite_plugin_copy_db_file() {
8282

8383
$destination = WP_CONTENT_DIR . '/db.php';
8484

85+
/*
86+
* When an existing "db.php" drop-in is detected, let's check if it's a known
87+
* plugin that we can continue supporting even when we override the drop-in.
88+
*/
89+
$override_db_dropin = false;
90+
if ( file_exists( $destination ) ) {
91+
require_once ABSPATH . '/wp-admin/includes/file.php';
92+
global $wp_filesystem;
93+
if ( ! $wp_filesystem ) {
94+
WP_Filesystem();
95+
}
96+
97+
// Look for the Query Monitor plugin.
98+
if ( str_contains( $wp_filesystem->get_contents( $destination ), 'new QM_DB(' ) ) {
99+
$override_db_dropin = true;
100+
}
101+
102+
if ( $override_db_dropin ) {
103+
$wp_filesystem->delete( $destination );
104+
}
105+
}
106+
85107
// Place database drop-in if not present yet, except in case there is
86108
// another database drop-in present already.
87109
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) {

admin-page.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ function sqlite_add_admin_menu() {
2626
* The admin page contents.
2727
*/
2828
function sqlite_integration_admin_screen() {
29+
$db_dropin_path = WP_CONTENT_DIR . '/db.php';
30+
31+
/*
32+
* When an existing "db.php" drop-in is detected, let's check if it's a known
33+
* plugin that we can continue supporting even when we override the drop-in.
34+
*/
35+
$override_db_dropin = false;
36+
if ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
37+
require_once ABSPATH . '/wp-admin/includes/file.php';
38+
global $wp_filesystem;
39+
if ( ! $wp_filesystem ) {
40+
WP_Filesystem();
41+
}
42+
43+
// Look for the Query Monitor plugin.
44+
if ( str_contains( $wp_filesystem->get_contents( $db_dropin_path ), 'new QM_DB(' ) ) {
45+
$override_db_dropin = true;
46+
}
47+
}
48+
2949
?>
3050
<div class="wrap">
3151
<h1><?php esc_html_e( 'SQLite integration.', 'sqlite-database-integration' ); ?></h1>
@@ -50,7 +70,7 @@ function sqlite_integration_admin_screen() {
5070
<div class="notice notice-error">
5171
<p><?php esc_html_e( 'We detected that the PDO SQLite driver is missing from your server (the pdo_sqlite extension is not loaded). Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p>
5272
</div>
53-
<?php elseif ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) : ?>
73+
<?php elseif ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! $override_db_dropin ) : ?>
5474
<?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?>
5575
<div class="notice notice-warning">
5676
<p>
@@ -103,6 +123,22 @@ function sqlite_integration_admin_screen() {
103123
</div>
104124
<h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2>
105125
<p><?php esc_html_e( 'This plugin will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the plugin you will get back to your previous MySQL database, with all your previous data intact.', 'sqlite-database-integration' ); ?></p>
126+
127+
<?php if ( $override_db_dropin ) : ?>
128+
<p>
129+
<strong>NOTE:</strong>
130+
<?php
131+
printf(
132+
/* translators: %s: db.php drop-in path */
133+
esc_html__( 'We’ve detected an existing database drop-in file at %s, created by the Query Monitor plugin.', 'sqlite-database-integration' ),
134+
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
135+
);
136+
?>
137+
<?php esc_html_e( 'To enable SQLite support, this file will need to be replaced.', 'sqlite-database-integration' ); ?>
138+
<?php esc_html_e( 'The Query Monitor plugin will continue to function correctly after the change. You can safely proceed with the installation.', 'sqlite-database-integration' ); ?>
139+
</p>
140+
<?php endif; ?>
141+
106142
<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>
107143

108144
<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>

0 commit comments

Comments
 (0)