@@ -80,10 +80,36 @@ function markdown_database_integration_configured_backend(): string {
8080 if ( defined ( 'MARKDOWN_DB_BACKEND ' ) ) {
8181 return (string ) MARKDOWN_DB_BACKEND ;
8282 }
83- if ( defined ( 'FQDB ' ) && is_file ( FQDB ) ) {
83+ if ( defined ( 'DB_PATH ' ) || defined ( 'FQDB ' ) ) {
84+ $ path = defined ( 'DB_PATH ' ) ? DB_PATH : FQDB ;
85+ return is_string ( $ path ) && is_file ( $ path ) ? 'sqlite ' : 'mdi-native ' ;
86+ }
87+
88+ $ directory = markdown_database_integration_sqlite_directory ();
89+ if ( defined ( 'DB_FILE ' ) ) {
90+ return is_file ( $ directory . DB_FILE ) ? 'sqlite ' : 'mdi-native ' ;
91+ }
92+
93+ // Handle the randomized secret database path.
94+ if ( is_file ( $ directory . 'db-path.php ' ) ) {
8495 return 'sqlite ' ;
8596 }
86- return is_file ( __DIR__ . '/database/.ht.sqlite ' ) ? 'sqlite ' : 'mdi-native ' ;
97+ return is_file ( $ directory . '.ht.sqlite ' ) || is_file ( $ directory . '.ht.sqlite.php ' ) ? 'sqlite ' : 'mdi-native ' ;
98+ }
99+ }
100+
101+ if ( ! function_exists ( 'markdown_database_integration_sqlite_directory ' ) ) {
102+ /** Resolve the SQLite storage directory before integration constants load. */
103+ function markdown_database_integration_sqlite_directory (): string {
104+ if ( defined ( 'FQDBDIR ' ) ) {
105+ $ directory = FQDBDIR ;
106+ } elseif ( defined ( 'DB_DIR ' ) ) {
107+ $ directory = DB_DIR ;
108+ } else {
109+ $ directory = defined ( 'WP_CONTENT_DIR ' ) ? WP_CONTENT_DIR : __DIR__ ;
110+ $ directory .= '/database ' ;
111+ }
112+ return rtrim ( $ directory , '/ \\' ) . '/ ' ;
87113 }
88114}
89115
@@ -326,12 +352,13 @@ function markdown_database_integration_primary_index_path( string $content_dir,
326352 define ( 'WP_SQLITE_AST_DRIVER ' , true );
327353}
328354
329- // Primary mode uses markdown-index.sqlite as the active query engine. The
330- // SQLite Integration install shim opens FQDB directly during wp_install(). Only
331- // point FQDB at the markdown index when the markdown store already represents an
355+ // Primary mode uses markdown-index.sqlite as the active query engine.
356+ // Only point the SQLite installation shim at the index when the store represents an
332357// installed site. Partial seed stores fall back to the existing SQLite database
333358// so already-installed Playground sites do not reset to the install screen.
334- if ( defined ( 'MARKDOWN_DB_MODE ' ) && 'primary ' === MARKDOWN_DB_MODE ) {
359+ $ markdown_db_primary = defined ( 'MARKDOWN_DB_MODE ' ) && 'primary ' === MARKDOWN_DB_MODE ;
360+ $ markdown_db_install_fallback = false ;
361+ if ( $ markdown_db_primary ) {
335362 $ markdown_db_content_dir = defined ( 'MARKDOWN_DB_CONTENT_DIR ' )
336363 ? MARKDOWN_DB_CONTENT_DIR
337364 : markdown_db_default_content_dir ();
@@ -353,6 +380,37 @@ function markdown_database_integration_primary_index_path( string $content_dir,
353380 if ( ! defined ( 'FQDB ' ) ) {
354381 define ( 'FQDB ' , MARKDOWN_DB_INDEX_PATH );
355382 }
383+ } else {
384+ $ markdown_db_install_fallback = true ;
385+ if ( ! defined ( 'MARKDOWN_DB_INSTALL_FALLBACK ' ) ) {
386+ define ( 'MARKDOWN_DB_INSTALL_FALLBACK ' , true );
387+ }
388+ }
389+ }
390+
391+ // Select configured paths before older integrations define FQDB by default.
392+ $ markdown_db_sqlite_storage_file = $ sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/class-wp-sqlite-storage.php ' ;
393+ $ markdown_db_sqlite_path = null ;
394+ if ( defined ( 'DB_PATH ' ) ) {
395+ $ markdown_db_sqlite_path = DB_PATH ;
396+ } elseif ( defined ( 'FQDB ' ) ) {
397+ $ markdown_db_sqlite_path = FQDB ;
398+ } elseif ( defined ( 'DB_FILE ' ) ) {
399+ $ markdown_db_sqlite_path = markdown_database_integration_sqlite_directory () . DB_FILE ;
400+ } elseif ( ! is_file ( $ markdown_db_sqlite_storage_file ) ) {
401+ $ markdown_db_sqlite_path = markdown_database_integration_sqlite_directory () . '.ht.sqlite ' ;
402+ if ( ! is_file ( $ markdown_db_sqlite_path ) && is_file ( $ markdown_db_sqlite_path . '.php ' ) ) {
403+ $ markdown_db_sqlite_path .= '.php ' ;
404+ }
405+ }
406+
407+ // Older integrations read the legacy constants instead of DB_PATH.
408+ if ( null !== $ markdown_db_sqlite_path ) {
409+ if ( ! defined ( 'FQDB ' ) ) {
410+ define ( 'FQDB ' , $ markdown_db_sqlite_path );
411+ }
412+ if ( ! defined ( 'FQDBDIR ' ) && ':memory: ' !== $ markdown_db_sqlite_path ) {
413+ define ( 'FQDBDIR ' , rtrim ( dirname ( $ markdown_db_sqlite_path ), '/ \\' ) . '/ ' );
356414 }
357415}
358416
@@ -368,6 +426,11 @@ function markdown_database_integration_primary_index_path( string $content_dir,
368426// Load the SQLite v2 driver stack (parser, lexer, connection, driver).
369427require_once $ sqlite_plugin_implementation_folder_path . '/wp-includes/database/load.php ' ;
370428
429+ // Load SQLite storage support when available.
430+ if ( is_file ( $ markdown_db_sqlite_storage_file ) ) {
431+ require_once $ markdown_db_sqlite_storage_file ;
432+ }
433+
371434// SQLite Integration renamed the canonical PDO driver after its latest release.
372435if ( ! class_exists ( 'WP_MySQL_On_SQLite ' , false ) ) {
373436 if ( class_exists ( 'WP_PDO_MySQL_On_SQLite ' , false ) ) {
@@ -386,24 +449,6 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
386449require_once $ sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/class-wp-sqlite-db.php ' ;
387450require_once $ sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/install-functions.php ' ;
388451
389- if ( defined ( 'MARKDOWN_DB_MODE ' ) && 'primary ' === MARKDOWN_DB_MODE ) {
390- $ markdown_db_content_dir = defined ( 'MARKDOWN_DB_CONTENT_DIR ' )
391- ? MARKDOWN_DB_CONTENT_DIR
392- : markdown_db_default_content_dir ();
393- $ markdown_db_state_dir = defined ( 'MARKDOWN_DB_STATE_DIR ' )
394- ? MARKDOWN_DB_STATE_DIR
395- : $ markdown_db_content_dir ;
396-
397- if ( ! markdown_database_integration_store_has_siteurl ( $ markdown_db_state_dir ) ) {
398- if ( ! defined ( 'MARKDOWN_DB_INSTALL_FALLBACK ' ) ) {
399- define ( 'MARKDOWN_DB_INSTALL_FALLBACK ' , true );
400- }
401- $ db_name = defined ( 'DB_NAME ' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here ' ;
402- $ GLOBALS ['wpdb ' ] = new WP_SQLite_DB ( $ db_name );
403- return ;
404- }
405- }
406-
407452// Load our markdown classes.
408453$ markdown_plugin_dir = null ;
409454
@@ -420,32 +465,46 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
420465 }
421466}
422467
423- // If the markdown plugin isn't installed yet, fall back to standard SQLite.
424- if ( ! $ markdown_plugin_dir ) {
425- // Fallback: standard SQLite behavior.
426- if ( defined ( ' DB_NAME ' ) && '' !== DB_NAME ) {
427- $ db_name = DB_NAME ;
428- } else {
429- $ db_name = ' database_name_here ' ;
468+ $ markdown_db_sqlite_fallback = $ markdown_db_install_fallback || ! $ markdown_plugin_dir ;
469+
470+ if ( ! $ markdown_db_sqlite_fallback ) {
471+ // MDI is storage-only; content-format dependencies belong to the application.
472+ $ composer_autoload = $ markdown_plugin_dir . ' /vendor/autoload.php ' ;
473+ if ( file_exists ( $ composer_autoload ) ) {
474+ require_once $ composer_autoload ;
430475 }
431- $ GLOBALS ['wpdb ' ] = new WP_SQLite_DB ( $ db_name );
432- return ;
476+
477+ require_once $ markdown_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php ' ;
478+ $ markdown_db_backend = WP_Markdown_Backend_Resolver ::configure_from_globals ();
479+ WP_Markdown_Backend_Resolver ::require_runtime_capabilities (
480+ $ markdown_db_backend ,
481+ defined ( 'MARKDOWN_DB_MODE ' ) ? (string ) MARKDOWN_DB_MODE : 'mirror '
482+ );
483+ }
484+
485+ // Primary mode creates its index during reconstruction, after storage is loaded.
486+ if ( class_exists ( 'WP_SQLite_Storage ' ) && ( ! $ markdown_db_primary || $ markdown_db_sqlite_fallback ) ) {
487+ $ storage = null !== $ markdown_db_sqlite_path
488+ ? WP_SQLite_Storage ::with_explicit_path ( $ markdown_db_sqlite_path )
489+ : WP_SQLite_Storage ::with_secret_path ( FQDBDIR );
490+ $ markdown_db_sqlite_path = $ storage ->initialize ();
491+ if ( ! defined ( 'FQDB ' ) ) {
492+ define ( 'FQDB ' , $ markdown_db_sqlite_path );
493+ }
494+ }
495+ if ( ! defined ( 'DB_PATH ' ) ) {
496+ define ( 'DB_PATH ' , $ markdown_db_sqlite_path );
433497}
434498
435- // Load composer autoloader if present. MDI is storage-only; content-format
436- // dependencies belong to the application layer above this drop-in.
437- $ composer_autoload = $ markdown_plugin_dir . '/vendor/autoload.php ' ;
438- if ( file_exists ( $ composer_autoload ) ) {
439- require_once $ composer_autoload ;
499+ $ db_name = defined ( 'DB_NAME ' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here ' ;
500+
501+ // Use standard SQLite for installation or when the markdown plugin is missing.
502+ if ( $ markdown_db_sqlite_fallback ) {
503+ $ GLOBALS ['wpdb ' ] = new WP_SQLite_DB ( $ db_name );
504+ return ;
440505}
441506
442507// Load markdown integration classes.
443- require_once $ markdown_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php ' ;
444- $ markdown_db_backend = WP_Markdown_Backend_Resolver ::configure_from_globals ();
445- WP_Markdown_Backend_Resolver ::require_runtime_capabilities (
446- $ markdown_db_backend ,
447- defined ( 'MARKDOWN_DB_MODE ' ) ? (string ) MARKDOWN_DB_MODE : 'mirror '
448- );
449508require_once $ markdown_plugin_dir . '/inc/class-wp-markdown-frontmatter-profiles.php ' ;
450509require_once $ markdown_plugin_dir . '/inc/class-wp-markdown-content-layout-profiles.php ' ;
451510if ( defined ( 'MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP ' ) && is_file ( MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP ) ) {
@@ -466,12 +525,6 @@ class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
466525}
467526
468527// Create the database connection — our WP_Markdown_DB extends WP_SQLite_DB.
469- if ( defined ( 'DB_NAME ' ) && '' !== DB_NAME ) {
470- $ db_name = DB_NAME ;
471- } else {
472- $ db_name = 'database_name_here ' ;
473- }
474-
475528$ GLOBALS ['wpdb ' ] = new WP_Markdown_DB ( $ db_name );
476529markdown_database_integration_enable_native_shadow ( $ GLOBALS ['wpdb ' ], $ markdown_plugin_dir );
477530
0 commit comments