-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from 9 commits
57ec70f
fbc91d7
d42e0c1
4a90867
4b29bcb
d892300
f68a189
4ac3a3c
2cc8058
61c7d48
711f65c
30a7f2a
7a775e4
8ef850c
bc1b218
2cfd817
f60fa36
35cea15
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 |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| const ResetHomepageModal = ( { items, closeModal } ) => { | ||
| const [ item ] = items; | ||
| const pageTitle = getItemTitle( item ); | ||
| const { isPageForPostsSet, isSaving } = useSelect( ( select ) => { | ||
| const { getEntityRecord, isSavingEntityRecord } = select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
|
|
||
| return { | ||
| isPageForPostsSet: siteSettings?.page_for_posts !== 0, | ||
| isSaving: isSavingEntityRecord( 'root', 'site' ), | ||
| }; | ||
| } ); | ||
|
|
||
| const { saveEditedEntityRecord, saveEntityRecord } = | ||
| useDispatch( coreStore ); | ||
| const { createSuccessNotice, createErrorNotice } = | ||
| useDispatch( noticesStore ); | ||
|
|
||
| async function onResetHomepage( event ) { | ||
| event.preventDefault(); | ||
|
|
||
| try { | ||
| // Save new home page settings. | ||
| await saveEditedEntityRecord( 'root', 'site', undefined, { | ||
| page_on_front: 0, | ||
| show_on_front: isPageForPostsSet ? 'page' : 'posts', | ||
| } ); | ||
|
|
||
| // This second call to a save function is a workaround for a bug in | ||
| // `saveEditedEntityRecord`. This forces the root site settings to be updated. | ||
| // See https://github.com/WordPress/gutenberg/issues/67161. | ||
| await saveEntityRecord( 'root', 'site', { | ||
| page_on_front: 0, | ||
| show_on_front: isPageForPostsSet ? 'page' : 'posts', | ||
| } ); | ||
|
|
||
| createSuccessNotice( __( 'Homepage reset' ), { | ||
| type: 'snackbar', | ||
| } ); | ||
| } catch ( error ) { | ||
| const typedError = error; | ||
| const errorMessage = | ||
| typedError.message && typedError.code !== 'unknown_error' | ||
| ? typedError.message | ||
| : __( 'An error occurred while resetting the homepage' ); | ||
| createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
| } finally { | ||
| closeModal?.(); | ||
| } | ||
| } | ||
|
|
||
| const modalWarning = ! isPageForPostsSet | ||
| ? __( 'This will set the homepage to display latest posts.' ) | ||
| : ''; | ||
|
|
||
| const modalText = sprintf( | ||
| // translators: %1$s: title of the page to be unset as the homepage, %2$s: homepage replacement warning message. | ||
| __( | ||
| 'Reset the site homepage? "%1$s" will no longer be set as the homepage. %2$s' | ||
| ), | ||
| pageTitle, | ||
| modalWarning | ||
| ); | ||
|
|
||
| // translators: Button label to confirm resetting the homepage. | ||
| const modalButtonLabel = __( 'Reset homepage' ); | ||
|
|
||
| return ( | ||
| <form onSubmit={ onResetHomepage }> | ||
| <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 useResetHomepageAction = () => { | ||
| const { pageOnFront } = useSelect( ( select ) => { | ||
| const { getEntityRecord } = select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| return { | ||
| pageOnFront: siteSettings?.page_on_front, | ||
| }; | ||
| } ); | ||
|
|
||
| return useMemo( | ||
| () => ( { | ||
| id: 'reset-homepage', | ||
| label: __( 'Reset homepage' ), | ||
| isEligible( post ) { | ||
| if ( pageOnFront !== post.id ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
mikachan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| RenderModal: ResetHomepageModal, | ||
| } ), | ||
| [ pageOnFront ] | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| const ResetPostsPageModal = ( { items, closeModal } ) => { | ||
| const [ item ] = items; | ||
| const pageTitle = getItemTitle( item ); | ||
| const { isPageOnFrontSet, isSaving } = useSelect( ( select ) => { | ||
| const { getEntityRecord, isSavingEntityRecord } = select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| return { | ||
| isPageOnFrontSet: siteSettings?.page_on_front !== 0, | ||
| isSaving: isSavingEntityRecord( 'root', 'site' ), | ||
| }; | ||
| } ); | ||
|
|
||
| const { saveEditedEntityRecord, saveEntityRecord } = | ||
| useDispatch( coreStore ); | ||
| const { createSuccessNotice, createErrorNotice } = | ||
| useDispatch( noticesStore ); | ||
|
|
||
| async function onResetPostsPage( event ) { | ||
| event.preventDefault(); | ||
|
|
||
| try { | ||
| // Save new posts page settings. | ||
| await saveEditedEntityRecord( 'root', 'site', undefined, { | ||
| page_for_posts: 0, | ||
| show_on_front: isPageOnFrontSet ? 'page' : 'posts', | ||
| } ); | ||
|
|
||
| // This second call to a save function is a workaround for a bug in | ||
| // `saveEditedEntityRecord`. This forces the root site settings to be updated. | ||
| // See https://github.com/WordPress/gutenberg/issues/67161. | ||
| await saveEntityRecord( 'root', 'site', { | ||
| page_for_posts: 0, | ||
| show_on_front: isPageOnFrontSet ? 'page' : 'posts', | ||
| } ); | ||
|
||
|
|
||
| createSuccessNotice( __( 'Posts page reset' ), { | ||
| type: 'snackbar', | ||
| } ); | ||
| } catch ( error ) { | ||
|
||
| const typedError = error; | ||
| const errorMessage = | ||
| typedError.message && typedError.code !== 'unknown_error' | ||
| ? typedError.message | ||
| : __( 'An error occurred while resetting the posts page' ); | ||
| createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
| } finally { | ||
| closeModal?.(); | ||
| } | ||
| } | ||
|
|
||
| const modalWarning = ! isPageOnFrontSet | ||
| ? __( 'This will set the homepage to display latest posts.' ) | ||
| : ''; | ||
|
|
||
| const modalText = sprintf( | ||
| // translators: %1$s: title of the page to be unset as the posts page, %2$s: post pages warning message. | ||
| __( | ||
| 'Reset the posts page? "%1$s" will no longer show the latest posts. %2$s' | ||
| ), | ||
| pageTitle, | ||
| modalWarning | ||
| ); | ||
|
|
||
| // translators: Button label to confirm resetting the posts page. | ||
| const modalButtonLabel = __( 'Reset posts page' ); | ||
|
|
||
| return ( | ||
| <form onSubmit={ onResetPostsPage }> | ||
| <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 useResetPostsPageAction = () => { | ||
| const { pageForPosts } = useSelect( ( select ) => { | ||
| const { getEntityRecord } = select( coreStore ); | ||
| const siteSettings = getEntityRecord( 'root', 'site' ); | ||
| return { | ||
| pageForPosts: siteSettings?.page_for_posts, | ||
| }; | ||
| } ); | ||
|
|
||
| return useMemo( | ||
| () => ( { | ||
| id: 'reset-posts-page', | ||
| label: __( 'Reset posts page' ), | ||
| isEligible( post ) { | ||
| if ( pageForPosts !== post.id ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
| RenderModal: ResetPostsPageModal, | ||
| } ), | ||
| [ pageForPosts ] | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.