-
Notifications
You must be signed in to change notification settings - Fork 383
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
Add debugging query arguments #3448
Closed
Closed
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
37de1c3
Add debugging query arguments
kienstra e738bcf
Use the filter 'amp_validation_error_sanitized' instead of another co…
kienstra 19363fc
Add a separate filter for 'amp_validation_error_sanitized', add top-l…
kienstra aeeda26
Merge branch 'develop' into add/query-args-debugging
kienstra 388de8e
Add a query var to accept 'excessive_css' errors
kienstra 17747a4
Merge branch 'develop' into add/query-args-debugging
kienstra 2c0b1d0
Add a query var to skip tree shaking
kienstra e41505d
Add a debug query var to disable AMP
kienstra ac1311e
In the case of redirecting to the non-AMP URL, remove the flags query…
kienstra fffbbbd
Begin to add the submenu in the admin bar for AMP debugging options
kienstra fe602ac
Address failed PHPUnit test in Travis
kienstra 405d30d
Attempt to address failed PHPUnit test by removing a filter
kienstra 39b0e2f
Merge branch 'develop' into add/query-args-debugging
kienstra e4be908
Delete the remove_all_filters() calls, instead register 'wp-element'
kienstra 81be395
Add the remaining query vars to the submenu
kienstra 114e5d8
Change skip_tree_shaking to disable_tree_shaking
kienstra 2bd69c3
Add the toggles for the query vars
kienstra 5f186af
Add the debug options to the admin bar even on non-AMP endpoints
kienstra 8b0c96f
Continue with query args.
kienstra 7b41a0b
Remove the JS to add the admin bar debug options
kienstra c5b0d3b
Add a submenu to the admin bar for the debug options
kienstra 00358e4
Merge branch 'develop' into add/query-args-debugging
kienstra 7a3733a
Remove the entry in webpack.config.js for the previous JS file
kienstra 687cf47
Fix failing unit test for AMP_Validation_Error_Taxonomy
kienstra ef0720c
Use different emojis for the checkboxes
kienstra 675f875
Fix unit test of dependent function
kienstra 996157a
Thanks to Alain's suggestion, add AMP_Debug class
kienstra e3bad9e
Remove 2 constants that were moved to AMP_Debug
kienstra 4b6a48f
Simplify tests and improve DocBlocks
kienstra d5e2b2d
Add a test for passing a random string as a query var
kienstra 1d40623
Add another assertion for a numeric string returning false
kienstra 42a29d0
Add a little documentation for AMP_Debug::has_flag()
kienstra a1929ab
Change the behavior for the query var being present but empty
kienstra 03a2395
Merge branch 'develop' of github.com:ampproject/amp-wp into add/query…
westonruter 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
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,119 @@ | ||
<?php | ||
/** | ||
* Class AMP_Debug | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Debug | ||
* | ||
* @since 1.4.1 | ||
*/ | ||
final class AMP_Debug { | ||
|
||
/** | ||
* A top-level query var for AMP flags. | ||
* | ||
* Debugging query vars should follow this. | ||
* For example, https://example.com/?amp&_flags[disable_post_processing]=1 | ||
* | ||
* @var string | ||
*/ | ||
const AMP_FLAGS_QUERY_VAR = 'amp_flags'; | ||
|
||
/** | ||
* A query var to disable post processing. | ||
* | ||
* @var string | ||
*/ | ||
const DISABLE_POST_PROCESSING_QUERY_VAR = 'disable_post_processing'; | ||
|
||
/** | ||
* A query var to disable the response cache. | ||
* | ||
* @var string | ||
*/ | ||
const DISABLE_RESPONSE_CACHE_QUERY_VAR = 'disable_response_cache'; | ||
|
||
/** | ||
* A query var to prevent a redirect to a non-AMP URL. | ||
* | ||
* @var string | ||
*/ | ||
const PREVENT_REDIRECT_TO_NON_AMP_QUERY_VAR = 'prevent_redirect'; | ||
|
||
/** | ||
* A query var to disable tree shaking. | ||
* | ||
* @var string | ||
*/ | ||
const DISABLE_TREE_SHAKING_QUERY_VAR = 'disable_tree_shaking'; | ||
|
||
/** | ||
* Query var to reject all validation errors. | ||
* | ||
* @var string | ||
*/ | ||
const REJECT_ALL_VALIDATION_ERRORS_QUERY_VAR = 'reject_all_errors'; | ||
|
||
/** | ||
* Query var to accept 'excessive_css' validation errors. | ||
* | ||
* @var string | ||
*/ | ||
const ACCEPT_EXCESSIVE_CSS_ERROR_QUERY_VAR = 'accept_excessive_css'; | ||
|
||
/** | ||
* Query var to disable AMP. | ||
* | ||
* @var string | ||
*/ | ||
const DISABLE_AMP_QUERY_VAR = 'disable_amp'; | ||
|
||
/** | ||
* Gets whether the flag as a query var should be considered true. | ||
* | ||
* If the flag is present but '', this will be true, like &_flags[disable_amp]. | ||
* It will also be true if it is '1' or 'true', but false for most other values. | ||
* The flag should be after the top-level flag of 'amp_flags'. | ||
* For example, with the flag 'disable_amp': | ||
* https://example.com/?amp&_flags[disable_amp]=1 | ||
* | ||
* @param string $flag The name of the flag (query var). | ||
* @return bool Whether the flag as a query var should be considered true. | ||
*/ | ||
public static function has_flag( $flag ) { | ||
if ( ! isset( $_GET[ self::AMP_FLAGS_QUERY_VAR ][ $flag ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
return false; | ||
} | ||
|
||
$flag_query_var = $_GET[ self::AMP_FLAGS_QUERY_VAR ][ $flag ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
if ( | ||
array_key_exists( $flag, self::get_all_query_vars() ) | ||
&& | ||
AMP_Validation_Manager::has_cap() | ||
) { | ||
return '' === $flag_query_var ? true : filter_var( $flag_query_var, FILTER_VALIDATE_BOOLEAN ); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Gets all of the debugging query vars. | ||
* | ||
* @return array An associative array of query var name => title. | ||
*/ | ||
public static function get_all_query_vars() { | ||
return [ | ||
self::DISABLE_POST_PROCESSING_QUERY_VAR => __( 'Disable post-processing', 'amp' ), | ||
self::DISABLE_RESPONSE_CACHE_QUERY_VAR => __( 'Disable response cache', 'amp' ), | ||
self::PREVENT_REDIRECT_TO_NON_AMP_QUERY_VAR => __( 'Prevent redirect', 'amp' ), | ||
self::REJECT_ALL_VALIDATION_ERRORS_QUERY_VAR => __( 'Reject all errors', 'amp' ), | ||
self::ACCEPT_EXCESSIVE_CSS_ERROR_QUERY_VAR => __( 'Accept excessive CSS', 'amp' ), | ||
self::DISABLE_AMP_QUERY_VAR => __( 'Disable AMP', 'amp' ), | ||
self::DISABLE_TREE_SHAKING_QUERY_VAR => __( 'Disable tree shaking', 'amp' ), | ||
]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -185,7 +185,7 @@ public static function init( $args = [] ) { | |
$args | ||
); | ||
|
||
self::$should_locate_sources = $args['should_locate_sources']; | ||
self::$should_locate_sources = $args['should_locate_sources'] && ! AMP_Debug::has_flag( AMP_Debug::PREVENT_REDIRECT_TO_NON_AMP_QUERY_VAR ); | ||
|
||
AMP_Validated_URL_Post_Type::register(); | ||
AMP_Validation_Error_Taxonomy::register(); | ||
|
@@ -485,12 +485,15 @@ public static function add_admin_bar_menu_items( $wp_admin_bar ) { | |
*/ | ||
if ( $is_single_version_available ) { | ||
$wp_admin_bar->add_node( $validate_item ); | ||
self::add_debugging_option_nodes( $wp_admin_bar, $amp_url ); | ||
} elseif ( ! is_amp_endpoint() ) { | ||
$wp_admin_bar->add_node( $link_item ); | ||
$wp_admin_bar->add_node( $validate_item ); | ||
self::add_debugging_option_nodes( $wp_admin_bar, $amp_url ); | ||
} else { | ||
$wp_admin_bar->add_node( $validate_item ); | ||
$wp_admin_bar->add_node( $link_item ); | ||
self::add_debugging_option_nodes( $wp_admin_bar, $amp_url ); | ||
} | ||
|
||
// Scrub the query var from the URL. | ||
|
@@ -519,6 +522,50 @@ function() { | |
self::$amp_admin_bar_item_added = true; | ||
} | ||
|
||
/** | ||
* Adds the debugging option links to the admin bar. | ||
* | ||
* @param WP_Admin_Bar $wp_admin_bar The admin bar to add the nodes to. | ||
* @param string $amp_url The URL of the AMP endpoint. | ||
*/ | ||
public static function add_debugging_option_nodes( &$wp_admin_bar, $amp_url ) { | ||
$parent_node_id = 'amp-debug'; | ||
$wp_admin_bar->add_node( | ||
[ | ||
'parent' => 'amp', | ||
'id' => $parent_node_id, | ||
'title' => esc_html__( 'Debugging options', 'amp' ), | ||
] | ||
); | ||
|
||
foreach ( AMP_Debug::get_all_query_vars() as $query_var => $title ) { | ||
$wp_admin_bar->add_node( | ||
[ | ||
'parent' => $parent_node_id, | ||
'id' => $query_var, | ||
'title' => self::get_debugging_option_title( $title, $query_var ), | ||
'href' => add_query_arg( AMP_Debug::AMP_FLAGS_QUERY_VAR, [ $query_var => 'true' ], $amp_url ), | ||
] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the debugging option title. | ||
* | ||
* If the current URL has the debugging option query var present, the title starts with a 'check mark' emoji. | ||
* For example, if the URL has the 'Prevent redirect' query var present, | ||
* the title in the Admin Bar will be '☑ Prevent redirect'. | ||
* | ||
* @param string $title_text The title of the option, like 'Prevent redirect'. | ||
* @param string $query_var The query var of the option. | ||
* @return string The debugging option title, possible with a 'check mark' emoji at the beginning. | ||
*/ | ||
public static function get_debugging_option_title( $title_text, $query_var ) { | ||
$checkbox = AMP_Debug::has_flag( $query_var ) ? '☑' : '☐'; | ||
return sprintf( '<span class="amp-debug-option" style="font-family:Roboto,Oxygen-Sans,sans-serif;font-size:17px">%s</span> %s', $checkbox, esc_html( $title_text ) ); | ||
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. Any reason why this can't be a checkbox input? 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. |
||
} | ||
|
||
/** | ||
* Add hooks for doing determining sources for validation errors during preprocessing/sanitizing. | ||
*/ | ||
|
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.
It's debatable whether
class-amp-debug.php
should be in thevalidation/
directory. Most of the debugging flags have to do with validation, but some don't, likedisable_amp
.