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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * Return all upload records belonging to the current visitor's batch
  87. * that are not yet attached to an order, optionally scoped to a product.
  88. *
  89. * @return array<int,object> list of DB rows, oldest first (ascending id)
  90. */
  91. public static function resolve_batch_files(Studiou_WC_FPP_DB $db, $product_id = 0) {
  92. $token = Studiou_WC_FPP_Upload::get_batch_token();
  93. if (empty($token)) {
  94. return array();
  95. }
  96. return $db->get_batch_files($token, (int) $product_id);
  97. }
  98. /**
  99. * Resolve the currently "attached" upload for this visitor (legacy single-file path).
  100. * Checks WC session first, then legacy cookie pair, and finally the first file in the
  101. * current batch as a last-resort fallback. Used only by the shortcode flow on 1.5+ —
  102. * the product-detail page uses resolve_batch_files() and pre-stamped file_record_id.
  103. *
  104. * @return array{attachment_id:int,file_record_id:int,file_name:string,thumb_url:string}|null
  105. */
  106. public static function resolve_uploaded_file(Studiou_WC_FPP_DB $db) {
  107. $attachment_id = 0;
  108. $file_record_id = 0;
  109. $file_name = '';
  110. $thumb_url = '';
  111. if (function_exists('WC') && WC()->session) {
  112. $attachment_id = (int) WC()->session->get('studiou_fpp_attachment_id');
  113. $file_record_id = (int) WC()->session->get('studiou_fpp_file_record_id');
  114. $file_name = (string) WC()->session->get('studiou_fpp_file_name');
  115. $thumb_url = (string) WC()->session->get('studiou_fpp_thumb_url');
  116. }
  117. if ($attachment_id <= 0 || $file_record_id <= 0) {
  118. $cookie_att = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT])
  119. ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_ATTACHMENT] : 0;
  120. $cookie_rec = isset($_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD])
  121. ? (int) $_COOKIE[Studiou_WC_FPP_Upload::COOKIE_RECORD] : 0;
  122. if ($cookie_att > 0 && $cookie_rec > 0) {
  123. $attachment_id = $cookie_att;
  124. $file_record_id = $cookie_rec;
  125. $file_name = '';
  126. $thumb_url = '';
  127. }
  128. }
  129. if ($attachment_id <= 0 || $file_record_id <= 0) {
  130. return null;
  131. }
  132. $record = $db->get_file_record($file_record_id);
  133. if (!$record) {
  134. return null;
  135. }
  136. if ((int) $record->attachment_id !== $attachment_id) {
  137. return null;
  138. }
  139. // A record already attached to an order is no longer "in flight" for add-to-cart
  140. if ((int) $record->order_id !== 0) {
  141. return null;
  142. }
  143. if ($file_name === '') {
  144. $file_name = (string) $record->file_name;
  145. }
  146. if ($thumb_url === '') {
  147. $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
  148. if ($thumb) {
  149. $thumb_url = $thumb[0];
  150. } else {
  151. $thumb_url = (string) wp_get_attachment_url($attachment_id);
  152. }
  153. }
  154. return array(
  155. 'attachment_id' => $attachment_id,
  156. 'file_record_id' => $file_record_id,
  157. 'file_name' => $file_name,
  158. 'thumb_url' => $thumb_url,
  159. );
  160. }
  161. /**
  162. * Safety net: if an FPP item was added without photo data, remove it.
  163. * This catches cases where validation was somehow bypassed.
  164. */
  165. public function check_after_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
  166. $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
  167. if (!$fpp_id) {
  168. return;
  169. }
  170. // If the cart item has no FPP attachment, remove it
  171. if (empty($cart_item_data['studiou_fpp_attachment_id'])) {
  172. WC()->cart->remove_cart_item($cart_item_key);
  173. wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
  174. }
  175. }
  176. public function ajax_set_upload_session() {
  177. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  178. $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
  179. $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
  180. $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : '';
  181. if (!$attachment_id || !$file_record_id) {
  182. wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product')));
  183. return;
  184. }
  185. if (WC()->session) {
  186. WC()->session->set('studiou_fpp_attachment_id', $attachment_id);
  187. WC()->session->set('studiou_fpp_file_record_id', $file_record_id);
  188. WC()->session->set('studiou_fpp_file_name', $file_name);
  189. // Store thumbnail URL directly so it can be used in cart without wp_get_attachment lookup
  190. $thumb_url = '';
  191. $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
  192. if ($thumb) {
  193. $thumb_url = $thumb[0];
  194. } else {
  195. $thumb_url = wp_get_attachment_url($attachment_id);
  196. }
  197. WC()->session->set('studiou_fpp_thumb_url', $thumb_url ?: '');
  198. }
  199. wp_send_json_success();
  200. }
  201. public function ajax_clear_upload_session() {
  202. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  203. if (WC()->session) {
  204. WC()->session->set('studiou_fpp_attachment_id', null);
  205. WC()->session->set('studiou_fpp_file_record_id', null);
  206. WC()->session->set('studiou_fpp_file_name', null);
  207. WC()->session->set('studiou_fpp_thumb_url', null);
  208. }
  209. Studiou_WC_FPP_Upload::clear_upload_cookies();
  210. wp_send_json_success();
  211. }
  212. }