Make WordPress Core

Opened 7 weeks ago

Last modified 2 weeks ago

#66224 new enhancement

Allow remove_post_type_support() to remove 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, which lets repeated add_post_type_support() calls merge their array arguments instead of overwriting them.

With that in place, remove_post_type_support() is the odd one out. It can only remove a feature as a whole. The editor feature now has several sub-features (default-mode, notes, ...), so turning off just one of them means removing the whole feature and registering it again with everything you want to keep:

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

// Today: to drop only 'notes'.
remove_post_type_support( 'custom', 'editor' );
add_post_type_support( 'custom', 'editor', array(
    'default-mode' => 'template-locked',
) );

This was raised by @wildworks on the #64156 PR.

Proposal

Add an optional third parameter, $sub_features (string or array of strings):

<?php
remove_post_type_support( 'custom', 'editor', 'notes' );
remove_post_type_support( 'custom', 'editor', array( 'notes', 'default-mode' ) );
  • For associative arguments, the given keys are removed regardless of their value.
  • For list arguments (e.g. a post-formats style feature), matching values are removed and the list is reindexed.
  • The feature itself stays supported, even when all of its sub-features are removed.
  • If the feature has no array arguments, or isn't registered, nothing happens.
  • Without the third parameter, behavior is unchanged and the whole feature is removed.

Checking for individual sub-features in post_type_supports() (also brought up on #64156) is left for a separate ticket.

Change History (2)

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


7 weeks ago
#1

  • Keywords has-patch has-unit-tests added

[!NOTE]
Stacked on #10426. Until that one lands, this diff includes its commits. The only new commit here is 9d3701c8bb.

#10426 makes repeated add_post_type_support() calls merge their array arguments. That leaves remove_post_type_support() as the odd one out, since it can only remove a whole feature. With editor now having several sub-features, turning off only notes means removing editor and registering it again with everything else you want to keep (@t-hamano raised this on #10426).

This adds an optional third $sub_features parameter:

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

remove_post_type_support( 'custom', 'editor', 'notes' );
// editor => array( array( 'default-mode' => 'template-locked' ) )
  • Associative arguments: the given keys are removed, whatever their value. This answers @westonruter's question on #10426 about matching values.
  • List arguments (a post-formats style feature): matching values are removed and the list is reindexed.
  • The feature stays supported, even with all its sub-features removed.
  • No-op if the feature has no array arguments or isn't registered.
  • Without the third parameter nothing changes, the whole feature is removed.

Checking individual sub-features in post_type_supports() is out of scope here.

​@mindctrl commented on ​PR #13884:


2 weeks ago
#2

@fabiankaegy it might be nice to add another test on the REST side that confirms a note can't be created after removing the notes sub-feature. In rest-comments-controller.php there's a test that confirms a note can't be created when a post type is registered without notes support. Maybe that's enough, but I was thinking another one that tests the path of a post type that did support notes but had it removed wouldn't hurt.

Something like this below test_cannot_create_note_without_post_type_support() is what I was thinking:

	/**
	 * @ticket 66224
	 */
	public function test_cannot_create_note_after_removing_notes_sub_feature() {
		remove_post_type_support( 'post', 'editor', 'notes' );

		wp_set_current_user( self::$admin_id );
		$post_id = self::factory()->post->create();

		$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
		$request->add_header( 'Content-Type', 'application/json' );
		$request->set_body(
			wp_json_encode(
				array(
					'post'    => $post_id,
					'content' => 'Call me Ishmael.',
					'author'  => self::$admin_id,
					'type'    => 'note',
				)
			)
		);
		$response = rest_get_server()->dispatch( $request );

		$this->assertErrorResponse( 'rest_comment_not_supported_post_type', $response, 403 );
		$this->assertTrue( post_type_supports( 'post', 'editor' ), 'The editor feature should remain supported.' );
	}
	
Note: See TracTickets for help on using tickets.