Skip to content

Commit

Permalink
Exclude both admin-bar stylesheets if either one is excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 18, 2019
1 parent 72b5a57 commit 2f106df
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions includes/sanitizers/class-amp-style-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ private function remove_admin_bar_if_css_excluded() {

if ( ! $included ) {
$admin_bar->parentNode->replaceChild(
$this->dom->createComment( ' ' . __( 'Admin bar removed to preserve AMP validity due to excessive CSS.', 'amp' ) . ' ' ),
$this->dom->createComment( ' ' . __( 'Admin bar (#wpadminbar) was removed to preserve AMP validity due to excessive CSS.', 'amp' ) . ' ' ),
$admin_bar
);
}
Expand Down Expand Up @@ -2981,21 +2981,44 @@ function( $a, $b ) use ( $group_config ) {
}
}

// If the main admin bar was excluded, make sure the other admin bar CSS is also excluded.
$should_exclude_admin_bar_inline_css = (
isset( $indices_by_stylesheet_element_id['admin-bar-css'] )
&&
false === $this->pending_stylesheets[ $indices_by_stylesheet_element_id['admin-bar-css'] ]['included']
&&
isset( $indices_by_stylesheet_element_id['admin-bar-inline-css'] )
&&
true === $this->pending_stylesheets[ $indices_by_stylesheet_element_id['admin-bar-inline-css'] ]['included']
);
if ( $should_exclude_admin_bar_inline_css ) {
$this->pending_stylesheets[ $indices_by_stylesheet_element_id['admin-bar-inline-css'] ]['included'] = false;
$included_count--;
}
$included_count -= $this->exclude_all_admin_bar_css_if_excessive( $indices_by_stylesheet_element_id );

return $included_count;
}

/**
* If the admin-bar CSS was excluded, make sure the admin-bar inline CSS is also excluded, and vice-versa.
*
* @param int[] $indices_by_stylesheet_element_id Lookup of stylesheet indices by stylesheet element ID.
* @return int Number of excluded styles.
*/
private function exclude_all_admin_bar_css_if_excessive( $indices_by_stylesheet_element_id ) {
$excluded_count = 0;

$admin_bar_style_element_ids = array( 'admin-bar-css', 'admin-bar-inline-css' );
$should_exclude_admin_bar_inline_css = false;
foreach ( $admin_bar_style_element_ids as $admin_bar_style_element_id ) {
if ( ! isset( $indices_by_stylesheet_element_id[ $admin_bar_style_element_id ] ) ) {
continue;
}
if ( false === $this->pending_stylesheets[ $indices_by_stylesheet_element_id[ $admin_bar_style_element_id ] ]['included'] ) {
$should_exclude_admin_bar_inline_css = true;
break;
}
}
if ( $should_exclude_admin_bar_inline_css ) {
foreach ( $admin_bar_style_element_ids as $admin_bar_style_element_id ) {
$needs_exclusion = (
isset( $indices_by_stylesheet_element_id[ $admin_bar_style_element_id ] )
&&
true === $this->pending_stylesheets[ $indices_by_stylesheet_element_id[ $admin_bar_style_element_id ] ]['included']
);
if ( $needs_exclusion ) {
$this->pending_stylesheets[ $indices_by_stylesheet_element_id[ $admin_bar_style_element_id ] ]['included'] = false;
$excluded_count++;
}
}
}
return $excluded_count;
}
}

0 comments on commit 2f106df

Please sign in to comment.