[wp-trac] [WordPress Trac] #36478: Add a `WP_Post_Type` class to represent post types

WordPress Trac noreply at wordpress.org
Mon Apr 11 14:35:00 UTC 2016


#36478: Add a `WP_Post_Type` class to represent post types
-------------------------------+-----------------------------
 Reporter:  flixos90           |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 We now have classes for users, posts, terms, comments, networks, sites and
 I think it would be a good next step to get rid of the basic post type
 objects and use instances of a specific class for these as well. Therefore
 I'm proposing to add a new class `WP_Post_Type` which is instantiated from
 the `register_post_type()` function. The global variable `$wp_post_types`
 would then contain these instances.

 What I'm thinking of is that we could move huge parts of the logic in
 `register_post_type()` into the constructor of the new class, more
 specifically, I think we should move everything that changes the actual
 post type object (for example filling `publicly_queryable` with the value
 from `public`, getting the labels) while keeping everything that acts
 outside of the post type object (for example adding query var, adding meta
 box callback) directly in the `register_post_type()` function.

 About the behavior of the class itself, I see two approaches: we could
 either use class properties for all the arguments of
 `register_post_type()` or we could put them into an `$args` array which
 would then be the only class property (for now). Then we could still
 access everything in a backwards-compatible way with PHP magic methods.
 What I like about the second approach is that it would allow to add any
 additional arguments, for example if a plugin does something custom with
 post types.

 If we create this class, we will have a base to work with in the future to
 centralize functionality that revolves around post types, for example I
 think it would be great if the post type objects would hold and manage its
 post type features - this way we could get rid of the private global
 variable that is currently used for that.

 I have already started writing a patch for this, just to test, which works
 fine so far, but before I refine the code to post it here, I would like to
 get some feedback about the idea in general.

 Also, if we tackle this, we can do the same thing for taxonomies
 (`WP_Taxonomy`), but that should be dealt with in a separate ticket I
 think.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36478>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list