Merge pull request #17164 from woocommerce/fix/17088

Fix image matching queries
This commit is contained in:
Gerhard Potgieter 2017-10-13 11:30:34 +02:00 committed by GitHub
commit 0146c7d240
1 changed files with 19 additions and 6 deletions

View File

@ -500,25 +500,38 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
}
$id = 0;
$upload_dir = wp_upload_dir();
$upload_dir = wp_upload_dir( null, false );
$base_url = $upload_dir['baseurl'] . '/';
// Check first if attachment is on WordPress uploads directory.
if ( false !== strpos( $url, $base_url ) ) {
// Search for yyyy/mm/slug.extension.
// Check first if attachment is inside the WordPress uploads directory, or we're given a filename only.
if ( false !== strpos( $url, $base_url ) || false === strpos( $url, '://' ) ) {
// Search for yyyy/mm/slug.extension or slug.extension - remove the base URL.
$file = str_replace( $base_url, '', $url );
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
'relation' => 'OR',
array(
'value' => str_replace( $base_url, '', $url ),
'compare' => 'LIKE',
'key' => '_wp_attached_file',
'value' => '^' . $file,
'compare' => 'REGEXP',
),
array(
'key' => '_wp_attached_file',
'value' => '/' . $file,
'compare' => 'LIKE',
),
array(
'key' => '_wc_attachment_source',
'value' => '/' . $file,
'compare' => 'LIKE',
),
),
);
} else {
// This is an external URL, so compare to source.
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',