0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

Added importer for custom theme settings (#15596)

closes: https://github.com/TryGhost/Ghost/issues/15542

- custom theme settings were not reinstated on import
- importing custom theme settings for the current active theme requires the theme be re-activated
This commit is contained in:
illiteratewriter 2022-10-21 19:32:32 +05:30 committed by GitHub
parent 11bea8d9f0
commit a701f2114d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,81 @@
const _ = require('lodash');
const Promise = require('bluebird');
const debug = require('@tryghost/debug')('importer:roles');
const BaseImporter = require('./base');
const models = require('../../../../models');
const {activate} = require('../../../../services/themes/activate');
class CustomThemeSettingsImporter extends BaseImporter {
constructor(allDataFromFile) {
super(allDataFromFile, {
modelName: 'CustomThemeSetting',
dataKeyToImport: 'custom_theme_settings'
});
}
beforeImport() {
debug('beforeImport');
return super.beforeImport();
}
doImport(options, importOptions) {
debug('doImport', this.modelName, this.dataToImport.length);
let ops = [];
_.each(this.dataToImport, (item) => {
ops.push(models.CustomThemeSetting.findOne({theme: item.theme, key: item.key}, options)
.then((setting) => {
if (_.isObject(item.value)) {
item.value = JSON.stringify(item.value);
}
if (setting) {
setting.set('value', item.value);
if (setting.hasChanged()) {
return setting.save(null, options)
.then((importedModel) => {
if (importOptions.returnImportedData) {
this.importedDataToReturn.push(importedModel.toJSON());
}
return importedModel;
})
.catch((err) => {
return this.handleError(err, item);
});
}
return Promise.resolve();
}
return models.CustomThemeSetting.add(item, options)
.then((importedModel) => {
if (importOptions.returnImportedData) {
this.importedDataToReturn.push(importedModel.toJSON());
}
return importedModel;
})
.catch((err) => {
return this.handleError(err, item);
});
})
.reflect());
});
const opsPromise = Promise.all(ops);
// activate function is called to refresh cache when importing custom theme settings for active theme
opsPromise.then(() => {
models.Settings.findOne({key: 'active_theme'})
.then((theme) => {
const currentTheme = theme.get('value');
if (this.dataToImport.some(themeSetting => themeSetting.theme === currentTheme)) {
activate(currentTheme);
}
});
});
return opsPromise;
}
}
module.exports = CustomThemeSettingsImporter;

View file

@ -13,6 +13,7 @@ const NewslettersImporter = require('./newsletters');
const ProductsImporter = require('./products');
const StripeProductsImporter = require('./stripe-products');
const StripePricesImporter = require('./stripe-prices');
const CustomThemeSettingsImporter = require('./custom-theme-settings');
const RolesImporter = require('./roles');
let importers = {};
let DataImporter;
@ -35,6 +36,7 @@ DataImporter = {
importers.stripe_products = new StripeProductsImporter(importData.data);
importers.stripe_prices = new StripePricesImporter(importData.data);
importers.posts = new PostsImporter(importData.data);
importers.custom_theme_settings = new CustomThemeSettingsImporter(importData.data);
return importData;
},