[wp-trac] [WordPress Trac] #37368: get_object_taxonomies() ignores output type argument for attachments

WordPress Trac noreply at wordpress.org
Tue Aug 16 07:27:55 UTC 2016


#37368: get_object_taxonomies() ignores output type argument for attachments
-------------------------------------------------+-------------------------
 Reporter:  Rarst                                |       Owner:
     Type:  defect (bug)                         |      Status:  reopened
 Priority:  normal                               |   Milestone:  Future
Component:  Taxonomy                             |  Release
 Severity:  normal                               |     Version:  3.0
 Keywords:  needs-unit-tests good-first-bug      |  Resolution:
  has-patch needs-testing                        |     Focuses:
-------------------------------------------------+-------------------------

Comment (by deeptiboddapati):

 I looked into this and it ended up being a lot bigger than it first
 seemed.

 So I looked at 3 functions get_object_taxonomies,
 get_attachment_taxonomies, get_post_taxonomies.

 I looked at get_post_taxonomies because it supposed to work with all
 WP_Post objects and seems to repeat the get_attachment_taxonomies
 function.

 get_object_taxonomies() and get_attachment_taxonomies()call each other to
 evaluate WP_Post objects that are attachments. But
 get_attachment_taxonomies() just passes it back to get_object_taxonomies()
 after doing a lot of unneeded text parsing.

 This circular dependency should be removed because a lot of other
 functions depend on get_object_taxonomies().

 I tested the functions and I found issues with each of the functions and I
 have questions about how to approach fixing them.
 I broke the problems and my questions down here.

 == get_object_taxonomies()==
 ==== Return ====
 the names or objects of the taxonomies which are registered for the
 requested object or object type, such as a post object or post type name.

 ==== Allowed input ====
 Param 1-
 * WP_Post object
 * String for post type name
 * Array? The docs don't specify what this array should be.
 '''Question: What is the array?'''

 Param 2-
 * String either 'names' or 'objects'

 === I/O table ===
 || Input I = Invalid ?= unsure                  ||Right return for
 "Objects"|| Right return for "Names"||
 || (object) Attachment                          || NO      || YES   ||
 || (string) "attachment"                        || YES     || YES   ||
 || ? (array) Attachment                         || YES     || YES   ||
 || ? (array) attachment objects*                || NO      || NO    ||
 || (object) Post                                || YES     || YES   ||
 || (string) "post"                              || YES     || YES   ||
 || ? (array) Post                               || YES     || YES   ||
 || ?(array) WP_Post objects assorted post types || NO      || NO    ||
 || ?(array) post_types as strings*              || YES     || YES   ||
 || I (int) id of post or attachment             || YES     || YES   ||


 === Issues ===

 *Docs say that you should be able to pass in an array. They don't specify
 what kind of array.
 It works for a post array.
 It doesn't work for an array of post arrays or an array of post objects.

 It works if you pass in an array with different post type names. For
 example:
 get_object_taxonomies(['attachment','post'], 'objects');
 returns the union of taxonomy objects associated with either attachments
 or posts
 '''Question: Is this an intended behavior?'''

 For param 2 it evaluates 'objects' as anything that is not 'names' so you
 could pass anything in.
 This returns taxonomy objects:
 get_object_taxonomies(WP_Post, false)
 '''Question: Is this an intended behavior or should it specifically check
 for 'objects'?'''


 == get_attachment_taxonomies()==
 ==== Return ====
 an array of names of taxonomies associated with a given attachment
 ==== Allowed input ====
 * An id of an Attachment
 * An Attachment object
 * Data Array? The docs don't specify what this data array should be.
 '''Question: What is the data array?'''

 === I/O table ===
 ||Input I= invalid ?=unsure   ||= Is it correct?    =||
 || (object) Attachment        || Yes                 ||
 || ?(array) Attachment        || Yes                 ||
 || (int) id of Attachment     || Yes                 ||
 || I (string) 'attachment'    || Yes                 ||
 || ?(array) Attachment objects|| Yes but with Notice*||
 || ?(array) Attachment arrays || Yes but with Notice*||
 || ?(array) Attachment ids    || Yes but with Notice*||
 || I (object) Post            || No                  ||
 || I (int) Post Id            || No                  ||


 === Issues ===

 Passing an array of Attachment objects, ids or arrays returns the names of
 the taxonomies correctly but displays a notice:

 Notice: Undefined property: stdClass::$ID in C:\MAMP\htdocs\trunk\src\wp-
 includes\media.php on line 2709

 It looks like it works by accident. If it wasn't chained on to
 get_object_taxonomies it wouldn't work.

 If you pass in a Post object or id it returns all taxonomy associated with
 Attachments. It should return an empty array since this is invalid input.

 '''Question: Should this return an empty array for invalid post types or
 return an array of Taxonomies associated with attachments? Is this within
 the scope of this bug fix?
 '''

 Inside this function it does a lot of text parsing and creates an array of
 the attachment's post_type mime type, file extension etc. Then it checks
 if there are any taxonomies associated with each of them. Currently
 taxonomies are only associated with Post Types.
 '''Question:Should I remove the superflous code?
 Curiosity Question: When were taxonomies associated with file extension
 and mime types?'''

 == get_post_taxonomies() ==
 ==== Return ====
 an array of names of taxonomies associated with a given WP_Post object.

 ==== Allowed input ====
 * A WP_Post object
 * An id of a WP_Post object

 '''Question: Should this function always return an empty array if input is
 invalid?'''

 === I/O table ===
 ||  Inputs, I = invalid        || Does it work correctly? ||
 || (object) Attachment         || YES                     ||
 || (int) Attachment Id         || YES                     ||
 || I (array) Attachment        || NO*                     ||
 || I (array) Attachment objects|| NO*                     ||
 || I (array) Attachment arrays || NO*                     ||
 || I (string) "attachment"     || YES Empty Array         ||
 || (object) Post               || YES                     ||
 || (int) Post Id               || YES                     ||
 || I (array) Post              || NO*                     ||
 || I (array) Post arrays       || NO*                     ||
 || I (array) Post objects      || NO*                     ||
 || I (string) "post"           || YES Empty Array         ||


 === Issues ===
 *These don't work correctly. Its just defaulting to post id=1. So it
 returns what ever taxonomies are associated with that post.
 It should return an empty array for invalid inputs.

 '''Question: Should I patch this in this bug or is this out of the scope
 of this bug fix?'''

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


More information about the wp-trac mailing list