-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Warn if a template does not have one main element #68656
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ export { default as EntitiesSavedStates } from './entities-saved-states'; | |
export { useIsDirty as useEntitiesSavedStatesIsDirty } from './entities-saved-states/hooks/use-is-dirty'; | ||
export { default as ErrorBoundary } from './error-boundary'; | ||
export { default as LocalAutosaveMonitor } from './local-autosave-monitor'; | ||
export { default as MainElementWarnings } from './main-element-warnings'; | ||
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. Do we want to make this component a public API? Probably better to have it for internal use only for now. |
||
export { default as PageAttributesCheck } from './page-attributes/check'; | ||
export { default as PageAttributesOrder } from './page-attributes/order'; | ||
export { default as PageAttributesPanel } from './page-attributes/panel'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
|
||
const checkMainTag = ( blocks, mainTagCount ) => { | ||
blocks.forEach( ( block ) => { | ||
if ( block.attributes.tagName === 'main' ) { | ||
mainTagCount++; | ||
} | ||
// If the block has innerBlocks, call the function again. | ||
if ( block.innerBlocks.length > 0 ) { | ||
mainTagCount = checkMainTag( block.innerBlocks, mainTagCount ); | ||
} | ||
} ); | ||
|
||
return mainTagCount; | ||
}; | ||
Comment on lines
+15
to
+27
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 like the direction here. I think we could optimize this by switching to the If I remember correctly, Here's what I've in mind: // Returns 0, 1 or 2. 0 and 2 are in violation. 1 is correct.
function checkMainTagCount( blocks = [], count = 0 ) {
for ( const block of blocks ) {
if ( block.attributes.tagName === 'main' ) {
count++;
}
// Look no further; we've found a violation.
if ( count > 1 ) {
break;
}
// Check nested blocks.
if ( block.innerBlocks.length > 0 ) {
count = checkMainTagCount( block.innerBlocks, count );
}
}
return count;
} |
||
|
||
export default function MainElementWarnings() { | ||
const { type, blocks } = useSelect( ( select ) => { | ||
const postType = select( editorStore ).getCurrentPostType(); | ||
const { getBlocks } = select( blockEditorStore ); | ||
return { | ||
type: postType, | ||
blocks: getBlocks(), | ||
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. We could move the post type check here and only return the |
||
}; | ||
}, [] ); | ||
|
||
const { createWarningNotice, removeNotice } = useDispatch( noticesStore ); | ||
|
||
useEffect( () => { | ||
if ( 'wp_template' === type ) { | ||
const mainTagCount = checkMainTag( blocks, 0 ); | ||
|
||
removeNotice( 'edit-site-main-notice' ); | ||
|
||
if ( 0 === mainTagCount || 1 < mainTagCount ) { | ||
createWarningNotice( | ||
__( 'Your template should have exactly one main element.' ), | ||
{ id: 'edit-site-main-notice' } | ||
); | ||
} | ||
} | ||
}, [ type, blocks, createWarningNotice, removeNotice ] ); | ||
|
||
return null; | ||
} |
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 was not sure about this and would welcome suggestions for better places to trigger the check from.