class-studiou-wc-fpp-product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_Product {
  6. public function __construct() {
  7. // Add product data tab
  8. add_filter('woocommerce_product_data_tabs', array($this, 'add_product_data_tab'));
  9. add_action('woocommerce_product_data_panels', array($this, 'add_product_data_panel'));
  10. add_action('woocommerce_process_product_meta', array($this, 'save_product_meta'));
  11. // AJAX for adding new media category from product edit page
  12. add_action('wp_ajax_studiou_wcfpp_add_media_category', array($this, 'ajax_add_media_category'));
  13. }
  14. public function add_product_data_tab($tabs) {
  15. $tabs['studiou_fpp'] = array(
  16. 'label' => __('Free Photo', 'studiou-wc-free-photo-product'),
  17. 'target' => 'studiou_fpp_product_data',
  18. 'class' => array('show_if_variable'),
  19. 'priority' => 80,
  20. );
  21. return $tabs;
  22. }
  23. public function add_product_data_panel() {
  24. global $post;
  25. $product_id = $post->ID;
  26. $enabled = get_post_meta($product_id, '_studiou_fpp_enabled', true);
  27. $media_category = get_post_meta($product_id, '_studiou_fpp_media_category', true);
  28. $max_file_size = get_post_meta($product_id, '_studiou_fpp_max_file_size', true);
  29. $max_uploads = get_post_meta($product_id, '_studiou_fpp_max_uploads', true);
  30. $upload_info_text = get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
  31. $min_width = get_post_meta($product_id, '_studiou_fpp_min_width', true);
  32. $min_height = get_post_meta($product_id, '_studiou_fpp_min_height', true);
  33. $max_width = get_post_meta($product_id, '_studiou_fpp_max_width', true);
  34. $max_height = get_post_meta($product_id, '_studiou_fpp_max_height', true);
  35. if (empty($max_file_size)) {
  36. $max_file_size = 50;
  37. }
  38. $categories = get_terms(array(
  39. 'taxonomy' => 'studiou_media_category',
  40. 'hide_empty' => false,
  41. ));
  42. if (is_wp_error($categories)) {
  43. $categories = array();
  44. }
  45. ?>
  46. <div id="studiou_fpp_product_data" class="panel woocommerce_options_panel">
  47. <div class="options_group">
  48. <?php
  49. woocommerce_wp_checkbox(array(
  50. 'id' => '_studiou_fpp_enabled',
  51. 'label' => __('Enable Free Photo', 'studiou-wc-free-photo-product'),
  52. 'description' => __('Enable custom photo upload for this product', 'studiou-wc-free-photo-product'),
  53. 'value' => $enabled,
  54. ));
  55. ?>
  56. </div>
  57. <div class="options_group">
  58. <p class="form-field _studiou_fpp_media_category_field">
  59. <label for="_studiou_fpp_media_category"><?php esc_html_e('Media Category', 'studiou-wc-free-photo-product'); ?></label>
  60. <select id="_studiou_fpp_media_category" name="_studiou_fpp_media_category" class="select short">
  61. <option value=""><?php esc_html_e('— Select category —', 'studiou-wc-free-photo-product'); ?></option>
  62. <?php foreach ($categories as $cat) : ?>
  63. <option value="<?php echo esc_attr($cat->term_id); ?>" <?php selected($media_category, $cat->term_id); ?>>
  64. <?php echo esc_html($cat->name); ?>
  65. </option>
  66. <?php endforeach; ?>
  67. </select>
  68. <button type="button" class="button studiou-fpp-add-media-cat-btn" title="<?php esc_attr_e('Add New Category', 'studiou-wc-free-photo-product'); ?>">+</button>
  69. <?php echo wc_help_tip(__('Uploaded files will be assigned to this media category.', 'studiou-wc-free-photo-product')); ?>
  70. </p>
  71. <div class="studiou-fpp-new-media-cat-form" style="display:none;">
  72. <p class="form-field">
  73. <label for="studiou_fpp_new_cat_name"><?php esc_html_e('New Category Name', 'studiou-wc-free-photo-product'); ?></label>
  74. <input type="text" id="studiou_fpp_new_cat_name" class="short" />
  75. <button type="button" class="button studiou-fpp-save-media-cat-btn"><?php esc_html_e('Add', 'studiou-wc-free-photo-product'); ?></button>
  76. <button type="button" class="button studiou-fpp-cancel-media-cat-btn"><?php esc_html_e('Cancel', 'studiou-wc-free-photo-product'); ?></button>
  77. </p>
  78. </div>
  79. </div>
  80. <div class="options_group">
  81. <?php
  82. woocommerce_wp_text_input(array(
  83. 'id' => '_studiou_fpp_max_file_size',
  84. 'label' => __('Max File Size (MB)', 'studiou-wc-free-photo-product'),
  85. 'description' => __('Maximum allowed file size for uploads in megabytes.', 'studiou-wc-free-photo-product'),
  86. 'desc_tip' => true,
  87. 'type' => 'number',
  88. 'custom_attributes' => array(
  89. 'step' => '1',
  90. 'min' => '1',
  91. 'max' => '500',
  92. ),
  93. 'value' => $max_file_size,
  94. ));
  95. woocommerce_wp_text_input(array(
  96. 'id' => '_studiou_fpp_max_uploads',
  97. 'label' => __('Max Uploads', 'studiou-wc-free-photo-product'),
  98. 'description' => __('Maximum number of photos a customer can upload for this product in a single visit. Leave empty or 0 for unlimited.', 'studiou-wc-free-photo-product'),
  99. 'desc_tip' => true,
  100. 'type' => 'number',
  101. 'custom_attributes' => array(
  102. 'step' => '1',
  103. 'min' => '0',
  104. ),
  105. 'value' => $max_uploads,
  106. ));
  107. ?>
  108. </div>
  109. <div class="options_group">
  110. <p class="form-field">
  111. <label><?php esc_html_e('Min Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
  112. <span class="wrap">
  113. <input type="number" id="_studiou_fpp_min_width" name="_studiou_fpp_min_width" class="short" style="width:80px;" min="0" step="1" value="<?php echo esc_attr($min_width); ?>" placeholder="<?php esc_attr_e('Width', 'studiou-wc-free-photo-product'); ?>" />
  114. &times;
  115. <input type="number" id="_studiou_fpp_min_height" name="_studiou_fpp_min_height" class="short" style="width:80px;" min="0" step="1" value="<?php echo esc_attr($min_height); ?>" placeholder="<?php esc_attr_e('Height', 'studiou-wc-free-photo-product'); ?>" />
  116. </span>
  117. <?php echo wc_help_tip(__('Minimum image resolution in pixels. Leave empty to skip. Width and height are interchangeable (portrait/landscape auto-detected).', 'studiou-wc-free-photo-product')); ?>
  118. </p>
  119. <p class="form-field">
  120. <label><?php esc_html_e('Max Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
  121. <span class="wrap">
  122. <input type="number" id="_studiou_fpp_max_width" name="_studiou_fpp_max_width" class="short" style="width:80px;" min="0" step="1" value="<?php echo esc_attr($max_width); ?>" placeholder="<?php esc_attr_e('Width', 'studiou-wc-free-photo-product'); ?>" />
  123. &times;
  124. <input type="number" id="_studiou_fpp_max_height" name="_studiou_fpp_max_height" class="short" style="width:80px;" min="0" step="1" value="<?php echo esc_attr($max_height); ?>" placeholder="<?php esc_attr_e('Height', 'studiou-wc-free-photo-product'); ?>" />
  125. </span>
  126. <?php echo wc_help_tip(__('Maximum image resolution in pixels. Leave empty to skip. Width and height are interchangeable (portrait/landscape auto-detected).', 'studiou-wc-free-photo-product')); ?>
  127. </p>
  128. </div>
  129. <div class="options_group">
  130. <?php
  131. woocommerce_wp_textarea_input(array(
  132. 'id' => '_studiou_fpp_upload_info_text',
  133. 'label' => __('Upload Info Text', 'studiou-wc-free-photo-product'),
  134. 'description' => __('Custom text displayed above the upload area on the product page. Leave empty for default text.', 'studiou-wc-free-photo-product'),
  135. 'desc_tip' => true,
  136. 'value' => $upload_info_text,
  137. 'style' => 'height:80px;',
  138. ));
  139. ?>
  140. </div>
  141. <div class="options_group">
  142. <p class="form-field">
  143. <label><?php esc_html_e('Shortcode', 'studiou-wc-free-photo-product'); ?></label>
  144. <code id="studiou-fpp-shortcode-display">[studiou_free_photo_product id="<?php echo esc_attr($product_id); ?>"]</code>
  145. <?php echo wc_help_tip(__('Use this shortcode to display this product with photo upload on any page.', 'studiou-wc-free-photo-product')); ?>
  146. </p>
  147. </div>
  148. </div>
  149. <?php
  150. }
  151. public function save_product_meta($product_id) {
  152. $enabled = isset($_POST['_studiou_fpp_enabled']) ? 'yes' : 'no';
  153. update_post_meta($product_id, '_studiou_fpp_enabled', $enabled);
  154. if (isset($_POST['_studiou_fpp_media_category'])) {
  155. update_post_meta($product_id, '_studiou_fpp_media_category', absint($_POST['_studiou_fpp_media_category']));
  156. }
  157. if (isset($_POST['_studiou_fpp_max_file_size'])) {
  158. $max_size = absint($_POST['_studiou_fpp_max_file_size']);
  159. if ($max_size < 1) $max_size = 1;
  160. if ($max_size > 500) $max_size = 500;
  161. update_post_meta($product_id, '_studiou_fpp_max_file_size', $max_size);
  162. }
  163. $max_uploads = isset($_POST['_studiou_fpp_max_uploads']) ? absint($_POST['_studiou_fpp_max_uploads']) : 0;
  164. update_post_meta($product_id, '_studiou_fpp_max_uploads', $max_uploads);
  165. if (isset($_POST['_studiou_fpp_upload_info_text'])) {
  166. update_post_meta($product_id, '_studiou_fpp_upload_info_text', sanitize_textarea_field($_POST['_studiou_fpp_upload_info_text']));
  167. }
  168. // Resolution limits (0 or empty = no limit)
  169. $res_fields = array('_studiou_fpp_min_width', '_studiou_fpp_min_height', '_studiou_fpp_max_width', '_studiou_fpp_max_height');
  170. foreach ($res_fields as $field) {
  171. $val = isset($_POST[$field]) ? absint($_POST[$field]) : 0;
  172. update_post_meta($product_id, $field, $val ?: '');
  173. }
  174. }
  175. public function ajax_add_media_category() {
  176. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  177. if (!current_user_can('manage_woocommerce')) {
  178. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  179. return;
  180. }
  181. $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
  182. if (empty($name)) {
  183. wp_send_json_error(array('message' => __('Category name is required.', 'studiou-wc-free-photo-product')));
  184. return;
  185. }
  186. $term = wp_insert_term($name, 'studiou_media_category');
  187. if (is_wp_error($term)) {
  188. wp_send_json_error(array('message' => $term->get_error_message()));
  189. return;
  190. }
  191. wp_send_json_success(array(
  192. 'term_id' => $term['term_id'],
  193. 'name' => $name,
  194. ));
  195. }
  196. public static function is_fpp_product($product_id) {
  197. return get_post_meta($product_id, '_studiou_fpp_enabled', true) === 'yes';
  198. }
  199. public static function get_product_max_file_size($product_id) {
  200. $size = get_post_meta($product_id, '_studiou_fpp_max_file_size', true);
  201. return !empty($size) ? (int) $size : 50;
  202. }
  203. /**
  204. * Max simultaneous uploads per batch for this product. 0 = unlimited.
  205. */
  206. public static function get_product_max_uploads($product_id) {
  207. $val = get_post_meta($product_id, '_studiou_fpp_max_uploads', true);
  208. return (int) $val;
  209. }
  210. public static function get_product_upload_info_text($product_id) {
  211. return get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
  212. }
  213. public static function get_product_media_category($product_id) {
  214. return (int) get_post_meta($product_id, '_studiou_fpp_media_category', true);
  215. }
  216. /**
  217. * Get resolution limits for a product.
  218. * Returns array with min_width, min_height, max_width, max_height (0 = no limit).
  219. */
  220. public static function get_product_resolution_limits($product_id) {
  221. return array(
  222. 'min_width' => (int) get_post_meta($product_id, '_studiou_fpp_min_width', true),
  223. 'min_height' => (int) get_post_meta($product_id, '_studiou_fpp_min_height', true),
  224. 'max_width' => (int) get_post_meta($product_id, '_studiou_fpp_max_width', true),
  225. 'max_height' => (int) get_post_meta($product_id, '_studiou_fpp_max_height', true),
  226. );
  227. }
  228. }