Skip to content
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

Merged
merged 96 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
c7101cf
Adds basic action and modal to set page as homepage
creativecoder Sep 16, 2024
585ef01
Adds permissions and settings checks to set as homepage action
creativecoder Sep 16, 2024
41474da
Adds proper description and handles unpublished pages
creativecoder Sep 17, 2024
921d0f4
Adds action to set homepage to show latest posts
creativecoder Sep 18, 2024
23cefbd
Doesn't show action if there's a front-page template
creativecoder Sep 19, 2024
fb35be0
Creates page for posts, when specified
creativecoder Sep 19, 2024
5de983c
Refactors modal component
creativecoder Sep 30, 2024
0f3a986
Fixes issues from rebase
creativecoder Oct 1, 2024
1bdf2e0
Only show option on published pages
mikachan Oct 7, 2024
937ad00
Update snackbar wording
mikachan Oct 7, 2024
7df97a8
Check item exists before running getItemTitle logic
mikachan Oct 7, 2024
91a8275
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Oct 7, 2024
44feed5
Make key optional on GetEntityRecord
mikachan Oct 7, 2024
768f9cb
Remove some ts-ignore comments
mikachan Oct 7, 2024
a04fb70
Add support for page_for_posts to Settings
mikachan Oct 7, 2024
7462ce7
Remove some more ts-ignores
mikachan Oct 7, 2024
2d2c856
Allow recordId to be optional
mikachan Oct 7, 2024
8bbd86d
Increase size of action modal
mikachan Oct 8, 2024
6fc3490
Implement choose existing page option
mikachan Oct 8, 2024
341a068
Fix number/string comparison
mikachan Oct 8, 2024
b27305f
Add initial e2e test
mikachan Oct 9, 2024
b387c34
Set post actions modal to medium
mikachan Oct 9, 2024
2429d77
Tweak ToggleGroupControl help text
mikachan Oct 9, 2024
57eceb1
Fix initial test
mikachan Oct 9, 2024
49be2ab
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Oct 23, 2024
40568dc
Remove extra useSiteSettings hook
mikachan Oct 23, 2024
489f4d6
Allow setting draft pages to homepage
mikachan Oct 23, 2024
c36e3ac
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Oct 29, 2024
3cb07e9
Fix merge conflict
mikachan Oct 29, 2024
9a5f4d7
Remove item check from getItemTitle
mikachan Oct 31, 2024
c4bd9ae
Remove posts page options
mikachan Nov 8, 2024
46071d7
Don't show homepage option if selected page is the page for posts
mikachan Nov 8, 2024
42e0791
Reload actions list when site settings change
mikachan Nov 8, 2024
3434c3f
Update tests
mikachan Nov 8, 2024
e84a33e
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 8, 2024
baea572
Remove call to __experimentalGetTemplateForLink
mikachan Nov 8, 2024
c413a17
Update tests
mikachan Nov 8, 2024
ba8becb
Remove item check in getItemTitle
mikachan Nov 12, 2024
7687e3c
Use useSelect instead of select
mikachan Nov 12, 2024
81b76f8
Remove PAGE_POST_TYPE constant
mikachan Nov 12, 2024
0ca96b0
Use saveEntityRecord instead of editEntityRecord
mikachan Nov 12, 2024
17754bd
Remove onSetLatestPostsHomepage option
mikachan Nov 12, 2024
e722ceb
Remove select for site settings from isEligible
mikachan Nov 12, 2024
91de6c0
Update post actions with site settings info
mikachan Nov 12, 2024
3c71533
Remove select for templates from isEligible
mikachan Nov 12, 2024
6b2f6b9
Skip last test for now
mikachan Nov 12, 2024
6a1f468
Restore whitespace
mikachan Nov 12, 2024
b81eb1c
Rename _select
mikachan Nov 13, 2024
6d36c1b
Remove sub-objects from additionalContext selectors
mikachan Nov 13, 2024
8b40105
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 15, 2024
3caa096
Remove duplicate page_for_posts definition
mikachan Nov 15, 2024
d20b87b
Fix page/post type error
mikachan Nov 15, 2024
29a1f72
Remove additional groups within additionalContext
mikachan Nov 15, 2024
0c5883a
Fix siteSettings in TitleView
mikachan Nov 15, 2024
24dcf8f
Move hasFrontPageTemplate check to private-actions
mikachan Nov 15, 2024
095bbe3
Add JSDoc to setAsHomepage
mikachan Nov 15, 2024
991ac5c
Refactor siteSettings in post-list
mikachan Nov 15, 2024
f2acc94
Move homepage action to edit-site
mikachan Nov 15, 2024
969ed4a
Revert unnecessary changes
mikachan Nov 15, 2024
259a476
Move getItemTitle to edit-site utils
mikachan Nov 15, 2024
efbf96c
Allow undefined on GetEntityRecord key
mikachan Nov 15, 2024
9de4488
Make it more clear that draft page will be published
mikachan Nov 15, 2024
5cbe7b4
Update draft page wording
mikachan Nov 18, 2024
7a2960a
Add set homepage action to post editor
mikachan Nov 18, 2024
4d45eaf
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 18, 2024
fb2ea4c
Attempt to fix build error
mikachan Nov 18, 2024
1a1f682
Remove homepage action from edit-site
mikachan Nov 18, 2024
c10846b
Remove extra line
mikachan Nov 18, 2024
5e5cf0c
Fix getting current homepage title
mikachan Nov 18, 2024
2b37368
Make key in getEntityRecord optional
mikachan Nov 18, 2024
43dd85f
Use getHomePage selector
mikachan Nov 18, 2024
4cd6988
Move canManageOptions and hasFrontPageTemplate to actions.js
mikachan Nov 18, 2024
b2e88eb
Make key optional in EntityRecordKey
mikachan Nov 19, 2024
953e999
Remove undefined from getEntityRecord calls
mikachan Nov 19, 2024
f01d347
Update packages/editor/src/components/post-actions/actions.js
mikachan Nov 20, 2024
d96e90e
Update getEntityRecord key docs
mikachan Nov 20, 2024
9685c3a
Refactor fetching currentHomePage
mikachan Nov 20, 2024
b029794
Disable modal buttons if saving
mikachan Nov 20, 2024
a9ea268
Store isPageDraft in useRef
mikachan Nov 20, 2024
e438496
Fix lint error
mikachan Nov 20, 2024
04a7ba4
Remove onActionPerformed
mikachan Nov 20, 2024
c97a17d
Fix current homepage test
mikachan Nov 20, 2024
0b8c27e
Remove duplicate getItemTitle function
mikachan Nov 21, 2024
f8df3b9
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 21, 2024
a175adc
Update logic for shouldShowSetAsHomepageAction
mikachan Nov 22, 2024
86af09a
Swap order of list of actions
mikachan Nov 22, 2024
10d97b7
Add comment about manual saveEntityRecord call
mikachan Nov 22, 2024
c23c35b
Remove unnecessary space
mikachan Nov 22, 2024
b1707e4
Remove temporary modalButtonLabel variable
mikachan Nov 22, 2024
d6a7059
Combine draft and publish status tests
mikachan Nov 22, 2024
a696688
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 22, 2024
6d01e34
Only allow action on pages with draft or publish status
mikachan Nov 25, 2024
18aeaf6
Remove handling of draft pages
mikachan Nov 25, 2024
d486bac
Merge branch 'trunk' into try/site-editor-homepage-settings
mikachan Nov 25, 2024
8bacdbb
Move closeModal into finally block
mikachan Nov 27, 2024
e1a7aa9
Refactor and remove renderModalBody
mikachan Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ _Parameters_

