WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#9614 closed defect (bug) (fixed)

Loosen typing in cat_is_ancestor_of

Reported by: filosofo Owned by: ryan
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.8
Component: Taxonomy Keywords: has-patch cat_is_ancestor_of
Focuses: Cc:

Description

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.

Attachments (1)

better_cat_ancestor.9614.diff (676 bytes) - added by filosofo 6 years ago.

Download all attachments as: .zip

Change History (2)

comment:1 @ryan6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [11031]) Fix typing in cat_is_ancestor_of(). Props filosofo. fixes #9614

Note: See TracTickets for help on using tickets.