WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 2 months ago

#49089 new defect (bug)

hook_suffix is undefined in WP_Screen class

Reported by: splendorstudio Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.1
Component: Administration Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Hi There,

There is an bug in WP_Screen class in 213 line which we are getting via $id = $GLOBALS['hook_suffix']; hook_suffix global, You can see this issue via some functions but,
I encountered with this issue via convert_to_screen() in ajax situation. Please let me know if there is any question.

Change History (4)

#1 @SergeyBiryukov
21 months ago

  • Component changed from General to Administration
  • Description modified (diff)

Related: #22039, #29933.

This ticket was mentioned in PR #1490 on WordPress/wordpress-develop by htdat.


2 months ago

  • Keywords has-patch added

#3 @htdat
2 months ago

When working on plugin Edit Flow, we came across this error https://github.com/Automattic/Edit-Flow/issues/654

Here is what we've got so far:

To replicate this issue with core

Error with track trace:

[12-Jul-2021 04:28:47 UTC] PHP Notice: Undefined index: hook_suffix in /var/www/html/wp-admin/includes/class-wp-screen.php on line 223
[12-Jul-2021 04:28:47 UTC] PHP Stack trace:
[12-Jul-2021 04:28:47 UTC] PHP 1. {main}() /var/www/html/wp-admin/admin-ajax.php:0
[12-Jul-2021 04:28:47 UTC] PHP 2. do_action() /var/www/html/wp-admin/admin-ajax.php:187
[12-Jul-2021 04:28:47 UTC] PHP 3. WP_Hook->do_action() /var/www/html/wp-includes/plugin.php:484
[12-Jul-2021 04:28:47 UTC] PHP 4. WP_Hook->apply_filters() /var/www/html/wp-includes/class-wp-hook.php:316
[12-Jul-2021 04:28:47 UTC] PHP 5. {closure:/var/www/html/wp-content/mu-plugins/trac-49089.php:11-24}() /var/www/html/wp-includes/class-wp-hook.php:292
[12-Jul-2021 04:28:47 UTC] PHP 6. WP_List_Table->construct() /var/www/html/wp-content/mu-plugins/trac-49089.php:14
[12-Jul-2021 04:28:47 UTC] PHP 7. convert_to_screen() /var/www/html/wp-admin/includes/class-wp-list-table.php:149
[12-Jul-2021 04:28:47 UTC] PHP 8. WP_Screen::get() /var/www/html/wp-admin/includes/template.php:2571

Root cause

The error happens around this line http://github.com/WordPress/WordPress/blob/9f91305af2bd18c914096cc5e5cc1d6882163200/wp-admin/includes/class-wp-screen.php#L223-L223

                        $id = $GLOBALS['hook_suffix'];

admin-ajax.php requests do not load file wp-admin/admin.php, which is loaded when admins browse wp-admin pages.
Then global $hook_suffix is not added and defined, hence, the PHP error Undefined index: hook_suffix happens.

A side note here: I searched hook_suffix and $hook_suffix across WordPress core and found out that this global $hook_suffix var is set up in file wp-admin/admin.php:

Solution

I think the ultimate solution here is to change the way we handle $GLOBALS['hook_suffix'] and property id of class WP_List_Table.

That's why I proposed assigning a value for property id with the wp_ajax_ prefix for this case in PR: https://github.com/WordPress/wordpress-develop/pull/1490

#4 @htdat
2 months ago

Another note here (thanks to @mikeyarce): This notice Undefined index will be converted into a warning in PHP8 and that might cause a bigger headache:

Ref: https://www.php.net/manual/en/migration80.incompatible.php:

A number of notices have been converted into warnings:
...
Attempting to read an undefined array key.

Note: See TracTickets for help on using tickets.