WordPress.org

Make WordPress Core

Changeset 47132


Ignore:
Timestamp:
01/29/2020 04:34:45 PM (17 months 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:
3 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    /**
  • branches/5.3/tests/phpunit/tests/media.php

    r46067 r47132  
    10591059    }
    10601060
    1061     function test_attachment_url_to_postid_schemes() {
     1061    /**
     1062     * @ticket 33109
     1063     */
     1064    function test_attachment_url_to_postid_with_different_scheme() {
    10621065        $image_path    = '2014/11/' . $this->img_name;
    10631066        $attachment_id = self::factory()->attachment->create_object(
     
    10701073        );
    10711074
    1072         /**
    1073          * @ticket 33109 Testing protocols not matching
    1074          */
    10751075        $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
    10761076        $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
     1077    }
     1078
     1079    /**
     1080     * @ticket 39768
     1081     */
     1082    function test_attachment_url_to_postid_should_be_case_sensitive() {
     1083        $image_path_lower_case    = '2014/11/' . $this->img_name;
     1084        $attachment_id_lower_case = self::factory()->attachment->create_object(
     1085            $image_path_lower_case,
     1086            0,
     1087            array(
     1088                'post_mime_type' => 'image/jpeg',
     1089                'post_type'      => 'attachment',
     1090            )
     1091        );
     1092
     1093        $image_path_upper_case    = '2014/11/' . ucfirst( $this->img_name );
     1094        $attachment_id_upper_case = self::factory()->attachment->create_object(
     1095            $image_path_upper_case,
     1096            0,
     1097            array(
     1098                'post_mime_type' => 'image/jpeg',
     1099                'post_type'      => 'attachment',
     1100            )
     1101        );
     1102
     1103        $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case;
     1104        $this->assertEquals( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
    10771105    }
    10781106
Note: See TracChangeset for help on using the changeset viewer.