@@ -482,7 +482,8 @@ private static function isWordPressDbInstalled(): bool {
482482 * @return void
483483 */
484484 private static function importDatabase (): void {
485- $ databaseFile = self ::ask ( 'Please specify the path to the database file ' );
485+ $ defaultDatabaseFile = self ::locateDatabaseFile ();
486+ $ databaseFile = self ::ask ( 'Please specify the path to the database file ' , $ defaultDatabaseFile );
486487
487488 if ( ! $ databaseFile ) {
488489 self ::writeError ( 'No database file provided. ' );
@@ -506,6 +507,103 @@ private static function importDatabase(): void {
506507 self ::runCommand ( $ cmd );
507508
508509 self ::writeInfo ( 'Database imported. ' );
510+
511+ // Get the site URL with `wp option get siteurl`
512+ $ defaultSiteUrl = shell_exec ( 'wp option get siteurl ' );
513+ if ( ! $ defaultSiteUrl ) {
514+ $ defaultSiteUrl = '// ' ;
515+ } else {
516+ $ defaultSiteUrl = strtolower ( $ defaultSiteUrl );
517+ $ defaultSiteUrl = str_replace ( 'https:// ' , '// ' , $ defaultSiteUrl );
518+ $ defaultSiteUrl = str_replace ( 'http:// ' , '// ' , $ defaultSiteUrl );
519+ $ defaultSiteUrl = trim ( $ defaultSiteUrl );
520+ }
521+
522+ $ localUrl = strtolower ( self ::$ info ['url ' ] );
523+ $ localUrl = str_replace ( 'https:// ' , '// ' , $ localUrl );
524+ $ localUrl = str_replace ( 'http:// ' , '// ' , $ localUrl );
525+ $ localUrl = trim ( $ localUrl );
526+
527+ if ( $ defaultSiteUrl === $ localUrl ) {
528+ return ;
529+ }
530+
531+ $ siteUrl = self ::ask ( 'Please specify the URL in the database file to perform the search-replace on ' , $ defaultSiteUrl );
532+
533+ $ siteUrl = trim ( strtolower ( $ siteUrl ) );
534+
535+ if ( ! $ siteUrl ) {
536+ self ::writeError ( 'No site URL provided. Skipping search-replace. ' );
537+ return ;
538+ }
539+
540+ self ::writeInfo ( 'Updating the database URLs... ' );
541+
542+ // Run `wp search-replace` to replace the site URL.
543+ $ cmd = sprintf (
544+ 'wp search-replace %s %s ' ,
545+ escapeshellarg ( $ siteUrl ),
546+ escapeshellarg ( $ localUrl )
547+ );
548+
549+ self ::runCommand ( $ cmd );
550+
551+ self ::writeInfo ( 'Database URLs updated. ' );
552+ }
553+
554+ /**
555+ * Locate the database file.
556+ *
557+ * @return string
558+ */
559+ private static function locateDatabaseFile (): string {
560+ $ patterns = [
561+ '*.sql ' ,
562+ '*.sql.gz ' ,
563+ '*.sql.bz2 ' ,
564+ '*.sql.zip ' ,
565+ '*.sql.tar ' ,
566+ '*.sql.tar.gz ' ,
567+ '*.sql.tar.bz2 ' ,
568+ '*.sql.7z ' ,
569+ '*.sql.xz ' ,
570+ 'wp-content/*.sql ' ,
571+ 'wp-content/*.sql.gz ' ,
572+ 'wp-content/*.sql.bz2 ' ,
573+ 'wp-content/*.sql.zip ' ,
574+ 'wp-content/*.sql.tar ' ,
575+ 'wp-content/*.sql.tar.gz ' ,
576+ 'wp-content/*.sql.tar.bz2 ' ,
577+ 'wp-content/*.sql.7z ' ,
578+ 'wp-content/*.sql.xz ' ,
579+ ];
580+
581+ $ files = [];
582+ foreach ( $ patterns as $ pattern ) {
583+ $ globResult = glob ( self ::translatePath ( $ pattern ) );
584+ if ( $ globResult !== false && ! empty ( $ globResult ) ) {
585+ $ files = array_merge ( $ files , $ globResult );
586+ }
587+ }
588+
589+ if ( empty ( $ files ) ) {
590+ return '' ;
591+ }
592+
593+ usort ( $ files , function ( $ a , $ b ) {
594+ $ mtimeA = filemtime ( $ a );
595+ $ mtimeB = filemtime ( $ b );
596+
597+ // First sort by modification time (descending - most recent first)
598+ if ( $ mtimeA !== $ mtimeB ) {
599+ return $ mtimeB <=> $ mtimeA ;
600+ }
601+
602+ // If same modification time, sort by filename descending
603+ return strcmp ( $ b , $ a );
604+ } );
605+
606+ return $ files [0 ];
509607 }
510608
511609 /**
0 commit comments