From 32835c0c3e68a8e69ab1b39a898e74d3e57dbdbf Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Thu, 13 Jun 2019 16:43:51 -0400 Subject: [PATCH 01/19] Added ability to filter latest posts by author --- .../block-library/src/latest-posts/edit.js | 29 +++++++++++++++++-- .../block-library/src/latest-posts/index.php | 6 ++++ .../src/query-controls/author-select.js | 16 ++++++++++ .../components/src/query-controls/index.js | 13 +++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 packages/components/src/query-controls/author-select.js diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index a98f9459ac7562..914c0eb631aa2a 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 USERS_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: [], + authorList: [], }; } @@ -61,6 +65,21 @@ class LatestPostsEdit extends Component { } } ); + 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() { @@ -69,8 +88,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 { categoriesList, authorList } = this.state; + const { displayPostContentRadio, displayPostContent, displayPostDate, postLayout, columns, order, orderBy, categories, users, postsToShow, excerptLength } = attributes; const inspectorControls = ( @@ -115,9 +134,12 @@ class LatestPostsEdit extends Component { { ...{ order, orderBy } } numberOfItems={ postsToShow } categoriesList={ categoriesList } + authorList={ authorList } + selectedAuthorId={ users } selectedCategoryId={ categories } onOrderChange={ ( value ) => setAttributes( { order: value } ) } onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) } + onAuthorChange={ ( value ) => setAttributes( { users: '' !== value ? value : undefined } ) } onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) } onNumberOfItemsChange={ ( value ) => setAttributes( { postsToShow: value } ) } /> @@ -244,10 +266,11 @@ class LatestPostsEdit extends Component { } export default withSelect( ( select, props ) => { - const { postsToShow, order, orderBy, categories } = props.attributes; + const { postsToShow, order, orderBy, categories, users } = props.attributes; const { getEntityRecords } = select( 'core' ); const latestPostsQuery = pickBy( { categories, + author: users, 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 d90f692644bacb..4775492bd00746 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -24,6 +24,9 @@ function render_block_core_latest_posts( $attributes ) { if ( isset( $attributes['categories'] ) ) { $args['category'] = $attributes['categories']; } + if ( isset( $attributes['users'] ) ) { + $args['author'] = $attributes['users']; + } $recent_posts = get_posts( $args ); @@ -171,6 +174,9 @@ function register_block_core_latest_posts() { 'type' => 'string', 'default' => 'date', ), + 'users' => array( + 'type' => 'string', + ), ), 'render_callback' => 'render_block_core_latest_posts', ) diff --git a/packages/components/src/query-controls/author-select.js b/packages/components/src/query-controls/author-select.js new file mode 100644 index 00000000000000..b5c35afce7d58d --- /dev/null +++ b/packages/components/src/query-controls/author-select.js @@ -0,0 +1,16 @@ +/** + * 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 ( + + ); +} diff --git a/packages/components/src/query-controls/index.js b/packages/components/src/query-controls/index.js index f3cbf47998325c..0cc6d9b36ada8c 100644 --- a/packages/components/src/query-controls/index.js +++ b/packages/components/src/query-controls/index.js @@ -8,19 +8,23 @@ import { __ } from '@wordpress/i18n'; */ import { RangeControl, SelectControl } from '../'; import CategorySelect from './category-select'; +import AuthorSelect from './author-select'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 100; export default function QueryControls( { categoriesList, + authorList, selectedCategoryId, + selectedAuthorId, numberOfItems, order, orderBy, maxItems = DEFAULT_MAX_ITEMS, minItems = DEFAULT_MIN_ITEMS, onCategoryChange, + onAuthorChange, onNumberOfItemsChange, onOrderChange, onOrderByChange, @@ -71,6 +75,15 @@ export default function QueryControls( { selectedCategoryId={ selectedCategoryId } onChange={ onCategoryChange } /> ), + onAuthorChange && ( + ), onNumberOfItemsChange && ( Date: Thu, 2 Apr 2020 09:23:34 -0400 Subject: [PATCH 02/19] Updates to catch up to changes in Gutenberg --- .../block-library/src/latest-posts/block.json | 3 ++ .../block-library/src/latest-posts/edit.js | 32 ++++++++----------- .../block-library/src/latest-posts/index.php | 1 + .../src/query-controls/author-select.js | 8 ++++- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index e730d861626502..779e101ea8e597 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -12,6 +12,9 @@ "categories": { "type": "array" }, + "users": { + "type": "array" + }, "postsToShow": { "type": "number", "default": 5 diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 3e0210400517ae..5d65e5a2bb1892 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -50,7 +50,6 @@ const CATEGORIES_LIST_QUERY = { const USERS_LIST_QUERY = { per_page: -1, }; -const MAX_POSTS_COLUMNS = 6; class LatestPostsEdit extends Component { constructor() { @@ -75,23 +74,20 @@ class LatestPostsEdit extends Component { if ( this.isStillMounted ) { this.setState( { categoriesList: [] } ); } - } - ); + } ); this.fetchRequest = apiFetch( { path: addQueryArgs( `/wp/v2/users`, USERS_LIST_QUERY ), - } ).then( - ( authorList ) => { + } ) + .then( ( authorList ) => { if ( this.isStillMounted ) { this.setState( { authorList } ); } - } - ).catch( - () => { + } ) + .catch( () => { if ( this.isStillMounted ) { this.setState( { authorList: [] } ); } - } - ); + } ); } componentWillUnmount() { @@ -99,7 +95,6 @@ class LatestPostsEdit extends Component { } render() { - const { attributes, setAttributes, @@ -108,10 +103,7 @@ class LatestPostsEdit extends Component { defaultImageWidth, defaultImageHeight, } = this.props; - const { - categoriesList, - authorList, - } = this.state; + const { categoriesList, authorList } = this.state; const { displayFeaturedImage, displayPostContentRadio, @@ -285,12 +277,16 @@ class LatestPostsEdit extends Component { onNumberOfItemsChange={ ( value ) => setAttributes( { postsToShow: value } ) } - authorList={ authorList } - selectedAuthorId={ users } - categoriesList={ categoriesList } categorySuggestions={ categorySuggestions } onCategoryChange={ selectCategories } selectedCategories={ categories } + onAuthorChange={ ( value ) => + setAttributes( { + users: '' !== value ? value : undefined, + } ) + } + authorList={ authorList } + selectedAuthorId={ users } /> { postLayout === 'grid' && ( diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index a9d4927d264fac..520aca77e35907 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -49,6 +49,7 @@ function render_block_core_latest_posts( $attributes ) { if ( isset( $attributes['categories'] ) ) { $args['category__in'] = array_column( $attributes['categories'], 'id' ); } + if ( isset( $attributes['users'] ) ) { $args['author'] = $attributes['users']; } diff --git a/packages/components/src/query-controls/author-select.js b/packages/components/src/query-controls/author-select.js index b5c35afce7d58d..25dbc2f7c638eb 100644 --- a/packages/components/src/query-controls/author-select.js +++ b/packages/components/src/query-controls/author-select.js @@ -4,7 +4,13 @@ import { buildTermsTree } from './terms'; import TreeSelect from '../tree-select'; -export default function AuthorSelect( { label, noOptionLabel, authorList, selectedAuthorId, onChange } ) { +export default function AuthorSelect( { + label, + noOptionLabel, + authorList, + selectedAuthorId, + onChange, +} ) { const termsTree = buildTermsTree( authorList ); return ( Date: Thu, 2 Apr 2020 16:08:31 -0400 Subject: [PATCH 03/19] Fixes error of: Notice: Undefined index: items in /wp-includes/rest-api.php on line "1245" --- packages/block-library/src/latest-posts/block.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index 779e101ea8e597..1bfdc221404852 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -15,6 +15,9 @@ "users": { "type": "array" }, + "items": { + "type": "array" + }, "postsToShow": { "type": "number", "default": 5 From 4218a56937ab01cc2c78376b2e9e2ce5040a0e8d Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Thu, 2 Apr 2020 16:16:00 -0400 Subject: [PATCH 04/19] Removed unused variables and fixed linting issues --- packages/block-library/src/latest-posts/edit.js | 5 ++--- packages/components/src/query-controls/index.js | 3 --- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 5d65e5a2bb1892..0b0afd7452a4ce 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -303,7 +303,7 @@ class LatestPostsEdit extends Component { : Math.min( MAX_POSTS_COLUMNS, latestPosts.length - ) + ) } required /> @@ -478,7 +478,6 @@ class LatestPostsEdit extends Component { } export default withSelect( ( select, props ) => { - const { featuredImageSizeSlug, postsToShow, @@ -535,6 +534,6 @@ export default withSelect( ( select, props ) => { return { ...post, featuredImageSourceUrl: url }; } return post; - } ), + } ), }; } )( LatestPostsEdit ); diff --git a/packages/components/src/query-controls/index.js b/packages/components/src/query-controls/index.js index d0ba0b87fdff03..c1451d2a08cc5e 100644 --- a/packages/components/src/query-controls/index.js +++ b/packages/components/src/query-controls/index.js @@ -7,7 +7,6 @@ import { __ } from '@wordpress/i18n'; * Internal dependencies */ import { RangeControl, SelectControl, FormTokenField } from '../'; -import CategorySelect from './category-select'; import AuthorSelect from './author-select'; const DEFAULT_MIN_ITEMS = 1; @@ -15,9 +14,7 @@ const DEFAULT_MAX_ITEMS = 100; const MAX_CATEGORIES_SUGGESTIONS = 20; export default function QueryControls( { - categoriesList, authorList, - selectedCategoryId, selectedAuthorId, categorySuggestions, selectedCategories, From 9b6521e54225a319d71e7a501f8d1a2f9e68aadd Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Mon, 6 Apr 2020 10:25:58 -0400 Subject: [PATCH 05/19] Removed Whitespace from index.php --- packages/block-library/src/latest-posts/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index 520aca77e35907..7c0d4a960aff76 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -49,7 +49,7 @@ function render_block_core_latest_posts( $attributes ) { if ( isset( $attributes['categories'] ) ) { $args['category__in'] = array_column( $attributes['categories'], 'id' ); } - + if ( isset( $attributes['users'] ) ) { $args['author'] = $attributes['users']; } @@ -176,4 +176,4 @@ function register_block_core_latest_posts() { ) ); } -add_action( 'init', 'register_block_core_latest_posts' ); +add_action( 'init', 'register_block_core_latest_posts' ); \ No newline at end of file From de5b002925593841db0eec40a96cb2ff5894f682 Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Mon, 6 Apr 2020 14:02:03 -0400 Subject: [PATCH 06/19] adding new line to index.php --- packages/block-library/src/latest-posts/index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index 7c0d4a960aff76..a9d4927d264fac 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -49,7 +49,6 @@ function render_block_core_latest_posts( $attributes ) { if ( isset( $attributes['categories'] ) ) { $args['category__in'] = array_column( $attributes['categories'], 'id' ); } - if ( isset( $attributes['users'] ) ) { $args['author'] = $attributes['users']; } @@ -176,4 +175,4 @@ function register_block_core_latest_posts() { ) ); } -add_action( 'init', 'register_block_core_latest_posts' ); \ No newline at end of file +add_action( 'init', 'register_block_core_latest_posts' ); From c3d4248f1e3f75885639a30438f3fc52f9cba1fc Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Wed, 29 Apr 2020 18:00:14 -0400 Subject: [PATCH 07/19] applied changes suggested by @draganescu to LatestPosts block --- packages/block-library/src/latest-posts/block.json | 5 +---- packages/block-library/src/latest-posts/edit.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index 1bfdc221404852..134ddeb1d46e4a 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -13,10 +13,7 @@ "type": "array" }, "users": { - "type": "array" - }, - "items": { - "type": "array" + "type": "number" }, "postsToShow": { "type": "number", diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 0b0afd7452a4ce..5b07060139621b 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -282,7 +282,7 @@ class LatestPostsEdit extends Component { selectedCategories={ categories } onAuthorChange={ ( value ) => setAttributes( { - users: '' !== value ? value : undefined, + users: '' !== value ? Number( value ) : undefined, } ) } authorList={ authorList } From 72cd7b8686a06885d4e7d600b7fe664bae8b6c0d Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Wed, 29 Apr 2020 18:00:55 -0400 Subject: [PATCH 08/19] linting edit --- packages/block-library/src/latest-posts/edit.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 5b07060139621b..ae08439f0fc743 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -282,7 +282,8 @@ class LatestPostsEdit extends Component { selectedCategories={ categories } onAuthorChange={ ( value ) => setAttributes( { - users: '' !== value ? Number( value ) : undefined, + users: + '' !== value ? Number( value ) : undefined, } ) } authorList={ authorList } From 5dc4a4afddd136a74b095f4e0e55e1012ae36dba Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Thu, 30 Apr 2020 14:48:57 -0400 Subject: [PATCH 09/19] Updated from users to author on LatestPosts block --- packages/block-library/src/latest-posts/block.json | 2 +- packages/block-library/src/latest-posts/edit.js | 10 +++++----- packages/block-library/src/latest-posts/index.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index d97f178412539b..2f5b137941269c 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -15,7 +15,7 @@ "type": "object" } }, - "users": { + "author": { "type": "number" }, "postsToShow": { diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 8ee7fe8f579d4e..68d99fdb53e85c 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -114,7 +114,7 @@ class LatestPostsEdit extends Component { order, orderBy, categories, - users, + author, postsToShow, excerptLength, featuredImageAlign, @@ -278,12 +278,12 @@ class LatestPostsEdit extends Component { selectedCategories={ categories } onAuthorChange={ ( value ) => setAttributes( { - users: + author: '' !== value ? Number( value ) : undefined, } ) } authorList={ authorList } - selectedAuthorId={ users } + selectedAuthorId={ author } /> { postLayout === 'grid' && ( @@ -481,7 +481,7 @@ export default withSelect( ( select, props ) => { order, orderBy, categories, - users, + author, } = props.attributes; const { getEntityRecords, getMedia } = select( 'core' ); const { getSettings } = select( 'core/block-editor' ); @@ -493,7 +493,7 @@ export default withSelect( ( select, props ) => { const latestPostsQuery = pickBy( { categories: catIds, - author: users, + author, 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 9fd7a227e12334..5ab8c76bdfdd69 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -49,8 +49,8 @@ function render_block_core_latest_posts( $attributes ) { if ( isset( $attributes['categories'] ) ) { $args['category__in'] = array_column( $attributes['categories'], 'id' ); } - if ( isset( $attributes['users'] ) ) { - $args['author'] = $attributes['users']; + if ( isset( $attributes['author'] ) ) { + $args['author'] = $attributes['author']; } $recent_posts = get_posts( $args ); From 270881f37a982a3ba639d3375fdde85e45ed3f17 Mon Sep 17 00:00:00 2001 From: Paul Stonier Date: Tue, 5 May 2020 16:27:03 -0400 Subject: [PATCH 10/19] Added display author option --- packages/block-library/src/latest-posts/block.json | 4 ++++ packages/block-library/src/latest-posts/edit.js | 14 ++++++++++++++ packages/block-library/src/latest-posts/index.php | 13 +++++++++++++ packages/block-library/src/latest-posts/style.scss | 3 ++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index 2f5b137941269c..037fc05be8b67f 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -38,6 +38,10 @@ "type": "boolean", "default": false }, + "displayAuthor": { + "type": "boolean", + "default": false + }, "postLayout": { "type": "string", "default": "list" diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 68d99fdb53e85c..34c35e53e4de50 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -109,6 +109,7 @@ class LatestPostsEdit extends Component { displayPostContentRadio, displayPostContent, displayPostDate, + displayAuthor, postLayout, columns, order, @@ -195,6 +196,13 @@ class LatestPostsEdit extends Component { + + setAttributes( { displayAuthor: value } ) + } + /> @@ -437,6 +446,11 @@ class LatestPostsEdit extends Component { __( '(no title)' ) ) } + { displayAuthor && ( +
+ { `by ${ post.author_info.name }` } +
+ ) } { displayPostDate && post.date_gmt && (