Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#36478 closed enhancement (duplicate)

Add a `WP_Post_Type` class to represent post types

Reported by: flixos90's profile flixos90 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: Cc:


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.

Change History (1)

#1 @ocean90
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #36217.

Note: See TracTickets for help on using tickets.