Skip to content

Instantly share code, notes, and snippets.

@bengreeley
Created February 2, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengreeley/29a6edaca5c370091dd2cafc3369e400 to your computer and use it in GitHub Desktop.
Save bengreeley/29a6edaca5c370091dd2cafc3369e400 to your computer and use it in GitHub Desktop.
Function that fixes duplicate filename bug found in attachment_url_to_postid
function attachment_url_to_postid_case_sensitive( $url ) {
global $wpdb;
$dir = wp_get_upload_dir();
$path = $url;
$site_url = parse_url( $dir['url'] );
$image_path = parse_url( $path );
//force the protocols to match if needed
if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
}
if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
}
$path_array = explode( '.', $path );
if( count( $path_array ) ) {
// Pop the last element off the array to grab the filename
$path_duplicated_filename = array_pop( $path_array );
// Piece everything back together, putting back any existing '.' that were in the filename
$path_duplicated_base = implode( '.', $path_array ) . '-';
$path_duplicated = $path_duplicated_base . '%' . $path_duplicated_filename;
}
// Perform the search, looking for the exact value OR a file in the format 'Filename-'
$sql = $wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND ( meta_value = %s OR meta_value LIKE %s )",
$path,
$path_duplicated
);
$results = $wpdb->get_results( $sql );
// Check to see if the path matches of the meta values returned (case-sensitive). If so, we've found the ID
$meta_values = wp_list_pluck( $results, 'meta_value', 'post_id' );
$post_id = array_search( $path, $meta_values, true );
$similarity_percent = 0;
$similarity_percent_temp = 0;
// If there is no exact match and multiple results, parse through and match for the correct URL.
if( count( $results ) > 1 &&
empty( $post_id ) ) {
// If an image has been uploaded as untitled.jpg and then another one is uploaded named Untitled.jpg, we need
// to match capitalization for the file. That file is most likely in the format Untitled-1.jpg in the meta_value
// field, so we'll need to check for that being case-sensitive
foreach( $results as $result ) {
// Separate just the name of the file.
$path_temp_array = explode( '.', $result->meta_value );
$path_temp = implode( '.', $path_temp_array );
// Checks to see if the base is a part of the string (case-sensitive). If so, we found our match.
if( false !== strpos( $path_temp, $path_duplicated_base ) ) {
// Calculate similarity % to get the most similar result if we have other filenames that are very similar.
similar_text( $path_temp, $path_duplicated_base, $similarity_percent_temp );
if( $similarity_percent_temp > $similarity_percent ) {
$similarity_percent = $similarity_percent_temp;
$post_id = $result->post_id;
}
}
}
}
if( empty( $post_id ) ) {
$post_id = 0;
}
/**
* Filters an attachment id found by URL.
*
* @since 4.2.0
*
* @param int|null $post_id The post_id (if any) found by the function.
* @param string $url The URL being looked up.
*/
return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment