-
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
Force Standard mode when fetching scannable URLs in Wizard #6683
Changes from 2 commits
cd8e0b4
2540a67
22590c6
43efaed
bf243ab
4c8d969
65c27b0
426af03
95ca977
b82d40b
8560351
8537b32
184569c
56908c3
883a88b
a7b6756
defe048
87c0dd0
9a27e3b
e464460
58d4b20
655ed23
d7adcd6
8e1f722
4277226
7928432
93ffb36
be6a029
e0495cb
9989bbf
7606643
ee32eb7
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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,11 +8,14 @@ | |||||||||||||||||||
|
||||||||||||||||||||
namespace AmpProject\AmpWP\Validation; | ||||||||||||||||||||
|
||||||||||||||||||||
use AMP_Options_Manager; | ||||||||||||||||||||
use AMP_Theme_Support; | ||||||||||||||||||||
use AMP_Validated_URL_Post_Type; | ||||||||||||||||||||
use AMP_Validation_Manager; | ||||||||||||||||||||
use AmpProject\AmpWP\Infrastructure\Delayed; | ||||||||||||||||||||
use AmpProject\AmpWP\Infrastructure\Registerable; | ||||||||||||||||||||
use AmpProject\AmpWP\Infrastructure\Service; | ||||||||||||||||||||
use AmpProject\AmpWP\Option; | ||||||||||||||||||||
use AmpProject\AmpWP\PairedRouting; | ||||||||||||||||||||
use WP_Error; | ||||||||||||||||||||
use WP_Post; | ||||||||||||||||||||
|
@@ -77,7 +80,14 @@ public function register() { | |||||||||||||||||||
'methods' => WP_REST_Server::READABLE, | ||||||||||||||||||||
'callback' => [ $this, 'get_items' ], | ||||||||||||||||||||
'permission_callback' => [ $this, 'get_items_permissions_check' ], | ||||||||||||||||||||
'args' => [], | ||||||||||||||||||||
'args' => [ | ||||||||||||||||||||
'force_standard_mode' => [ | ||||||||||||||||||||
'description' => __( 'Indicates whether to force Standard template mode.', 'amp' ), | ||||||||||||||||||||
'type' => 'boolean', | ||||||||||||||||||||
'required' => false, | ||||||||||||||||||||
'default' => false, | ||||||||||||||||||||
], | ||||||||||||||||||||
], | ||||||||||||||||||||
], | ||||||||||||||||||||
'schema' => [ $this, 'get_public_item_schema' ], | ||||||||||||||||||||
] | ||||||||||||||||||||
|
@@ -112,6 +122,19 @@ public function get_items_permissions_check( $request ) { | |||||||||||||||||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||||||||||||||||||||
*/ | ||||||||||||||||||||
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||||||||||||||||||||
// Allow query parameter to force a response to be served with Standard mode (AMP-first). This is used as | ||||||||||||||||||||
// part of Site Scanning in order to determine if the primary theme is suitable for serving AMP. | ||||||||||||||||||||
if ( ! amp_is_canonical() && $request->get_param( 'force_standard_mode' ) ) { | ||||||||||||||||||||
add_filter( | ||||||||||||||||||||
'option_' . AMP_Options_Manager::OPTION_NAME, | ||||||||||||||||||||
function ( $options ) { | ||||||||||||||||||||
$options[ Option::THEME_SUPPORT ] = AMP_Theme_Support::STANDARD_MODE_SLUG; | ||||||||||||||||||||
|
||||||||||||||||||||
return $options; | ||||||||||||||||||||
} | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
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. This is very much inspired by a similar filter introduced in #6615. I was trying to write a unit test for that but I couldn't get the b/tests/php/src/Validation/ScannableURLsRestControllerTest.php
--- a/tests/php/src/Validation/ScannableURLsRestControllerTest.php (revision 2540a67282420840f4c6cf109dc960b3db589970)
+++ b/tests/php/src/Validation/ScannableURLsRestControllerTest.php (date 1635969779679)
@@ -142,6 +142,14 @@
$this->assertNull( $scannable_url_entry['stale'] );
}
}
+
+ // Test `force_standard_mode` query parameter.
+ $request_with_forced_standard_mode = new WP_REST_Request( 'GET', '/amp/v1/scannable-urls', [ 'force_standard_mode' => true ] );
+ $response_with_forced_standard_mode = rest_get_server()->dispatch( $request_with_forced_standard_mode );
+
+ $this->assertFalse( $response_with_forced_standard_mode->is_error() );
+ $scannable_urls_in_standard_mode = $response_with_forced_standard_mode->get_data();
+ $this->assertGreaterThan( 2, count( $scannable_urls_in_standard_mode ), 'Expected more than two URLs since in Standard mode.' );
}
/**
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. It's probably because the amp-wp/src/Validation/ScannableURLProvider.php Lines 79 to 87 in 46e0b76
I determined this was indeed the case, but there was also a secondary cause: when an option is not set in the DB, the As opposed to a filter, I was originally thinking that So it seems filters are the way to go, and to remove any caching in Oh, and a third reason this doesn't work is the // Test `force_standard_mode` query parameter.
$request_with_forced_standard_mode = new WP_REST_Request( 'GET', '/amp/v1/scannable-urls' );
$request_with_forced_standard_mode->set_param( 'force_standard_mode', 'true' ); 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. |
||||||||||||||||||||
return rest_ensure_response( | ||||||||||||||||||||
array_map( | ||||||||||||||||||||
function ( $item ) use ( $request ) { | ||||||||||||||||||||
|
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.
Since the scannable URLs are now fetched in a different way, there was no reason to keep the
useEffect
. ThevalidateNonce
check can be done directly in the context provider body.