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

Added ability to filter latest posts by author #16169

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
32835c0
Added ability to filter latest posts by author
Jun 13, 2019
7a82c73
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
Jul 15, 2019
3d523b3
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier Oct 22, 2019
f928d9e
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier Nov 12, 2019
9a21f51
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier Apr 1, 2020
a2cf201
Updates to catch up to changes in Gutenberg
pstonier Apr 2, 2020
9e46c53
Fixes error of: Notice: Undefined index: items in /wp-includes/rest-a…
pstonier Apr 2, 2020
4218a56
Removed unused variables and fixed linting issues
pstonier Apr 2, 2020
9b6521e
Removed Whitespace from index.php
pstonier Apr 6, 2020
de5b002
adding new line to index.php
pstonier Apr 6, 2020
c3d4248
applied changes suggested by @draganescu to LatestPosts block
pstonier Apr 29, 2020
72cd7b8
linting edit
pstonier Apr 29, 2020
2f2c2c1
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier Apr 29, 2020
5dc4a4a
Updated from users to author on LatestPosts block
pstonier Apr 30, 2020
270881f
Added display author option
pstonier May 5, 2020
873c8da
Updated edit.js on LatestPosts to match front end on author label
pstonier May 5, 2020
29cdc7e
Attempt to fix tests on LatestPosts
pstonier May 6, 2020
c36a40c
Update e2e tests for LatestPostsa
pstonier May 6, 2020
da4dd59
Fix for PHP Unit tests
pstonier May 6, 2020
7449834
Updated rendering of author name on editor view
pstonier May 6, 2020
080b917
Changed 'posted by' to 'by' on the byline
pstonier May 6, 2020
916855c
Fixed linting issue in index.php for LatestPost
pstonier May 6, 2020
8ce915e
Fixed formatting issues in index.php for LatestPosts
pstonier May 6, 2020
49e87dc
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier May 28, 2020
78f742a
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
pstonier Jun 1, 2020
ead1488
fix to undefined author_info
pstonier Jun 1, 2020
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
7 changes: 7 additions & 0 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"type": "object"
}
},
"author": {
"type": "number"
},
"postsToShow": {
"type": "number",
"default": 5
Expand All @@ -31,6 +34,10 @@
"type": "number",
"default": 55
},
"displayAuthor": {
"type": "boolean",
"default": false
},
"displayPostDate": {
"type": "boolean",
"default": false
Expand Down
45 changes: 44 additions & 1 deletion packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ import {
const CATEGORIES_LIST_QUERY = {
per_page: -1,
};
const USERS_LIST_QUERY = {
per_page: -1,
draganescu marked this conversation as resolved.
Show resolved Hide resolved
};

class LatestPostsEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
categoriesList: [],
authorList: [],
};
}

