-
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
DataViews Extensibility: Add a hook to allow third-party scripts to register/unregister post type actions #64138
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee07103
DataViews Extensibility: Add a hook to allow third-party scripts to r…
youknowriad f41e562
Remove * support
youknowriad 273c3ef
Fix native
youknowriad 509f759
Remove small copy/paste error
youknowriad a400615
Remove useless unit test
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,7 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import type { Action } from '@wordpress/dataviews'; | ||
import { doAction } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import deletePost from '../actions/delete-post'; | ||
import exportPattern from '../actions/export-pattern'; | ||
import resetPost from '../actions/reset-post'; | ||
import trashPost from '../actions/trash-post'; | ||
import permanentlyDeletePost from '../actions/permanently-delete-post'; | ||
import restorePost from '../actions/restore-post'; | ||
import type { PostType } from '../types'; | ||
import { store as editorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
export function registerEntityAction< Item >( | ||
kind: string, | ||
|
@@ -28,3 +43,56 @@ export function unregisterEntityAction( | |
actionId, | ||
}; | ||
} | ||
|
||
export function setIsReady( kind: string, name: string ) { | ||
return { | ||
type: 'SET_IS_READY' as const, | ||
kind, | ||
name, | ||
}; | ||
} | ||
|
||
export const registerPostTypeActions = | ||
( postType: string ) => | ||
async ( { registry }: { registry: any } ) => { | ||
const isReady = unlock( registry.select( editorStore ) ).isEntityReady( | ||
'postType', | ||
postType | ||
); | ||
if ( isReady ) { | ||
return; | ||
} | ||
|
||
unlock( registry.dispatch( editorStore ) ).setIsReady( | ||
'postType', | ||
postType | ||
); | ||
|
||
const postTypeConfig = ( await registry | ||
.resolveSelect( coreStore ) | ||
.getPostType( postType ) ) as PostType; | ||
|
||
const actions = [ | ||
postTypeConfig.slug === 'wp_block' ? exportPattern : undefined, | ||
resetPost, | ||
restorePost, | ||
deletePost, | ||
trashPost, | ||
permanentlyDeletePost, | ||
]; | ||
|
||
registry.batch( () => { | ||
actions.forEach( ( action ) => { | ||
if ( action === undefined ) { | ||
return; | ||
} | ||
unlock( registry.dispatch( editorStore ) ).registerEntityAction( | ||
'postType', | ||
postType, | ||
action | ||
); | ||
} ); | ||
} ); | ||
|
||
doAction( 'core.registerPostTypeActions', Promise.resolve(), postType ); | ||
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. Could our action registration use the same hook? 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 can see two sides here:
|
||
}; |
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,22 +1,14 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSelector } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { State } from './reducer'; | ||
|
||
export const getEntityActions = createSelector( | ||
( state: State, kind: string, name: string ) => { | ||
return [ | ||
...( state.actions[ kind ]?.[ name ] ?? [] ), | ||
...( state.actions[ kind ]?.[ '*' ] ?? [] ), | ||
]; | ||
}, | ||
( state: State, kind: string, name: string ) => [ | ||
state.actions[ kind ]?.[ name ], | ||
state.actions[ kind ]?.[ '*' ], | ||
] | ||
); | ||
const EMPTY_ARRAY = [] as []; | ||
|
||
export function getEntityActions( state: State, kind: string, name: string ) { | ||
return state.actions[ kind ]?.[ name ] ?? EMPTY_ARRAY; | ||
} | ||
|
||
export function isEntityReady( state: State, kind: string, name: string ) { | ||
return state.isReady[ kind ]?.[ name ]; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't love these kinds of effects tbh. Not sure what the alternative is, I guess filtered context wouldn't work.
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.
This is for me a good thing: Only register the actions and run the filter when needed (when using usePostAction hook), that said, I agree that the code is weird, but I don't know of another pattern that provides the same benefits.