question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea draft posts cannot be trashed

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
felixarntzcommented, Jun 10, 2021

@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.

1reaction
aaemnnosttvcommented, Jun 14, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't Delete it. Draft it! [Update: or Archive it!] | Blog
WordPress does have a “Trash” feature. Once you delete a post, it's held in a “trash can” for 30 days before it is...
Read more >
retrieve my draft comment when the underlying post was deleted
I was typing a very long reply and it's now saved as a draft, and visible on the list in Settings > Draft....
Read more >
All Those Deleted Drafts. Let's Discuss.
Bloggers, do you ever write down a post idea as a draft? Do you ever actually go back ... I know I've deleted...
Read more >
Topic: Body of post deleted upon saving draft - WordPress.com
I was near finished writing a post, and to ensure that my work would be saved I clicked “Save Draft” – but all...
Read more >
Instagram Drafts: How to Save/Find/Delete Drafts on IG
Instagram can save posts, Reels, and Stories as drafts. ... Once saved, an Instagram draft can be posted or deleted as you see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found