0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added beta {{products}} helper for tiers list on post (#13267)

refs https://github.com/TryGhost/Team/issues/1004

- adds new `{{products}}` helper behind `multipleProducts` flag
- `{{products}}` outputs a string with list of products that have access to specific post when used in a post context in theme
- outputs empty string when used out of a post context and without access to `visibility` property
- uses all available posts for a site via the global products data
- updates {{content}} helper cta to use this new helper to show list of tiers with access to post
This commit is contained in:
Rishabh Garg 2021-09-10 09:30:41 +05:30 committed by GitHub
parent c7972c20a5
commit 2272f84cc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 2 deletions

View file

@ -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);
});
};

View file

@ -8,7 +8,7 @@
<h2>This post is for subscribers only</h2>
{{/has}}
{{#has visibility="filter"}}
<h2>This post is for subscribers on a higher tier only</h2>
<h2>This post is for subscribers on the {{products}} only </h2>
{{/has}}
{{#if @member}}
<a class="gh-btn" data-portal="account/plans" style="color:{{accentColor}}">Upgrade your account</a>

View file

@ -12,6 +12,7 @@ const registerAllCoreHelpers = function registerAllCoreHelpers() {
registerThemeHelper('cancel_link', coreHelpers.cancel_link);
registerThemeHelper('concat', coreHelpers.concat);
registerThemeHelper('content', coreHelpers.content);
registerThemeHelper('products', coreHelpers.products);
registerThemeHelper('date', coreHelpers.date);
registerThemeHelper('encode', coreHelpers.encode);
registerThemeHelper('excerpt', coreHelpers.excerpt);

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'];
const experimentalHelpers = ['match', 'products'];
const expectedHelpers = _.concat(hbsHelpers, ghostHelpers, experimentalHelpers);