Skip to content

Commit 9ddac9e

Browse files
committed
Preserve admin bar for inner shell requests
1 parent 399c90a commit 9ddac9e

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

includes/class-amp-theme-support.php

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,54 @@ public static function get_requested_app_shell_component() {
15271527
return null;
15281528
}
15291529

1530+
/**
1531+
* Prepare outer app shell.
1532+
*
1533+
* @param DOMElement $content_element Content element.
1534+
*/
1535+
protected function prepare_outer_app_shell_document( DOMElement $content_element ) {
1536+
while ( $content_element->firstChild ) {
1537+
$content_element->removeChild( $content_element->firstChild );
1538+
}
1539+
}
1540+
1541+
/**
1542+
* Prepare inner app shell.
1543+
*
1544+
* @param DOMElement $content_element Content element.
1545+
*/
1546+
protected function prepare_inner_app_shell_document( DOMElement $content_element ) {
1547+
$dom = $content_element->ownerDocument;
1548+
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
1549+
1550+
$admin_bar = $dom->getElementById( 'wpadminbar' );
1551+
if ( $admin_bar ) {
1552+
$admin_bar->parentNode->removeChild( $admin_bar );
1553+
}
1554+
1555+
// @todo This should not be removing style elements.
1556+
$remove_siblings = function( DOMElement $node ) {
1557+
while ( $node->previousSibling ) {
1558+
$node->parentNode->removeChild( $node->previousSibling );
1559+
}
1560+
while ( $node->nextSibling ) {
1561+
$node->parentNode->removeChild( $node->nextSibling );
1562+
}
1563+
};
1564+
1565+
$node = $content_element;
1566+
do {
1567+
$remove_siblings( $node );
1568+
$node = $node->parentNode;
1569+
} while ( $node && $node !== $body );
1570+
1571+
// Restore admin bar element.
1572+
if ( $body && $admin_bar ) {
1573+
$body->appendChild( $admin_bar );
1574+
}
1575+
}
1576+
1577+
15301578
/**
15311579
* Process response to ensure AMP validity.
15321580
*
@@ -1740,26 +1788,9 @@ public static function prepare_response( $response, $args = array() ) {
17401788
return esc_html__( 'Unable to locate content_element_id.', 'amp' );
17411789
}
17421790
if ( 'outer' === $app_shell_component ) {
1743-
while ( $content_element->firstChild ) {
1744-
$content_element->removeChild( $content_element->firstChild );
1745-
}
1791+
self::prepare_outer_app_shell_document( $content_element );
17461792
} elseif ( 'inner' === $app_shell_component ) {
1747-
// @todo This should not be removing wpadminbar.
1748-
// @todo This should not be removing style elements.
1749-
$remove_siblings = function( DOMElement $node ) {
1750-
while ( $node->previousSibling ) {
1751-
$node->parentNode->removeChild( $node->previousSibling );
1752-
}
1753-
while ( $node->nextSibling ) {
1754-
$node->parentNode->removeChild( $node->nextSibling );
1755-
}
1756-
};
1757-
1758-
$node = $content_element;
1759-
do {
1760-
$remove_siblings( $node );
1761-
$node = $node->parentNode;
1762-
} while ( $node && 'body' !== $node->nodeName );
1793+
self::prepare_inner_app_shell_document( $content_element );
17631794
}
17641795
}
17651796

0 commit comments

Comments
 (0)