mirror of
https://github.com/WordPress/WordPress.git
synced 2026-06-19 07:37:07 +00:00
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. Built from https://develop.svn.wordpress.org/trunk@62408 git-svn-id: http://core.svn.wordpress.org/trunk@61689 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -85,6 +85,9 @@ final class WP_Hook implements Iterator, ArrayAccess {
|
||||
}
|
||||
|
||||
$idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
|
||||
if ( null === $idx ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$priority_existed = isset( $this->callbacks[ $priority ] );
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class WP_Widget_Factory {
|
||||
*/
|
||||
public function register( $widget ) {
|
||||
if ( $widget instanceof WP_Widget ) {
|
||||
$this->widgets[ spl_object_hash( $widget ) ] = $widget;
|
||||
$this->widgets[ spl_object_id( $widget ) ] = $widget;
|
||||
} else {
|
||||
$this->widgets[ $widget ] = new $widget();
|
||||
}
|
||||
@@ -74,7 +74,7 @@ class WP_Widget_Factory {
|
||||
*/
|
||||
public function unregister( $widget ) {
|
||||
if ( $widget instanceof WP_Widget ) {
|
||||
unset( $this->widgets[ spl_object_hash( $widget ) ] );
|
||||
unset( $this->widgets[ spl_object_id( $widget ) ] );
|
||||
} else {
|
||||
unset( $this->widgets[ $widget ] );
|
||||
}
|
||||
|
||||
+17
-14
@@ -986,33 +986,36 @@ function _wp_call_all_hook( $args ) {
|
||||
* @since 2.2.3
|
||||
* @since 5.3.0 Removed workarounds for spl_object_hash().
|
||||
* `$hook_name` and `$priority` are no longer used,
|
||||
* and the function always returns a string.
|
||||
* and no longer returns false, but can still return void for invalid callbacks.
|
||||
* @since 6.9.0 Returns explicit null if an invalid callback is supplied.
|
||||
* @since 7.1.0 Uses spl_object_id() instead of spl_object_hash() for performance.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param string $hook_name Unused. The name of the filter to build ID for.
|
||||
* @param callable|string|array $callback The callback to generate ID for. The callback may
|
||||
* or may not exist.
|
||||
* @param int $priority Unused. The order in which the functions
|
||||
* associated with a particular action are executed.
|
||||
* @return string|null Unique function ID for usage as array key.
|
||||
* Null if a valid `$callback` is not passed.
|
||||
* @param string $hook_name Unused. The name of the filter to build ID for.
|
||||
* @param callable $callback The callback to generate ID for. The callback may
|
||||
* or may not exist.
|
||||
* @param int $priority Unused. The order in which the functions
|
||||
* associated with a particular action are executed.
|
||||
* @return string|null Unique function ID for usage as array key, or null if it couldn't be determined.
|
||||
*/
|
||||
function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) {
|
||||
function _wp_filter_build_unique_id( $hook_name, $callback, $priority ): ?string {
|
||||
if ( is_string( $callback ) ) {
|
||||
return $callback;
|
||||
}
|
||||
|
||||
if ( is_object( $callback ) ) {
|
||||
// Closures are currently implemented as objects.
|
||||
$callback = array( $callback, '' );
|
||||
} else {
|
||||
$callback = (array) $callback;
|
||||
return (string) spl_object_id( (object) $callback );
|
||||
}
|
||||
|
||||
$callback = (array) $callback;
|
||||
if ( ! isset( $callback[1] ) || ! is_string( $callback[1] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( is_object( $callback[0] ) ) {
|
||||
// Object class calling.
|
||||
return spl_object_hash( $callback[0] ) . $callback[1];
|
||||
return ( (string) spl_object_id( $callback[0] ) ) . $callback[1];
|
||||
} elseif ( is_string( $callback[0] ) ) {
|
||||
// Static calling.
|
||||
return $callback[0] . '::' . $callback[1];
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '7.1-alpha-62407';
|
||||
$wp_version = '7.1-alpha-62408';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
||||
Reference in New Issue
Block a user