Skip to content

Commit e03f621

Browse files
authored
Merge pull request #6767 from ampproject/update/sandboxing-admin-menu-item
Improve Sandboxing info in AMP admin bar menu item
2 parents 7d4346c + 0bb2081 commit e03f621

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

src/Sandboxing.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,31 +250,52 @@ public function finalize_document( Document $dom, $effective_sandboxing_level )
250250

251251
$this->remove_required_amp_markup_if_not_used( $dom, $effective_sandboxing_level );
252252

253-
$amp_admin_bar_menu_item = $dom->xpath->query( '//div[ @id = "wpadminbar" ]//li[ @id = "wp-admin-bar-amp" ]/a' )->item( 0 );
253+
$amp_admin_bar_menu_item = $dom->xpath->query( '//div[ @id = "wpadminbar" ]//li[ @id = "wp-admin-bar-amp" ]' )->item( 0 );
254254
if ( $amp_admin_bar_menu_item instanceof Element ) {
255-
$span = $dom->createElement( Tag::SPAN );
256-
$span->setAttribute(
257-
Attribute::TITLE,
258-
sprintf(
259-
/* translators: %d is the effective sandboxing level */
260-
__( 'Effective sandboxing level: %d', 'amp' ),
261-
$effective_sandboxing_level
262-
)
263-
);
255+
264256
switch ( $effective_sandboxing_level ) {
265257
case 1:
266-
$text = '1️⃣';
258+
$text = '1️⃣';
259+
$title = __( 'Sandboxing level: Loose (1)', 'amp' );
267260
break;
268261
case 2:
269-
$text = '2️⃣';
262+
$text = '2️⃣';
263+
$title = __( 'Sandboxing level: Moderate (2)', 'amp' );
270264
break;
271265
default:
272-
$text = '3️⃣';
266+
$text = '3️⃣';
267+
$title = __( 'Sandboxing level: Strict (3)', 'amp' );
273268
break;
274269
}
275-
$span->textContent = $text;
276-
$amp_admin_bar_menu_item->appendChild( $dom->createTextNode( ' ' ) );
277-
$amp_admin_bar_menu_item->appendChild( $span );
270+
271+
$amp_link = $dom->xpath->query( './a', $amp_admin_bar_menu_item )->item( 0 );
272+
if ( $amp_link instanceof Element ) {
273+
$span = $dom->createElement( Tag::SPAN );
274+
$span->setAttribute( Attribute::TITLE, $title );
275+
$span->textContent = $text;
276+
277+
$amp_link->appendChild( $dom->createTextNode( ' ' ) );
278+
$amp_link->appendChild( $span );
279+
}
280+
281+
$amp_submenu_ul = $dom->xpath->query( './div/ul[ @id = "wp-admin-bar-amp-default" ]', $amp_admin_bar_menu_item )->item( 0 );
282+
if ( $amp_submenu_ul instanceof Element ) {
283+
$level_li = $dom->createElement( Tag::LI );
284+
$level_li->setAttribute( Attribute::ID, 'wp-admin-bar-amp-sandboxing-level' );
285+
286+
$link = $dom->createElement( Tag::A );
287+
$link->setAttribute( Attribute::CLASS_, 'ab-item' );
288+
$link->textContent = $title;
289+
if ( current_user_can( 'manage_options' ) ) {
290+
$link->setAttribute(
291+
Attribute::HREF,
292+
add_query_arg( 'page', AMP_Options_Manager::OPTION_NAME, admin_url( 'admin.php' ) ) . '#sandboxing'
293+
);
294+
}
295+
296+
$level_li->appendChild( $link );
297+
$amp_submenu_ul->appendChild( $level_li );
298+
}
278299
}
279300
}
280301
}

tests/php/src/SandboxingTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,25 @@ public function test_finalize_document_and_get_effective_level( $min_level, $bod
266266
<script async nomodule src="https://cdn.ampproject.org/v0.js" crossorigin="anonymous"></script>
267267
<meta name="generator" content="AMP Plugin v2.1; foo=bar">
268268
</head>
269-
<body>%s</body>
269+
<body>
270+
%s
271+
272+
<div id="wpadminbar" class="nojq">
273+
<div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="Toolbar">
274+
<ul id="wp-admin-bar-root-default" class="ab-top-menu">
275+
<li id="wp-admin-bar-amp" class="menupop">
276+
<a class="ab-item" aria-haspopup="true" href="#" title="Validate URL"><span id="amp-admin-bar-item-status-icon" class="ab-icon amp-icon amp-valid"></span> AMP</a>
277+
<div class="ab-sub-wrapper">
278+
<ul id="wp-admin-bar-amp-default" class="ab-submenu">
279+
<li id="wp-admin-bar-amp-settings"><a class="ab-item" href="#">Settings</a></li>
280+
<li id="wp-admin-bar-amp-support"><a class="ab-item" href="#">Get support</a></li>
281+
</ul>
282+
</div>
283+
</li>
284+
</ul>
285+
</div>
286+
</div>
287+
</body>
270288
</html>
271289
',
272290
$body
@@ -291,6 +309,16 @@ public function test_finalize_document_and_get_effective_level( $min_level, $bod
291309
foreach ( $expressions as $expression ) {
292310
$this->assertEquals( $expected_required_amp_markup ? 1 : 0, $dom->xpath->query( $expression )->length, $expression );
293311
}
312+
313+
$root_path = '//div[ @id = "wpadminbar" ]//li[ @id = "wp-admin-bar-amp" ]';
314+
$this->assertInstanceOf(
315+
Element::class,
316+
$dom->xpath->query( $root_path . '/a/span[ contains( @title, "Sandboxing level" ) ]' )->item( 0 )
317+
);
318+
$this->assertInstanceOf(
319+
Element::class,
320+
$dom->xpath->query( $root_path . '//li[ @id = "wp-admin-bar-amp-sandboxing-level" ]//a[ @href and contains( ., "Sandboxing level" ) ]' )->item( 0 )
321+
);
294322
}
295323

296324
/**

0 commit comments

Comments
 (0)