Skip to content

Commit 2c2435f

Browse files
committed
Unify the SQLite setup screen
Combine installation requirements, database location guidance, and the install action in one panel. Clarify that switching does not copy the existing content, while preserving installation guards and Query Monitor support.
1 parent e513393 commit 2c2435f

9 files changed

Lines changed: 528 additions & 179 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ function sqlite_activation() {
4646

4747
check_admin_referer( 'sqlite-install' );
4848

49+
/*
50+
* The drop-in would fail every request when it cannot create or write the
51+
* database. A stale install link can skip the check on the settings screen,
52+
* so return there to show the error.
53+
*/
54+
if ( ! sqlite_plugin_is_database_location_writable() ) {
55+
wp_safe_redirect( sqlite_plugin_get_admin_page_url() );
56+
exit;
57+
}
58+
4959
sqlite_plugin_copy_db_file();
5060

5161
// WordPress will automatically redirect to the install screen here.

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
color: #008a20;
8181
}
8282

83+
.sqlite-integration .sqlite-error {
84+
color: #d63638;
85+
}
86+
8387
.sqlite-integration .sqlite-box,
8488
.sqlite-integration .sqlite-help {
8589
background: #fff;
@@ -123,6 +127,10 @@
123127
margin: 0;
124128
}
125129

130+
.sqlite-integration .sqlite-setup-header p {
131+
margin: 8px 0 0;
132+
}
133+
126134
.sqlite-integration .sqlite-data p {
127135
margin: 0 0 10px;
128136
}
@@ -248,8 +256,48 @@
248256
cursor: pointer;
249257
}
250258

