Skip to content
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

Fatal error due to admin bar disabled #6189

Closed
amarinediary opened this issue May 10, 2021 · 3 comments
Closed

Fatal error due to admin bar disabled #6189

amarinediary opened this issue May 10, 2021 · 3 comments
Labels

Comments

@amarinediary
Copy link

amarinediary commented May 10, 2021

Bug Description

Hiding the Wordpress admin bar via the show_admin_bar hook filter will result in a fatal error. on the AMP Validated URL screen on Recheck user action.

Fatal error: Uncaught DivisionByZeroError:
Division by zero in C:\xampp\htdocs\www\wordpress\wp-content\plugins\amp\includes\validation\class-amp-validated-url-post-type.php:2542 Stack trace: #0 C:\xampp\htdocs\www\wordpress\wp-admin\includes\template.php(1389):
AMP_Validated_URL_Post_Type::print_stylesheets_meta_box(Object(WP_Post), Array)

  1. C:\xampp\htdocs\www\wordpress\wp-admin\edit-form-advanced.php(688): do_meta_boxes(Object(WP_Screen), 'normal', Object(WP_Post))
  2. C:\xampp\htdocs\www\wordpress\wp-admin\post.php(206): require('C:\xampp\htdocs...')
  3. {main} thrown in C:\xampp\htdocs\www\wordpress\wp-content\plugins\amp\includes\validation\class-amp-validated-url-post-type.php on line 2542
    There has been a critical error on this website. Please check your site admin email inbox for instructions. Learn more about troubleshooting WordPress.

Expected Behaviour

Using recheck shouldn't return a fatal error when the admin bar isn't displayed;

Steps to reproduce

  1. Go to function.php
  2. Add the following function:
<?php

add_filter( 'show_admin_bar', function() {
    if ( ! is_admin() )
        return false;
} );

3 .Go to the AMP Validated URL page and click Recheck
4. See error

Screenshots

muyjdfxreg

Additional context

  • WordPress version: 5.7.1
  • Plugin version: 2.1.1
  • Gutenberg plugin version (if applicable): Not applicable
  • AMP plugin template mode: Standard
  • PHP version: 8.0
  • OS: Windows
  • Browser: Chrome
  • Device: PC

Additional information

All plugins disabled. Fresh WordPress install and blank theme. Site health passed.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

@amarinediary amarinediary added the Bug Something isn't working label May 10, 2021
@westonruter
Copy link
Member

I'm not able to reproduce the error due to the the admin bar being hidden. Nevertheless, your PHP code to hide the admin bar should be modified as follows:

add_filter( 'show_admin_bar', function( $show ) {
	if ( ! is_admin() ) {
		$show = false;
	}
	return $show;
} );

The error seems unrelated to the admin bar being shown. At issue is this code here:

$percentage = $stylesheet['final_size'] / ( $included_final_size + $excluded_final_size );

Apparently for you one stylesheet has a $included_final_size and $excluded_final_size which both add up to zero, which is surprising. As a quick fix to assist with debugging and to prevent the fatal error you can try this patch:

diff --git a/includes/validation/class-amp-validated-url-post-type.php b/includes/validation/class-amp-validated-url-post-type.php
index 2e0ad95e2..f51332c6c 100644
--- a/includes/validation/class-amp-validated-url-post-type.php
+++ b/includes/validation/class-amp-validated-url-post-type.php
@@ -2539,11 +2539,16 @@ static function ( $a, $b ) use ( $stylesheets ) {
 					</td>
 					<td class="column-percentage">
 						<?php
-						$percentage = $stylesheet['final_size'] / ( $included_final_size + $excluded_final_size );
+						$denominator = ( $included_final_size + $excluded_final_size );
 						?>
-						<meter value="<?php echo esc_attr( $stylesheet['final_size'] ); ?>" min="0" max="<?php echo esc_attr( $included_final_size + $excluded_final_size ); ?>" title="<?php esc_attr_e( 'Stylesheet bytes of total CSS added to page', 'amp' ); ?>">
-							<?php echo esc_html( round( ( $percentage ) * 100 ) ) . '%'; ?>
-						</meter>
+						<?php if ( $denominator > 0 ) : ?>
+							<?php $percentage = $stylesheet['final_size'] / $denominator; ?>
+							<meter value="<?php echo esc_attr( $stylesheet['final_size'] ); ?>" min="0" max="<?php echo esc_attr( $included_final_size + $excluded_final_size ); ?>" title="<?php esc_attr_e( 'Stylesheet bytes of total CSS added to page', 'amp' ); ?>">
+								<?php echo esc_html( round( ( $percentage ) * 100 ) ) . '%'; ?>
+							</meter>
+						<?php else : ?>
+							included/excluded final sizes are zero
+						<?php endif; ?>
 					</td>
 					<td class="column-priority">
 						<?php echo esc_html( $stylesheet['priority'] ); ?>

@westonruter
Copy link
Member

@amarinediary Did you find the cause of the problem?

@amarinediary
Copy link
Author

Yes sorry, indeed, apparently not properly hiding the admin bar was trouble shooting with the amp plugin. You did answer that question perfectly, and I feel ashamed for opening it !

@westonruter westonruter added Invalid Support Help with setup, implementation, or "How do I?" questions. and removed Bug Something isn't working Support Help with setup, implementation, or "How do I?" questions. labels May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants