[wp-trac] [WordPress Trac] #52877: The Widget's Callback Array Stores Wrong Values
    WordPress Trac 
    noreply at wordpress.org
       
    Sun Mar 21 12:45:09 UTC 2021
    
    
  
#52877: The Widget's Callback Array Stores Wrong Values
--------------------------+----------------------------------------
 Reporter:  oxibug        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Widgets       |    Version:  5.7
 Severity:  major         |   Keywords:  needs-patch has-unit-tests
  Focuses:                |
--------------------------+----------------------------------------
 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.
 )
 }}}
-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52877>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
    
    
More information about the wp-trac
mailing list