[wp-trac] [WordPress Trac] #37721: improve error handling of is_object_in_term in taxonomy.php

WordPress Trac noreply at wordpress.org
Wed Aug 24 04:03:44 UTC 2016


#37721: improve error handling of is_object_in_term in taxonomy.php
--------------------------+---------------------------
 Reporter:  rpayne7264    |       Owner:  boonebgorges
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:  4.6.1
Component:  Taxonomy      |     Version:  4.6
 Severity:  normal        |  Resolution:
 Keywords:  commit        |     Focuses:
--------------------------+---------------------------

Comment (by rpayne7264):

 As an additional precaution, you may want to add a check at the entrance
 of wp_list_pluck() to ensure that the $list param is, indeed, and array.

 This is how I would code it:


 {{{
 function wp_list_pluck( $list, $field, $index_key = null ) {
         $newlist = array();
         if(! is_array( $list ))return $newlist;
         if ( ! $index_key ) {
                 /*
                  * This is simple. Could at some point wrap array_column()
                  * if we knew we had an array of arrays.
                  */
                 foreach ( $list as $key => $value ) {
                         if ( is_object( $value ) ) {
                                 $list[ $key ] = $value->$field;
                         } else {
                                 $list[ $key ] = $value[ $field ];
                         }
                 }
                 return $list;
         }

         /*
          * When index_key is not set for a particular item, push the value
          * to the end of the stack. This is how array_column() behaves.
          */

         foreach ( $list as $value ) {
                 if ( is_object( $value ) ) {
                         if ( isset( $value->$index_key ) ) {
                                 $newlist[ $value->$index_key ] =
 $value->$field;
                         } else {
                                 $newlist[] = $value->$field;
                         }
                 } else {
                         if ( isset( $value[ $index_key ] ) ) {
                                 $newlist[ $value[ $index_key ] ] = $value[
 $field ];
                         } else {
                                 $newlist[] = $value[ $field ];
                         }
                 }
         }

         return $newlist;
 }
 }}}

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


More information about the wp-trac mailing list