- _kind_ `string`: Kind of the edited entity record.
- _name_ `string`: Name of the edited entity record.
- _recordId_ `number|string`: Record ID of the edited entity record.
- _recordId_ `number|string=`: Record ID of the edited entity record.
- _edits_ `Object`: The edits.
- _options_ `Object`: Options for the edit.
- _options.undoIgnore_ `[boolean]`: Whether to ignore the edit in undo history or not.
Expand Down Expand Up @@ -888,7 +888,7 @@ _Parameters_

- _kind_ `string`: Kind of the entity.
- _name_ `string`: Name of the entity.
- _recordId_ `Object`: ID of the record.
- _recordId_ `Object=`: ID of the record.
- _options_ `Object=`: Saving options.

### saveEntityRecord
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ _Parameters_

- _kind_ `string`: Kind of the edited entity record.
- _name_ `string`: Name of the edited entity record.
- _recordId_ `number|string`: Record ID of the edited entity record.
- _recordId_ `number|string=`: Record ID of the edited entity record.
- _edits_ `Object`: The edits.
- _options_ `Object`: Options for the edit.
- _options.undoIgnore_ `[boolean]`: Whether to ignore the edit in undo history or not.
Expand Down Expand Up @@ -298,7 +298,7 @@ _Parameters_

- _kind_ `string`: Kind of the entity.
- _name_ `string`: Name of the entity.
- _recordId_ `Object`: ID of the record.
- _recordId_ `Object=`: ID of the record.
- _options_ `Object=`: Saving options.

