|
|
@@ -24,12 +24,6 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
|
|
|
// Safety net: if item was added without photo, remove it immediately
|
|
|
add_action('woocommerce_add_to_cart', array($this, 'check_after_add_to_cart'), 10, 6);
|
|
|
-
|
|
|
- // AJAX: store uploaded file reference in WC session (legacy — shortcode path)
|
|
|
- add_action('wp_ajax_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
|
|
|
- add_action('wp_ajax_nopriv_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
|
|
|
- add_action('wp_ajax_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
|
|
|
- add_action('wp_ajax_nopriv_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -249,72 +243,28 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Resolve the currently "attached" upload for this visitor (legacy single-file path).
|
|
|
- * Checks WC session first, then legacy cookie pair, and finally the first file in the
|
|
|
- * current batch as a last-resort fallback. Used only by the shortcode flow on 1.5+ —
|
|
|
- * the product-detail page uses resolve_batch_files() and pre-stamped file_record_id.
|
|
|
+ * Resolve the single "current" upload for callers that still think in single-file
|
|
|
+ * terms (shortcode fallback, the validate_add_to_cart fallback branch). Returns the
|
|
|
+ * oldest unbound file in the visitor's batch, or null if none.
|
|
|
+ *
|
|
|
+ * Since 1.5.3 this is just a thin wrapper on the batch transport — the legacy
|
|
|
+ * WC-session and legacy `studiou_fpp_att_id` / `studiou_fpp_rec_id` cookies are gone.
|
|
|
*
|
|
|
* @return array{attachment_id:int,file_record_id:int,file_name:string,thumb_url:string}|null
|
|
|
*/
|
|
|
public static function resolve_uploaded_file(Studiou_WC_FPP_DB $db) {
|
|
|
- $attachment_id = 0;
|
|
|
- $file_record_id = 0;
|
|
|
- $file_name = '';
|
|
|
- $thumb_url = '';
|
|
|
-
|
|
|
- if (function_exists('WC') && WC()->session) {
|
|
|
- $attachment_id = (int) WC()->session->get('studiou_fpp_attachment_id');
|
|
|
- $file_record_id = (int) WC()->session->get('studiou_fpp_file_record_id');
|
|
|
- $file_name = (string) WC()->session->get('studiou_fpp_file_name');
|
|
|
- $thumb_url = (string) WC()->session->get('studiou_fpp_thumb_url');
|
|
|
- }
|
|
|
-
|
|
|
- if ($attachment_id <= 0 || $file_record_id <= 0) {
|
|
|
- $cookie_att = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT])
|
|
|
- ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT] : 0;
|
|
|
- $cookie_rec = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD])
|
|
|
- ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD] : 0;
|
|
|
- if ($cookie_att > 0 && $cookie_rec > 0) {
|
|
|
- $attachment_id = $cookie_att;
|
|
|
- $file_record_id = $cookie_rec;
|
|
|
- $file_name = '';
|
|
|
- $thumb_url = '';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ($attachment_id <= 0 || $file_record_id <= 0) {
|
|
|
+ $files = self::resolve_batch_files($db);
|
|
|
+ if (empty($files)) {
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- $record = $db->get_file_record($file_record_id);
|
|
|
- if (!$record) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- if ((int) $record->attachment_id !== $attachment_id) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- // A record already attached to an order is no longer "in flight" for add-to-cart
|
|
|
- if ((int) $record->order_id !== 0) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- if ($file_name === '') {
|
|
|
- $file_name = (string) $record->file_name;
|
|
|
- }
|
|
|
- if ($thumb_url === '') {
|
|
|
- $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
|
|
|
- if ($thumb) {
|
|
|
- $thumb_url = $thumb[0];
|
|
|
- } else {
|
|
|
- $thumb_url = (string) wp_get_attachment_url($attachment_id);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ $first = $files[0];
|
|
|
+ $att_id = (int) $first->attachment_id;
|
|
|
+ $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
|
|
|
return array(
|
|
|
- 'attachment_id' => $attachment_id,
|
|
|
- 'file_record_id' => $file_record_id,
|
|
|
- 'file_name' => $file_name,
|
|
|
- 'thumb_url' => $thumb_url,
|
|
|
+ 'attachment_id' => $att_id,
|
|
|
+ 'file_record_id' => (int) $first->id,
|
|
|
+ 'file_name' => (string) $first->file_name,
|
|
|
+ 'thumb_url' => $thumb ? $thumb[0] : (string) wp_get_attachment_url($att_id),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -335,49 +285,4 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function ajax_set_upload_session() {
|
|
|
- check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
|
|
|
-
|
|
|
- $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
|
|
|
- $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
|
|
|
- $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : '';
|
|
|
-
|
|
|
- if (!$attachment_id || !$file_record_id) {
|
|
|
- wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product')));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (WC()->session) {
|
|
|
- WC()->session->set('studiou_fpp_attachment_id', $attachment_id);
|
|
|
- WC()->session->set('studiou_fpp_file_record_id', $file_record_id);
|
|
|
- WC()->session->set('studiou_fpp_file_name', $file_name);
|
|
|
-
|
|
|
- // Store thumbnail URL directly so it can be used in cart without wp_get_attachment lookup
|
|
|
- $thumb_url = '';
|
|
|
- $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
|
|
|
- if ($thumb) {
|
|
|
- $thumb_url = $thumb[0];
|
|
|
- } else {
|
|
|
- $thumb_url = wp_get_attachment_url($attachment_id);
|
|
|
- }
|
|
|
- WC()->session->set('studiou_fpp_thumb_url', $thumb_url ?: '');
|
|
|
- }
|
|
|
-
|
|
|
- wp_send_json_success();
|
|
|
- }
|
|
|
-
|
|
|
- public function ajax_clear_upload_session() {
|
|
|
- check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
|
|
|
-
|
|
|
- if (WC()->session) {
|
|
|
- WC()->session->set('studiou_fpp_attachment_id', null);
|
|
|
- WC()->session->set('studiou_fpp_file_record_id', null);
|
|
|
- WC()->session->set('studiou_fpp_file_name', null);
|
|
|
- WC()->session->set('studiou_fpp_thumb_url', null);
|
|
|
- }
|
|
|
-
|
|
|
- Studiou_WC_FPP_Upload::clear_upload_cookies();
|
|
|
-
|
|
|
- wp_send_json_success();
|
|
|
- }
|
|
|
}
|