Make WordPress Core


Ignore:
Timestamp:
12/25/2019 02:24:38 AM (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.
Fixes #39768.

File:
1 edited

Legend:

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

    r46772 r47010  
    42964296
    42974297    $sql = $wpdb->prepare(
    4298         "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
     4298        "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
    42994299        $path
    43004300    );
    43014301
    4302     $post_id = $wpdb->get_var( $sql );
     4302    $results = $wpdb->get_results( $sql );
     4303    $post_id = null;
     4304
     4305    if ( $results ) {
     4306        // Use the first available result, but prefer a case-sensitive match, if exists.
     4307        $post_id = reset( $results )->post_id;
     4308
     4309        if ( count( $results ) > 1 ) {
     4310            foreach ( $results as $result ) {
     4311                if ( $path === $result->meta_value ) {
     4312                    $post_id = $result->post_id;
     4313                    break;
     4314                }
     4315            }
     4316        }
     4317    }
    43034318
    43044319    /**
Note: See TracChangeset for help on using the changeset viewer.