Make WordPress Core


Ignore:
Timestamp:
08/21/2026 04:15:34 PM (3 months ago)
Author:
westonruter
Message:

Plugins: Prevent object IDs from becoming integer array keys.

To improve performance, r62408 replaced spl_object_hash() with spl_object_id() when building the array keys for WP_Hook::$callbacks and for WP_Widget_Factory::$widgets. Even when the spl_object_id() return value is cast to a string to preserve the previous string array keys, PHP automatically converts such numeric strings to int when assigning the array key. This resulted in a back-compat breakage for plugins that expected to use the array keys in string functions, including possible fatal errors when using strict_types.

To preserve the previous string key behavior, any array keys using spl_object_id() are now prefixed with a string to prevent coercion to int. This not only fixes the expected array key types when iterating over these arrays, but it also fixes the return type for WP_Widget_Factory::get_widget_key() so it actually returns a string.

In addition to the added tests, the non-decimal-int-string type in PHPStan is leveraged instead of a plain string to prevent this regression from returning.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13209.
Follow-up to r62408, r62733.

Props westonruter, dugi-digitaly, sergeybiryukov.
See #58291.
Fixes #65919.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/widgets.php

    r61466 r63334  
    4444                unregister_widget( $widget_class );
    4545                $this->assertArrayNotHasKey( $widget_class, $wp_widget_factory->widgets );
     46        }
     47
     48        /**
     49         * Tests that a widget registered as an instance is keyed by a string.
     50         *
     51         * WP_Widget_Factory::$widgets is public, and its keys were strings in every release before the
     52         * object ID was introduced. PHP casts an array key from string to int whenever the string is the
     53         * canonical decimal representation of an integer, so a bare spl_object_id() value would change
     54         * the type of the keys that consumers of that property read back.
     55         *
     56         * @see register_widget()
     57         * @ticket 65919
     58         *
     59         * @global WP_Widget_Factory $wp_widget_factory
     60         */
     61        public function test_register_widget_instance_is_keyed_by_string() {
     62                global $wp_widget_factory;
     63
     64                register_widget( new WP_Widget_Search() );
     65
     66                $this->assertCount( 1, $wp_widget_factory->widgets );
     67                $this->assertIsString( array_key_first( $wp_widget_factory->widgets ) );
     68        }
     69
     70        /**
     71         * Tests that the key returned for a widget registered as an instance is a string.
     72         *
     73         * WP_Widget_Factory::get_widget_key() is documented as returning a string, and core passes what
     74         * it returns to the_widget(), which in turn passes it to the 'the_widget' action, both of which
     75         * document the value as a string.
     76         *
     77         * @see WP_Widget_Factory::get_widget_key()
     78         * @ticket 65919
     79         *
     80         * @global WP_Widget_Factory $wp_widget_factory
     81         */
     82        public function test_get_widget_key_for_instance_returns_string() {
     83                global $wp_widget_factory;
     84
     85                $widget                              = new WP_Widget_Search();
     86                $widget->id_base                     = 'better_search';
     87                $widget->name                        = 'Better Search';
     88                $widget->option_name                 = 'widget_' . $widget->id_base;
     89                $widget->widget_options['classname'] = 'widget_' . $widget->id_base;
     90                $widget->control_options['id_base']  = $widget->id_base;
     91
     92                register_widget( $widget );
     93
     94                $this->assertIsString( $wp_widget_factory->get_widget_key( 'better_search' ) );
     95                $this->assertSame( $widget, $wp_widget_factory->get_widget_object( 'better_search' ) );
    4696        }
    4797
Note: See TracChangeset for help on using the changeset viewer.