-
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
Changes from 1 commit
37de1c3
e738bcf
19363fc
aeeda26
388de8e
17747a4
2c0b1d0
e41505d
ac1311e
fffbbbd
fe602ac
405d30d
39b0e2f
e4be908
81be395
114e5d8
2bd69c3
5f186af
8b0c96f
7b41a0b
c5b0d3b
00358e4
7a3733a
687cf47
ef0720c
675f875
996157a
e3bad9e
4b6a48f
d5e2b2d
1d40623
42a29d0
a1929ab
03a2395
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 |
---|---|---|
|
@@ -95,6 +95,27 @@ class AMP_Theme_Support { | |
*/ | ||
const READER_MODE_TEMPLATE_DIRECTORY = 'amp'; | ||
|
||
/** | ||
* 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'; | ||
|
||
/** | ||
* Sanitizer classes. | ||
* | ||
|
@@ -1872,6 +1893,10 @@ public static function start_output_buffering() { | |
newrelic_disable_autorum(); | ||
} | ||
|
||
if ( isset( $_GET[ self::DISABLE_POST_PROCESSING_QUERY_VAR ] ) && AMP_Validation_Manager::has_cap() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
return; | ||
} | ||
|
||
ob_start( [ __CLASS__, 'finish_output_buffering' ] ); | ||
self::$is_output_buffering = true; | ||
} | ||
|
@@ -2000,13 +2025,17 @@ public static function prepare_response( $response, $args = [] ) { | |
* | ||
* @param bool $enable_response_caching Whether response caching is enabled. | ||
*/ | ||
$enable_response_caching = apply_filters( 'amp_response_caching_enabled', ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ! empty( $args['enable_response_caching'] ) ); | ||
$enable_response_caching = apply_filters( 'amp_response_caching_enabled', ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ! empty( $args['enable_response_caching'] ) ); | ||
$is_disabled_with_query_var = isset( $_GET[ self::DISABLE_RESPONSE_CACHE_QUERY_VAR ] ) && AMP_Validation_Manager::has_cap(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
|
||
$enable_response_caching = ( | ||
$enable_response_caching | ||
&& | ||
! AMP_Validation_Manager::should_validate_response() | ||
&& | ||
! is_customize_preview() | ||
&& | ||
! $is_disabled_with_query_var | ||
); | ||
|
||
// When response caching is enabled, determine if it should be turned off for cache misses. | ||
|
@@ -2121,7 +2150,15 @@ public static function prepare_response( $response, $args = [] ) { | |
AMP_HTTP::send_server_timing( 'amp_processor_cache_hit', -$prepare_response_start ); | ||
|
||
// Redirect to non-AMP version. | ||
if ( ! amp_is_canonical() && ! is_singular( AMP_Story_Post_Type::POST_TYPE_SLUG ) && $blocking_error_count > 0 ) { | ||
if ( | ||
! amp_is_canonical() | ||
&& | ||
! is_singular( AMP_Story_Post_Type::POST_TYPE_SLUG ) | ||
&& | ||
$blocking_error_count > 0 | ||
&& | ||
! self::prevent_redirect_to_non_amp() | ||
) { | ||
if ( AMP_Validation_Manager::has_cap() ) { | ||
$non_amp_url = add_query_arg( AMP_Validation_Manager::VALIDATION_ERRORS_QUERY_VAR, $blocking_error_count, $non_amp_url ); | ||
} | ||
|
@@ -2266,7 +2303,7 @@ public static function prepare_response( $response, $args = [] ) { | |
$script->appendChild( $dom->createTextNode( 'document.addEventListener( "DOMContentLoaded", function() { document.write = function( text ) { throw new Error( "[AMP-WP] Prevented document.write() call with: " + text ); }; } );' ) ); | ||
$head->appendChild( $script ); | ||
} | ||
} elseif ( ! self::is_customize_preview_iframe() ) { | ||
} elseif ( ! self::is_customize_preview_iframe() && ! self::prevent_redirect_to_non_amp() ) { | ||
$response = esc_html__( 'Redirecting to non-AMP version.', 'amp' ); | ||
|
||
if ( $cache_response ) { | ||
|
@@ -2278,6 +2315,15 @@ public static function prepare_response( $response, $args = [] ) { | |
$non_amp_url = add_query_arg( AMP_Validation_Manager::VALIDATION_ERRORS_QUERY_VAR, $blocking_error_count, $non_amp_url ); | ||
} | ||
|
||
// Remove debugging query args. | ||
$non_amp_url = remove_query_arg( | ||
[ | ||
self::DISABLE_RESPONSE_CACHE_QUERY_VAR, | ||
AMP_Validation_Error_Taxonomy::REJECT_ALL_VALIDATION_ERRORS_QUERY_VAR, | ||
], | ||
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. Per above, this could just remove 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. Sure, I'll apply this also. 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. Good idea, it's applied here. |
||
$non_amp_url | ||
); | ||
|
||
/* | ||
* Temporary redirect because AMP page may return with blocking validation errors when auto-accepting sanitization | ||
* is not enabled. A 302 will allow the errors to be fixed without needing to bust any redirect caches. | ||
|
@@ -2313,6 +2359,19 @@ public static function prepare_response( $response, $args = [] ) { | |
return $response; | ||
} | ||
|
||
/** | ||
* Whether to prevent a redirect to a non-AMP URL. | ||
* | ||
* Usually, if there are unaccepted validation errors, | ||
* the AMP URL will redirect to a non-AMP URL. | ||
* This overrides that behavior, for debugging purposes. | ||
* | ||
* @return bool Whether the prevent a redirect. | ||
*/ | ||
public static function prevent_redirect_to_non_amp() { | ||
return isset( $_GET[ self::PREVENT_REDIRECT_TO_NON_AMP_QUERY_VAR ] ) && AMP_Validation_Manager::has_cap(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
} | ||
|
||
/** | ||
* Check for cache misses. When found, store in an option to retain the URL. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,13 @@ class AMP_Validation_Error_Taxonomy { | |
*/ | ||
const VALIDATION_ERRORS_CLEARED_QUERY_VAR = 'amp_validation_errors_cleared'; | ||
|
||
/** | ||
* Query var to reject all validation errors. | ||
* | ||
* @var string | ||
*/ | ||
const REJECT_ALL_VALIDATION_ERRORS_QUERY_VAR = 'reject_all_errors'; | ||
|
||
/** | ||
* The <option> value to not filter at all, like for 'All Statuses'. | ||
* | ||
|
@@ -465,6 +472,11 @@ public static function get_validation_error_sanitization( $error ) { | |
$forced = 'with_preview'; | ||
} | ||
|
||
if ( isset( $_GET[ self::REJECT_ALL_VALIDATION_ERRORS_QUERY_VAR ] ) && AMP_Validation_Manager::has_cap() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
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. As opposed to adding a new condition here, I suggest using the 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. Good idea, how does this look? |
||
$status = self::VALIDATION_ERROR_ACK_REJECTED_STATUS; | ||
$forced = 'with_query_var'; | ||
} | ||
|
||
/** | ||
* Filters whether the validation error should be sanitized. | ||
* | ||
|
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.
Instead of introducing these as top-level query parameters, how about putting them all under some
amp_flags
query var? This would make it easier to scrub them from the URL and it would reduce opportunities for collision. So So here it could be checkingisset( $_GET['amp_flags'][ self::DISABLE_POST_PROCESSING_QUERY_VAR ] )
.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.
That's a really good idea.
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.
19363fc adds an amp_flags query var, to access these query vars like you mentioned.
For example:
https://loc.test/your-post/?amp&_flags[reject_all_errors]