Expand All @@ -71,6 +75,19 @@ class LatestPostsEdit extends Component {
this.setState( { categoriesList: [] } );
}
} );
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/users`, USERS_LIST_QUERY ),
} )
.then( ( authorList ) => {
if ( this.isStillMounted ) {
this.setState( { authorList } );
}
} )
.catch( () => {
if ( this.isStillMounted ) {
this.setState( { authorList: [] } );
}
} );
}

componentWillUnmount() {
Expand All @@ -86,17 +103,19 @@ class LatestPostsEdit extends Component {
defaultImageWidth,
defaultImageHeight,
} = this.props;
const { categoriesList } = this.state;
const { categoriesList, authorList } = this.state;
const {
displayFeaturedImage,
displayPostContentRadio,
displayPostContent,
displayPostDate,
displayAuthor,
postLayout,
columns,
order,
orderBy,
categories,
author,
postsToShow,
excerptLength,
featuredImageAlign,
Expand Down Expand Up @@ -177,6 +196,13 @@ class LatestPostsEdit extends Component {
</PanelBody>

<PanelBody title={ __( 'Post meta settings' ) }>
<ToggleControl
label={ __( 'Display author name' ) }
checked={ displayAuthor }
onChange={ ( value ) =>
setAttributes( { displayAuthor: value } )
}
/>
<ToggleControl
label={ __( 'Display post date' ) }
checked={ displayPostDate }
Expand Down Expand Up @@ -258,6 +284,14 @@ class LatestPostsEdit extends Component {
categorySuggestions={ categorySuggestions }
onCategoryChange={ selectCategories }
selectedCategories={ categories }
onAuthorChange={ ( value ) =>
setAttributes( {
author:
'' !== value ? Number( value ) : undefined,
} )
}
authorList={ authorList }
selectedAuthorId={ author }
/>

{ postLayout === 'grid' && (
Expand Down Expand Up @@ -333,6 +367,7 @@ class LatestPostsEdit extends Component {
'wp-block-latest-posts__list': true,
'is-grid': postLayout === 'grid',
'has-dates': displayPostDate,
'has-author': displayAuthor,
[ `columns-${ columns }` ]: postLayout === 'grid',
} ) }
>
Expand Down Expand Up @@ -411,6 +446,12 @@ class LatestPostsEdit extends Component {
__( '(no title)' )
) }
</a>
{ displayAuthor && (
<div className="wp-block-latest-posts__post-author">
{ __( 'Posted by' ) }{ ' ' }
pstonier marked this conversation as resolved.
Show resolved Hide resolved
{ post.author_info.name }
</div>
) }
{ displayPostDate && post.date_gmt && (
<time
dateTime={ format(
Expand Down Expand Up @@ -455,6 +496,7 @@ export default withSelect( ( select, props ) => {
order,
orderBy,
categories,
author,
} = props.attributes;
const { getEntityRecords, getMedia } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );
Expand All @@ -466,6 +508,7 @@ export default withSelect( ( select, props ) => {
const latestPostsQuery = pickBy(
{
categories: catIds,
author,
order,
orderby: orderBy,
per_page: postsToShow,
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function render_block_core_latest_posts( $attributes ) {
if ( isset( $attributes['categories'] ) ) {
$args['category__in'] = array_column( $attributes['categories'], 'id' );
}
if ( isset( $attributes['author'] ) ) {
$args['author'] = $attributes['author'];
}

$recent_posts = get_posts( $args );

Expand Down Expand Up @@ -95,6 +98,14 @@ function render_block_core_latest_posts( $attributes ) {
$title
);

if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
$list_items_markup .= sprintf(
'<div class="wp-block-latest-posts__post-author">%1$s %2$s</div>',
__( 'Posted by' ),
get_the_author_meta( 'display_name', $post->post_author )
);
}

if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
Expand Down Expand Up @@ -144,6 +155,10 @@ function render_block_core_latest_posts( $attributes ) {
$class .= ' has-dates';
}

if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
$class .= ' has-author';
}

if ( isset( $attributes['className'] ) ) {
$class .= ' ' . $attributes['className'];
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/latest-posts/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
}
}

.wp-block-latest-posts__post-date {
.wp-block-latest-posts__post-date,
.wp-block-latest-posts__post-author {
display: block;
color: $dark-gray-300;
font-size: $default-font-size;
Expand Down
22 changes: 22 additions & 0 deletions packages/components/src/query-controls/author-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Internal dependencies
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';

export default function AuthorSelect( {
label,
noOptionLabel,
authorList,
selectedAuthorId,
onChange,
} ) {
const termsTree = buildTermsTree( authorList );
return (
<TreeSelect
{ ...{ label, noOptionLabel, onChange } }
tree={ termsTree }
selectedId={ selectedAuthorId }
/>
);
}
15 changes: 14 additions & 1 deletion packages/components/src/query-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { RangeControl, SelectControl, FormTokenField } from '../';
import AuthorSelect from './author-select';

const DEFAULT_MIN_ITEMS = 1;
const DEFAULT_MAX_ITEMS = 100;
const MAX_CATEGORIES_SUGGESTIONS = 20;

export default function QueryControls( {
authorList,
selectedAuthorId,
categorySuggestions,
selectedCategories,
numberOfItems,
Expand All @@ -21,6 +24,7 @@ export default function QueryControls( {
maxItems = DEFAULT_MAX_ITEMS,
minItems = DEFAULT_MIN_ITEMS,
onCategoryChange,
onAuthorChange,
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
Expand Down Expand Up @@ -77,7 +81,16 @@ export default function QueryControls( {
maxSuggestions={ MAX_CATEGORIES_SUGGESTIONS }
/>
),

onAuthorChange && (
<AuthorSelect
key="query-controls-author-select"
authorList={ authorList }
label={ __( 'Author' ) }
noOptionLabel={ __( 'All' ) }
selectedAuthorId={ selectedAuthorId }
onChange={ onAuthorChange }
/>
),
onNumberOfItemsChange && (
<RangeControl
key="query-controls-range-control"
Expand Down
4 changes: 4 additions & 0 deletions packages/e2e-tests/fixtures/block-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export const EXPECTED_TRANSFORMS = {
originalBlock: 'Latest Posts',
availableTransforms: [ 'Group' ],
},
'core__latest-posts__displayAuthor': {
originalBlock: 'Latest Posts',
availableTransforms: [ 'Group' ],
},
'core__latest-posts__displayPostDate': {
originalBlock: 'Latest Posts',
availableTransforms: [ 'Group' ],
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/blocks/core__latest-posts.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/latest-posts {"postsToShow":5,"displayPostDate":false} /-->
<!-- wp:core/latest-posts {"postsToShow":5,"displayAuthor":false,"displayPostDate":false} /-->
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__latest-posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": false,
"postLayout": "list",
"columns": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"blockName": "core/latest-posts",
"attrs": {
"postsToShow": 5,
"displayAuthor": false,
"displayPostDate": false
},
"innerBlocks": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": true,
"postLayout": "list",
"columns": 3,
Expand Down