Opened 5 years ago
Last modified 3 years ago
#52877 new defect (bug)
The Widget's Callback Array Stores Wrong Values
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | major | Version: | 5.7 |
| Component: | Widgets | Keywords: | needs-patch has-unit-tests |
| Focuses: | Cc: |
Description
While trying to get Widget's saved settings I stuck a very weird issue, The objects [number] and [id] in the Widget's Object [callback][0] always refer to the last widget created.
Create Two or more of the same Widget, Then print out the settings, You will notice that the [number] and [id] inside the [callback] array different than the main [id] and [number] inside the [params] array, So when use the [number] from the callback array it refers to a wrong index which is the last created widget's index not the current.
add_action( 'wp', 'test_widgets' );
function test_widgets() {
global $wp_registered_widgets;
$widgets_ids = retrieve_widgets( FALSE );
foreach( $widgets_ids as $sb_id => $widgets ) {
if( 'wp_inactive_widgets' === (string) $sb_id ) {
continue;
}
foreach( $widgets as $active_widget_id ) {
if( ! isset( $wp_registered_widgets[ $active_widget_id ] ) ) {
continue;
}
echo print_r( $wp_registered_widgets[ $active_widget_id ] );
/**
* Widget Object
* @var \WP_Widget
* */
$obj_widget = $wp_registered_widgets[ $active_widget_id ]['callback'][0];
/* If you use the callback object to get the Widget's index it'll refer to a wrong index */
/* TRY: echo $obj_widget->number; */
}
}
}
Output:
Array
(
[name] => Meta
[id] => meta-4
[callback] => Array
(
[0] => WP_Widget_Meta Object
(
[id_base] => meta
[name] => Meta
[option_name] => widget_meta
[alt_option_name] =>
[widget_options] => Array
(
[classname] => widget_meta
[customize_selective_refresh] => 1
[description] => Login, RSS, & WordPress.org links.
)
[control_options] => Array
(
[id_base] => meta
)
[number] => 4
[id] => meta-4
[updated] =>
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 4
)
)
[classname] => widget_meta
[customize_selective_refresh] => 1
[description] => Login, RSS, & WordPress.org links.
)
1Array
(
[name] => Meta
[id] => meta-3
[callback] => Array
(
[0] => WP_Widget_Meta Object
(
[id_base] => meta
[name] => Meta
[option_name] => widget_meta
[alt_option_name] =>
[widget_options] => Array
(
[classname] => widget_meta
[customize_selective_refresh] => 1
[description] => Login, RSS, & WordPress.org links.
)
[control_options] => Array
(
[id_base] => meta
)
[number] => 4
[id] => meta-4
[updated] =>
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 3
)
)
[classname] => widget_meta
[customize_selective_refresh] => 1
[description] => Login, RSS, & WordPress.org links.
)
Note: See
TracTickets for help on using
tickets.
I also see this same issue. Widget is called something like 'widget-1234' and the callback shows 'widget-137'. I suspect the callback parameter is being passed by reference inside of a loop.
Version: WordPress 6.1.1
OS: Nginx