Make WordPress Core


Ignore:
Timestamp:
01/29/2020 04:34:45 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Media: Make sure attachment_url_to_postid() performs a case-sensitive search for the uploaded file name.

Previously, the first available match was returned, regardless of the case, which was not always the expected result.

Props archon810, ben.greeley, tristangemus, vsamoletov, SergeyBiryukov.
Merges [47010] to the 5.3 branch.
Fixes #39768.

Location:
branches/5.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

  • branches/5.3/src/wp-includes/media.php

    r46773 r47132  
    42564256
    42574257    $sql = $wpdb->prepare(
    4258         "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
     4258        "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
    42594259        $path
    42604260    );
    42614261
    4262     $post_id = $wpdb->get_var( $sql );
     4262    $results = $wpdb->get_results( $sql );
     4263    $post_id = null;
     4264
     4265    if ( $results ) {
     4266        // Use the first available result, but prefer a case-sensitive match, if exists.
     4267        $post_id = reset( $results )->post_id;
     4268
     4269        if ( count( $results ) > 1 ) {
     4270            foreach ( $results as $result ) {
     4271                if ( $path === $result->meta_value ) {
     4272                    $post_id = $result->post_id;
     4273                    break;
     4274                }
     4275            }
     4276        }
     4277    }
    42634278
    42644279    /**
Note: See TracChangeset for help on using the changeset viewer.