### saveEntityRecord
Expand Down
14 changes: 7 additions & 7 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,12 @@ export const deleteEntityRecord =
* Returns an action object that triggers an
* edit to an entity record.
*
* @param {string} kind Kind of the edited entity record.
* @param {string} name Name of the edited entity record.
* @param {number|string} recordId Record ID of the edited entity record.
* @param {Object} edits The edits.
* @param {Object} options Options for the edit.
* @param {boolean} [options.undoIgnore] Whether to ignore the edit in undo history or not.
* @param {string} kind Kind of the edited entity record.
* @param {string} name Name of the edited entity record.
* @param {number|string=} recordId Record ID of the edited entity record.
* @param {Object} edits The edits.
* @param {Object} options Options for the edit.
* @param {boolean} [options.undoIgnore] Whether to ignore the edit in undo history or not.
*
* @return {Object} Action object.
*/
Expand Down Expand Up @@ -775,7 +775,7 @@ export const __experimentalBatch =
*
* @param {string} kind Kind of the entity.
* @param {string} name Name of the entity.
* @param {Object} recordId ID of the record.
* @param {Object=} recordId ID of the record.
* @param {Object=} options Saving options.
*/
export const saveEditedEntityRecord =
Expand Down
4 changes: 4 additions & 0 deletions packages/core-data/src/entity-types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ declare module './base-entity-records' {
* Site icon.
*/
site_icon: number;
/**
* The ID of the page that should display posts.
*/
page_for_posts: number;
mikachan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export interface GetEntityRecord {
state: State,
kind: string,
name: string,
key: EntityRecordKey,
key?: EntityRecordKey,
getdave marked this conversation as resolved.
Show resolved Hide resolved
query?: GetRecordsHttpQuery
): EntityRecord | undefined;

Expand All @@ -321,7 +321,7 @@ export interface GetEntityRecord {
>(
kind: string,
name: string,
key: EntityRecordKey,
key?: EntityRecordKey,
query?: GetRecordsHttpQuery
) => EntityRecord | undefined;
__unstableNormalizeArgs?: ( args: EntityRecordArgs ) => EntityRecordArgs;
Expand Down
2 changes: 1 addition & 1 deletion packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ function Paste( { children } ) {
_Parameters_

- _mapSelect_ `T`: Function called on every state change. The returned value is exposed to the component implementing this hook. The function receives the `registry.select` method on the first argument and the `registry` on the second argument. When a store key is passed, all selectors for the store will be returned. This is only meant for usage of these selectors in event callbacks, not for data needed to create the element tree.
- _deps_ `unknown[]`: If provided, this memoizes the mapSelect so the same `mapSelect` is invoked on every state change unless the dependencies change.
- _deps_ `[unknown[]]`: If provided, this memoizes the mapSelect so the same `mapSelect` is invoked on every state change unless the dependencies change.

_Returns_

Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/components/use-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function _useMappingSelect( suspense, mapSelect, deps ) {
* When a store key is passed, all selectors for the store will be
* returned. This is only meant for usage of these selectors in event
* callbacks, not for data needed to create the element tree.
* @param {unknown[]} deps If provided, this memoizes the mapSelect so the same `mapSelect` is
* @param {unknown[]} [deps] If provided, this memoizes the mapSelect so the same `mapSelect` is
* invoked on every state change unless the dependencies change.
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function ActionModal< Item >( {
__experimentalHideHeader={ !! action.hideModalHeader }
onRequestClose={ closeModal ?? ( () => {} ) }
focusOnMount="firstContentElement"
size="small"
size="medium"
overlayClassName={ `dataviews-action-modal dataviews-action-modal__${ kebabCase(
action.id
) }` }
Expand Down
9 changes: 8 additions & 1 deletion packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,16 @@ export default function PostList( { postType } ) {
context: 'list',
} );
const editAction = useEditPostAction();
const siteSettings = useSelect( ( select ) => {
return select( coreStore ).getEntityRecord( 'root', 'site' );
} );
const pageOnFront = siteSettings?.page_on_front;
const pageForPosts = siteSettings?.page_for_posts;
const actions = useMemo(
() => [ editAction, ...postTypeActions ],
[ postTypeActions, editAction ]
// The "Set as homepage" action eligibility depends on the pageOnFront and pageForPosts settings.
mikachan marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line react-hooks/exhaustive-deps
[ postTypeActions, editAction, pageOnFront, pageForPosts ]
);

const [ showAddPostModal, setShowAddPostModal ] = useState( false );
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function ActionWithModal( { action, item, ActionTrigger, onClose } ) {
action.id
) }` }
focusOnMount="firstContentElement"
size="small"
size="medium"
>
<RenderModal
items={ [ item ] }
Expand Down
12 changes: 12 additions & 0 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
exportPattern,
permanentlyDeletePost,
restorePost,
setAsHomepage,
trashPost,
renamePost,
resetPost,
Expand Down Expand Up @@ -87,6 +88,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 All @@ -113,6 +122,9 @@ export const registerPostTypeActions =
? duplicatePattern
: undefined,
postTypeConfig.supports?.title ? renamePost : undefined,
canManageOptions && postTypeConfig.slug === 'page'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this logic as part of the editor package, so I presumed both the site editor & post editor would have the action. However, I've noticed the post editor doesn't:

Site editor Posts editor
Screenshot 2024-11-14 at 10 06 28 Screenshot 2024-11-14 at 10 06 03

It turns out to be that the canManageOptions check is undefined for the post editor. Would you know why is that?

Checked a few things and:

  • the canCreate check works fine in the post editor
  • the canManageOptions is actually resolved when using the post editor to edit a post (not a page)

🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While duplicating the action for the post editor, I've managed to get the new homepage action to show so hopefully that gets around the canManageOptions check being undefined, but getEntityRecord( 'postType', 'page', [pageId] ) doesn't seem to work in this editor. I'm not sure why, it's like the editor only has access to the current page that's being edited.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've figured out a way to handle getting the current homepage in every context (page list, site editor and post editor). I'm not sure if it's the best way, but it seems to be working. I think the record was being checked too soon, so I've wrapped it in a useEffect in 5e5cf0c.

? setAsHomepage
: undefined,
postTypeConfig?.supports?.[ 'page-attributes' ]
? reorderPage
: undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/dataviews/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type PostStatus =
| 'published'
Copy link
Member

Choose a reason for hiding this comment

The 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 wordpress/fields package (see). It sounds to me the fields package should be the central package for that kind of information about fields and posts. Not for this PR, just sharing a potential follow-up to clean things up later.

| 'publish'
| 'draft'
| 'pending'
| 'private'
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const TEMPLATE_POST_TYPE = 'wp_template';
export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
export const PATTERN_POST_TYPE = 'wp_block';
export const NAVIGATION_POST_TYPE = 'wp_navigation';
export const PAGE_POST_TYPE = 'page';
mikachan marked this conversation as resolved.
Show resolved Hide resolved
export const TEMPLATE_ORIGINS = {
custom: 'custom',
theme: 'theme',
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ Undocumented declaration.

Undocumented declaration.

### setAsHomepage

Undocumented declaration.

### slugField

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { default as permanentlyDeletePost } from './permanently-delete-post';
export { default as restorePost } from './restore-post';
export { default as trashPost } from './trash-post';
export { default as deletePost } from './delete-post';
export { default as setAsHomepage } from './set-as-homepage';
Loading
Loading