Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize post id arrays in includes and excludes #651

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { addQueryArgs } from '@wordpress/url';
* Internal dependencies
*/
import metadata from './block.json';
import { getBlockQueries } from './utils';
import { getBlockQueries, sanitizePostList } from './utils';

const { name } = metadata;
export const STORE_NAMESPACE = `newspack-blocks/${ name }`;
Expand Down Expand Up @@ -131,7 +131,7 @@ const createFetchPostsSaga = blockName => {
return acc;
}, [] );

let exclude = [ ...specificPostsId, getCurrentPostId() ];
let exclude = sanitizePostList( [ ...specificPostsId, getCurrentPostId() ] );
while ( blockQueries.length ) {
const nextBlock = blockQueries.shift();
nextBlock.postsQuery.exclude = exclude;
Expand Down
9 changes: 6 additions & 3 deletions src/blocks/homepage-articles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export const queryCriteriaFromAttributes = attributes => {
tagExclusions,
} = pick( attributes, POST_QUERY_ATTRIBUTES );

const isSpecificPostModeActive = specificMode && specificPosts && specificPosts.length;

const cleanPosts = sanitizePostList( specificPosts );
const isSpecificPostModeActive = specificMode && cleanPosts && cleanPosts.length;
const criteria = pickBy(
isSpecificPostModeActive
? {
include: specificPosts,
include: cleanPosts,
orderby: 'include',
per_page: specificPosts.length,
}
Expand All @@ -82,6 +82,9 @@ export const queryCriteriaFromAttributes = attributes => {
return criteria;
};

export const sanitizePostList = postList =>
postList.map( id => parseInt( id ) ).filter( id => id > 0 );

export const getBlockQueries = ( blocks, blockName ) =>
blocks.flatMap( block => {
const homepageArticleBlocks = [];
Expand Down