[wp-trac] [WordPress Trac] #55133: Using an array containing a single object as first $arg in do_action()

WordPress Trac noreply at wordpress.org
Thu Feb 10 10:59:38 UTC 2022


#55133: Using an array containing a single object as first $arg in do_action()
-------------------------------------------------+-------------------------
 Reporter:  pskli                                |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Plugins                              |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch 2nd-opinion needs-dev-     |     Focuses:
  note                                           |
-------------------------------------------------+-------------------------

Comment (by pskli):

 Honestly, I'm wondering how this kind of behavior mentioned in this ticket
 has not been mentioned previously by plugin devs, as:
 {{{#!php
 function do_action_with_array_of_unique_object() {
         $var = [ get_post( 1 ) ];
         do_action( 'some_action', $var );
 }

 function debug_unique_post_variable( $array_with_one_wp_post ) {
         error_log( print_r( $array_with_one_wp_post, true ) );
 }
 add_action( 'some_action', __NAMESPACE__ . '\\debug_unique_post_variable',
 10, 1 );
 }}}

 …would result in getting a `WP_Post` instead of an array.

 I agree that it could break plugins relying on the current behavior, but I
 believe that, for plugins aware of that behavior, they're probably doing
 an `! is_array( $var ) { $var = [ $var ] };` to bypass this unexpected
 result, don't you think?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55133#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list