Skip to content

Commit

Permalink
Adds permissions and settings checks to set as homepage action
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed Sep 16, 2024
1 parent 7ac666c commit 5d49655
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 14 additions & 4 deletions packages/editor/src/dataviews/actions/set-as-homepage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { select, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
Expand All @@ -18,7 +18,6 @@ import { store as noticesStore } from '@wordpress/notices';
* Internal dependencies
*/
import { PAGE_POST_TYPE } from '../../store/constants';
import { unlock } from '../../lock-unlock';
import { getItemTitle } from './utils';
import type { CoreDataError, PostWithPermissions } from '../types';

Expand All @@ -30,8 +29,17 @@ const renamePost: Action< PostWithPermissions > = {
return false;
}

// TODO: add user permissions check.
return post.type === PAGE_POST_TYPE;
if ( post.type !== PAGE_POST_TYPE ) {
return false;
}

// @ts-ignore
const { page_on_front: pageOnFront } = select(
coreStore
// @ts-ignore
).getEntityRecord( 'root', 'site' );

return pageOnFront !== post.id;
},
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
const [ item ] = items;
Expand All @@ -44,11 +52,13 @@ const renamePost: Action< PostWithPermissions > = {
async function onSetAsHomepage( event: React.FormEvent ) {
event.preventDefault();
try {
// @ts-ignore
await editEntityRecord( 'root', 'site', undefined, {
page_on_front: item.id,
} );
closeModal?.();
// Persist edited entity.
// @ts-ignore
await saveEditedEntityRecord( 'root', 'site', undefined, {
page_on_front: item.id,
} );
Expand Down
12 changes: 11 additions & 1 deletion packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export const registerPostTypeActions =
kind: 'postType',
name: postType,
} );

const canManageOptions = await registry
.resolveSelect( coreStore )
.canUser( 'update', {
kind: 'root',
name: 'site',
} );

const currentTheme = await registry
.resolveSelect( coreStore )
.getCurrentTheme();
Expand Down Expand Up @@ -116,7 +124,9 @@ export const registerPostTypeActions =
? reorderPage
: undefined,
postTypeConfig.slug === 'wp_block' ? exportPattern : undefined,
postTypeConfig.slug === 'page' ? setAsHomepage : undefined,
canManageOptions && postTypeConfig.slug === 'page'
? setAsHomepage
: undefined,
resetPost,
restorePost,
deletePost,
Expand Down

0 comments on commit 5d49655

Please sign in to comment.