diff --git a/core/frontend/helpers/foreach.js b/core/frontend/helpers/foreach.js index 7612a62ac6..c1111fdbac 100644 --- a/core/frontend/helpers/foreach.js +++ b/core/frontend/helpers/foreach.js @@ -3,7 +3,7 @@ // // Block helper designed for looping through posts const _ = require('lodash'); -const {logging, i18n, hbs} = require('../services/proxy'); +const {logging, i18n, hbs, checks} = require('../services/proxy'); const {Utils: hbsUtils, handlebars: {createFrame}} = hbs; const ghostHelperUtils = require('@tryghost/helpers').utils; @@ -15,9 +15,16 @@ module.exports = function foreach(items, options) { if (hbsUtils.isFunction(items)) { items = items.call(this); } - + let visibility = options.hash.visibility; + if (_.isArray(items) && items.length > 0 && checks.isPost(items[0])) { + visibility = visibility || 'all'; + } else if (_.isObject(items) && _.isArray(Object.values(items))) { + if (Object.values(items).length > 0 && checks.isPost(Object.values(items)[0])) { + visibility = visibility || 'all'; + } + } // Exclude items which should not be visible in the theme - items = ghostHelperUtils.visibility.filter(items, options.hash.visibility); + items = ghostHelperUtils.visibility.filter(items, visibility); // Initial values set based on parameters sent through. If nothing sent, set to defaults const {fn, inverse, hash, data, ids} = options; diff --git a/test/unit/helpers/foreach_spec.js b/test/unit/helpers/foreach_spec.js index fbd0cc81ee..14db57d101 100644 --- a/test/unit/helpers/foreach_spec.js +++ b/test/unit/helpers/foreach_spec.js @@ -260,12 +260,28 @@ describe('{{#foreach}} helper', function () { } }; + const objectHashWithVis = { + posts: { + first: {title: 'first', visibility: 'members', slug: 'first', html: ''}, + second: {title: 'second'}, + third: {title: 'third'}, + fourth: {title: 'fourth'}, + fifth: {title: 'fifth'} + } + }; + const arrayHash = { posts: [ {title: 'first'}, {title: 'second'}, {title: 'third'}, {title: 'fourth'}, {title: 'fifth'} ] }; + const arrayHashWithVis = { + posts: [ + {title: 'first', visibility: 'members', slug: 'first', html: ''}, {title: 'second'}, {title: 'third'}, {title: 'fourth'}, {title: 'fifth'} + ] + }; + const arrayHash2 = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'}; const objectHash2 = { @@ -386,6 +402,13 @@ describe('{{#foreach}} helper', function () { shouldCompileToExpected(templateString, objectHash, expected); }); + it('foreach with limit 3 and post with members visibility', function () { + const templateString = '