diff --git a/packages/edit-site/src/components/dataviews-actions/index.js b/packages/edit-site/src/components/dataviews-actions/index.js index 01a42ffab2e304..09b7597c6cb341 100644 --- a/packages/edit-site/src/components/dataviews-actions/index.js +++ b/packages/edit-site/src/components/dataviews-actions/index.js @@ -11,7 +11,6 @@ import { privateApis as routerPrivateApis } from '@wordpress/router'; */ import { PATTERN_TYPES } from '../../utils/constants'; import { unlock } from '../../lock-unlock'; -import { useSetAsHomepageAction } from './set-as-homepage'; const { useHistory } = unlock( routerPrivateApis ); @@ -42,5 +41,3 @@ export const useEditPostAction = () => { [ history ] ); }; - -export { useSetAsHomepageAction }; diff --git a/packages/edit-site/src/components/dataviews-actions/set-as-homepage.js b/packages/edit-site/src/components/dataviews-actions/set-as-homepage.js deleted file mode 100644 index 4b1e4f3a9edfb4..00000000000000 --- a/packages/edit-site/src/components/dataviews-actions/set-as-homepage.js +++ /dev/null @@ -1,229 +0,0 @@ -/** - * 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 SetAsHomepageModal = ( { items, closeModal, onActionPerformed } ) => { - const [ item ] = items; - const pageTitle = getItemTitle( item ); - const { currentHomePage, showOnFront } = useSelect( ( select ) => { - const { getEntityRecord } = select( coreStore ); - const siteSettings = getEntityRecord( 'root', 'site', undefined ); - const pageOnFront = siteSettings?.page_on_front; - return { - currentHomePage: getEntityRecord( 'postType', 'page', pageOnFront ), - showOnFront: siteSettings?.show_on_front, - }; - } ); - - const { saveEditedEntityRecord, saveEntityRecord } = - useDispatch( coreStore ); - const { createSuccessNotice, createErrorNotice } = - useDispatch( noticesStore ); - const isPageDraft = item.status === 'draft'; - - async function onSetPageAsHomepage( event ) { - event.preventDefault(); - - try { - // If selected page is set to draft, publish the page. - if ( isPageDraft ) { - await saveEntityRecord( 'postType', 'page', { - ...item, - status: 'publish', - } ); - } - - // Save new home page settings. - await saveEntityRecord( 'root', 'site', { - page_on_front: item.id, - show_on_front: 'page', - } ); - - closeModal?.(); - - await saveEditedEntityRecord( 'root', 'site', undefined, { - page_on_front: item.id, - show_on_front: 'page', - } ); - - createSuccessNotice( __( 'Homepage updated' ), { - type: 'snackbar', - } ); - - onActionPerformed?.( items ); - } 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' } ); - } - } - - const renderModalBody = () => { - if ( 'posts' === showOnFront ) { - return ( - <> - - { sprintf( - // translators: %s: title of the page to be set as the homepage. - __( - 'Set "%s" as the site homepage? This will replace the current homepage which is set to display latest posts.' - ), - pageTitle - ) } - - - ); - } - - if ( isPageDraft ) { - return ( - <> - - { sprintf( - // translators: %s: title of the page to be set as the homepage. - __( - '"%s" is a draft and will be published automatically if set as the homepage. Publish page and set as the site homepage?' - ), - pageTitle - ) } - - - ); - } - - const modalTranslatedString = - // translators: %1$s: title of page to be set as the home page. %2$s: title of the current home page. - __( - 'Set "%1$s" as the site homepage? This will replace the current homepage: "%2$s"' - ); - - return ( - - { sprintf( - modalTranslatedString, - pageTitle, - getItemTitle( currentHomePage ) - ) }{ ' ' } - - ); - }; - - return ( -
- - { renderModalBody() } - - - - - -
- ); -}; - -export const useSetAsHomepageAction = () => { - const { - canManageOptions, - hasFrontPageTemplate, - pageOnFront, - pageForPosts, - } = useSelect( ( select ) => { - const { getEntityRecord, getEntityRecords } = select( coreStore ); - const siteSettings = getEntityRecord( 'root', 'site', undefined ); - const templates = getEntityRecords( 'postType', 'wp_template', { - per_page: -1, - } ); - - return { - pageOnFront: siteSettings?.page_on_front, - pageForPosts: siteSettings?.page_for_posts, - canManageOptions: select( coreStore ).canUser( 'update', { - kind: 'root', - name: 'site', - } ), - hasFrontPageTemplate: !! templates?.find( - ( template ) => - 'slug' in template && template.slug === 'front-page' - ), - }; - } ); - - return useMemo( - () => ( { - id: 'set-as-homepage', - label: __( 'Set as homepage' ), - isEligible( post ) { - if ( post.status === 'trash' ) { - 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; - } - - // Don't show the action if the user can't manage site options. - if ( ! canManageOptions ) { - return false; - } - - // A front-page template overrides homepage settings, - // so don't show the setAsHomepage action if it's present. - if ( hasFrontPageTemplate ) { - return false; - } - - return true; - }, - RenderModal: SetAsHomepageModal, - } ), - [ canManageOptions, hasFrontPageTemplate, pageForPosts, pageOnFront ] - ); -}; diff --git a/packages/edit-site/src/components/post-list/index.js b/packages/edit-site/src/components/post-list/index.js index 4ab68304e2a679..b87af54361f659 100644 --- a/packages/edit-site/src/components/post-list/index.js +++ b/packages/edit-site/src/components/post-list/index.js @@ -30,10 +30,7 @@ import { import AddNewPostModal from '../add-new-post'; import { unlock } from '../../lock-unlock'; -import { - useEditPostAction, - useSetAsHomepageAction, -} from '../dataviews-actions'; +import { useEditPostAction } from '../dataviews-actions'; import { usePrevious } from '@wordpress/compose'; const { usePostActions, usePostFields } = unlock( editorPrivateApis ); @@ -348,11 +345,10 @@ export default function PostList( { postType } ) { context: 'list', } ); const editAction = useEditPostAction(); - const setAsHomepageAction = useSetAsHomepageAction(); const actions = useMemo( - () => [ editAction, setAsHomepageAction, ...postTypeActions ], - [ postTypeActions, editAction, setAsHomepageAction ] + () => [ editAction, ...postTypeActions ], + [ postTypeActions, editAction ] ); const [ showAddPostModal, setShowAddPostModal ] = useState( false ); diff --git a/packages/edit-site/src/utils/get-item-title.d.ts b/packages/edit-site/src/utils/get-item-title.d.ts deleted file mode 100644 index 736d68c98f5f0e..00000000000000 --- a/packages/edit-site/src/utils/get-item-title.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export function getItemTitle( item: any ): string; -//# sourceMappingURL=get-item-title.d.ts.map diff --git a/packages/edit-site/src/utils/get-item-title.d.ts.map b/packages/edit-site/src/utils/get-item-title.d.ts.map deleted file mode 100644 index 76a514bc9b97f3..00000000000000 --- a/packages/edit-site/src/utils/get-item-title.d.ts.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"get-item-title.d.ts","sourceRoot":"","sources":["get-item-title.js"],"names":[],"mappings":"AAQA,gDAWC"} \ No newline at end of file diff --git a/packages/editor/src/components/post-actions/set-as-homepage.js b/packages/editor/src/components/post-actions/set-as-homepage.js index 073f1f7408b268..4b1e4f3a9edfb4 100644 --- a/packages/editor/src/components/post-actions/set-as-homepage.js +++ b/packages/editor/src/components/post-actions/set-as-homepage.js @@ -16,9 +16,7 @@ import { store as noticesStore } from '@wordpress/notices'; /** * Internal dependencies */ -import { getItemTitle } from '../../../../../packages/edit-site/src/utils/get-item-title'; - -// This action is duplicated and copied from packages/edit-site/src/components/dataviews-actions/set-as-homepage.js +import { getItemTitle } from '../../utils/get-item-title'; const SetAsHomepageModal = ( { items, closeModal, onActionPerformed } ) => { const [ item ] = items; diff --git a/packages/edit-site/src/utils/get-item-title.js b/packages/editor/src/utils/get-item-title.js similarity index 77% rename from packages/edit-site/src/utils/get-item-title.js rename to packages/editor/src/utils/get-item-title.js index 34e083da77cd75..b98160cbde3dcc 100644 --- a/packages/edit-site/src/utils/get-item-title.js +++ b/packages/editor/src/utils/get-item-title.js @@ -3,8 +3,7 @@ */ import { decodeEntities } from '@wordpress/html-entities'; -// TODO: This function is duplicated in packages/editor/src/dataviews/actions/utils.ts, -// so they should be consolidated. +// This function is copied from packages/editor/src/dataviews/actions/utils.ts. export function getItemTitle( item ) { if ( typeof item.title === 'string' ) {