WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

#19663 closed enhancement (fixed)

auto-draft garbage collection should be done via cron, not when starting new post

Reported by: _ck_ Owned by: ryan
Milestone: 3.4 Priority: normal
Severity: normal Version: 3.3
Component: Performance Keywords: has-patch commit
Focuses: Cc:

Description

On small sites this isn't a problem but active, larger sites with multiple authors, the issue becomes magnified.

A user should not have to wait when clicking on post -> add new
but it can happen because of auto-draft deletion is forced to happen at that time for ALL authors in a system.

in wp-admin/includes/post.php function get_default_post_to_edit
garbage collection is currently done like so

if ( $create_in_db ) {
		// Cleanup old auto-drafts more than 7 days old
		$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
		foreach ( (array) $old_posts as $delete )
			wp_delete_post( $delete, true ); // Force delete
		$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
		$post = get_post( $post_id );
		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
			set_post_format( $post, get_option( 'default_post_format' ) );
	} 

Seems trivial but it's not. It can cause a large number of queries to occur as meta is also cleared and causes plugins related to new/deleted posts to fire. Even if there is nothing to cleanup, the code still fires for every author on every new post.

I propose a simple change instead, where the deletion code is moved simply to it's own small function and added to a once-a-day wp-cron that could be fired for example early in the morning or re-scheduled as desired.

This way an author can be guaranteed to have faster access to "add new" since it's otherwise a very complex page that needs many more of it's own queries to initialize and often several plugins to affect the new post abilities.

Attachments (2)

patch_19663.diff (1.7 KB) - added by mgolawala 4 years ago.
Move deletion of auto-draft posts to the wp_scheduled_delete cron function
19663.diff (2.9 KB) - added by ryan 3 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 @nacin4 years ago

  • Milestone changed from Awaiting Review to 3.4
  • Type changed from defect (bug) to enhancement

Yeah, this is a good idea.

@mgolawala4 years ago

Move deletion of auto-draft posts to the wp_scheduled_delete cron function

comment:2 @mgolawala4 years ago

  • Cc mgolawala added
  • Keywords has-patch added
  • Version changed from 3.3 to 3.3.1

I moved the inline deletion of auto-posts from the wp-admin/includes/post.php: get_default_post_to_edit and made it part of the daily garbage collection cron entry that cleans out posts, comments, pages, attachments etc.

Let me know if you would like it to be organized differently. From a code organization standpoint, I could use one of the alternate twotechniques:

  • Create a separate cron schedule for it to be more frequent than daily (configured via UI?)
  • Have the post.php entry queue up the separate cron entry for auto-post garbage collection, and then have the auto-post garbage collection function deque it self. This would end up invoking the garbage collection as often as before, just asynchronously.

Let me know your thoughts.

comment:3 @DrewAPicture4 years ago

  • Version changed from 3.3.1 to 3.3

The version field is used to indicate the first version in which a problem was identified in, or reproduced in.

comment:4 @nacin3 years ago

  • Keywords commit added

I like this.

comment:5 @scribu3 years ago

  • Cc scribu added

comment:6 @scribu3 years ago

There was an older ticket for this: #16785

comment:7 @ryan3 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [20440]:

Move deletion of auto-draft posts to the wp_scheduled_delete cron function. Props mgolawala. fixes #19663

comment:8 @ryan3 years ago

This caused an avalanche on wordpress.com. Let's schedule this only from post-new.php.

comment:9 @ryan3 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

@ryan3 years ago

comment:10 @ryan3 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In [20453]:

Schedule auto-draft deletion from post-new.php instead of from admin.php. This provides better throttling for large multisite installs and reduces the risk of a delete avalanche.

fixes #19663

Note: See TracTickets for help on using tickets.