-
Notifications
You must be signed in to change notification settings - Fork 384
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
Immediately add/remove DevTools-related AMP admin menu items when setting updated #6878
Changes from 2 commits
2d84185
32233f8
3cd8cc6
41dac1d
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 |
---|---|---|
|
@@ -151,7 +151,7 @@ function Root( { appRoot } ) { | |
const [ focusedSection, setFocusedSection ] = useState( global.location.hash.replace( /^#/, '' ) ); | ||
|
||
const { hasOptionsChanges, fetchingOptions, saveOptions } = useContext( Options ); | ||
const { hasDeveloperToolsOptionChange, saveDeveloperToolsOption } = useContext( User ); | ||
const { hasDeveloperToolsOptionChange, saveDeveloperToolsOption, developerToolsOption } = useContext( User ); | ||
const { templateModeWasOverridden } = useContext( ReaderThemes ); | ||
const { isSkipped } = useContext( SiteScanContext ); | ||
|
||
|
@@ -161,13 +161,46 @@ function Root( { appRoot } ) { | |
const onSubmit = useCallback( ( event ) => { | ||
event.preventDefault(); | ||
|
||
/* | ||
* Adds the developer tools 'Validated URLs' & 'Error Index' on turning ON | ||
* the 'Enable Developer Tools' toggle button and saving. | ||
*/ | ||
const addAmpDevOptions = ( optionTitle, optionUrl ) => { | ||
const wpAmpSettings = document.querySelector( '#toplevel_page_amp-options ul' ); | ||
|
||
if ( wpAmpSettings === null ) { | ||
return; | ||
} | ||
|
||
const listElement = document.createElement( 'li' ); | ||
const anchorElement = document.createElement( 'a' ); | ||
anchorElement.innerText = optionTitle; | ||
anchorElement.setAttribute( 'href', optionUrl ); | ||
listElement.appendChild( anchorElement ); | ||
wpAmpSettings.appendChild( listElement ); | ||
}; | ||
|
||
if ( hasOptionsChanges ) { | ||
saveOptions(); | ||
} | ||
|
||
if ( hasDeveloperToolsOptionChange ) { | ||
saveDeveloperToolsOption(); | ||
/* | ||
* Add/Remove Dev Tools 'Validated URLs' & 'Error Index' on 'Enable Developer Tools' ON/OFF on save. | ||
*/ | ||
if ( ! developerToolsOption ) { | ||
document.querySelectorAll( '#toplevel_page_amp-options ul li a[href*="post_type=amp_validated_url"]' ) | ||
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. While this works, it seems it could be hardened a bit to prevent removing unexpected menu items. |
||
.forEach( ( menuItem ) => { | ||
menuItem.parentNode.remove(); | ||
} ); | ||
return; | ||
} | ||
|
||
addAmpDevOptions( __( 'Validated URLs', 'amp' ), 'edit.php?post_type=amp_validated_url' ); | ||
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. The URL here is actually already exported as part of |
||
addAmpDevOptions( __( 'Error Index', 'amp' ), 'edit-tags.php?taxonomy=amp_validation_error&post_type=amp_validated_url' ); | ||
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. To go along with the previous, we could add |
||
} | ||
}, [ hasDeveloperToolsOptionChange, hasOptionsChanges, saveDeveloperToolsOption, saveOptions ] ); | ||
}, [ hasDeveloperToolsOptionChange, hasOptionsChanges, saveDeveloperToolsOption, saveOptions, developerToolsOption ] ); | ||
|
||
/** | ||
* Scroll to the focused element on load or when it changes. | ||
|
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 would make sense to be defined at the top level of the module, rather than as a nested callback.