|
@@ -5,7 +5,12 @@ if (!defined('WPINC')) {
|
|
|
|
|
|
|
|
class Studiou_WC_FPP_Single_Product {
|
|
class Studiou_WC_FPP_Single_Product {
|
|
|
|
|
|
|
|
- public function __construct() {
|
|
|
|
|
|
|
+ /** @var Studiou_WC_FPP_DB */
|
|
|
|
|
+ private $db;
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct($db) {
|
|
|
|
|
+ $this->db = $db;
|
|
|
|
|
+
|
|
|
// Render upload area below the variation table on single product page
|
|
// Render upload area below the variation table on single product page
|
|
|
add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35);
|
|
add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35);
|
|
|
|
|
|
|
@@ -34,21 +39,20 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
$product_id = $product->get_id();
|
|
$product_id = $product->get_id();
|
|
|
$max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
|
|
$max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
|
|
|
|
|
|
|
|
- // Pass existing session state to JS for restore on page load
|
|
|
|
|
|
|
+ // Pass existing state (WC session or cookie fallback) to JS for restore on page load
|
|
|
$session_data = array();
|
|
$session_data = array();
|
|
|
- if (WC()->session) {
|
|
|
|
|
- $att_id = WC()->session->get('studiou_fpp_attachment_id');
|
|
|
|
|
- if ($att_id) {
|
|
|
|
|
- $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
|
|
|
|
|
- $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single');
|
|
|
|
|
- $session_data = array(
|
|
|
|
|
- 'attachment_id' => $att_id,
|
|
|
|
|
- 'file_record_id' => WC()->session->get('studiou_fpp_file_record_id'),
|
|
|
|
|
- 'file_name' => WC()->session->get('studiou_fpp_file_name'),
|
|
|
|
|
- 'thumbnail_url' => $thumb ? $thumb[0] : '',
|
|
|
|
|
- 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''),
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $resolved = self::resolve_uploaded_file($this->db);
|
|
|
|
|
+ if ($resolved) {
|
|
|
|
|
+ $att_id = (int) $resolved['attachment_id'];
|
|
|
|
|
+ $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
|
|
|
|
|
+ $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single');
|
|
|
|
|
+ $session_data = array(
|
|
|
|
|
+ 'attachment_id' => $att_id,
|
|
|
|
|
+ 'file_record_id' => (int) $resolved['file_record_id'],
|
|
|
|
|
+ 'file_name' => $resolved['file_name'],
|
|
|
|
|
+ 'thumbnail_url' => $thumb ? $thumb[0] : '',
|
|
|
|
|
+ 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''),
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data);
|
|
wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data);
|
|
@@ -83,7 +87,7 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Validate before add-to-cart: block if FPP product and no photo in session.
|
|
|
|
|
|
|
+ * Validate before add-to-cart: block if FPP product and no photo reference resolvable.
|
|
|
*/
|
|
*/
|
|
|
public function validate_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array()) {
|
|
public function validate_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array()) {
|
|
|
$fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
|
|
$fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
|
|
@@ -91,18 +95,87 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
return $passed;
|
|
return $passed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Check WC session for uploaded file
|
|
|
|
|
- if (WC()->session) {
|
|
|
|
|
- $attachment_id = WC()->session->get('studiou_fpp_attachment_id');
|
|
|
|
|
- if ($attachment_id) {
|
|
|
|
|
- return $passed;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (self::resolve_uploaded_file($this->db)) {
|
|
|
|
|
+ return $passed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
|
|
wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Resolve the currently "attached" upload for this visitor, checking WC session first
|
|
|
|
|
+ * and falling back to a first-party cookie written at upload time.
|
|
|
|
|
+ *
|
|
|
|
|
+ * The cookie fallback is required because pvtfw 1.9.3+ / WC 10.7.0+ may route
|
|
|
|
|
+ * Add to Cart through the WC Store API, which identifies the session via the
|
|
|
|
|
+ * Cart-Token header rather than the classic wp_woocommerce_session_* cookie —
|
|
|
|
|
+ * that path sees a different (empty) session than the one the upload wrote to.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @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) {
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return array(
|
|
|
|
|
+ 'attachment_id' => $attachment_id,
|
|
|
|
|
+ 'file_record_id' => $file_record_id,
|
|
|
|
|
+ 'file_name' => $file_name,
|
|
|
|
|
+ 'thumb_url' => $thumb_url,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Safety net: if an FPP item was added without photo data, remove it.
|
|
* Safety net: if an FPP item was added without photo data, remove it.
|
|
|
* This catches cases where validation was somehow bypassed.
|
|
* This catches cases where validation was somehow bypassed.
|
|
@@ -158,8 +231,11 @@ class Studiou_WC_FPP_Single_Product {
|
|
|
WC()->session->set('studiou_fpp_attachment_id', null);
|
|
WC()->session->set('studiou_fpp_attachment_id', null);
|
|
|
WC()->session->set('studiou_fpp_file_record_id', null);
|
|
WC()->session->set('studiou_fpp_file_record_id', null);
|
|
|
WC()->session->set('studiou_fpp_file_name', 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();
|
|
wp_send_json_success();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|