Make WordPress Core

Opened 10 years ago

Last modified 3 weeks ago

#37667 new enhancement

Handle post type support in `WP_Post_Type`

Reported by: flixos90 Owned by:
Priority: normal Milestone: Future Release
Component: Posts, Post Types Version: 4.6
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Currently post type features are stored in a global variable $_wp_post_type_features. Now that we have WP_Post_Type, I think it would make sense to let the class handle this functionality. Post type features are part of a post type, so they should also be part of it in the implementation. This would also allow us to get rid of the global variable we currently use (note that it's private, so it's not intended to be used directly by other developers).

There's a single possible caveat I would see with this implementation: That would be if post type support is added before the post type is even registered. However, I would think we might be able to change this (with a dev note) since it should be best practices to add post type support either directly (via the supports argument in register_post_type()) or after registering the post type. Adding a post type feature without the post type being registered is not logical in my opinion.

Attachments (3)

37667.diff​ (10.3 KB ) - added by flixos90 10 years ago.
37667.2.diff​ (18.8 KB ) - added by flixos90 10 years ago.
37667.3.diff​ (22.0 KB ) - added by flixos90 10 years ago.

Download all attachments as: .zip

Change History (13)

#1 @flixos90
10 years ago

  • Keywords has-patch 2nd-opinion added

37667.diff​ is a patch for what the implementation of this proposal could look like.

All features are stored in a private $features property in WP_Post_Type. The related methods explicitly handle subtypes (like we have for attachments with attachment:audio and attachment:video) - for example, calling $attachment_post_type->supports( 'thumbnail', 'audio' ) would be the same as calling post_type_supports( 'attachment:audio', 'thumbnail' ).

There's a method remove_supports() in WP_Post_Type which might not be necessary any longer after this change. It could be left as is or deprecated.

#2 @swissspidy
10 years ago

  • Keywords needs-unit-tests added

+1 for pursuing this.

#3 @flixos90
10 years ago

@swissspidy: What do you think about the (possible) problem with adding post type support before the post type is registered? How should we handle this? Add bool return values to add_post_type_support()? Or return WP_Error? Or _doing_it_wrong()?

#4 @swissspidy
10 years ago

@flixos90 wp_oembed_add_provider() and WP_oEmbed::_add_provider_early() come to mind.

If the post type doesn't exist yet, the feature could still be added to the global and upon post type registration it would be taken out of there and added to the actual post type object.

Adding a return value wouldn't help much as developers don't check them since it's a void function. A _doing_it_wrong() could be triggered so that eventually we might get rid of the global array in the future.

#5 @flixos90
10 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

37667.2.diff​ is an updated patch.

I checked how WP_oEmbed handles their almost similar problem, however what's different is that we are likely to remove that backwards-compatible functionality at some point (to get rid of the global). We could have gotten rid of the global now by moving the functionality to a static property and some (semi-)privat static methods, but I don't think we should add a bunch of functionality there if we're going to get rid of it at some point. I added _doing_it_wrong() notices that are thrown when calling add_post_type_support() or remove_post_type_support() before the post type is registered. It still works now, using the global, but that should be removed later as discussed. The other functions get_all_post_type_supports(), post_type_supports() and get_post_types_by_support() don't throw a notice as I think they can be used before the post types are registered. They will just return an empty array (or false) when we get rid of the global.

I also added several unit tests for the new methods of WP_Post_Type and the post type support functions.

#6 @flixos90
10 years ago

37667.3.diff​ actually gets rid of the $_wp_post_type_features global by using a similar technique like WP_oEmbed does (early post type features).

#7 @swissspidy
10 years ago

  • Milestone Awaiting Review → Future Release

#8 @desrosj
8 years ago

  • Keywords needs-refresh added; 2nd-opinion removed

37667.3.diff​ needs a refresh to apply cleanly.

This ticket was mentioned in ​Slack in #core by sirlouen. ​View the logs.


13 months ago

This ticket was mentioned in ​PR #14751 on ​WordPress/wordpress-develop by ​@whopiyush.


3 weeks ago
#10

  • Keywords needs-refresh removed

Post type features are stored in the $_wp_post_type_features global, which WP_Post_Type has to reach for through procedural wrappers. The ticket proposes moving that storage into the class, since the features belong to the post type. This implements that and removes the global.

The three patches on the ticket are ten years old and no longer apply. The approach here follows the ticket's own reasoning and its final patch.

What changed

The five procedural functions keep their signatures, docblocks and behaviour, and their bodies now delegate to the class: add_post_type_support(), remove_post_type_support(), get_all_post_type_supports(), post_type_supports() and get_post_types_by_support(). Nothing a plugin calls has changed except that the global is gone.

WP_Post_Type gains supports(), add_support() and remove_support(). As described in the ticket, supports() accepts a subtype, so $attachment->supports( 'thumbnail', 'audio' ) is equivalent to post_type_supports( 'attachment:audio', 'thumbnail' ).

One design decision worth reviewing

The store is a private static keyed by post type name, not a per-object property. Two things in the existing behaviour ruled out putting it on the object, and both are easy to miss:

  1. Subtypes have no object. attachment:audio and attachment:video are added with add_post_type_support() in create_initial_post_types() and are never registered post types. An object-backed store cannot hold them at all, and post_type_supports( 'attachment:audio', 'thumbnail' ) would start returning false.
  1. get_post_types_by_support() returns insertion order. Today that order is the global's key order, which interleaves the subtypes: post, page, attachment:audio, attachment:video for thumbnail. Deriving the list by iterating registered post types would reorder it and drop the subtypes.

A static keyed by name keeps both, and still lets the object's methods resolve through $this->name. This is also why add_supports() is unchanged: register_post_type() calls it before assigning $wp_post_types[ $post_type ], so the store must be addressable by name at that point.

Support added before a post type is registered still works, which the ticket discusses at length: the entry is simply in the store already when the type is registered, so it applies then. That is the same behaviour as today, without needing the notice that an earlier patch on the ticket added.

Verification

A characterisation harness dumping 100 lines of observable API output was run against trunk and against the patch and diffed. Every line matches: all core post types' feature maps, all subtype queries, all five get_post_types_by_support() operator forms in order, add/remove round trips including args, the editor-implies-autosave rule, supports => false, defaults, early adds before registration, early adds never registered, and unregister cleanup. The only difference is the section that reads the global itself, which now reports it absent.

Test results: post group 1172 tests, 3066 assertions, 1 skipped. phpcs reports 0 errors.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Sonnet
Used for: test cases, code review

Note: See TracTickets for help on using tickets.