Skip to content

Commit

Permalink
Fix: Make PCH Related Posts filters work for non-admins
Browse files Browse the repository at this point in the history
  • Loading branch information
acicovic committed May 9, 2024
1 parent f70172e commit f03c678
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'eb6c5ec223f3254dbe5a');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'a16d4a6df7ef859f50df');
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export const RelatedPostsPanel = (): JSX.Element => {
const noAuthorsExist = 0 === postData.authors.length;
const noTagsExist = 0 === postData.tags.length;
const noCategoriesExist = 0 === postData.categories.length;
const authorIsUnavailable = filterTypeIsAuthor && ! postData.authors.includes( filter.value );
const tagIsUnavailable = filterTypeIsTag && ! postData.tags.includes( filter.value );
const sectionIsUnavailable = filterTypeIsSection && ! postData.categories.includes( filter.value );

Expand All @@ -308,6 +309,8 @@ export const RelatedPostsPanel = (): JSX.Element => {
setFilter( { type: PostFilterType.Tag, value: postData.tags[ 0 ] } );
} else if ( sectionIsUnavailable ) {
setFilter( { type: PostFilterType.Section, value: postData.categories[ 0 ] } );
} else if ( authorIsUnavailable ) {
setFilter( { type: PostFilterType.Author, value: postData.authors[ 0 ] } );
} else {
fetchPosts( FETCH_RETRIES );
}
Expand Down Expand Up @@ -376,7 +379,7 @@ export const RelatedPostsPanel = (): JSX.Element => {

// No filter data could be retrieved. Prevent the component from rendering.
if ( postData.authors.length === 0 && postData.categories.length === 0 &&
postData.tags.length === 0
postData.tags.length === 0 && isPostDataReady
) {
return (
<div className="wp-parsely-related-posts">
Expand Down
6 changes: 3 additions & 3 deletions src/content-helper/editor-sidebar/related-posts/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function usePostData(): PostData {

if ( Number.isInteger( authorId ) ) {
authorRecords = getEntityRecords(
'root', 'user', { include: [ authorId ] }
'root', 'user', { include: [ authorId ], context: 'view' }
) ?? undefined; // Coalescing null to undefined
} else {
authorRecords = null;
Expand All @@ -66,7 +66,7 @@ export function usePostData(): PostData {
categoryIds.every( Number.isInteger )
) {
categoryRecords = getEntityRecords(
'taxonomy', 'category', { include: categoryIds }
'taxonomy', 'category', { include: categoryIds, context: 'view' }
) ?? undefined; // Coalescing null to undefined
} else {
categoryRecords = null;
Expand All @@ -76,7 +76,7 @@ export function usePostData(): PostData {
tagIds.every( Number.isInteger )
) {
tagRecords = getEntityRecords(
'taxonomy', 'post_tag', { include: tagIds }
'taxonomy', 'post_tag', { include: tagIds, context: 'view' }
) ?? undefined; // Coalescing null to undefined
} else {
tagRecords = null;
Expand Down

0 comments on commit f03c678

Please sign in to comment.