Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 26 additions & 17 deletions src/lib/import-export/export/exporters/default-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ export class DefaultExporter extends EventEmitter implements Exporter {
private archiveBuilder!: archiver.Archiver;
private backup: BackupContents;
private readonly options: ExportOptions;
private readonly pathsToExclude = [
'wp-content/mu-plugins/sqlite-database-integration',
'wp-content/mu-plugins/0-allowed-redirect-hosts.php',
'wp-content/mu-plugins/0-check-theme-availability.php',
'wp-content/mu-plugins/0-deactivate-jetpack-modules.php',
'wp-content/mu-plugins/0-dns-functions.php',
'wp-content/mu-plugins/0-permalinks.php',
'wp-content/mu-plugins/0-wp-config-constants-polyfill.php',
'wp-content/mu-plugins/0-sqlite.php',
'wp-content/mu-plugins/0-thumbnails.php',
'wp-content/mu-plugins/0-https-for-reverse-proxy.php',
'wp-content/mu-plugins/0-sqlite-command.php',
];

private isExcludedPath( pathToCheck: string ) {

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.

Nice refactor. I find the code more readable now.

const pathsToExclude = [
'wp-content/mu-plugins/sqlite-database-integration',
'wp-content/database',
'wp-content/db.php',
'wp-content/debug.log',
'wp-content/mu-plugins/0-allowed-redirect-hosts.php',
'wp-content/mu-plugins/0-check-theme-availability.php',
'wp-content/mu-plugins/0-deactivate-jetpack-modules.php',
'wp-content/mu-plugins/0-dns-functions.php',
'wp-content/mu-plugins/0-permalinks.php',
'wp-content/mu-plugins/0-wp-config-constants-polyfill.php',
'wp-content/mu-plugins/0-sqlite.php',
'wp-content/mu-plugins/0-thumbnails.php',
'wp-content/mu-plugins/0-https-for-reverse-proxy.php',
'wp-content/mu-plugins/0-sqlite-command.php',
];
return pathsToExclude.some( ( pathToExclude ) =>
pathToCheck.startsWith( path.normalize( pathToExclude ) )
);
}

constructor( options: ExportOptions ) {
super();
Expand Down Expand Up @@ -177,11 +186,8 @@ export class DefaultExporter extends EventEmitter implements Exporter {
if ( stat.isDirectory() ) {
this.archiveBuilder.directory( fullPath, archivePath, ( entry ) => {
const fullArchivePath = path.join( archivePath, entry.name );
const isExcluded = this.pathsToExclude.some( ( pathToExclude ) =>
fullArchivePath.startsWith( path.normalize( pathToExclude ) )
);
if (
isExcluded ||
this.isExcludedPath( fullArchivePath ) ||
entry.name.includes( '.git' ) ||
entry.name.includes( 'node_modules' ) ||
entry.name.includes( 'cache' )
Expand All @@ -191,6 +197,9 @@ export class DefaultExporter extends EventEmitter implements Exporter {
return entry;
} );
} else {
if ( this.isExcludedPath( archivePath ) ) {
continue;
}
this.archiveBuilder.file( fullPath, { name: archivePath } );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,23 @@ platformTestSuite( 'DefaultExporter', ( { normalize } ) => {
isFile: () => true,
},
{
path: normalize( '/path/to/site' ),
name: 'wp-config.php',
path: normalize( '/path/to/site/wp-content' ),
name: 'debug.log',
isFile: () => true,
},
{
path: normalize( '/path/to/site/wp-content' ),
name: 'db.php',
isFile: () => true,
},
{
path: normalize( '/path/to/site/wp-content/database' ),
name: '.ht.sqlite',
isFile: () => true,
},
{
path: normalize( '/path/to/site/wp-content/mu-plugins/sqlite-database-integration' ),
name: 'example-load.php',
isFile: () => true,
},
];
Expand Down Expand Up @@ -569,4 +584,59 @@ platformTestSuite( 'DefaultExporter', ( { normalize } ) => {
sqlFiles: [],
} );
} );

it( "shouldn't include files like database, db.php and debug.log to the archive, even if specified", async () => {
const options = {
...mockOptions,
includes: {
database: true,
wpContent: true,
},
specificSelectionPaths: [
'database/.ht.sqlite',
'db.php',
'debug.log',
'mu-plugins/sqlite-database-integration/example-load.php',
],
};

const exporter = new DefaultExporter( options );
await exporter.export();

expect( fsPromises.stat ).toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/debug.log' )
);
expect( mockArchiver.file ).not.toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/debug.log' ),
{ name: 'wp-content/debug.log' }
);

expect( fsPromises.stat ).toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/db.php' )
);
expect( mockArchiver.file ).not.toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/db.php' ),
{ name: 'wp-content/db.php' }
);

expect( fsPromises.stat ).toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/database/.ht.sqlite' )
);
expect( mockArchiver.file ).not.toHaveBeenCalledWith(
normalize( '/path/to/site/wp-content/database/.ht.sqlite' ),
{ name: 'wp-content/database/.ht.sqlite' }
);

expect( fsPromises.stat ).toHaveBeenCalledWith(
normalize(
'/path/to/site/wp-content/mu-plugins/sqlite-database-integration/example-load.php'
)
);
expect( mockArchiver.file ).not.toHaveBeenCalledWith(
normalize(
'/path/to/site/wp-content/mu-plugins/sqlite-database-integration/example-load.php'
),
{ name: 'wp-content/mu-plugins/sqlite-database-integration/example-load.php' }
);
} );
} );
Loading