Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#66225 new enhancement

Allow post_type_supports() to check for individual sub-features

Reported by: fabiankaegy Owned by:
Priority: normal Milestone: Awaiting Review
Component: Posts, Post Types Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Follow-up to #64156 and #66224.

#64156 lets repeated add_post_type_support() calls merge their array arguments, and #66224 lets remove_post_type_support() remove individual sub-features. post_type_supports() still only answers whether the feature as a whole is supported.

To check whether a post type has notes enabled on the editor feature today, you have to reach into the raw arguments yourself:

<?php
$supports = get_all_post_type_supports( 'custom' );
$has_notes = isset( $supports['editor'][0]['notes'] ) && $supports['editor'][0]['notes'];

This was suggested by @mamaduka on the #64156 PR.

Proposal

Add an optional third parameter, $sub_feature:

<?php
post_type_supports( 'custom', 'editor', 'notes' );
  • For associative arguments, the sub-feature is supported when its key is set to a non-empty value, so 'notes' => false counts as not supported.
  • For list arguments (e.g. a post-formats style feature), the sub-feature is supported when the list contains it.
  • If the feature isn't supported, or has no array arguments, the result is false.
  • Without the third parameter, behavior is unchanged.

Change History (1)

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


7 weeks ago
#1

  • Keywords has-patch has-unit-tests added

[!NOTE]
Stacked on #10426 and #13884. Until those land, this diff includes their commits. The only new commit here is 34df31795f.

#10426 merges array arguments across add_post_type_support() calls and #13884 lets remove_post_type_support() drop individual sub-features. post_type_supports() is the last piece. Right now it can only tell you whether editor is supported, not whether notes is enabled on it. @Mamaduka suggested this on #10426.

This adds an optional third $sub_feature parameter:

add_post_type_support( 'custom', 'editor', array(
    'default-mode' => 'template-locked',
    'notes'        => true,
) );

post_type_supports( 'custom', 'editor', 'notes' );    // true
post_type_supports( 'custom', 'editor', 'missing' );  // false
  • Associative arguments: supported when the key is set to a non-empty value. 'notes' => false counts as not supported.
  • List arguments (a post-formats style feature): supported when the list contains the value.
  • Returns false if the feature isn't supported or has no array arguments.
  • Without the third parameter nothing changes.

I went with a non-empty check instead of isset() so that explicitly disabling a sub-feature with false reads as unsupported. Happy to change that if folks prefer a plain key check.

Note: See TracTickets for help on using tickets.