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: Uncaught Error: Call to a member function is_block_editor() on null #15209

Closed
auerserg opened this issue Apr 26, 2019 · 7 comments
Closed
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time [Type] Bug An existing feature does not function as intended [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.

Comments

@auerserg
Copy link

auerserg commented Apr 26, 2019

Describe the bug
In our theme we use MerlinWp Wizard and get following error during the activation

Uncaught Error: Call to a member function is_block_editor() on null in E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\plugins\gutenberg\lib\widgets.php:13
Stack trace:
#0 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(286): gutenberg_block_editor_admin_print_styles('')
#1 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\plugin.php(465): WP_Hook->do_action(Array)
#3 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\themes\5th-avenue\inc\integrations\merlin\merlin.php(462): do_action('admin_print_sty...')
#4 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\themes\5th-avenue\inc\integrations\merlin\merlin.php(410): Merlin->header()
#5 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(284): Merlin in E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\plugins\gutenberg\lib\widgets.php on line 13
function gutenberg_block_editor_admin_print_styles() {
	if ( get_current_screen()->is_block_editor() ) {
		/** This action is documented in wp-admin/admin-footer.php */
		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
		do_action( 'admin_print_styles-widgets.php' );
	}
}
add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );

To reproduce
Steps to reproduce the behavior:

  1. Install theme with MerlinWp Wizard
  2. Skip the Wizard installation
  3. Switch from Parent Theme to Child Theme to trigger Merlin Wizard
  4. See error

Expected behavior
The reason for this is that the function get_current_screen() returns null. As specified in the documentation for this function

/**
 * Get the current screen object
 *
 * @since 3.1.0
 *
 * @global WP_Screen $current_screen
 *
 * @return WP_Screen|null Current screen object or null when screen not defined.
 */

You need to add a check if the function get_current_screen() returns the WP_Screen class and then call the method is_block_editor()

Additional context

  • Gutenberg 5.5.0
@auerserg
Copy link
Author

Solution
Need to change

function gutenberg_block_editor_admin_print_styles() {
	$current_screen = get_current_screen()
	if ( ! is_null ( $current_screen ) && $current_screen->is_block_editor() ) {
		/** This action is documented in wp-admin/admin-footer.php */
		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
		do_action( 'admin_print_styles-widgets.php' );
	}
}
add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );

@aduth
Copy link
Member

aduth commented Apr 26, 2019

There were similar issues previously as in #14547 (comment) where get_current_screen will either (a) not be defined as a function or (b) return null when called, depending on whether the current screen is an admin screen. In this specific instance, it may have been expected that since the logic of this function occurs in the action handler of 'admin_print_styles' that certain assumptions about it being an admin screen could be made.

That being said, it seems with the recurring presence of these sorts of errors that we should always be considerate of accounting for the possible failure conditions. This should also apply to all other action handlers of the same file where similar logic occurs.

@aduth aduth added [Type] Bug An existing feature does not function as intended [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. Good First Issue An issue that's suitable for someone looking to contribute for the first time labels Apr 26, 2019
@jackbillstrom
Copy link

Sometimes conflicts occur, I tried installing the Classic Editor, which helped.

But it might just be in my case.

@SecondLineThemesHQ
Copy link

@auerserg - This might be a workaround, but you can use set_current_screen() within the class-merlin.php file:

	if( is_null ( get_current_screen() )) {
		set_current_screen('Merlin');
	}

Right after this block -

	if ( empty( $_GET['page'] ) || $this->merlin_url !== $_GET['page'] ) {
		return;
	}

@strarsis
Copy link
Contributor

strarsis commented Jun 7, 2019

Hi, same issue when opening the setup assistant of WP Security Audit Log plugin.
Related: https://wordpress.org/support/topic/call-to-a-member-function-is_block_editor-on-null/

@aduth
Copy link
Member

aduth commented Jun 7, 2019

This is slated to be fixed by #15983.

@jorgefilipecosta
Copy link
Member

Fixed in #15983.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time [Type] Bug An existing feature does not function as intended [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.
Projects
None yet
Development

No branches or pull requests

6 participants