diff --git a/core/frontend/helpers/products.js b/core/frontend/helpers/products.js new file mode 100644 index 0000000000..a1a32d90ea --- /dev/null +++ b/core/frontend/helpers/products.js @@ -0,0 +1,68 @@ +// # 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 nql = require('@nexes/nql'); +const isString = require('lodash/isString'); +const {SafeString, labs} = require('../services/proxy'); + +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 === 'filter') { + const nqlFilter = nql(this.visibility_filter); + accessProductsList = productsList.filter((product) => { + return nqlFilter.queryJSON({product: product.slug}); + }); + } + + 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); + }); +}; diff --git a/core/frontend/helpers/tpl/content-cta.hbs b/core/frontend/helpers/tpl/content-cta.hbs index 47f4e37156..652242db25 100644 --- a/core/frontend/helpers/tpl/content-cta.hbs +++ b/core/frontend/helpers/tpl/content-cta.hbs @@ -8,7 +8,7 @@