Skip to content

Extend Sandboxing experiment to Paired AMP modes #7288

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

Merged
merged 61 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
6814d52
Add sandboxing drawer in all template modes
thelovekesh Sep 29, 2022
29c704d
Update sandboxing service to be enabled during all template modes
thelovekesh Oct 7, 2022
c201f1f
Add function to determine sandboxing level if enabled
thelovekesh Oct 7, 2022
b8fed2c
Update amp_add_amphtml_link() function to compatible with sandboxing …
thelovekesh Oct 7, 2022
f49dbc8
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Oct 26, 2022
4d920da
Add media attribute in alternate link
thelovekesh Oct 26, 2022
dae2ca6
Fix valid code in comment phpcs error
thelovekesh Oct 26, 2022
275219b
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Oct 31, 2022
ab7ca11
Update option keys to be used from Option interface
thelovekesh Oct 31, 2022
1f1cd6b
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 1, 2022
6b68b99
Move sandboxing level determining function to helper functions
thelovekesh Nov 1, 2022
dcccebc
Add test case for amp_get_sandboxing_level()
thelovekesh Nov 1, 2022
353561f
Add check to stop redirecting from AMP to non-AMP if sandboxing is no…
thelovekesh Nov 2, 2022
40688e0
Show `Exit mobile version` button when mobile redirection is disabled…
thelovekesh Nov 2, 2022
de8fd70
Update actions for enabling exit mobile version on AMP pages
thelovekesh Nov 3, 2022
2ff8bc2
Add guard to return early if mobile version switcher link's text is n…
thelovekesh Nov 3, 2022
48a0553
Get sandboxing level if AMP url is present
thelovekesh Nov 3, 2022
e70fd6d
Test when mobile version switcher text is empty
thelovekesh Nov 3, 2022
a592b27
Test amphtml link when sandboxing is not set to strict
thelovekesh Nov 3, 2022
a516138
Use callback on action hook
thelovekesh Nov 4, 2022
fcaef98
Add test cases for adding mobile version switcher link when mobile re…
thelovekesh Nov 4, 2022
97b5e41
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 4, 2022
07a319e
Add type casting to confirm that video id is returned as int
thelovekesh Nov 4, 2022
ee5fec8
Move AMPDrawer component to main setting page
thelovekesh Nov 7, 2022
2ed973b
Add scoll to element to keep it on viewport
thelovekesh Nov 7, 2022
5569b62
Update method name
thelovekesh Nov 9, 2022
65bca88
Add required hooks in private methods for reuse
thelovekesh Nov 9, 2022
d6bd3dc
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 11, 2022
a88c679
Update hooks calls with respective function calls
thelovekesh Nov 12, 2022
59f92f0
Add helper function for adding alternate link
thelovekesh Nov 12, 2022
59c7e23
Add test cases for amp_add_alternate_link()
thelovekesh Nov 12, 2022
9bfb9c1
Update alternate link string with amp_add_alternate_link helper function
thelovekesh Nov 12, 2022
37271a8
Update amp_add_amphtml_link to echo instead of return
thelovekesh Nov 12, 2022
7e610b9
Add covers tag for add coverage for mobile switcher hooks
thelovekesh Nov 12, 2022
58b1735
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 12, 2022
3827e98
Remove unused namespace alias
thelovekesh Nov 12, 2022
d0607bb
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 15, 2022
c232aca
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 16, 2022
78d3614
Refactor logic to include mobile alternate link
thelovekesh Nov 21, 2022
0a4f7f8
Update test cases for amphtml link helper function
thelovekesh Nov 21, 2022
e95acec
Update test cases for mobile alternate link addition in head
thelovekesh Nov 21, 2022
80e9278
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 21, 2022
99c1d51
Prevent removing required AMP markup if there are builtin AMP tags pr…
westonruter Nov 21, 2022
dd9217a
Add workaround for cdata_malformed_utf8_json test
westonruter Nov 21, 2022
60f4254
Update conditions before printing mobile alternate link
thelovekesh Nov 22, 2022
da412cd
Add check for AMP request to avoid adding mobile alternate link in AM…
thelovekesh Nov 22, 2022
4b97e0c
Fix variable naming
thelovekesh Nov 22, 2022
f297693
Remove vague param from mobile switcher head hooks method
thelovekesh Nov 22, 2022
e0c7e96
Add `@since` tag
thelovekesh Nov 22, 2022
f813e71
Update test cases to test mobile alternative link in canonical mode
thelovekesh Nov 22, 2022
ad0972d
Fix tests and logic for add_mobile_alternative_link()
thelovekesh Nov 23, 2022
3a5e798
Add test cases to ensure the state of amp_is_request() in add_mobile_…
thelovekesh Nov 23, 2022
5e87dbf
Setup WP query before reseting hooks
thelovekesh Nov 23, 2022
0d2c16f
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 24, 2022
0624608
Add tests cases for SandboxingTest::remove_required_amp_markup_if_not…
thelovekesh Nov 24, 2022
819ef5e
Add guard to check if AMP is available before printing mobile alterna…
thelovekesh Nov 30, 2022
22560a0
Merge branch 'develop' into add/sandboxing-in-all-modes
thelovekesh Nov 30, 2022
d789352
Remove hooks that maybe adds mobile switcher links
thelovekesh Nov 30, 2022
e9fc07e
Remove method to add all hooks for mobile switcher links
thelovekesh Nov 30, 2022
b4ee07b
Remove test cases for adding mobile switcher link in register
thelovekesh Nov 30, 2022
dbe9350
Add tests cases for mobile switcher link hooks helper methods
thelovekesh Nov 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions assets/src/settings-page/sandboxing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { CheckboxControl } from '@wordpress/components';
*/
import { Options } from '../components/options-context-provider';
import { AMPDrawer } from '../components/amp-drawer';
import { STANDARD } from '../common/constants';