251-
.sqlite-integration .sqlite-installation .notice {
252-
margin-inline: 0;
259+
.sqlite-integration .sqlite-setup-header {
260+
padding: 20px;
261+
border-bottom: 1px solid #c3c4c7;
262+
}
263+
264+
.sqlite-integration .sqlite-setup-heading {
265+
display: flex;
266+
flex-wrap: wrap;
267+
align-items: center;
268+
justify-content: space-between;
269+
gap: 12px;
270+
}
271+
272+
.sqlite-integration .sqlite-setup-heading h2 {
273+
margin: 0;
274+
font-size: 16px;
275+
line-height: 1.5;
276+
}
277+
278+
.sqlite-integration .sqlite-inactive {
279+
color: #50575e;
280+
}
281+
282+
.sqlite-integration .sqlite-inactive .sqlite-status-dot {
283+
background: #787c82;
284+
}
285+
286+
.sqlite-integration .sqlite-setup-next {
287+
padding: 20px;
288+
border-top: 1px solid #c3c4c7;
289+
}
290+
291+
.sqlite-integration .sqlite-setup-next h3 {
292+
margin-top: 0;
293+
}
294+
295+
.sqlite-integration .sqlite-setup-actions {
296+
display: flex;
297+
flex-wrap: wrap;
298+
align-items: center;
299+
gap: 12px;
300+
margin-top: 20px;
253301
}
254302

255303
@media (pointer: coarse) {

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

Lines changed: 152 additions & 118 deletions
Large diffs are not rendered by default.

‎packages/plugin-sqlite-database-integration/storage-health.php‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ function sqlite_plugin_get_database_location_warning() {
2828
* @return string Location status: public, outside, memory, or unknown.
2929
*/
3030
function sqlite_plugin_get_database_location_status() {
31-
// The drop-in loads the storage only after SQLite is set up.
32-
if ( ! class_exists( 'WP_SQLite_Storage', false ) ) {
33-
require_once __DIR__ . '/wp-includes/sqlite/class-wp-sqlite-storage.php';
34-
}
35-
3631
list( $path, $is_secret_root ) = sqlite_plugin_get_database_location();
37-
if ( ! sqlite_plugin_is_valid_database_location( $path, $is_secret_root ) ) {
32+
if ( null === sqlite_plugin_get_database_storage( $path, $is_secret_root ) ) {
3833
return 'unknown';
3934
}
4035
if ( ':memory:' === $path ) {
@@ -80,14 +75,14 @@ function sqlite_plugin_get_database_location_status() {
8075
}
8176

8277
/**
83-
* Read the active path, or the planned legacy/default location before setup.
78+
* Check whether the drop-in can create and write the database at the configured location.
8479
*
8580
* @access private
86-
* @return mixed Configured path, which may be invalid.
81+
* @return bool Whether the location is valid and writable.
8782
*/
88-
function sqlite_plugin_get_database_path() {
89-
list( $path ) = sqlite_plugin_get_database_location();
90-
return $path;
83+
function sqlite_plugin_is_database_location_writable() {
84+
$storage = sqlite_plugin_get_database_storage( ...sqlite_plugin_get_database_location() );
85+
return null !== $storage && $storage->is_writable();
9186
}
9287

9388
/**
@@ -121,27 +116,27 @@ function sqlite_plugin_get_database_location() {
121116
}
122117

123118
/**
124-
* Check whether the drop-in accepts a database location.
119+
* Create a storage manager for a location without initializing the storage.
125120
*
126121
* @access private
127122
* @param mixed $path Configured path.
128123
* @param bool $is_secret_root Whether the path is the directory of a secret path.
129-
* @return bool Whether the drop-in can use the location.
124+
* @return WP_SQLite_Storage|null Storage manager, or null when the drop-in rejects the location.
130125
*/
131-
function sqlite_plugin_is_valid_database_location( $path, $is_secret_root ) {
126+
function sqlite_plugin_get_database_storage( $path, $is_secret_root ) {
132127
if ( ! is_string( $path ) ) {
133-
return false;
128+
return null;
129+
}
130+
131+
// The drop-in loads the storage only after SQLite is set up.
132+
if ( ! class_exists( 'WP_SQLite_Storage', false ) ) {
133+
require_once __DIR__ . '/wp-includes/sqlite/class-wp-sqlite-storage.php';
134134
}
135135
try {
136-
if ( $is_secret_root ) {
137-
WP_SQLite_Storage::with_secret_path( $path );
138-
} else {
139-
WP_SQLite_Storage::with_explicit_path( $path );
140-
}
136+
return $is_secret_root ? WP_SQLite_Storage::with_secret_path( $path ) : WP_SQLite_Storage::with_explicit_path( $path );
141137
} catch ( RuntimeException $exception ) {
142-
return false;
138+
return null;
143139
}
144-
return true;
145140
}
146141

147142
/**

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,44 @@ public function unlock(): void {
315315
}
316316
}
317317

318+
/**
319+
* Check whether PHP can create and write the database files.
320+
*
321+
* No files are created, so permissions may still change before initialization.
322+
* An initialized secret path can keep a read-only root, so a recorded database
323+
* path is trusted without loading it.
324+
*
325+
* @return bool Whether the storage is writable.
326+
*/
327+
public function is_writable(): bool {
328+
// An in-memory database has no files.
329+
if ( ':memory:' === $this->database_path ) {
330+
return true;
331+
}
332+
333+
if ( null === $this->database_path ) {
334+
if ( @is_file( $this->database_path_file ) ) {
335+
return true;
336+
}
337+
} elseif ( @file_exists( $this->database_path ) && ! @is_writable( $this->database_path ) ) {
338+
return false;
339+
}
340+
341+
// Like ensure_protected_directory(), find the existing directory to create missing ones in.
342+
$directory = $this->database_root;
343+
if ( ! @is_dir( $directory ) ) {
344+
for ( $directory = rtrim( $directory, '/\\' ); ! @is_dir( $directory ); $directory = dirname( $directory ) ) {
345+
// Another file is in the way, or no ancestor directory exists.
346+
if ( false !== @lstat( $directory ) || dirname( $directory ) === $directory ) {
347+
return false;
348+
}
349+
}
350+
}
351+
352+
// Database files and storage locks are created in this directory.
353+
return @is_writable( $directory );
354+
}
355+
318356
/**
319357
* Create a storage manager. Use with_secret_path() or with_explicit_path().
320358
*/

‎tests/phpunit/WP_SQLite_Database_Integration_Authorization_Test.php‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
require_once ABSPATH . 'wp-admin/includes/plugin.php';
44
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
55
require_once WP_CONTENT_DIR . '/plugins/sqlite-database-integration/load.php';
6+
require_once __DIR__ . '/WP_SQLite_Dropin_Test_Helpers.php';
67

78
class WP_SQLite_Database_Integration_Authorization_Test extends WP_UnitTestCase {
9+
use WP_SQLite_Dropin_Test_Helpers;
810

911
/**
1012
* @var array|null
@@ -784,43 +786,6 @@ function () {
784786
);
785787
}
786788

787-
private function run_without_dropin( $callback ) {
788-
$dropin_path = WP_CONTENT_DIR . '/db.php';
789-
$dropin_backup_path = tempnam( WP_CONTENT_DIR, 'db.php.' );
790-
791-
$this->assertNotFalse( $dropin_backup_path );
792-
$this->assertTrue( unlink( $dropin_backup_path ) );
793-
$this->assertTrue( rename( $dropin_path, $dropin_backup_path ) );
794-
795-
$restore_dropin = function () use ( $dropin_path, $dropin_backup_path ) {
796-
if ( ! file_exists( $dropin_backup_path ) ) {
797-
return file_exists( $dropin_path );
798-
}
799-
800-
return rename( $dropin_backup_path, $dropin_path );
801-
};
802-
$restore_on_shutdown = true;
803-
register_shutdown_function(
804-
function () use ( &$restore_on_shutdown, $restore_dropin ) {
805-
if ( $restore_on_shutdown ) {
806-
call_user_func( $restore_dropin );
807-
}
808-
}
809-
);
810-
811-
try {
812-
$result = call_user_func( $callback );
813-
} finally {
814-
$dropin_restored = call_user_func( $restore_dropin );
815-
$restore_on_shutdown = false;
816-
}
817-
818-
$this->assertTrue( $dropin_restored );
819-
$this->assertFileExists( $dropin_path );
820-
821-
return $result;
822-
}
823-
824789
private function assert_valid_nonce_reaches_install_redirect() {
825790
$nonce = wp_create_nonce( 'sqlite-install' );
826791
$redirect_url = null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
trait WP_SQLite_Dropin_Test_Helpers {
4+
5+
private function run_without_dropin( $callback ) {
6+
$dropin_path = WP_CONTENT_DIR . '/db.php';
7+
$dropin_backup_path = tempnam( WP_CONTENT_DIR, 'db.php.' );
8+
9+
$this->assertNotFalse( $dropin_backup_path );
10+
$this->assertTrue( unlink( $dropin_backup_path ) );
11+
$this->assertTrue( rename( $dropin_path, $dropin_backup_path ) );
12+
13+
$restore_dropin = function () use ( $dropin_path, $dropin_backup_path ) {
14+
if ( ! file_exists( $dropin_backup_path ) ) {
15+
return file_exists( $dropin_path );
16+
}
17+
18+
return rename( $dropin_backup_path, $dropin_path );
19+
};
20+
$restore_on_shutdown = true;
21+
register_shutdown_function(
22+
function () use ( &$restore_on_shutdown, $restore_dropin ) {
23+
if ( $restore_on_shutdown ) {
24+
call_user_func( $restore_dropin );
25+
}
26+
}
27+
);
28+
29+
try {
30+
$result = call_user_func( $callback );
31+
} finally {
32+
$dropin_restored = call_user_func( $restore_dropin );
33+
$restore_on_shutdown = false;
34+
}
35+
36+
$this->assertTrue( $dropin_restored );
37+
$this->assertFileExists( $dropin_path );
38+
39+
return $result;
40+
}
41+
}

0 commit comments

Comments
 (0)