From 0997a4722fc0318e50c19447005e05bcdfb0e9fd Mon Sep 17 00:00:00 2001 From: Erik Teichmann Date: Sun, 19 Jan 2020 14:09:04 -0500 Subject: [PATCH] Add tag selector to lastest posts widget, add tag selector to queries --- .../block-library/src/latest-posts/edit.js | 28 +++++++++++++++++-- .../block-library/src/latest-posts/index.php | 7 +++++ .../components/src/query-controls/index.js | 13 +++++++++ .../src/query-controls/tag-select.js | 16 +++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 packages/components/src/query-controls/tag-select.js diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 50e4add6e8fff7..1592b5495d78ad 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -34,6 +34,9 @@ import { withSelect } from '@wordpress/data'; const CATEGORIES_LIST_QUERY = { per_page: -1, }; +const TAGS_LIST_QUERY = { + per_page: -1, +}; const MAX_POSTS_COLUMNS = 6; class LatestPostsEdit extends Component { @@ -41,6 +44,7 @@ class LatestPostsEdit extends Component { super( ...arguments ); this.state = { categoriesList: [], + tagsList: [], }; } @@ -61,6 +65,21 @@ class LatestPostsEdit extends Component { } } ); + this.fetchRequest = apiFetch( { + path: addQueryArgs( `/wp/v2/tags`, TAGS_LIST_QUERY ), + } ).then( + ( tagsList ) => { + if ( this.isStillMounted ) { + this.setState( { tagsList } ); + } + } + ).catch( + () => { + if ( this.isStillMounted ) { + this.setState( { tagsList: [] } ); + } + } + ); } componentWillUnmount() { @@ -70,7 +89,8 @@ class LatestPostsEdit extends Component { render() { const { attributes, setAttributes, latestPosts } = this.props; const { categoriesList } = this.state; - const { displayPostContentRadio, displayPostContent, displayPostDate, postLayout, columns, order, orderBy, categories, postsToShow, excerptLength } = attributes; + const { tagsList } = this.state; + const { displayPostContentRadio, displayPostContent, displayPostDate, postLayout, columns, order, orderBy, categories, tags, postsToShow, excerptLength } = attributes; const inspectorControls = ( @@ -116,9 +136,12 @@ class LatestPostsEdit extends Component { numberOfItems={ postsToShow } categoriesList={ categoriesList } selectedCategoryId={ categories } + tagsList={ tagsList } + selectedTagId={ tags } onOrderChange={ ( value ) => setAttributes( { order: value } ) } onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) } onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) } + onTagChange={ ( value ) => setAttributes( { tags: '' !== value ? value : undefined } ) } onNumberOfItemsChange={ ( value ) => setAttributes( { postsToShow: value } ) } /> { postLayout === 'grid' && @@ -244,10 +267,11 @@ class LatestPostsEdit extends Component { } export default withSelect( ( select, props ) => { - const { postsToShow, order, orderBy, categories } = props.attributes; + const { postsToShow, order, orderBy, categories, tags } = props.attributes; const { getEntityRecords } = select( 'core' ); const latestPostsQuery = pickBy( { categories, + tags, order, orderby: orderBy, per_page: postsToShow, diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index b8fc02cf80daa1..a178e07c476a58 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -25,6 +25,10 @@ function render_block_core_latest_posts( $attributes ) { $args['category'] = $attributes['categories']; } + if ( isset( $attributes['tags'] ) ) { + $args['tag'] = $attributes['tags']; + } + $recent_posts = get_posts( $args ); $list_items_markup = ''; @@ -133,6 +137,9 @@ function register_block_core_latest_posts() { 'categories' => array( 'type' => 'string', ), + 'tags' => array( + 'type' => 'string', + ), 'postsToShow' => array( 'type' => 'number', 'default' => 5, diff --git a/packages/components/src/query-controls/index.js b/packages/components/src/query-controls/index.js index f3cbf47998325c..7c2b532d473d50 100644 --- a/packages/components/src/query-controls/index.js +++ b/packages/components/src/query-controls/index.js @@ -8,6 +8,7 @@ import { __ } from '@wordpress/i18n'; */ import { RangeControl, SelectControl } from '../'; import CategorySelect from './category-select'; +import TagSelect from './tag-select'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 100; @@ -15,12 +16,15 @@ const DEFAULT_MAX_ITEMS = 100; export default function QueryControls( { categoriesList, selectedCategoryId, + tagsList, + selectedTagId, numberOfItems, order, orderBy, maxItems = DEFAULT_MAX_ITEMS, minItems = DEFAULT_MIN_ITEMS, onCategoryChange, + onTagChange, onNumberOfItemsChange, onOrderChange, onOrderByChange, @@ -71,6 +75,15 @@ export default function QueryControls( { selectedCategoryId={ selectedCategoryId } onChange={ onCategoryChange } /> ), + onTagChange && ( + ), onNumberOfItemsChange && ( + ); +}