Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag selector to latest posts widget, add tag selector to queries #19755

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ 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 {
constructor() {
super( ...arguments );
this.state = {
categoriesList: [],
tagsList: [],
};
}

Expand All @@ -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() {
Expand All @@ -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 = (
<InspectorControls>
Expand Down Expand Up @@ -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' &&
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function render_block_core_latest_posts( $attributes ) {
$args['category'] = $attributes['categories'];
}

if ( isset( $attributes['tags'] ) ) {
$args['tag'] = $attributes['tags'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$args['tag'] = $attributes['tags'];
$args['tag_id'] = $attributes['tags'];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WP_Query args expect the tag argument to be one or more slugs.

}

$recent_posts = get_posts( $args );

$list_items_markup = '';
Expand Down Expand Up @@ -133,6 +137,9 @@ function register_block_core_latest_posts() {
'categories' => array(
'type' => 'string',
),
'tags' => array(
'type' => 'string',
),
'postsToShow' => array(
'type' => 'number',
'default' => 5,
Expand Down
13 changes: 13 additions & 0 deletions packages/components/src/query-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ 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;

export default function QueryControls( {
categoriesList,
selectedCategoryId,
tagsList,
selectedTagId,
numberOfItems,
order,
orderBy,
maxItems = DEFAULT_MAX_ITEMS,
minItems = DEFAULT_MIN_ITEMS,
onCategoryChange,
onTagChange,
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
Expand Down Expand Up @@ -71,6 +75,15 @@ export default function QueryControls( {
selectedCategoryId={ selectedCategoryId }
onChange={ onCategoryChange }
/> ),
onTagChange && (
<TagSelect
key="query-controls-tag-select"
tagsList={ tagsList }
label={ __( 'Tag' ) }
noOptionLabel={ __( 'All' ) }
selectedTagId={ selectedTagId }
onChange={ onTagChange }
/> ),
onNumberOfItemsChange && (
<RangeControl
key="query-controls-range-control"
Expand Down
16 changes: 16 additions & 0 deletions packages/components/src/query-controls/tag-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Internal dependencies
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';

export default function TagSelect( { label, noOptionLabel, tagsList, selectedTagId, onChange } ) {
const termsTree = buildTermsTree( tagsList );
return (
<TreeSelect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is the best form input to use. Isn't the one used at document level tag assignment better TokenInput ?

{ ...{ label, noOptionLabel, onChange } }
tree={ termsTree }
selectedId={ selectedTagId }
/>
);
}