Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Studio: Remove wp-content on import
  • Loading branch information
kozer committed Aug 20, 2024
commit 3c64387ecda54eec8bbfa41bab5254add1e1cd26
25 changes: 25 additions & 0 deletions src/lib/import-export/import/importers/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ abstract class BaseBackupImporter extends BaseImporter {
const dbPath = path.join( databaseDir, '.ht.sqlite' );

await this.moveExistingDatabaseToTrash( dbPath );
await this.moveExistingWpContentToTrash( rootPath );
await this.createEmptyDatabase( dbPath );
await this.importWpConfig( rootPath );
await this.importWpContent( rootPath );
Expand Down Expand Up @@ -156,6 +157,30 @@ abstract class BaseBackupImporter extends BaseImporter {
await shell.trashItem( dbPath );
}

protected async moveExistingWpContentToTrash( rootPath: string ): Promise< void > {
const wpContentDir = path.join( rootPath, 'wp-content' );
try {
if ( ! fs.existsSync( wpContentDir ) ) {
return;
}
const contentToKeep = [ 'mu-plugins', 'database', 'db.php' ];

const contents = await fsPromises.readdir( wpContentDir );

for ( const content of contents ) {
const contentPath = path.join( wpContentDir, content );

if ( contentToKeep.includes( content ) ) {
continue;
}

await shell.trashItem( contentPath );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's delete those instead of trashing them. Running multiple imports produces many plugins/themes/uploads entries in the trash, which can be confusing. The confirmation window already makes it clear that import will replace all files and the database.

Ideally, we back up the whole site directory before import starts and revert to that state if import fails, but it would be an idea for another issue. The current solution tries to implement that, but not in a robust way.

}
} catch {
return
}
}

protected async importWpConfig( rootPath: string ): Promise< void > {
if ( ! this.backup.wpConfig ) {
return;
Expand Down