Skip to content

Commit

Permalink
Regenerate media thumbnails when importing any WordPress backup (Auto…
Browse files Browse the repository at this point in the history
…mattic#592)

* It runs wp media regenerate --yes WP-CLI commands after importing a Jetpack, Local, Playground or .wpress backup, fixing the thumbnails in ``
* There is a new import step for Jetpack sites that say Regenerating media…
* Add tests for the new step
  • Loading branch information
sejas authored Oct 11, 2024
1 parent 2f12994 commit 0131b1c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/hooks/tests/use-import-export.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ describe( 'useImportExport hook', () => {
expect( result.current.importState ).toEqual( {
[ SITE_ID ]: {
statusMessage: 'Importing WordPress content…',
progress: 90,
isNewSite: false,
},
} );

emitImportEvent( SITE_ID, ImportEvents.IMPORT_MEDIA_REGENERATE_START );
expect( result.current.importState ).toEqual( {
[ SITE_ID ]: {
statusMessage: 'Regenerating media…',
progress: 95,
isNewSite: false,
},
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/use-import-export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ export const ImportExportProvider = ( { children }: { children: React.ReactNode
...rest,
[ siteId ]: {
...currentProgress,
progress: 90,
},
} ) );
break;
case ImporterEvents.IMPORT_MEDIA_REGENERATE_START:
setImportState( ( { [ siteId ]: currentProgress, ...rest } ) => ( {
...rest,
[ siteId ]: {
...currentProgress,
statusMessage: __( 'Regenerating media…' ),
progress: 95,
},
} ) );
Expand Down
2 changes: 2 additions & 0 deletions src/lib/import-export/import/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const ImporterEvents = {
IMPORT_WP_CONTENT_COMPLETE: 'import_wp_content_complete',
IMPORT_META_START: 'import_meta',
IMPORT_META_COMPLETE: 'import_meta_complete',
IMPORT_MEDIA_REGENERATE_START: 'import_media_regenerate_start',
IMPORT_MEDIA_REGENERATE_COMPLETE: 'import_media_regenerate_complete',
IMPORT_COMPLETE: 'import_complete',
IMPORT_ERROR: 'import_error',
} as const;
Expand Down
24 changes: 21 additions & 3 deletions src/lib/import-export/import/importers/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ abstract class BaseBackupImporter extends BaseImporter {
this.meta = await this.parseMetaFile();
}
await this.importDatabase( rootPath, siteId, this.backup.sqlFiles );
await this.regenerateMedia( siteId );

this.emit( ImportEvents.IMPORT_COMPLETE );
return {
Expand All @@ -155,6 +156,19 @@ abstract class BaseBackupImporter extends BaseImporter {

protected abstract parseMetaFile(): Promise< MetaFileData | undefined >;

protected async regenerateMedia( siteId: string ): Promise< void > {
const server = SiteServer.get( siteId );
if ( ! server ) {
throw new Error( 'Site not found.' );
}
this.emit( ImportEvents.IMPORT_MEDIA_REGENERATE_START );
const { stderr } = await server.executeWpCliCommand( 'media regenerate --yes' );
if ( stderr ) {
console.error( 'Error during media regeneration:', stderr );
}
this.emit( ImportEvents.IMPORT_MEDIA_REGENERATE_COMPLETE );
}

protected async createEmptyDatabase( dbPath: string ): Promise< void > {
await fsPromises.writeFile( dbPath, '' );
}
Expand Down Expand Up @@ -190,10 +204,14 @@ abstract class BaseBackupImporter extends BaseImporter {
}

protected async importWpConfig( rootPath: string ): Promise< void > {
if ( ! this.backup.wpConfig ) {
return;
const wpConfigPath = path.join( rootPath, 'wp-config.php' );
const wpConfigSamplePath = path.join( rootPath, 'wp-config-sample.php' );

if ( this.backup.wpConfig ) {
await fsPromises.copyFile( this.backup.wpConfig, wpConfigPath );
} else if ( ! fs.existsSync( wpConfigPath ) && fs.existsSync( wpConfigSamplePath ) ) {
await fsPromises.copyFile( wpConfigSamplePath, wpConfigPath );
}
await fsPromises.copyFile( this.backup.wpConfig, `${ rootPath }/wp-config.php` );
}

protected async importWpContent( rootPath: string ): Promise< void > {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,13 @@ describe( 'JetpackImporter', () => {
expect( fs.copyFile ).toHaveBeenCalledTimes( 4 );
expect( fs.readFile ).toHaveBeenCalledWith( '/tmp/extracted/studio.json', 'utf-8' );
} );

it( 'should regenerate media after import', async () => {
const importer = new JetpackImporter( mockBackupContents );
await importer.import( mockStudioSitePath, mockStudioSiteId );

const siteServer = SiteServer.get( mockStudioSiteId );
expect( siteServer?.executeWpCliCommand ).toHaveBeenCalledWith( 'media regenerate --yes' );
} );
} );
} );

0 comments on commit 0131b1c

Please sign in to comment.