From 449c2e21f123b7abe2b21f826592f21ce1d1857c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 29 Jan 2020 16:36:03 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/branches/5.3@47132 git-svn-id: http://core.svn.wordpress.org/branches/5.3@46932 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 19 +++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 3783865958..cecfb5c0d7 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -4255,11 +4255,26 @@ function attachment_url_to_postid( $url ) { } $sql = $wpdb->prepare( - "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", + "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path ); - $post_id = $wpdb->get_var( $sql ); + $results = $wpdb->get_results( $sql ); + $post_id = null; + + if ( $results ) { + // Use the first available result, but prefer a case-sensitive match, if exists. + $post_id = reset( $results )->post_id; + + if ( count( $results ) > 1 ) { + foreach ( $results as $result ) { + if ( $path === $result->meta_value ) { + $post_id = $result->post_id; + break; + } + } + } + } /** * Filters an attachment id found by URL. diff --git a/wp-includes/version.php b/wp-includes/version.php index de621212ea..d39139e859 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3.3-alpha-47131'; +$wp_version = '5.3.3-alpha-47132'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.