|
@@ -764,12 +764,31 @@ class Studiou_WC_Product_Manage_Product_Import {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Find an attachment previously imported from this exact source URL.
|
|
|
|
|
|
|
+ * Find an attachment previously imported from this source URL.
|
|
|
*
|
|
*
|
|
|
- * Uses get_posts (not a raw $wpdb query) so a row whose attachment post was
|
|
|
|
|
- * deleted but whose meta was somehow orphaned can't resolve to a missing post.
|
|
|
|
|
- * Orders by ID ascending so the earliest (canonical) attachment wins
|
|
|
|
|
- * deterministically if more than one ever carries the same source URL.
|
|
|
|
|
|
|
+ * v1.6.1 — primary match: `_studiou_wcpcm_source_url` postmeta exact ==
|
|
|
|
|
+ * the trimmed URL. This is the precise path; future re-imports of the
|
|
|
|
|
+ * same URL are O(1) DB hits.
|
|
|
|
|
+ *
|
|
|
|
|
+ * v1.7.2 — fallback match: for attachments that pre-date the v1.6.1
|
|
|
|
|
+ * dedup (so the source-URL postmeta was never written), match by the
|
|
|
|
|
+ * uploads-relative path stored in `_wp_attached_file`. The URL is
|
|
|
|
|
+ * parsed for its path, the configured uploads baseurl path is
|
|
|
|
|
+ * stripped, and what remains (e.g. `2024/03/18-IMG_3319.jpg`) is the
|
|
|
|
|
+ * value WordPress already stores in `_wp_attached_file`. Hits backfill
|
|
|
|
|
+ * `_studiou_wcpcm_source_url` on the matched attachment so the next
|
|
|
|
|
+ * import of the same URL takes the precise path.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Trade-off: two distinct URLs that happen to resolve to the same
|
|
|
|
|
+ * uploads-relative path would collapse to one attachment. That can
|
|
|
|
|
+ * only happen if both URLs point at the same WordPress install's
|
|
|
|
|
+ * uploads tree — i.e. they really ARE the same file. The fallback
|
|
|
|
|
+ * therefore stays accurate.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Uses get_posts (not a raw $wpdb query) so a row whose attachment post
|
|
|
|
|
+ * was deleted but whose meta was orphaned can't resolve to a missing
|
|
|
|
|
+ * post. Orders by ID ascending so the earliest (canonical) attachment
|
|
|
|
|
+ * wins deterministically.
|
|
|
*
|
|
*
|
|
|
* @param string $url Trimmed source URL.
|
|
* @param string $url Trimmed source URL.
|
|
|
* @return int Attachment ID, or 0 if none.
|
|
* @return int Attachment ID, or 0 if none.
|
|
@@ -794,7 +813,60 @@ class Studiou_WC_Product_Manage_Product_Import {
|
|
|
),
|
|
),
|
|
|
));
|
|
));
|
|
|
|
|
|
|
|
- return !empty($ids) ? (int) $ids[0] : 0;
|
|
|
|
|
|
|
+ if (!empty($ids)) {
|
|
|
|
|
+ return (int) $ids[0];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // v1.7.2 — fallback by uploads-relative path.
|
|
|
|
|
+ $url_path = parse_url($url, PHP_URL_PATH);
|
|
|
|
|
+ if (!is_string($url_path) || $url_path === '') {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $upload_dir = wp_upload_dir();
|
|
|
|
|
+ if (!empty($upload_dir['error']) || empty($upload_dir['baseurl'])) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $base_path = parse_url($upload_dir['baseurl'], PHP_URL_PATH);
|
|
|
|
|
+ if (!is_string($base_path) || $base_path === '' || strpos($url_path, $base_path) !== 0) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $rel = ltrim(substr($url_path, strlen($base_path)), '/');
|
|
|
|
|
+ if ($rel === '') {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $rel = urldecode($rel); // CSV URLs may percent-encode spaces, etc.
|
|
|
|
|
+
|
|
|
|
|
+ $fallback_ids = get_posts(array(
|
|
|
|
|
+ 'post_type' => 'attachment',
|
|
|
|
|
+ 'post_status' => 'inherit',
|
|
|
|
|
+ 'posts_per_page' => 1,
|
|
|
|
|
+ 'orderby' => 'ID',
|
|
|
|
|
+ 'order' => 'ASC',
|
|
|
|
|
+ 'fields' => 'ids',
|
|
|
|
|
+ 'no_found_rows' => true,
|
|
|
|
|
+ 'update_post_meta_cache' => false,
|
|
|
|
|
+ 'update_post_term_cache' => false,
|
|
|
|
|
+ 'meta_query' => array(
|
|
|
|
|
+ array(
|
|
|
|
|
+ 'key' => '_wp_attached_file',
|
|
|
|
|
+ 'value' => $rel,
|
|
|
|
|
+ 'compare' => '=',
|
|
|
|
|
+ ),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ));
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($fallback_ids)) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $matched = (int) $fallback_ids[0];
|
|
|
|
|
+ // Backfill the source-URL postmeta so the next import of this URL
|
|
|
|
|
+ // hits the precise path instead of taking the fallback again.
|
|
|
|
|
+ update_post_meta($matched, '_studiou_wcpcm_source_url', $url);
|
|
|
|
|
+ if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
+ error_log('STUDIOU WC IMPORT: image cache HIT (fallback by _wp_attached_file) ' . $url . ' -> ' . $matched . '; backfilled _studiou_wcpcm_source_url');
|
|
|
|
|
+ }
|
|
|
|
|
+ return $matched;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|