Make WordPress Core

Changeset 49084


Ignore:
Timestamp:
10/01/2020 05:53:38 PM (4 years ago)
Author:
helen
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r49041 r49084  
    59935993    $attachment_id = (int) $attachment_id;
    59945994
    5995     $post = get_post( $attachment_id );
    5996     if ( ! $post ) {
     5995    $data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     5996
     5997    if ( empty( $data ) ) {
    59975998        return false;
    59985999    }
    5999 
    6000     $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
    60016000
    60026001    if ( $unfiltered ) {
     
    60136012     * @param int        $attachment_id Attachment post ID.
    60146013     */
    6015     return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
     6014    return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
    60166015}
    60176016
  • trunk/tests/phpunit/tests/media.php

    r49050 r49084  
    23892389
    23902390    /**
     2391     * @ticket 50679
     2392     */
     2393    function test_wp_get_attachment_metadata_should_return_false_if_no_attachment() {
     2394        $post_id = self::factory()->post->create();
     2395        $data   = wp_get_attachment_metadata( $post_id );
     2396        $this->assertFalse( $data );
     2397    }
     2398
     2399    /**
    23912400     * @ticket 37813
    23922401     */
Note: See TracChangeset for help on using the changeset viewer.