Skip to content

Commit

Permalink
Follow up changes
Browse files Browse the repository at this point in the history
- Sanitize query params
  • Loading branch information
GP-Dan-Tovbein committed Jan 9, 2025
1 parent f558372 commit 40999b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
14 changes: 14 additions & 0 deletions assets/src/block-editor/QueryLoopBlockExtension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ export const setupQueryLoopBlockExtension = () => {
wp.data.select('core/block-editor').getSelectedBlock(),
]);

useEffect(() => {
const IS_NEW_IA = window.p4_vars.options.new_ia;
const queryPostType = IS_NEW_IA ? 'p4_action' : 'page';

if(query.postType !== queryPostType) {
setAttributes({
query: {
...query,
postType: IS_NEW_IA ? 'p4_action' : 'page',
}
});
}
}, [query])

return useMemo(() => (
<>
<InspectorControls>
Expand Down
25 changes: 5 additions & 20 deletions assets/src/blocks/ActionsList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export const registerActionsListBlock = () => {

const queryPostType = IS_NEW_IA ? 'p4_action' : 'page';

console.log("+++ IS_NEW_IA: %d ACT_PAGE: %d queryPostType: %s +++", IS_NEW_IA, ACT_PAGE, queryPostType)
console.log('Query', {
const query = {
pages: 0,
perPage: 3,
offset: 0,
order: 'desc',
orderBy: 'date',
postStatus: 'publish',
author: '',
search: '',
exclude: [],
Expand All @@ -29,8 +29,8 @@ export const registerActionsListBlock = () => {
postType: queryPostType,
postIn: [],
hasPassword: false,
...!IS_NEW_IA && {postParent: ACT_PAGE},
});
...!IS_NEW_IA && {parent: ACT_PAGE},
};

registerBlockVariation('core/query', {
name: ACTIONS_LIST_BLOCK_NAME,
Expand All @@ -44,22 +44,7 @@ export const registerActionsListBlock = () => {
attributes: {
namespace: ACTIONS_LIST_BLOCK_NAME,
className: 'actions-list p4-query-loop is-custom-layout-grid',
query: {
pages: 0,
perPage: 3,
offset: 0,
order: 'desc',
orderBy: 'date',
author: '',
search: '',
exclude: [],
sticky: '',
inherit: false,
postType: queryPostType,
postIn: [],
hasPassword: false,
...!IS_NEW_IA && {postParent: ACT_PAGE},
},
query,
layout: {
type: 'grid',
columnCount: 3,
Expand Down
3 changes: 0 additions & 3 deletions src/Blocks/Covers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public function __construct()
}

$attributes['covers'] = self::get_covers($attributes);
// var_dump(self::get_covers($attributes));
// die();
// $attributes['covers'] = [];

return self::hydrate_frontend($attributes, $content);
},
Expand Down
13 changes: 13 additions & 0 deletions src/Blocks/QueryLoopExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,26 @@ public static function registerFrontendQuery(): void
'query_loop_block_query_vars',
function ($query, $block) {
$blockQuery = $block->context['query'] ?? [];

// Sanitize query only applied to ActionsList block.
if ((bool) array_key_exists('isCustomQueryLoop', $blockQuery)) {
$is_new_ia = !empty(planet4_get_option('new_ia'));
$query['post_type'] = $is_new_ia ? 'p4_action' : 'page';

if(!$is_new_ia) {
$query['post_parent'] = $blockQuery['parent'];
}
}

if (!empty($blockQuery['postIn'])) {
$query['post__in'] = array_map('intval', (array) $blockQuery['postIn']);
$query['orderby'] = 'post__in';
}

if (isset($blockQuery['hasPassword'])) {
$query['has_password'] = (bool) $blockQuery['hasPassword'];
}

return $query;
},
10,
Expand Down

0 comments on commit 40999b8

Please sign in to comment.