-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Data Views: Add action for pages to set site homepage #65426
Changes from all commits
c7101cf
585ef01
41474da
921d0f4
23cefbd
fb35be0
5de983c
0f3a986
1bdf2e0
937ad00
7df97a8
91a8275
44feed5
768f9cb
a04fb70
7462ce7
2d2c856
8bbd86d
6fc3490
341a068
b27305f
b387c34
2429d77
57eceb1
49be2ab
40568dc
489f4d6
c36e3ac
3cb07e9
9a5f4d7
c4bd9ae
46071d7
42e0791
3434c3f
e84a33e
baea572
c413a17
ba8becb
7687e3c
81b76f8
0ca96b0
17754bd
e722ceb
91de6c0
3c71533
6b2f6b9
6a1f468
b81eb1c
6d36c1b
8b40105
3caa096
d20b87b
29a1f72
0c5883a
24dcf8f
095bbe3
991ac5c
f2acc94
969ed4a
259a476
efbf96c
9de4488
5cbe7b4
7a2960a
4d45eaf
fb2ea4c
1a1f682
c10846b
5e5cf0c
2b37368
43dd85f
4cd6988
b2e88eb
953e999
f01d347
d96e90e
9685c3a
b029794
a9ea268
e438496
04a7ba4
c97a17d
0b8c27e
f8df3b9
a175adc
86af09a
10d97b7
c23c35b
b1707e4
d6a7059
a696688
6d01e34
18aeaf6
d486bac
8bacdbb
e1a7aa9
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,174 @@ | ||
/** | ||
* 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 '../../dataviews/actions/utils'; | ||
|
||
const SetAsHomepageModal = ( { items, closeModal } ) => { | ||
const [ item ] = items; | ||
const pageTitle = getItemTitle( item ); | ||
const { showOnFront, currentHomePage, isSaving } = useSelect( | ||
( select ) => { | ||
const { getEntityRecord, isSavingEntityRecord } = | ||
select( coreStore ); | ||
const siteSettings = getEntityRecord( 'root', 'site' ); | ||
const currentHomePageItem = getEntityRecord( | ||
'postType', | ||
'page', | ||
siteSettings?.page_on_front | ||
); | ||
return { | ||
showOnFront: siteSettings?.show_on_front, | ||
currentHomePage: currentHomePageItem, | ||
isSaving: isSavingEntityRecord( 'root', 'site' ), | ||
}; | ||
} | ||
); | ||
const currentHomePageTitle = currentHomePage | ||
? getItemTitle( currentHomePage ) | ||
: ''; | ||
|
||
const { saveEditedEntityRecord, saveEntityRecord } = | ||
useDispatch( coreStore ); | ||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
|
||
async function onSetPageAsHomepage( event ) { | ||
event.preventDefault(); | ||
|
||
try { | ||
// Save new home page settings. | ||
await saveEditedEntityRecord( 'root', 'site', undefined, { | ||
page_on_front: item.id, | ||
show_on_front: 'page', | ||
} ); | ||
|
||
// 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: item.id, | ||
show_on_front: 'page', | ||
} ); | ||
|
||
createSuccessNotice( __( 'Homepage updated' ), { | ||
type: 'snackbar', | ||
} ); | ||
} catch ( error ) { | ||
const typedError = error; | ||
const errorMessage = | ||
typedError.message && typedError.code !== 'unknown_error' | ||
? typedError.message | ||
: __( 'An error occurred while setting the homepage' ); | ||
createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
} finally { | ||
closeModal?.(); | ||
} | ||
} | ||
|
||
const modalWarning = | ||
'posts' === showOnFront | ||
? __( | ||
'This will replace the current homepage which is set to display latest posts.' | ||
) | ||
: sprintf( | ||
// translators: %s: title of the current home page. | ||
__( 'This will replace the current homepage: "%s"' ), | ||
currentHomePageTitle | ||
); | ||
|
||
const modalText = sprintf( | ||
// translators: %1$s: title of the page to be set as the homepage, %2$s: homepage replacement warning message. | ||
__( 'Set "%1$s" as the site homepage? %2$s' ), | ||
pageTitle, | ||
modalWarning | ||
); | ||
|
||
// translators: Button label to confirm setting the specified page as the homepage. | ||
const modalButtonLabel = __( 'Set homepage' ); | ||
|
||
return ( | ||
<form onSubmit={ onSetPageAsHomepage }> | ||
getdave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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 useSetAsHomepageAction = () => { | ||
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-homepage', | ||
label: __( 'Set as homepage' ), | ||
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: SetAsHomepageModal, | ||
} ), | ||
[ pageForPosts, pageOnFront ] | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
type PostStatus = | ||
| 'published' | ||
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. I just realized we had this in the editor that we also have in the |
||
| 'publish' | ||
| 'draft' | ||
| 'pending' | ||
| 'private' | ||
|
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.
When I made
key
inGetEntityRecord
optional, this line errored as an "Unused '@ts-expect-error' directive.", so I removed it and everything seems good when building 🤞