-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Post excerpt: Fix JavaScript error and other misc. bug fixes #48730
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
Changes from 7 commits
213aa20
6034547
2ef5354
afc2add
8890e76
36bebd6
aab350d
564abcc
67ca5f3
439a690
1bf5831
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import classnames from 'classnames'; | |
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEntityProp } from '@wordpress/core-data'; | ||
| import { useEntityProp, store as coreStore } from '@wordpress/core-data'; | ||
| import { useMemo } from '@wordpress/element'; | ||
| import { | ||
| AlignmentToolbar, | ||
|
|
@@ -18,6 +18,7 @@ import { | |
| } from '@wordpress/block-editor'; | ||
| import { PanelBody, ToggleControl, RangeControl } from '@wordpress/components'; | ||
| import { __, _x } from '@wordpress/i18n'; | ||
| import { useSelect } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -32,13 +33,40 @@ export default function PostExcerptEditor( { | |
| } ) { | ||
| const isDescendentOfQueryLoop = Number.isFinite( queryId ); | ||
| const userCanEdit = useCanEditEntity( 'postType', postType, postId ); | ||
| const isEditable = userCanEdit && ! isDescendentOfQueryLoop; | ||
|
|
||
| const [ | ||
| rawExcerpt, | ||
| setExcerpt, | ||
| { rendered: renderedExcerpt, protected: isProtected } = {}, | ||
| ] = useEntityProp( 'postType', postType, 'excerpt', postId ); | ||
|
|
||
| // Check if the post type supports excerpts. | ||
| let postTypeSupportsExcerpts = useSelect( ( select ) => | ||
| postType | ||
| ? !! select( coreStore ).getPostType( postType )?.supports.excerpt | ||
| : false | ||
| ); | ||
|
|
||
| /** | ||
| * Add an exception for the page post type, | ||
| * which is registered without support for the excerpt UI, | ||
| * but supports saving the excerpt to the database. | ||
| * See: https://core.trac.wordpress.org/browser/branches/6.1/src/wp-includes/post.php#L65 | ||
| * Without this exception, users that have excerpts saved to the database will | ||
| * not be able to edit the excerpts. | ||
| */ | ||
| if ( postType === 'page' ) { | ||
| postTypeSupportsExcerpts = true; | ||
| } | ||
|
|
||
| /** | ||
| * The excerpt is editable if: | ||
| * - The user can edit the post | ||
| * - It is not a descendent of a Query Loop block | ||
| * - The post type supports excerpts | ||
| */ | ||
| const isEditable = | ||
| userCanEdit && ! isDescendentOfQueryLoop && postTypeSupportsExcerpts; | ||
|
|
||
| const blockProps = useBlockProps( { | ||
| className: classnames( { | ||
| [ `has-text-align-${ textAlign }` ]: textAlign, | ||
|
|
@@ -122,15 +150,14 @@ export default function PostExcerptEditor( { | |
| /** | ||
| * The excerpt length setting needs to be applied to both | ||
| * the raw and the rendered excerpt depending on which is being used. | ||
| * If there is no excerpt the value is set to '' to make sure it is not undefined (See line 89). | ||
| */ | ||
| const rawOrRenderedExcerpt = !! renderedExcerpt | ||
| ? strippedRenderedExcerpt | ||
| : rawExcerpt; | ||
| let rawOrRenderedExcerpt = rawExcerpt || strippedRenderedExcerpt; | ||
|
||
| rawOrRenderedExcerpt = rawOrRenderedExcerpt.trim(); | ||
carolinan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let trimmedExcerpt = ''; | ||
| if ( wordCountType === 'words' ) { | ||
| trimmedExcerpt = rawOrRenderedExcerpt | ||
| .trim() | ||
| .split( ' ', excerptLength ) | ||
| .join( ' ' ); | ||
| } else if ( wordCountType === 'characters_excluding_spaces' ) { | ||
|
|
@@ -143,7 +170,6 @@ export default function PostExcerptEditor( { | |
| * so that the spaces are excluded from the word count. | ||
| */ | ||
| const excerptWithSpaces = rawOrRenderedExcerpt | ||
| .trim() | ||
| .split( '', excerptLength ) | ||
| .join( '' ); | ||
|
|
||
|
|
@@ -152,14 +178,13 @@ export default function PostExcerptEditor( { | |
| excerptWithSpaces.replaceAll( ' ', '' ).length; | ||
|
|
||
| trimmedExcerpt = rawOrRenderedExcerpt | ||
| .trim() | ||
| .split( '', excerptLength + numberOfSpaces ) | ||
| .join( '' ); | ||
| } else if ( wordCountType === 'characters_including_spaces' ) { | ||
| trimmedExcerpt = rawOrRenderedExcerpt.trim().split( '', excerptLength ); | ||
| trimmedExcerpt = rawOrRenderedExcerpt.split( '', excerptLength ); | ||
| } | ||
|
|
||
| trimmedExcerpt = trimmedExcerpt + '...'; | ||
| const isTrimmed = trimmedExcerpt !== rawOrRenderedExcerpt ? true : false; | ||
aristath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const excerptContent = isEditable ? ( | ||
| <RichText | ||
|
|
@@ -168,17 +193,19 @@ export default function PostExcerptEditor( { | |
| value={ | ||
| isSelected | ||
| ? rawOrRenderedExcerpt | ||
| : ( trimmedExcerpt !== '...' ? trimmedExcerpt : '' ) || | ||
| : ( ! isTrimmed | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ? rawOrRenderedExcerpt | ||
| : trimmedExcerpt + '…' ) || | ||
carolinan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __( 'No post excerpt found' ) | ||
| } | ||
| onChange={ setExcerpt } | ||
| tagName="p" | ||
| /> | ||
| ) : ( | ||
| <p className={ excerptClassName }> | ||
| { trimmedExcerpt !== '...' | ||
| ? trimmedExcerpt | ||
| : __( 'No post excerpt found' ) } | ||
| { ! isTrimmed | ||
| ? rawOrRenderedExcerpt || __( 'No post excerpt found' ) | ||
| : trimmedExcerpt + '…' } | ||
| </p> | ||
| ); | ||
| return ( | ||
|
|
@@ -208,7 +235,6 @@ export default function PostExcerptEditor( { | |
| value={ excerptLength } | ||
| onChange={ ( value ) => { | ||
| setAttributes( { excerptLength: value } ); | ||
| setExcerpt(); | ||
| } } | ||
| min="10" | ||
| max="100" | ||
|
|
||
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.
A few notes:
useSelectneeds apostTypedependency.trueearly whenpostType === pagefrom the selector.booleanwill do the trick.The end results will be something like this: