From deaac68f8d31844cc3bba28d482d433feb524291 Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Wed, 6 Sep 2017 09:38:59 -0600
Subject: [PATCH] allow attachment_url_to_postid() to find post_id for URL of
intermediate sized image
---
src/wp-includes/media.php | 37 +++++++++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 4 deletions(-)
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
index 9ec3f61..fe88634 100644
a
|
b
|
function attachment_url_to_postid( $url ) { |
3922 | 3922 | $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
3923 | 3923 | } |
3924 | 3924 | |
3925 | | $sql = $wpdb->prepare( |
3926 | | "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", |
3927 | | $path |
3928 | | ); |
| 3925 | $mime_type = wp_check_filetype( $path ); |
| 3926 | $is_image = 0 === strpos( $mime_type['type'], 'image/' ); |
| 3927 | |
| 3928 | if ( $is_image ) { |
| 3929 | $sql = $wpdb->prepare( |
| 3930 | "SELECT pm.post_id |
| 3931 | FROM $wpdb->postmeta as pm INNER JOIN $wpdb->posts as p ON ( pm.post_id = p.ID ) |
| 3932 | WHERE p.post_type = 'attachment' AND p.post_status = 'inherit' AND |
| 3933 | pm.meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s", |
| 3934 | '%' . basename( $path ) . '%' |
| 3935 | ); |
| 3936 | } |
| 3937 | else { |
| 3938 | $sql = $wpdb->prepare( |
| 3939 | "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", |
| 3940 | $path |
| 3941 | ); |
| 3942 | } |
| 3943 | |
3929 | 3944 | $post_id = $wpdb->get_var( $sql ); |
3930 | 3945 | |
| 3946 | if ( $post_id && $is_image ) { |
| 3947 | $meta = wp_get_attachment_metadata( $post_id ); |
| 3948 | if ( isset( $meta['file'], $meta['sizes'] ) ) { |
| 3949 | $path = basename( $path ); |
| 3950 | |
| 3951 | $original_file = basename( $meta['file'] ); |
| 3952 | $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' ); |
| 3953 | |
| 3954 | if ( ! ( $original_file === $path || in_array( $path, $cropped_image_files ) ) ) { |
| 3955 | $post_id = null; |
| 3956 | } |
| 3957 | } |
| 3958 | } |
| 3959 | |
3931 | 3960 | /** |
3932 | 3961 | * Filters an attachment id found by URL. |
3933 | 3962 | * |