site-kit-wp: Idea draft posts cannot be trashed

Bug Description

As pointed out in https://github.com/google/site-kit-wp/issues/3357#issuecomment-856660512, draft posts that were automatically created based on an Idea Hub idea are initially empty posts (via add_filter( 'wp_insert_post_empty_content', '__return_false' ), which WordPress generally doesn’t allow. We work around it during draft creation. However, with WordPress’s built-in functionality to trash posts, that workaround is obviously not present.

Trashing a post in WordPress is technically an “update” to that post, so WordPress will still fail on it because the post is empty.

It should be possible to delete an idea draft post as expected, either by immediately deleting it or being able to trash it (in that case also untrash it).


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • When modifying any post based on an Idea Hub idea (via related metadata), such a modification should never fail due to the post being empty (i.e. having no title or no content).
    • In other words, the wp_insert_post_empty_content filter should be used with a dynamic callback that disables the blocking behavior if the post is an Idea Hub idea post.
    • This should work for any interaction with such a post and any post status.

Implementation Brief

Implement is_idea_post method

  • Add a new private function, Idea_Hub::is_idea_post, which should receive a single $post_id parameter
  • The function should call Idea_Hub::get_post_idea, passing the $post_idin order to determine whether the given post is an Idea Hub post.
  • It should return true if the post is an Idea Hub post and false if not.
  • Add test coverage for this new method

Add an empty content filter to bypass Wordpress’s default behaviour

  • Inside Idea_Hub::register, add the 'wp_insert_post_empty_content' filter. Call the is_idea_post function inside it so that if the given post is an Idea Hub post, the default ‘empty post’ behaviour is bypassed:
add_filter(
	'wp_insert_post_empty_content',
	function ( $maybe_empty, $post_id ) {
		if ( $this->is_idea_post( $post_id ) ) {
			return false; // Don't consider it empty.
		}
		return $maybe_empty;
	},
	10,
	2
);

Test Coverage

  • New coverage should be added for the is_idea_post method. Ensure existing tests still pass.

Visual Regression Changes

  • None anticipated

QA Brief

  • Enable the idea hub module via tester plugin, open the console on /wp-admin/admin.php?page=googlesitekit-dashboard,
  • Create a draft Idea Hub post:
googlesitekit.data.stores["modules/idea-hub"].actions.createIdeaDraftPost({
    "name": "ideas/7612031899179595408",
    "text": "How to speed up your WordPress site",
    "topics": [
      {
        "mid": "/m/09kqc",
        "display_name": "Websites"
      }
    ]
  })
  • Go to your WordPress posts, you should see the ideahub draft, now try and trash it. If you were able to trash it, that is a success.

Changelog entry

  • Allow posts with Idea Hub drafts to be trashed.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

@aaemnnosttv Good point - I’ve updated the ACs accordingly.

@johnPhillips Please review the above, the code snippet in https://github.com/google/site-kit-wp/issues/3514#issuecomment-858813794 is pretty much what we’ll need as a foundation.

@felixarntz if we were to want or need to query all Idea Hub posts in the future, which field would we rely on to get them all?

@johnPhillips please update the IB to remove the part about the added post meta. For now the new method can probably just use Idea_Hub::get_post_idea internally.

@aaemnnosttv Oh I missed that suggestion - I don’t think that’s necessary, let’s rely on the three fields being present to know that it is an Idea Hub idea post.