-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
"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 | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ class LatestPostsEdit extends Component { | |
super( ...arguments ); | ||
this.state = { | ||
categoriesList: [], | ||
postTypeList: [], | ||
}; | ||
} | ||
|
||
|
@@ -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() { | ||
|
@@ -87,7 +101,7 @@ class LatestPostsEdit extends Component { | |
defaultImageWidth, | ||
defaultImageHeight, | ||
} = this.props; | ||
const { categoriesList } = this.state; | ||
const { categoriesList, postTypeList } = this.state; | ||
const { | ||
displayFeaturedImage, | ||
displayPostContentRadio, | ||
|
@@ -104,8 +118,9 @@ class LatestPostsEdit extends Component { | |
featuredImageSizeSlug, | ||
featuredImageSizeWidth, | ||
featuredImageSizeHeight, | ||
postType, | ||
} = attributes; | ||
const suggestions = categoriesList.reduce( | ||
const categorySuggestions = categoriesList.reduce( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 } ); | ||
}; | ||
|
@@ -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 | ||
|
@@ -254,10 +274,11 @@ class LatestPostsEdit extends Component { | |
value: item.name || item.value, | ||
} ) ) | ||
} | ||
suggestions={ Object.keys( suggestions ) } | ||
suggestions={ Object.keys( categorySuggestions ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ) } | ||
|
@@ -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, | ||
|
@@ -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 } ) ); | ||
|
There was a problem hiding this comment.
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
It's probably an editor spaces setting issue :)