/**
* Component rendering the Sandboxing experiment.
Expand All @@ -27,13 +26,8 @@ export function Sandboxing( { focusedSection } ) {
const { updateOptions, editedOptions: {
sandboxing_enabled: sandboxingEnabled,
sandboxing_level: sandboxingLevel,
theme_support: themeSupport,
} } = useContext( Options );

if ( STANDARD !== themeSupport ) {
return null;
}

return (
<AMPDrawer
heading={ (
Expand Down
28 changes: 25 additions & 3 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\QueryVar;
use AmpProject\AmpWP\Services;
use AmpProject\AmpWP\Sandboxing;

/**
* Determine whether AMP is enabled on the current site.
Expand Down Expand Up @@ -404,7 +405,7 @@ function amp_is_available() {
}

$message = sprintf(
/* translators: 1: amp_is_available() function, 2: amp_is_request() function, 3: is_amp_endpoint() function */
/* translators: 1: amp_is_available function, 2: amp_is_request function, 3: is_amp_endpoint function */
__( '%1$s (or %2$s, formerly %3$s) was called too early and so it will not work properly.', 'amp' ),
'`amp_is_available()`',
'`amp_is_request()`',
Expand Down Expand Up @@ -740,10 +741,15 @@ function amp_add_amphtml_link() {
return;
}

$amp_url = amp_add_paired_endpoint( amp_get_current_url() );
$amp_url = amp_add_paired_endpoint( amp_get_current_url() );
$sandboxing_level = amp_get_sandboxing_level();
if ( $amp_url ) {
$amp_url = remove_query_arg( QueryVar::NOAMP, $amp_url );
printf( '<link rel="amphtml" href="%s">', esc_url( $amp_url ) );
if ( 1 === $sandboxing_level || 2 === $sandboxing_level ) {
printf( '<link rel="alternate" type="text/html" media="only screen and (max-width: 640px)" href="%s">', esc_url( $amp_url ) );
} else {
printf( '<link rel="amphtml" href="%s">', esc_url( $amp_url ) );
}
}
}

Expand Down Expand Up @@ -2163,3 +2169,19 @@ function amp_remove_paired_endpoint( $url ) {
return $url;
}
}

