Skip to content

Commit

Permalink
Merge pull request #6615 from ampproject/add/validate-store-url-query…
Browse files Browse the repository at this point in the history
…-var

Add AMP-first preview and args to validate request for Site Scanning
  • Loading branch information
westonruter authored Oct 26, 2021
2 parents ef0548b + c226596 commit 746d7ac
Show file tree
Hide file tree
Showing 9 changed files with 950 additions and 47 deletions.
22 changes: 8 additions & 14 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ public static function prepare_response( $response, $args = [] ) {
}
$did_redirect = $status_code >= 300 && $status_code < 400 && $sent_location_header;

if ( AMP_Validation_Manager::$is_validate_request && ! $did_redirect ) {
if ( AMP_Validation_Manager::is_validate_request() && ! $did_redirect ) {
if ( ! headers_sent() ) {
status_header( 400 );
header( 'Content-Type: application/json; charset=utf-8' );
Expand Down Expand Up @@ -1973,7 +1973,7 @@ public static function prepare_response( $response, $args = [] ) {

$dom = Document::fromHtml( $response, Options::DEFAULTS );

if ( AMP_Validation_Manager::$is_validate_request ) {
if ( AMP_Validation_Manager::is_validate_request() ) {
AMP_Validation_Manager::remove_illegal_source_stack_comments( $dom );
}

Expand Down Expand Up @@ -2029,18 +2029,12 @@ public static function prepare_response( $response, $args = [] ) {
do_action( 'amp_server_timing_stop', 'amp_sanitizer' );

// Respond early with results if performing a validate request.
if ( AMP_Validation_Manager::$is_validate_request ) {
status_header( 200 );
header( 'Content-Type: application/json; charset=utf-8' );
$data = [
'http_status_code' => $status_code,
'php_fatal_error' => false,
];
if ( $last_error && in_array( $last_error['type'], [ E_ERROR, E_RECOVERABLE_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_PARSE ], true ) ) {
$data['php_fatal_error'] = $last_error;
}
$data = array_merge( $data, AMP_Validation_Manager::get_validate_response_data( $sanitization_results ) );
return wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
if ( AMP_Validation_Manager::is_validate_request() ) {
return AMP_Validation_Manager::send_validate_response(
$sanitization_results,
$status_code,
$last_error
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validated-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public static function normalize_url_for_storage( $url ) {
// Query args to be removed from validated URLs.
$removable_query_vars = array_merge(
wp_removable_query_args(),
[ 'preview_id', 'preview_nonce', 'preview', QueryVar::NOAMP ]
[ 'preview_id', 'preview_nonce', 'preview', QueryVar::NOAMP, AMP_Validation_Manager::VALIDATE_QUERY_VAR ]
);

// Normalize query args, removing all that are not recognized or which are removable.
Expand Down
Loading

0 comments on commit 746d7ac

Please sign in to comment.