Media: Don't unnecessarily check for a valid attachment before getting meta.

This makes `wp_get_attachment_metadata()` run significantly faster. See ticket for benchmarking.

Props Tkama, donmhico.
Fixes #50679.


git-svn-id: https://develop.svn.wordpress.org/trunk@49084 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Helen Hou-Sandi 2020-10-01 17:53:38 +00:00
parent 7cca745052
commit 150d9bd17e
2 changed files with 13 additions and 5 deletions

View File

@ -5992,13 +5992,12 @@ function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
$attachment_id = (int) $attachment_id;
$post = get_post( $attachment_id );
if ( ! $post ) {
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
if ( empty( $data ) ) {
return false;
}
$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
if ( $unfiltered ) {
return $data;
}
@ -6012,7 +6011,7 @@ function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
* if the object does not exist.
* @param int $attachment_id Attachment post ID.
*/
return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
}
/**

View File

@ -2387,6 +2387,15 @@ EOF;
return $data;
}
/**
* @ticket 50679
*/
function test_wp_get_attachment_metadata_should_return_false_if_no_attachment() {
$post_id = self::factory()->post->create();
$data = wp_get_attachment_metadata( $post_id );
$this->assertFalse( $data );
}
/**
* @ticket 37813
*/