[wp-trac] [WordPress Trac] #9614: Loosen typing in cat_is_ancestor_of

WordPress Trac wp-trac at lists.automattic.com
Tue Apr 21 16:56:11 GMT 2009


#9614: Loosen typing in cat_is_ancestor_of
--------------------------+-------------------------------------------------
 Reporter:  filosofo      |       Owner:  ryan                        
     Type:  defect (bug)  |      Status:  new                         
 Priority:  normal        |   Milestone:  2.8                         
Component:  Taxonomy      |     Version:  2.8                         
 Severity:  normal        |    Keywords:  has-patch cat_is_ancestor_of
--------------------------+-------------------------------------------------
 '''Problem:'''
 cat_is_ancestor_of is too strict in checking the type of its arguments; if
 something is a numeric string rather than an integer per se, it fails
 silently.

 '''Example:'''
 I get caught by this with some regularity.  Let's say you get an array of
 a post's categories like so:

 {{{$post_cats = wp_get_post_categories($the_post_id);}}}

 {{{$post_cats}}} is an array of cat ids as numeric strings (''not''
 integers), which is normally fine with most loosely-typed PHP code.

 However, iterate through that array with {{{cat_is_ancestor_of}}} to see
 if one of those categories is a child of another, and
 {{{cat_is_ancestor_of}}} will fail to identify any ancestor-descendant
 relationships, because it checks whether its arguments are integers (which
 numeric strings aren't).

 '''Solution:'''
 Since arguments for {{{cat_is_ancestor_of}}} can be either category ids or
 category objects, instead of checking that something is an integer, we
 should instead check if something is missing object properties, as my
 patch does.

 '''Really Cool Side Benefit:'''
 I was concerned about performance, so I tested the time it took to run
 this comparison over 10,000 different numbers, for both the existing code
 and my patch.  My patch produced consistent ''performance improvements of
 40%'' on average.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/9614>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list