diff --git a/includes/class-newspack-blocks.php b/includes/class-newspack-blocks.php index 2480798d9..666a91bab 100644 --- a/includes/class-newspack-blocks.php +++ b/includes/class-newspack-blocks.php @@ -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, @@ -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 ) ) { diff --git a/src/blocks/carousel/edit.js b/src/blocks/carousel/edit.js index bec85d339..e69d0b91c 100644 --- a/src/blocks/carousel/edit.js +++ b/src/blocks/carousel/edit.js @@ -150,6 +150,7 @@ class Edit extends Component { authors, autoplay, categories, + includeSubcategories, customTaxonomies, delay, hideControls, @@ -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 } ) } diff --git a/src/blocks/carousel/index.js b/src/blocks/carousel/index.js index e91b4d69b..902232afa 100644 --- a/src/blocks/carousel/index.js +++ b/src/blocks/carousel/index.js @@ -64,6 +64,10 @@ export const settings = { categories: { type: 'array', }, + includeSubcategories: { + type: 'boolean', + default: true + }, tags: { type: 'array', }, diff --git a/src/blocks/homepage-articles/block.json b/src/blocks/homepage-articles/block.json index 5c0119b26..5209b933d 100644 --- a/src/blocks/homepage-articles/block.json +++ b/src/blocks/homepage-articles/block.json @@ -100,6 +100,10 @@ "default": [], "items": { "type": "integer" } }, + "includeSubcategories": { + "type": "boolean", + "default": true + }, "tags": { "type": "array", "default": [], diff --git a/src/blocks/homepage-articles/edit.js b/src/blocks/homepage-articles/edit.js index d6a0f6918..6959ed601 100644 --- a/src/blocks/homepage-articles/edit.js +++ b/src/blocks/homepage-articles/edit.js @@ -266,6 +266,7 @@ class Edit extends Component { specificPosts, postsToShow, categories, + includeSubcategories, customTaxonomies, columns, colGap, @@ -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' ) } diff --git a/src/blocks/homepage-articles/utils.ts b/src/blocks/homepage-articles/utils.ts index 31f6c5049..2559ab2fd 100644 --- a/src/blocks/homepage-articles/utils.ts +++ b/src/blocks/homepage-articles/utils.ts @@ -27,6 +27,7 @@ const POST_QUERY_ATTRIBUTES = [ 'postsToShow', 'authors', 'categories', + 'includeSubcategories', 'excerptLength', 'tags', 'customTaxonomies', @@ -43,6 +44,7 @@ type HomepageArticlesAttributes = { postsToShow: number; authors: AuthorId[]; categories: CategoryId[]; + includeSubcategories: boolean; excerptLength: number; postType: PostType[]; showExcerpt: boolean; @@ -87,6 +89,7 @@ export const queryCriteriaFromAttributes = ( attributes: Block[ 'attributes' ] ) postsToShow, authors, categories, + includeSubcategories, excerptLength, postType, showExcerpt, @@ -111,6 +114,7 @@ export const queryCriteriaFromAttributes = ( attributes: Block[ 'attributes' ] ) : { postsToShow, categories, + includeSubcategories, authors, tags, tagExclusions, diff --git a/src/components/query-controls.js b/src/components/query-controls.js index b9983142a..d2a22e14e 100644 --- a/src/components/query-controls.js +++ b/src/components/query-controls.js @@ -223,6 +223,8 @@ class QueryControls extends Component { onAuthorsChange, categories, onCategoriesChange, + includeSubcategories, + onIncludeSubcategoriesChange, tags, onTagsChange, customTaxonomies, @@ -290,6 +292,13 @@ class QueryControls extends Component { label={ __( 'Categories', 'newspack-blocks' ) } /> ) } + { onIncludeSubcategoriesChange && ( + + ) } { onTagsChange && (