/**
* Determine sandboxing level if enabled.
*
* @return int Following values are possible:
* 0: Sandbox is disabled.
* 1: Sandboxing level: Loose.
* 2: Sandboxing level: Moderate.
* 3: Sandboxing level: Strict.
*/
function amp_get_sandboxing_level() {
if ( ! AMP_Options_Manager::get_option( Option::SANDBOXING_ENABLED ) ) {
return 0;
}
return AMP_Options_Manager::get_option( Option::SANDBOXING_LEVEL );
}
6 changes: 5 additions & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,8 @@ public static function finalize_validation( Document $dom ) {
return true;
}

$sandboxing_level = amp_get_sandboxing_level();

/*
* In AMP-first, documents with invalid AMP markup can still be served. The amp attribute will be omitted in
* order to prevent GSC from complaining about a validation error already surfaced inside of WordPress.
Expand All @@ -2041,8 +2043,10 @@ public static function finalize_validation( Document $dom ) {
* Otherwise, if in Paired AMP then redirect to the non-AMP version if the current user isn't an user who
* can manage validation error statuses (access developer tools) and change the AMP options for the template
* mode. Such users should be able to see kept invalid markup on the AMP page even though it is invalid.
*
* Also, if sandboxing is not set to strict mode, then the page should be displayed to the user.
*/
if ( amp_is_canonical() ) {
if ( amp_is_canonical() || ( 1 === $sandboxing_level || 2 === $sandboxing_level ) ) {
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/MobileRedirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public function register() {
add_filter( 'get_comments_link', [ $this, 'add_noamp_mobile_query_var' ] ); // For get_comments_link().
add_filter( 'respond_link', [ $this, 'add_noamp_mobile_query_var' ] ); // For comments_popup_link().
}

return;
}

// Show `Exit mobile version` link even mobile redirection is disabled and in Reader mode.
if ( AMP_Theme_Support::READER_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT ) && $this->is_mobile_request() ) {
add_action( 'wp_head', [ $this, 'add_mobile_version_switcher_styles' ] );
add_action( 'wp_footer', [ $this, 'add_mobile_version_switcher_link' ] );
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/Sandboxing.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,12 @@ public function sanitize_options( $options, $new_options ) {
* Add hooks.
*/
public function add_hooks() {
// Limit to Standard mode for now. To support in Transitional/Reader we'd need to discontinue redirecting invalid
// AMP to non-AMP and omit the amphtml link (in which case it would only be relevant when mobile redirection is
// enabled).
if ( ! amp_is_canonical() ) {
return;
}
$sandboxing_level = amp_get_sandboxing_level();

if ( ! AMP_Options_Manager::get_option( Option::SANDBOXING_ENABLED ) ) {
if ( 0 === $sandboxing_level ) {
return;
}

$sandboxing_level = AMP_Options_Manager::get_option( Option::SANDBOXING_LEVEL );

// Opt-in to the new script sanitization logic in the script sanitizer.
add_filter(
'amp_content_sanitizers',
Expand Down
25 changes: 25 additions & 0 deletions tests/php/test-amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2646,4 +2646,29 @@ public static function mock_site_icon( $site_icon ) {
unset( $site_icon );
return self::MOCK_SITE_ICON;
}

/**
* Test get sandboxing level.
*
* @covers ::amp_get_sandboxing_level()
*/
public function test_amp_get_sandboxing_level() {
// If sandboxing is disabled, then the level should be 0.
$this->assertEquals( 0, amp_get_sandboxing_level() );

// Enable sandboxing.
AMP_Options_Manager::update_option( Option::SANDBOXING_ENABLED, true );

// Enable level 1 which is Loose.
AMP_Options_Manager::update_option( Option::SANDBOXING_LEVEL, 1 );
$this->assertEquals( 1, amp_get_sandboxing_level() );

// Enable level 2 which is moderate.
AMP_Options_Manager::update_option( Option::SANDBOXING_LEVEL, 2 );
$this->assertEquals( 2, amp_get_sandboxing_level() );

// Enable level 3 which is Strict.
AMP_Options_Manager::update_option( Option::SANDBOXING_LEVEL, 3 );
$this->assertEquals( 3, amp_get_sandboxing_level() );
}
}