-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Pages: Add "Set as posts page" action #67650
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
57ec70f
Move getItemTitle to its own file
mikachan fbc91d7
Add unset homepage action
mikachan d42e0c1
Add unset as posts page action
mikachan 4a90867
Add set as posts page action
mikachan 4b29bcb
Update homepage action tests
mikachan d892300
Rename unset options to reset
mikachan f68a189
Reword posts page reset notice
mikachan 4ac3a3c
Merge branch 'trunk' into add/more-homepage-actions
mikachan 2cc8058
Ensure Move to trash is always at end of list
mikachan 61c7d48
Update packages/editor/src/components/post-actions/actions.js
mikachan 711f65c
Update packages/editor/src/components/post-actions/reset-homepage.js
mikachan 30a7f2a
Remove getItemTitle from utils index.js
mikachan 7a775e4
Merge branch 'trunk' into add/more-homepage-actions
mikachan 8ef850c
Remove reset actions
mikachan bc1b218
Slight refactor to modal warning in set as posts page action
mikachan 2cfd817
Remove use of saveEditedEntityRecord
mikachan f60fa36
Check for currentPostsPage before setting modalwarning
mikachan 35cea15
Add full stop to action success notices
mikachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
packages/editor/src/components/post-actions/set-as-posts-page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { useMemo } from '@wordpress/element'; | ||
| import { | ||
| Button, | ||
| __experimentalText as Text, | ||
| __experimentalHStack as HStack, | ||
| __experimentalVStack as VStack, | ||
| } from '@wordpress/components'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { store as noticesStore } from '@wordpress/notices'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getItemTitle } from '../../utils/get-item-title'; | ||
|
|
||
| const SetAsPostsPageModal = ( { items, closeModal } ) => { | ||
| const [ item ] = items; | ||
| const pageTitle = getItemTitle( item ); | ||
| const { currentPostsPage, isPageForPostsSet, isSaving } = useSelect( | ||
| ( select ) => { | ||
| const { getEntityRecord, isSavingEntityRecord } = | ||
| select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| const currentPostsPageItem = getEntityRecord( | ||
| 'postType', | ||
| 'page', | ||
| siteSettings?.page_for_posts | ||
| ); | ||
| return { | ||
| currentPostsPage: currentPostsPageItem, | ||
| isPageForPostsSet: siteSettings?.page_for_posts !== 0, | ||
| isSaving: isSavingEntityRecord( 'root', 'site' ), | ||
| }; | ||
| } | ||
| ); | ||
|
|
||
| const { saveEntityRecord } = useDispatch( coreStore ); | ||
| const { createSuccessNotice, createErrorNotice } = | ||
| useDispatch( noticesStore ); | ||
|
|
||
| async function onSetPageAsPostsPage( event ) { | ||
| event.preventDefault(); | ||
|
|
||
| try { | ||
| await saveEntityRecord( 'root', 'site', { | ||
| page_for_posts: item.id, | ||
| show_on_front: 'page', | ||
| } ); | ||
|
|
||
| createSuccessNotice( __( 'Post page updated' ), { | ||
| type: 'snackbar', | ||
| } ); | ||
| } catch ( error ) { | ||
| const errorMessage = | ||
| error.message && error.code !== 'unknown_error' | ||
| ? error.message | ||
| : __( 'An error occurred while setting the posts page.' ); | ||
| createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
| } finally { | ||
| closeModal?.(); | ||
| } | ||
| } | ||
|
|
||
| const modalWarning = | ||
| isPageForPostsSet && currentPostsPage | ||
| ? sprintf( | ||
| // translators: %s: title of the current posts page. | ||
| __( 'This will replace the current posts page: "%s"' ), | ||
| getItemTitle( currentPostsPage ) | ||
| ) | ||
| : __( 'This page will show the latest posts.' ); | ||
|
|
||
| const modalText = sprintf( | ||
| // translators: %1$s: title of the page to be set as the posts page, %2$s: posts page replacement warning message. | ||
| __( 'Set "%1$s" as the posts page? %2$s' ), | ||
| pageTitle, | ||
| modalWarning | ||
| ); | ||
|
|
||
| // translators: Button label to confirm setting the specified page as the posts page. | ||
| const modalButtonLabel = __( 'Set posts page' ); | ||
|
|
||
| return ( | ||
| <form onSubmit={ onSetPageAsPostsPage }> | ||
| <VStack spacing="5"> | ||
| <Text>{ modalText }</Text> | ||
| <HStack justify="right"> | ||
| <Button | ||
| __next40pxDefaultSize | ||
| variant="tertiary" | ||
| onClick={ () => { | ||
| closeModal?.(); | ||
| } } | ||
| disabled={ isSaving } | ||
| accessibleWhenDisabled | ||
| > | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
| <Button | ||
| __next40pxDefaultSize | ||
| variant="primary" | ||
| type="submit" | ||
| disabled={ isSaving } | ||
| accessibleWhenDisabled | ||
| > | ||
| { modalButtonLabel } | ||
| </Button> | ||
| </HStack> | ||
| </VStack> | ||
| </form> | ||
| ); | ||
| }; | ||
|
|
||
| export const useSetAsPostsPageAction = () => { | ||
| const { pageOnFront, pageForPosts } = useSelect( ( select ) => { | ||
| const { getEntityRecord } = select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| return { | ||
| pageOnFront: siteSettings?.page_on_front, | ||
| pageForPosts: siteSettings?.page_for_posts, | ||
| }; | ||
| } ); | ||
|
|
||
| return useMemo( | ||
| () => ( { | ||
| id: 'set-as-posts-page', | ||
| label: __( 'Set as posts page' ), | ||
| isEligible( post ) { | ||
| if ( post.status !== 'publish' ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( post.type !== 'page' ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Don't show the action if the page is already set as the homepage. | ||
| if ( pageOnFront === post.id ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Don't show the action if the page is already set as the page for posts. | ||
| if ( pageForPosts === post.id ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
| RenderModal: SetAsPostsPageModal, | ||
| } ), | ||
| [ pageForPosts, pageOnFront ] | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { decodeEntities } from '@wordpress/html-entities'; | ||
|
|
||
| /** | ||
| * Helper function to get the title of a post item. | ||
| * This is duplicated from the `@wordpress/fields` package. | ||
| * `packages/fields/src/actions/utils.ts` | ||
| * | ||
| * @param {Object} item The post item. | ||
| * @return {string} The title of the item, or an empty string if the title is not found. | ||
| */ | ||
| export function getItemTitle( item ) { | ||
| if ( typeof item.title === 'string' ) { | ||
| return decodeEntities( item.title ); | ||
| } | ||
| if ( item.title && 'rendered' in item.title ) { | ||
| return decodeEntities( item.title.rendered ); | ||
| } | ||
| if ( item.title && 'raw' in item.title ) { | ||
| return decodeEntities( item.title.raw ); | ||
| } | ||
| return ''; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.