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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_Single_Product {
  6. public function __construct() {
  7. // Render upload area below the variation table on single product page
  8. add_action('woocommerce_single_product_summary', array($this, 'render_upload_area'), 35);
  9. // Server-side validation: block add-to-cart without photo (priority 1 = very early)
  10. add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_to_cart'), 1, 6);
  11. // Safety net: if item was added without photo, remove it immediately
  12. add_action('woocommerce_add_to_cart', array($this, 'check_after_add_to_cart'), 10, 6);
  13. // AJAX: store uploaded file reference in WC session
  14. add_action('wp_ajax_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
  15. add_action('wp_ajax_nopriv_studiou_wcfpp_set_upload_session', array($this, 'ajax_set_upload_session'));
  16. add_action('wp_ajax_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
  17. add_action('wp_ajax_nopriv_studiou_wcfpp_clear_upload_session', array($this, 'ajax_clear_upload_session'));
  18. }
  19. public function render_upload_area() {
  20. global $product;
  21. if (!$product || !Studiou_WC_FPP_Product::is_fpp_product($product->get_id())) {
  22. return;
  23. }
  24. wp_enqueue_style('studiou-wcfpp-frontend');
  25. wp_enqueue_script('studiou-wcfpp-frontend');
  26. $product_id = $product->get_id();
  27. $max_file_size = Studiou_WC_FPP_Product::get_product_max_file_size($product_id);
  28. // Pass existing session state to JS for restore on page load
  29. $session_data = array();
  30. if (WC()->session) {
  31. $att_id = WC()->session->get('studiou_fpp_attachment_id');
  32. if ($att_id) {
  33. $thumb = wp_get_attachment_image_src($att_id, 'thumbnail');
  34. $preview = wp_get_attachment_image_src($att_id, 'woocommerce_single');
  35. $session_data = array(
  36. 'attachment_id' => $att_id,
  37. 'file_record_id' => WC()->session->get('studiou_fpp_file_record_id'),
  38. 'file_name' => WC()->session->get('studiou_fpp_file_name'),
  39. 'thumbnail_url' => $thumb ? $thumb[0] : '',
  40. 'preview_url' => $preview ? $preview[0] : ($thumb ? $thumb[0] : ''),
  41. );
  42. }
  43. }
  44. wp_localize_script('studiou-wcfpp-frontend', 'studiouWcfppSession', $session_data);
  45. include STUDIOU_WCFPP_PLUGIN_DIR . 'views/single-product-upload.php';
  46. }
  47. /**
  48. * Resolve whether a product_id or its parent is FPP-enabled.
  49. */
  50. private function resolve_fpp_product_id($product_id, $variation_id = 0) {
  51. // Check direct product
  52. if (Studiou_WC_FPP_Product::is_fpp_product($product_id)) {
  53. return $product_id;
  54. }
  55. // Check parent of the product (in case product_id is variation)
  56. $product = wc_get_product($product_id);
  57. if ($product && $product->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($product->get_parent_id())) {
  58. return $product->get_parent_id();
  59. }
  60. // Check variation_id's parent
  61. if ($variation_id && $variation_id !== $product_id) {
  62. $variation = wc_get_product($variation_id);
  63. if ($variation && $variation->get_parent_id() && Studiou_WC_FPP_Product::is_fpp_product($variation->get_parent_id())) {
  64. return $variation->get_parent_id();
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * Validate before add-to-cart: block if FPP product and no photo in session.
  71. */
  72. public function validate_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array()) {
  73. $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
  74. if (!$fpp_id) {
  75. return $passed;
  76. }
  77. // Check WC session for uploaded file
  78. if (WC()->session) {
  79. $attachment_id = WC()->session->get('studiou_fpp_attachment_id');
  80. if ($attachment_id) {
  81. return $passed;
  82. }
  83. }
  84. wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
  85. return false;
  86. }
  87. /**
  88. * Safety net: if an FPP item was added without photo data, remove it.
  89. * This catches cases where validation was somehow bypassed.
  90. */
  91. public function check_after_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
  92. $fpp_id = $this->resolve_fpp_product_id($product_id, $variation_id);
  93. if (!$fpp_id) {
  94. return;
  95. }
  96. // If the cart item has no FPP attachment, remove it
  97. if (empty($cart_item_data['studiou_fpp_attachment_id'])) {
  98. WC()->cart->remove_cart_item($cart_item_key);
  99. wc_add_notice(__('Please upload a photo before adding to cart.', 'studiou-wc-free-photo-product'), 'error');
  100. }
  101. }
  102. public function ajax_set_upload_session() {
  103. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  104. $attachment_id = isset($_POST['attachment_id']) ? absint($_POST['attachment_id']) : 0;
  105. $file_record_id = isset($_POST['file_record_id']) ? absint($_POST['file_record_id']) : 0;
  106. $file_name = isset($_POST['file_name']) ? sanitize_text_field($_POST['file_name']) : '';
  107. if (!$attachment_id || !$file_record_id) {
  108. wp_send_json_error(array('message' => __('Invalid file data.', 'studiou-wc-free-photo-product')));
  109. return;
  110. }
  111. if (WC()->session) {
  112. WC()->session->set('studiou_fpp_attachment_id', $attachment_id);
  113. WC()->session->set('studiou_fpp_file_record_id', $file_record_id);
  114. WC()->session->set('studiou_fpp_file_name', $file_name);
  115. // Store thumbnail URL directly so it can be used in cart without wp_get_attachment lookup
  116. $thumb_url = '';
  117. $thumb = wp_get_attachment_image_src($attachment_id, 'thumbnail');
  118. if ($thumb) {
  119. $thumb_url = $thumb[0];
  120. } else {
  121. $thumb_url = wp_get_attachment_url($attachment_id);
  122. }
  123. WC()->session->set('studiou_fpp_thumb_url', $thumb_url ?: '');
  124. }
  125. wp_send_json_success();
  126. }
  127. public function ajax_clear_upload_session() {
  128. check_ajax_referer('studiou-wcfpp-front-nonce', 'nonce');
  129. if (WC()->session) {
  130. WC()->session->set('studiou_fpp_attachment_id', null);
  131. WC()->session->set('studiou_fpp_file_record_id', null);
  132. WC()->session->set('studiou_fpp_file_name', null);
  133. }
  134. wp_send_json_success();
  135. }
  136. }