🔥 Removed legacy product + price helpers from themes

refs: https://github.com/TryGhost/Team/issues/1145
refs: https://github.com/TryGhost/Ghost/issues/14446

- remove legacy members theme helpers @products @product @price and @members.products
- all of these have been replaced with new concepts with the introduction of tiers
This commit is contained in:
Hannah Wolfe 2022-05-12 18:52:39 +01:00
parent afd92813c8
commit b6d9389124
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
6 changed files with 2 additions and 157 deletions

View File

@ -1,65 +0,0 @@
// # Products Helper
// Usage: `{{products}}`, `{{products separator=' - '}}`
//
// Returns a string of the products with access to the post.
// By default, products are separated by commas.
const {labs} = require('../services/proxy');
const {SafeString} = require('../services/handlebars');
const isString = require('lodash/isString');
function products(options = {}) {
options = options || {};
options.hash = options.hash || {};
const separator = isString(options.hash.separator) ? options.hash.separator : '';
let output = '';
let productsList = [];
if (options.data.product) {
productsList = [options.data.product];
}
if (options.data.products) {
productsList = options.data.products;
}
let accessProductsList = [];
if (['members', 'paid', 'public'].includes(this.visibility)) {
accessProductsList = productsList;
}
if (this.visibility === 'tiers') {
accessProductsList = this.tiers;
}
if (accessProductsList.length > 0) {
const productNames = accessProductsList.map(product => product.name);
if (accessProductsList.length === 1) {
output = productNames[0] + ' tier';
} else {
if (separator) {
output = productNames.join(separator) + ' tiers';
} else {
const firsts = productNames.slice(0, productNames.length - 1);
const last = productNames[productNames.length - 1];
output = firsts.join(', ') + ' and ' + last + ' tiers';
}
}
}
return new SafeString(output);
}
module.exports = function productsLabsWrapper() {
let self = this;
let args = arguments;
return labs.enabledHelper({
flagKey: 'multipleProducts',
flagName: 'Tiers',
helperName: 'products',
helpUrl: 'https://ghost.org/docs/themes/'
}, () => {
return products.apply(self, args);
});
};

View File

@ -1,64 +1,10 @@
const hbs = require('../engine');
const urlUtils = require('../../../../shared/url-utils');
const {api} = require('../../proxy');
const settingsCache = require('../../../../shared/settings-cache');
const customThemeSettingsCache = require('../../../../shared/custom-theme-settings-cache');
const labs = require('../../../../shared/labs');
const activeTheme = require('../active');
function calculateLegacyPriceData(products) {
const defaultPrice = {
amount: 0,
currency: 'usd',
interval: 'year',
nickname: ''
};
function makePriceObject(price) {
const numberAmount = 0 + price.amount;
const dollarAmount = numberAmount ? Math.round(numberAmount / 100) : 0;
return {
valueOf() {
return dollarAmount;
},
amount: numberAmount,
currency: price.currency,
nickname: price.name,
interval: price.interval
};
}
const defaultProduct = products.find((product) => {
return product.type === 'paid';
}) || {};
const monthlyPrice = makePriceObject(defaultProduct.monthly_price || defaultPrice);
const yearlyPrice = makePriceObject(defaultProduct.yearly_price || defaultPrice);
const priceData = {
monthly: monthlyPrice,
yearly: yearlyPrice,
currency: monthlyPrice ? monthlyPrice.currency : defaultPrice.currency
};
return priceData;
}
async function getProductAndPricesData() {
try {
const page = await api.productsPublic.browse({
include: ['monthly_price', 'yearly_price', 'benefits'],
limit: 'all',
filter: 'active:true'
});
return page.products;
} catch (err) {
return [];
}
}
function getSiteData() {
let siteData = settingsCache.getPublic();
@ -85,16 +31,6 @@ async function updateGlobalTemplateOptions(req, res, next) {
image_sizes: activeTheme.get().config('image_sizes')
};
const themeSettingsData = customThemeSettingsCache.getAll();
const productData = await getProductAndPricesData();
const priceData = calculateLegacyPriceData(productData);
let products = null;
let product = null;
if (productData.length === 1) {
product = productData[0];
} else {
products = productData;
}
// @TODO: only do this if something changed?
{
@ -103,9 +39,6 @@ async function updateGlobalTemplateOptions(req, res, next) {
site: siteData,
labs: labsData,
config: themeData,
price: priceData,
product,
products,
custom: themeSettingsData
}
});

View File

@ -34,8 +34,7 @@ function updateLocalTemplateOptions(req, res, next) {
});
}),
paid: req.member.status !== 'free',
status: req.member.status,
products: req.member.products
status: req.member.status
} : null;
hbs.updateLocalTemplateOptions(res.locals, _.merge({}, localTemplateOptions, {

View File

@ -201,22 +201,6 @@ describe('Front-end members behaviour', function () {
});
});
describe('Price data', function () {
it('Can be used as a number, and with the price helper', async function () {
// Check out test/utils/fixtures/themes/price-data-test-theme/index.hbs
// To see where this is coming from.
//
const legacyUse = /You can use the price data as a number and currency: £12/;
const withPriceHelper = /You can pass price data to the price helper: £12/;
await request.get('/')
.expect((res) => {
should.exist(res.text.match(legacyUse));
should.exist(res.text.match(withPriceHelper));
});
});
});
describe('Content gating', function () {
let publicPost;
let membersPost;

View File

@ -13,7 +13,7 @@ describe('Helpers', function () {
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'raw', 'reading_time', 't', 'tags', 'title', 'twitter_url',
'url'
];
const experimentalHelpers = ['match', 'products', 'tiers'];
const experimentalHelpers = ['match', 'tiers'];
const expectedHelpers = _.concat(hbsHelpers, ghostHelpers, experimentalHelpers);

View File

@ -1,6 +0,0 @@
<p class="number">
You can use the price data as a number and currency: {{price currency=@price.currency}}{{@price.monthly}}
</p>
<p class="with-price-helper">
You can pass price data to the price helper: {{price @price.monthly}}
</p>