Skip to content

Commit

Permalink
feat: allow Homepage Posts and Carousel blocks to include subcategori…
Browse files Browse the repository at this point in the history
…es (#1482)
  • Loading branch information
DennisNDean authored Jun 28, 2023
1 parent c4b30f9 commit faa8734
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
29 changes: 19 additions & 10 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,16 +628,17 @@ function ( $acc, $block ) use ( $block_name ) {
if ( current_user_can( 'edit_others_posts' ) && isset( $attributes['includedPostStatuses'] ) ) {
$included_post_statuses = $attributes['includedPostStatuses'];
}
$authors = isset( $attributes['authors'] ) ? $attributes['authors'] : array();
$categories = isset( $attributes['categories'] ) ? $attributes['categories'] : array();
$tags = isset( $attributes['tags'] ) ? $attributes['tags'] : array();
$custom_taxonomies = isset( $attributes['customTaxonomies'] ) ? $attributes['customTaxonomies'] : array();
$tag_exclusions = isset( $attributes['tagExclusions'] ) ? $attributes['tagExclusions'] : array();
$category_exclusions = isset( $attributes['categoryExclusions'] ) ? $attributes['categoryExclusions'] : array();
$specific_posts = isset( $attributes['specificPosts'] ) ? $attributes['specificPosts'] : array();
$posts_to_show = intval( $attributes['postsToShow'] );
$specific_mode = isset( $attributes['specificMode'] ) ? intval( $attributes['specificMode'] ) : false;
$args = array(
$authors = isset( $attributes['authors'] ) ? $attributes['authors'] : array();
$categories = isset( $attributes['categories'] ) ? $attributes['categories'] : array();
$include_subcategories = isset( $attributes['includeSubcategories'] ) ? intval( $attributes['includeSubcategories'] ) : false;
$tags = isset( $attributes['tags'] ) ? $attributes['tags'] : array();
$custom_taxonomies = isset( $attributes['customTaxonomies'] ) ? $attributes['customTaxonomies'] : array();
$tag_exclusions = isset( $attributes['tagExclusions'] ) ? $attributes['tagExclusions'] : array();
$category_exclusions = isset( $attributes['categoryExclusions'] ) ? $attributes['categoryExclusions'] : array();
$specific_posts = isset( $attributes['specificPosts'] ) ? $attributes['specificPosts'] : array();
$posts_to_show = intval( $attributes['postsToShow'] );
$specific_mode = isset( $attributes['specificMode'] ) ? intval( $attributes['specificMode'] ) : false;
$args = array(
'post_type' => $post_type,
'post_status' => $included_post_statuses,
'suppress_filters' => false,
Expand Down Expand Up @@ -666,6 +667,14 @@ function ( $acc, $block ) use ( $block_name ) {
);
}
if ( $categories && count( $categories ) ) {
if ( $include_subcategories === 1 ){
foreach ( $categories as $parent ) {
$children[] = get_categories( array( 'child_of' => $parent ) );
foreach ( $children[0] as $child ) {
$categories[] = $child->term_id;
}
}
}
$args['category__in'] = $categories;
}
if ( $tags && count( $tags ) ) {
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class Edit extends Component {
authors,
autoplay,
categories,
includeSubcategories,
customTaxonomies,
delay,
hideControls,
Expand Down Expand Up @@ -374,6 +375,10 @@ class Edit extends Component {
onAuthorsChange={ value => setAttributes( { authors: value } ) }
categories={ categories }
onCategoriesChange={ value => setAttributes( { categories: value } ) }
includeSubcategories={ includeSubcategories }
onIncludeSubcategoriesChange={ value =>
setAttributes( { includeSubcategories: value } )
}
tags={ tags }
onTagsChange={ value => setAttributes( { tags: value } ) }
onCustomTaxonomiesChange={ value => setAttributes( { customTaxonomies: value } ) }
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/carousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export const settings = {
categories: {
type: 'array',
},
includeSubcategories: {
type: 'boolean',
default: true
},
tags: {
type: 'array',
},
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/homepage-articles/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
"default": [],
"items": { "type": "integer" }
},
"includeSubcategories": {
"type": "boolean",
"default": true
},
"tags": {
"type": "array",
"default": [],
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/homepage-articles/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class Edit extends Component {
specificPosts,
postsToShow,
categories,
includeSubcategories,
customTaxonomies,
columns,
colGap,
Expand Down Expand Up @@ -359,6 +360,8 @@ class Edit extends Component {
onAuthorsChange={ handleAttributeChange( 'authors' ) }
categories={ categories }
onCategoriesChange={ handleAttributeChange( 'categories' ) }
includeSubcategories={ includeSubcategories }
onIncludeSubcategoriesChange={ handleAttributeChange( 'includeSubcategories' ) }
tags={ tags }
onTagsChange={ handleAttributeChange( 'tags' ) }
onCustomTaxonomiesChange={ handleAttributeChange( 'customTaxonomies' ) }
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const POST_QUERY_ATTRIBUTES = [
'postsToShow',
'authors',
'categories',
'includeSubcategories',
'excerptLength',
'tags',
'customTaxonomies',
Expand All @@ -43,6 +44,7 @@ type HomepageArticlesAttributes = {
postsToShow: number;
authors: AuthorId[];
categories: CategoryId[];
includeSubcategories: boolean;
excerptLength: number;
postType: PostType[];
showExcerpt: boolean;
Expand Down Expand Up @@ -87,6 +89,7 @@ export const queryCriteriaFromAttributes = ( attributes: Block[ 'attributes' ] )
postsToShow,
authors,
categories,
includeSubcategories,
excerptLength,
postType,
showExcerpt,
Expand All @@ -111,6 +114,7 @@ export const queryCriteriaFromAttributes = ( attributes: Block[ 'attributes' ] )
: {
postsToShow,
categories,
includeSubcategories,
authors,
tags,
tagExclusions,
Expand Down
9 changes: 9 additions & 0 deletions src/components/query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ class QueryControls extends Component {
onAuthorsChange,
categories,
onCategoriesChange,
includeSubcategories,
onIncludeSubcategoriesChange,
tags,
onTagsChange,
customTaxonomies,
Expand Down Expand Up @@ -290,6 +292,13 @@ class QueryControls extends Component {
label={ __( 'Categories', 'newspack-blocks' ) }
/>
) }
{ onIncludeSubcategoriesChange && (
<ToggleControl
checked={ includeSubcategories }
onChange={ onIncludeSubcategoriesChange }
label={ __( 'Include subcategories ', 'newspack-blocks' ) }
/>
) }
{ onTagsChange && (
<AutocompleteTokenField
tokens={ tags || [] }
Expand Down

0 comments on commit faa8734

Please sign in to comment.