VIP-Coding-Standards icon indicating copy to clipboard operation
VIP-Coding-Standards copied to clipboard

New Sniff: wp_get_post_revisions memory issues

Open mikeyarce opened this issue 5 years ago • 2 comments

What problem would the enhancement address for VIP?

wp_get_post_revisions can be problematic because it loads the entire post object for each revision. I've seen this in CLI commands and other places, where you might be looping through a set of posts, and then suddenly have to load the entire post objects for every revision a post has. If those revisions are large and numerous - it quickly fails.

Related trac: https://core.trac.wordpress.org/ticket/34560

Describe the solution you'd like

We should suggest they only get the IDs for the revisions instead of the whole object:

wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );

What code should be reported as a violation?

Anything that doesn't have the fields argument, like:

 wp_get_post_revisions( $post->ID );

What code should not be reported as a violation?

Getting the ids field or other fields instead of the whole object.

wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );

mikeyarce avatar Feb 11 '20 21:02 mikeyarce

Getting the ids field or other fields instead of the whole object.

@mikeyarce Am I right in thinking that the conditions for success (no violation) is:

  • second argument must exist and be an array
  • second argument must have a fields key?

Any further constraints?

Would this apply to WordPress.com VIP as well?

GaryJones avatar Feb 11 '20 22:02 GaryJones

@GaryJones those conditions are perfect, can't think of any other constraints.

Would this apply to WordPress.com VIP as well?

Yes, it would!

mikeyarce avatar Feb 13 '20 16:02 mikeyarce