Skip to content

Commit 5d99e11

Browse files
authored
[N/A] Better import db support during setup (#199)
1 parent 437b372 commit 5d99e11

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

bin/composer-scripts/ProjectEvents/PostInstallScript.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
/**

bin/composer-scripts/ProjectEvents/PreScripts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PreScripts extends ComposerScript {
2020
'viget/viget-blocks-toolkit' => 'viget-blocks-toolkit',
2121
'viget/viget-form-blocks' => 'viget-form-blocks',
2222
'viget/viget-parts-kit' => 'viget-parts-kit',
23+
'viget/wp-sonny' => 'wp-sonny',
2324
];
2425

2526
/**

0 commit comments

Comments
 (0)