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 CPT selector to Latest Posts #20831

Closed
wants to merge 2 commits into from
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
143 changes: 73 additions & 70 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
{
"name": "core/latest-posts",
"category": "widgets",
"attributes": {
"align": {
"type": "string",
"enum": ["left", "center", "right", "wide", "full"]
},
"className": {
"type": "string"
},
"categories": {
"type": "array"
},
"postsToShow": {
"type": "number",
"default": 5
},
"displayPostContent": {
"type": "boolean",
"default": false
},
"displayPostContentRadio": {
"type": "string",
"default": "excerpt"
},
"excerptLength": {
"type": "number",
"default": 55
},
"displayPostDate": {
"type": "boolean",
"default": false
},
"postLayout": {
"type": "string",
"default": "list"
},
"columns": {
"type": "number",
"default": 3
},
"order": {
"type": "string",
"default": "desc"
},
"orderBy": {
"type": "string",
"default": "date"
},
"displayFeaturedImage": {
"type": "boolean",
"default": false
},
"featuredImageAlign": {
"type": "string",
"enum": ["left", "center", "right"]
},
"featuredImageSizeSlug": {
"type": "string",
"default":"thumbnail"
},
"featuredImageSizeWidth": {
"type": "number",
"default":null
},
"featuredImageSizeHeight": {
"type": "number",
"default":null
}
}
"name": "core/latest-posts",
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this file has been reformatted and for no good reason. We should revert this change to only include

 "postType": {
   "type": "string"
 },

It's probably an editor spaces setting issue :)

"category": "widgets",
"attributes": {
"align": {
"type": "string",
"enum": ["left", "center", "right", "wide", "full"]
},
"className": {
"type": "string"
},
"categories": {
"type": "array"
},
"postType": {
"type": "string"
},
"postsToShow": {
"type": "number",
"default": 5
},
"displayPostContent": {
"type": "boolean",
"default": false
},
"displayPostContentRadio": {
"type": "string",
"default": "excerpt"
},
"excerptLength": {
"type": "number",
"default": 55
},
"displayPostDate": {
"type": "boolean",
"default": false
},
"postLayout": {
"type": "string",
"default": "list"
},
"columns": {
"type": "number",
"default": 3
},
"order": {
"type": "string",
"default": "desc"
},
"orderBy": {
"type": "string",
"default": "date"
},
"displayFeaturedImage": {
"type": "boolean",
"default": false
},
"featuredImageAlign": {
"type": "string",
"enum": ["left", "center", "right"]
},
"featuredImageSizeSlug": {
"type": "string",
"default": "thumbnail"
},
"featuredImageSizeWidth": {
"type": "number",
"default": null
},
"featuredImageSizeHeight": {
"type": "number",
"default": null
}
}
}
40 changes: 34 additions & 6 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LatestPostsEdit extends Component {
super( ...arguments );
this.state = {
categoriesList: [],
postTypeList: [],
};
}

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

componentWillUnmount() {
Expand All @@ -87,7 +101,7 @@ class LatestPostsEdit extends Component {
defaultImageWidth,
defaultImageHeight,
} = this.props;
const { categoriesList } = this.state;
const { categoriesList, postTypeList } = this.state;
const {
displayFeaturedImage,
displayPostContentRadio,
Expand All @@ -104,8 +118,9 @@ class LatestPostsEdit extends Component {
featuredImageSizeSlug,
featuredImageSizeWidth,
featuredImageSizeHeight,
postType,
} = attributes;
const suggestions = categoriesList.reduce(
const categorySuggestions = categoriesList.reduce(
Copy link
Contributor

Choose a reason for hiding this comment

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

you changed this here from my comment in the other PR right?

( accumulator, category ) => ( {
...accumulator,
[ category.name ]: category,
Expand All @@ -117,7 +132,7 @@ class LatestPostsEdit extends Component {
// Categories that are already will be objects, while new additions will be strings (the name).
// allCategories nomalizes the array so that they are all objects.
const allCategories = tokens.map( ( token ) =>
typeof token === 'string' ? suggestions[ token ] : token
typeof token === 'string' ? categorySuggestions[ token ] : token
);
setAttributes( { categories: allCategories } );
};
Expand Down Expand Up @@ -243,6 +258,11 @@ class LatestPostsEdit extends Component {
onNumberOfItemsChange={ ( value ) =>
setAttributes( { postsToShow: value } )
}
onPostTypeChange={ ( value ) =>
setAttributes( { postType: value } )
}
postTypeList={ postTypeList }
selectedPostType={ postType }
/>
{ categoriesList.length > 0 && (
<FormTokenField
Expand All @@ -254,10 +274,11 @@ class LatestPostsEdit extends Component {
value: item.name || item.value,
} ) )
}
suggestions={ Object.keys( suggestions ) }
suggestions={ Object.keys( categorySuggestions ) }
Copy link
Contributor

Choose a reason for hiding this comment

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

same, this is from the other PR. if we change in both places we might end up with conflicts 😁

onChange={ selectCategories }
/>
) }

{ postLayout === 'grid' && (
<RangeControl
label={ __( 'Columns' ) }
Expand Down Expand Up @@ -445,14 +466,16 @@ export default withSelect( ( select, props ) => {
order,
orderBy,
categories,
postType,
} = props.attributes;
const { getEntityRecords, getMedia } = select( 'core' );
const { getEntityRecords, getMedia, getPostTypes } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );
const { imageSizes, imageDimensions } = getSettings();
const catIds =
categories && categories.length > 0
? categories.map( ( cat ) => cat.id )
: [];

const latestPostsQuery = pickBy(
{
categories: catIds,
Expand All @@ -463,7 +486,12 @@ export default withSelect( ( select, props ) => {
( value ) => ! isUndefined( value )
);

const posts = getEntityRecords( 'postType', 'post', latestPostsQuery );
const posts = getEntityRecords(
'postType',
postType || 'post',
latestPostsQuery
);

const imageSizeOptions = imageSizes
.filter( ( { slug } ) => slug !== 'full' )
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );
Expand Down
3 changes: 3 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['postType'] ) ) {
$args['post_type'] = $attributes['postType'];
}

$recent_posts = get_posts( $args );

Expand Down
15 changes: 15 additions & 0 deletions packages/components/src/query-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default function QueryControls( {
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
onPostTypeChange,
postTypeList,
selectedPostType,
} ) {
return [
onOrderChange && onOrderByChange && (
Expand Down Expand Up @@ -86,6 +89,18 @@ export default function QueryControls( {
{ ...MOBILE_CONTROL_PROPS }
/>
),
onPostTypeChange && (
<SelectControl
label={ __( 'Post types' ) }
value={ selectedPostType }
options={ Object.values( postTypeList ).map( ( item ) => ( {
label: item.name,
value: item.slug,
} ) ) }
onChange={ onPostTypeChange }
{ ...MOBILE_CONTROL_PROPS }
/>
),
onNumberOfItemsChange && (
<RangeControl
key="query-controls-range-control"
Expand Down