• Resolved Gwyneth Llewelyn

    (@gwynethllewelyn)


    Hi all,

    I’ve done a quick search here on these forums, and, although a lot of Uncaught TypeErrors have been reported, this seems to be a ‘new’ one — at least, as far as I could search. It’s also quite easy to fix.

    I’m on Ubuntu 20.04.2 LTS, running nginx/1.19.6 with PHP 8.0.2 (provided via php-fpm), WordPress 5.6.1 and Jetpack 9.4. My configuration is rather a complex one (many things can go wrong), but this time I can trace it down to Jetpack. The following error is thrown when saving a (Jetpack) widget (after adding it):

    
    2021/02/07 15:50:04 [error] 3799479#3799479: *372 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given in <WP_PATH>/wp-content/plugins/jetpack/modules/widget-visibility/widget-conditions.php:27
    

    (path redacted)

    The culprit, around line 27, is the following bit of code:

    
                    // TODO: Replace ?$current_screen->is_block_editor()? with ?wp_should_load_block_editor_scripts_and_styles()? that is introduced in WP 5.6
                    if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
    

    Under PHP 7.X, if $current_screen was null, this would throw a warning or a notice, but PHP 8.X, as we all know, is so much stricter, and since you cannot apply method_exists() to a null object — it throws a fatal error. The consequence is that the widget never gets saved and/or added to a sidebar.

    A trivial, quick & dirty fix is simply to check if $current_screen is empty, e.g.:

    
    if ( ! empty( $current_screen ) && method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
    

    That should make PHP 8.0 happy.

    Note that the comment before that line implies that this particular bit of code will be replaced by a different function; as such, I’d imagine that future versions of Jetpack will change the way this works.

    In the meantime — unless Automattic releases a quick & dirty patch first! — now you know what to do to keep WordPress happy ??

    P.S. There are other, similar annoyances when using PHP 8.0, but fortunately, most just fill the logs with warnings, not fatal errors.

    • This topic was modified 3 years, 9 months ago by Gwyneth Llewelyn. Reason: Fixed backticks-inside-backticks issue, which made the comment unreadable
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Gwyneth Llewelyn

    (@gwynethllewelyn)

    Slight update: I’ve tried it on a different site, and it’s not only Jetpack widgets that throw this error — ‘core’ WP widgets will also throw it.

    Plugin Contributor Ryan C.

    (@ryancowles)

    Hi @gwynethllewelyn! Thanks for the detailed explanation here. In your reply, you mention that this happens with Core WordPress widgets as well. Just to ensure that we’re following along from our side, can you provide us with the exact steps that result in the problem?

    I want to see if we can reproduce this issue on a vanilla WordPress installation, so we can ensure that we get it reported in the right place if need be ??

    Thank you!

    Facing the same issue here. Happens with all the widgets (core and third-party.)

    Plugin Contributor Stef (a11n)

    (@erania-pinnera)

    Hey @muhammedswalihct,

    As Ryan asked already above, can you tell us the exact steps you took that resulted in these errors?

    We’d like to replicate this at our end, so the more information you can share (also links, screenshots etc), the better!

    1. Go to Appearance > Widgets.
    2. Choose any widget in any widget area.
    3. Click on the Visibility button.
    4. Try to change almost ANY visibility settings into anything.
    5. Click the Save button.
    6. It will return the above-mentioned error in the error log.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Thank you both for the report! We’ll get this fixed in a future version of Jetpack. You can follow our progress here:
    https://github.com/Automattic/jetpack/pull/18775

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Uncaught TypeError: method_exists() […] in widget-conditions.php’ is closed to new replies.