class-studiou-wc-fpp-single-product.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_Single_Product {
  6. /** @var Studiou_WC_FPP_DB */
  7. private $db;
  8. public function __construct($db) {
  9. $this->db = $db;
  10. // Render upload area below the variation table on single product page
  11. add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35);
  12. // Server-side validation: block add-to-cart without photo (priority 1 = very early)
  13. add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_to_cart'), 1, 6);
  14. // Safety net: if item was added without photo, remove it immediately
  15. add_action('woocommerce_add_to_cart', array($this, 'check_after_add_to_cart'), 10, 6);
  16. // AJAX: store uploaded file reference in WC session
  17. add_action('wp_ajax_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
  18. add_action('wp_ajax_nopriv_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
  19. add_action('wp_ajax_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
  20. add_action('wp_ajax_nopriv_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
  21. }
  22. public function render_upload_area() {
  23. global $product;
  24. if (!$product || !Studiou_WC_FPP_Product::is_fpp_product($product->get_id())) {
  25. return;
  26. }
  27. wp_enqueue_style('studiou-wcfpp-frontend');
  28. wp_enqueue_script('studiou-wcfpp-frontend');
  29. $product_id = $product->get_id();
  30. $max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
  31. // Pass existing state (WC session or cookie fallback) to JS for restore on page load
  32. $session_data = array();
  33. $resolved = self::resolve_uploaded_file($this->db);
  34. if ($resolved) {
  35. $att_id = (int) $resolved['attachment_id'];
  36. $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
  37. $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single');
  38. $session_data = array(
  39. 'attachment_id' => $att_id,
  40. 'file_record_id' => (int) $resolved['file_record_id'],
  41. 'file_name' => $resolved['file_name'],
  42. 'thumbnail_url' => $thumb ? $thumb[0] : '',
  43. 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''),
  44. );
  45. }
  46. wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data);
  47. include STUDIOU_WCFPP_PLUGIN_DIR . 'views/single-product-upload.php';
  48. }
  49. /**
  50. * Resolve whether a product_id or its parent is FPP-enabled.
  51. */
  52. private function resolve_fpp_product_id($product_id, $variation_id = 0) {
  53. // Check direct product
  54. if (Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  55. return $product_id;
  56. }
  57. // Check parent of the product (in case product_id is variation)
  58. $product = wc_get_product($product_id);
  59. if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
  60. return $product->get_parent_id();
  61. }
  62. // Check variation_id's parent
  63. if ($variation_id && $variation_id !== $product_id) {
  64. $variation = wc_get_product($variation_id);
  65. if ($variation && $variation->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($variation->get_parent_id())) {
  66. return $variation->get_parent_id();
  67. }
  68. }
  69. return false;
  70. }
  71. /**
  72. * Validate before add-to-cart: block if FPP product and no photo reference resolvable.
  73. */
  74. public function validate_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array()) {
  75. $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
  76. if (!$fpp_id) {
  77. return $passed;
  78. }
  79. if (self::resolve_uploaded_file($this->db)) {
  80. return $passed;
  81. }
  82. wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
  83. return false;
  84. }
  85. /**
  86. * Resolve the currently "attached" upload for this visitor, checking WC session first
  87. * and falling back to a first-party cookie written at upload time.
  88. *
  89. * The cookie fallback is required because pvtfw 1.9.3+ / WC 10.7.0+ may route
  90. * Add to Cart through the WC Store API, which identifies the session via the
  91. * Cart-Token header rather than the classic wp_woocommerce_session_* cookie —
  92. * that path sees a different (empty) session than the one the upload wrote to.
  93. *
  94. * @return array{attachment_id:int,file_record_id:int,file_name:string,thumb_url:string}|null
  95. */
  96. public static function resolve_uploaded_file(Studiou_WC_FPP_DB $db) {
  97. $attachment_id = 0;
  98. $file_record_id = 0;
  99. $file_name = '';
  100. $thumb_url = '';
  101. if (function_exists('WC') && WC()->session) {
  102. $attachment_id = (int) WC()->session->get('studiou_fpp_attachment_id');
  103. $file_record_id = (int) WC()->session->get('studiou_fpp_file_record_id');
  104. $file_name = (string) WC()->session->get('studiou_fpp_file_name');
  105. $thumb_url = (string) WC()->session->get('studiou_fpp_thumb_url');
  106. }
  107. if ($attachment_id <= 0 || $file_record_id <= 0) {
  108. $cookie_att = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT])
  109. ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT] : 0;
  110. $cookie_rec = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD])
  111. ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD] : 0;
  112. if ($cookie_att > 0 && $cookie_rec > 0) {
  113. $attachment_id = $cookie_att;
  114. $file_record_id = $cookie_rec;
  115. $file_name = '';
  116. $thumb_url = '';
  117. }
  118. }
  119. if ($attachment_id <= 0 || $file_record_id <= 0) {
  120. return null;
  121. }
  122. $record = $db->get_file_record($file_record_id);
  123. if (!$record) {
  124. return null;
  125. }
  126. if ((int) $record->attachment_id !== $attachment_id) {
  127. return null;
  128. }
  129. // A record already attached to an order is no longer "in flight" for add-to-cart
  130. if ((int) $record->order_id !== 0) {
  131. return null;
  132. }
  133. if ($file_name === '') {
  134. $file_name = (string) $record->file_name;
  135. }
  136. if ($thumb_url === '') {
  137. $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
  138. if ($thumb) {
  139. $thumb_url = $thumb[0];
  140. } else {
  141. $thumb_url = (string) wp_get_attachment_url($attachment_id);
  142. }
  143. }
  144. return array(
  145. 'attachment_id' => $attachment_id,
  146. 'file_record_id' => $file_record_id,
  147. 'file_name' => $file_name,
  148. 'thumb_url' => $thumb_url,
  149. );
  150. }
  151. /**
  152. * Safety net: if an FPP item was added without photo data, remove it.
  153. * This catches cases where validation was somehow bypassed.
  154. */
  155. public function check_after_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
  156. $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
  157. if (!$fpp_id) {
  158. return;
  159. }
  160. // If the cart item has no FPP attachment, remove it
  161. if (empty($cart_item_data['studiou_fpp_attachment_id'])) {
  162. WC()->cart->remove_cart_item($cart_item_key);
  163. wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
  164. }
  165. }
  166. public function ajax_set_upload_session() {
  167. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  168. $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
  169. $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
  170. $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : '';
  171. if (!$attachment_id || !$file_record_id) {
  172. wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product')));
  173. return;
  174. }
  175. if (WC()->session) {
  176. WC()->session->set('studiou_fpp_attachment_id', $attachment_id);
  177. WC()->session->set('studiou_fpp_file_record_id', $file_record_id);
  178. WC()->session->set('studiou_fpp_file_name', $file_name);
  179. // Store thumbnail URL directly so it can be used in cart without wp_get_attachment lookup
  180. $thumb_url = '';
  181. $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
  182. if ($thumb) {
  183. $thumb_url = $thumb[0];
  184. } else {
  185. $thumb_url = wp_get_attachment_url($attachment_id);
  186. }
  187. WC()->session->set('studiou_fpp_thumb_url', $thumb_url ?: '');
  188. }
  189. wp_send_json_success();
  190. }
  191. public function ajax_clear_upload_session() {
  192. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  193. if (WC()->session) {
  194. WC()->session->set('studiou_fpp_attachment_id', null);
  195. WC()->session->set('studiou_fpp_file_record_id', null);
  196. WC()->session->set('studiou_fpp_file_name', null);
  197. WC()->session->set('studiou_fpp_thumb_url', null);
  198. }
  199. Studiou_WC_FPP_Upload::clear_upload_cookies();
  200. wp_send_json_success();
  201. }
  202. }