-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data Views: Add action for pages to set site homepage (#65426)
* Adds basic action and modal to set page as homepage * Adds permissions and settings checks to set as homepage action * Adds proper description and handles unpublished pages * Adds action to set homepage to show latest posts * Doesn't show action if there's a front-page template * Creates page for posts, when specified * Refactors modal component * Fixes issues from rebase * Only show option on published pages * Update snackbar wording * Check item exists before running getItemTitle logic * Make key optional on GetEntityRecord * Remove some ts-ignore comments * Add support for page_for_posts to Settings * Remove some more ts-ignores * Allow recordId to be optional * Increase size of action modal * Implement choose existing page option * Fix number/string comparison * Add initial e2e test * Set post actions modal to medium * Tweak ToggleGroupControl help text * Fix initial test * Remove extra useSiteSettings hook * Allow setting draft pages to homepage * Fix merge conflict * Remove item check from getItemTitle * Remove posts page options * Don't show homepage option if selected page is the page for posts * Reload actions list when site settings change * Update tests * Remove call to __experimentalGetTemplateForLink * Update tests * Remove item check in getItemTitle * Use useSelect instead of select * Remove PAGE_POST_TYPE constant * Use saveEntityRecord instead of editEntityRecord * Remove onSetLatestPostsHomepage option * Remove select for site settings from isEligible * Update post actions with site settings info * Remove select for templates from isEligible * Skip last test for now * Restore whitespace * Rename _select * Remove sub-objects from additionalContext selectors * Remove duplicate page_for_posts definition * Fix page/post type error * Remove additional groups within additionalContext * Fix siteSettings in TitleView * Move hasFrontPageTemplate check to private-actions * Add JSDoc to setAsHomepage * Refactor siteSettings in post-list * Move homepage action to edit-site * Revert unnecessary changes * Move getItemTitle to edit-site utils * Allow undefined on GetEntityRecord key * Make it more clear that draft page will be published * Update draft page wording * Add set homepage action to post editor * Attempt to fix build error * Remove homepage action from edit-site * Remove extra line * Fix getting current homepage title * Make key in getEntityRecord optional * Use getHomePage selector * Move canManageOptions and hasFrontPageTemplate to actions.js * Make key optional in EntityRecordKey * Remove undefined from getEntityRecord calls * Update packages/editor/src/components/post-actions/actions.js Co-authored-by: Dave Smith <[email protected]> * Update getEntityRecord key docs * Refactor fetching currentHomePage * Disable modal buttons if saving * Store isPageDraft in useRef * Fix lint error * Remove onActionPerformed * Fix current homepage test * Remove duplicate getItemTitle function * Update logic for shouldShowSetAsHomepageAction * Swap order of list of actions * Add comment about manual saveEntityRecord call * Remove unnecessary space * Remove temporary modalButtonLabel variable * Combine draft and publish status tests * Only allow action on pages with draft or publish status * Remove handling of draft pages * Move closeModal into finally block * Refactor and remove renderModalBody --------- Co-authored-by: Sarah Norris <[email protected]> Co-authored-by: Sarah Norris <[email protected]> Co-authored-by: Dave Smith <[email protected]> Co-authored-by: creativecoder <[email protected]> Co-authored-by: mikachan <[email protected]> Co-authored-by: ntsekouras <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: jsnajdr <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: oandregal <[email protected]> Co-authored-by: getdave <[email protected]> Co-authored-by: jameskoster <[email protected]> Co-authored-by: richtabor <[email protected]> Co-authored-by: ramonjd <[email protected]> Co-authored-by: jasmussen <[email protected]> Co-authored-by: mtias <[email protected]>
- Loading branch information
1 parent
b54d1fe
commit d64cdab
Showing
12 changed files
with
296 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
174 changes: 174 additions & 0 deletions
174
packages/editor/src/components/post-actions/set-as-homepage.js
This file contains 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,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 }> | ||
<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 ] | ||
); | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
type PostStatus = | ||
| 'published' | ||
| 'publish' | ||
| 'draft' | ||
| 'pending' | ||
| 'private' | ||
|
This file contains 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 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.