-
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
Integrate admin bar with AMP dev mode #3187
Merged
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3380451
Revert "Suppress admin bar from being shown during validation requests"
westonruter e3d4f57
Skip sanitizing admin bar (and removing it) in favor of ignoring (in …
westonruter 8640d1a
Prevent tree-shaking under #wpadminbar when admin bar is showing
westonruter f75586f
Prevent admin bar from even attempting to be displayed on story template
westonruter a81fb73
Remove redundant document argument from is_document_in_dev_mode()
westonruter bfad786
Skip converting elements in dev mode; skip processing style attribute…
westonruter 5bb7466
Add filters for whether dev mode is enabled and what elements get it
westonruter d615d39
Bring back remove_admin_bar_if_css_excluded and admin-bar CSS priorit…
westonruter 277131f
Prevent dev mode when admin bar shown for unauthenticated users
westonruter 4b533d2
Fix placement of dev_mode_enabled variable definition
westonruter e3025cd
Fix undefined dev_mode_xpaths being used in cache key
westonruter a726d41
Fix typos in comments and add array casting
westonruter 58f5eb3
Merge branch 'develop' of github.com:ampproject/amp-wp into add/dev-m…
westonruter d1ac164
Move dev mode mutations into new sanitizer
westonruter 5ba10b4
Remove now-unnecessary admin_bar_showing arg for style sanitizer
westonruter 38fa7ba
Add tests for init_admin_bar
westonruter 71c195c
Add test for amp_is_dev_mode()
westonruter 2471036
Add test for amp_get_content_sanitizers()
westonruter 7c57708
Add tests for AMP_Dev_Mode_Sanitizer
westonruter e15bbe3
Include dashicons in dev mode but only if exclusively connected to ad…
westonruter d79be8d
Remove amp_dev_mode_enabled filter in test
westonruter 5de22ff
Remove useless variable
schlessera 290adf7
Add asserts to make sure we are not enqueueing both versions of dashi…
schlessera 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 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Class AMP_Dev_Mode_Sanitizer | ||
* | ||
* Add the data-ampdevmode to the document element and to the elements specified by the supplied args. | ||
* | ||
* @since 1.3 | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Dev_Mode_Sanitizer | ||
* | ||
* @since 1.3 | ||
*/ | ||
final class AMP_Dev_Mode_Sanitizer extends AMP_Base_Sanitizer { | ||
|
||
/** | ||
* Array of flags used to control sanitization. | ||
* | ||
* @var array { | ||
* @type string[] $element_xpaths XPath expressions for elements to add the data-ampdevmode attribute to. | ||
* } | ||
*/ | ||
protected $args; | ||
|
||
/** | ||
* Sanitize document for dev mode. | ||
* | ||
* @since 1.3 | ||
*/ | ||
public function sanitize() { | ||
$this->dom->documentElement->setAttribute( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, '' ); | ||
|
||
$xpath = new DOMXPath( $this->dom ); | ||
$element_xpaths = ! empty( $this->args['element_xpaths'] ) ? $this->args['element_xpaths'] : []; | ||
foreach ( $element_xpaths as $element_xpath ) { | ||
foreach ( $xpath->query( $element_xpath ) as $node ) { | ||
if ( $node instanceof DOMElement ) { | ||
$node->setAttribute( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, '' ); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 think it would be preferable to have this piece of logic be part of the
AMP_Dev_Mode_Sanitizer
class instead, and then make that sanitizer decide when it acts or not, instead of hardcoding it in here.It should be the responsibility of the
AMP_Dev_Mode_Sanitizer
to know when to do something and what to do, not some helper function collection.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 did think about that, but I think it's preferable here because this function is inherently connected directly with WordPress as it applies a filter. There are two reasons for why this should not be in the sanitizer:
amp_get_content_sanitizers()
be deterministic in what the resulting sanitized document be. In other words, we cannot apply filters inside of a sanitizer because then we cannot use the sanitizer args as a source of truth for computing a cache key for the post-processed output, as we can now.Additionally, this
amp_is_dev_mode()
function would be useful outside of the context of a sanitizer. For example, a theme/plugin may want to call this function during template generation to determine whether it can output some dev code (e.g. Query Monitor).The
amp_is_dev_mode()
is gatekeeping whether or not theAMP_Dev_Mode_Sanitizer
should be used in the first place. So it is operating at a higher level.Aside: should it rather be named
is_amp_dev_mode()
?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'm seeing the
amp_
as a prefix here, as this could just as well be used outside of the AMP plugin, so I think the name is fine.