Make WordPress Core


Ignore:
Timestamp:
05/22/2026 06:59:15 AM (4 months ago)
Author:
westonruter
Message:

Plugins: Improve hook performance by using spl_object_id() instead of spl_object_hash() to construct unique IDs.

  • Also use spl_object_id() similarly when registering and unregistering classic widgets.
  • Improve typing and phpdoc in _wp_filter_build_unique_id(). Return null for malformed callbacks.
  • Add tests for _wp_filter_build_unique_id().
  • Improve type safety of WP_Hook::add_filter() in case an invalid callback is provided for parity with ::has_filter() and ::remove_filter().

Developed in https://github.com/WordPress/wordpress-develop/pull/11865
Follow-up to r46220, r46801, r60179.

Props bor0, westonruter, SergeyBiryukov, schlessera, arshidkv12, knutsp, spacedmonkey, swissspidy.
See #64898.
Fixes #58291.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-widget-factory.php

    r54133 r62408  
    5858        public function register( $widget ) {
    5959                if ( $widget instanceof WP_Widget ) {
    60                         $this->widgets[ spl_object_hash( $widget ) ] = $widget;
     60                        $this->widgets[ spl_object_id( $widget ) ] = $widget;
    6161                } else {
    6262                        $this->widgets[ $widget ] = new $widget();
     
    7575        public function unregister( $widget ) {
    7676                if ( $widget instanceof WP_Widget ) {
    77                         unset( $this->widgets[ spl_object_hash( $widget ) ] );
     77                        unset( $this->widgets[ spl_object_id( $widget ) ] );
    7878                } else {
    7979                        unset( $this->widgets[ $widget ] );
Note: See TracChangeset for help on using the changeset viewer.