Uncaught TypeError: method_exists() […] in widget-conditions.php
-
Hi all,
I’ve done a quick search here on these forums, and, although a lot of
Uncaught TypeError
s 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
wasnull
, this would throw a warning or a notice, but PHP 8.X, as we all know, is so much stricter, and since you cannot applymethod_exists()
to anull
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.
- The topic ‘Uncaught TypeError: method_exists() […] in widget-conditions.php’ is closed to new replies.