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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $upload_info_text = get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
  30. $min_width = get_post_meta($product_id, '_studiou_fpp_min_width', true);
  31. $min_height = get_post_meta($product_id, '_studiou_fpp_min_height', true);
  32. $max_width = get_post_meta($product_id, '_studiou_fpp_max_width', true);
  33. $max_height = get_post_meta($product_id, '_studiou_fpp_max_height', true);
  34. if (empty($max_file_size)) {
  35. $max_file_size = 50;
  36. }
  37. $categories = get_terms(array(
  38. 'taxonomy' => 'studiou_media_category',
  39. 'hide_empty' => false,
  40. ));
  41. if (is_wp_error($categories)) {
  42. $categories = array();
  43. }
  44. ?>
  45. <div id="studiou_fpp_product_data" class="panel woocommerce_options_panel">
  46. <div class="options_group">
  47. <?php
  48. woocommerce_wp_checkbox(array(
  49. 'id' => '_studiou_fpp_enabled',
  50. 'label' => __('Enable Free Photo', 'studiou-wc-free-photo-product'),
  51. 'description' => __('Enable custom photo upload for this product', 'studiou-wc-free-photo-product'),
  52. 'value' => $enabled,
  53. ));
  54. ?>
  55. </div>
  56. <div class="options_group">
  57. <p class="form-field _studiou_fpp_media_category_field">
  58. <label for="_studiou_fpp_media_category"><?php esc_html_e('Media Category', 'studiou-wc-free-photo-product'); ?></label>
  59. <select id="_studiou_fpp_media_category" name="_studiou_fpp_media_category" class="select short">
  60. <option value=""><?php esc_html_e('— Select category —', 'studiou-wc-free-photo-product'); ?></option>
  61. <?php foreach ($categories as $cat) : ?>
  62. <option value="<?php echo esc_attr($cat->term_id); ?>" <?php selected($media_category, $cat->term_id); ?>>
  63. <?php echo esc_html($cat->name); ?>
  64. </option>
  65. <?php endforeach; ?>
  66. </select>
  67. <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>
  68. <?php echo wc_help_tip(__('Uploaded files will be assigned to this media category.', 'studiou-wc-free-photo-product')); ?>
  69. </p>
  70. <div class="studiou-fpp-new-media-cat-form" style="display:none;">
  71. <p class="form-field">
  72. <label for="studiou_fpp_new_cat_name"><?php esc_html_e('New Category Name', 'studiou-wc-free-photo-product'); ?></label>
  73. <input type="text" id="studiou_fpp_new_cat_name" class="short" />
  74. <button type="button" class="button studiou-fpp-save-media-cat-btn"><?php esc_html_e('Add', 'studiou-wc-free-photo-product'); ?></button>
  75. <button type="button" class="button studiou-fpp-cancel-media-cat-btn"><?php esc_html_e('Cancel', 'studiou-wc-free-photo-product'); ?></button>
  76. </p>
  77. </div>
  78. </div>
  79. <div class="options_group">
  80. <?php
  81. woocommerce_wp_text_input(array(
  82. 'id' => '_studiou_fpp_max_file_size',
  83. 'label' => __('Max File Size (MB)', 'studiou-wc-free-photo-product'),
  84. 'description' => __('Maximum allowed file size for uploads in megabytes.', 'studiou-wc-free-photo-product'),
  85. 'desc_tip' => true,
  86. 'type' => 'number',
  87. 'custom_attributes' => array(
  88. 'step' => '1',
  89. 'min' => '1',
  90. 'max' => '500',
  91. ),
  92. 'value' => $max_file_size,
  93. ));
  94. ?>
  95. </div>
  96. <div class="options_group">
  97. <p class="form-field">
  98. <label><?php esc_html_e('Min Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
  99. <span class="wrap">
  100. <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'); ?>" />
  101. &times;
  102. <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'); ?>" />
  103. </span>
  104. <?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')); ?>
  105. </p>
  106. <p class="form-field">
  107. <label><?php esc_html_e('Max Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
  108. <span class="wrap">
  109. <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'); ?>" />
  110. &times;
  111. <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'); ?>" />
  112. </span>
  113. <?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')); ?>
  114. </p>
  115. </div>
  116. <div class="options_group">
  117. <?php
  118. woocommerce_wp_textarea_input(array(
  119. 'id' => '_studiou_fpp_upload_info_text',
  120. 'label' => __('Upload Info Text', 'studiou-wc-free-photo-product'),
  121. 'description' => __('Custom text displayed above the upload area on the product page. Leave empty for default text.', 'studiou-wc-free-photo-product'),
  122. 'desc_tip' => true,
  123. 'value' => $upload_info_text,
  124. 'style' => 'height:80px;',
  125. ));
  126. ?>
  127. </div>
  128. <div class="options_group">
  129. <p class="form-field">
  130. <label><?php esc_html_e('Shortcode', 'studiou-wc-free-photo-product'); ?></label>
  131. <code id="studiou-fpp-shortcode-display">[studiou_free_photo_product id="<?php echo esc_attr($product_id); ?>"]</code>
  132. <?php echo wc_help_tip(__('Use this shortcode to display this product with photo upload on any page.', 'studiou-wc-free-photo-product')); ?>
  133. </p>
  134. </div>
  135. </div>
  136. <?php
  137. }
  138. public function save_product_meta($product_id) {
  139. $enabled = isset($_POST['_studiou_fpp_enabled']) ? 'yes' : 'no';
  140. update_post_meta($product_id, '_studiou_fpp_enabled', $enabled);
  141. if (isset($_POST['_studiou_fpp_media_category'])) {
  142. update_post_meta($product_id, '_studiou_fpp_media_category', absint($_POST['_studiou_fpp_media_category']));
  143. }
  144. if (isset($_POST['_studiou_fpp_max_file_size'])) {
  145. $max_size = absint($_POST['_studiou_fpp_max_file_size']);
  146. if ($max_size < 1) $max_size = 1;
  147. if ($max_size > 500) $max_size = 500;
  148. update_post_meta($product_id, '_studiou_fpp_max_file_size', $max_size);
  149. }
  150. if (isset($_POST['_studiou_fpp_upload_info_text'])) {
  151. update_post_meta($product_id, '_studiou_fpp_upload_info_text', sanitize_textarea_field($_POST['_studiou_fpp_upload_info_text']));
  152. }
  153. // Resolution limits (0 or empty = no limit)
  154. $res_fields = array('_studiou_fpp_min_width', '_studiou_fpp_min_height', '_studiou_fpp_max_width', '_studiou_fpp_max_height');
  155. foreach ($res_fields as $field) {
  156. $val = isset($_POST[$field]) ? absint($_POST[$field]) : 0;
  157. update_post_meta($product_id, $field, $val ?: '');
  158. }
  159. }
  160. public function ajax_add_media_category() {
  161. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  162. if (!current_user_can('manage_woocommerce')) {
  163. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  164. return;
  165. }
  166. $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
  167. if (empty($name)) {
  168. wp_send_json_error(array('message' => __('Category name is required.', 'studiou-wc-free-photo-product')));
  169. return;
  170. }
  171. $term = wp_insert_term($name, 'studiou_media_category');
  172. if (is_wp_error($term)) {
  173. wp_send_json_error(array('message' => $term->get_error_message()));
  174. return;
  175. }
  176. wp_send_json_success(array(
  177. 'term_id' => $term['term_id'],
  178. 'name' => $name,
  179. ));
  180. }
  181. public static function is_fpp_product($product_id) {
  182. return get_post_meta($product_id, '_studiou_fpp_enabled', true) === 'yes';
  183. }
  184. public static function get_product_max_file_size($product_id) {
  185. $size = get_post_meta($product_id, '_studiou_fpp_max_file_size', true);
  186. return !empty($size) ? (int) $size : 50;
  187. }
  188. public static function get_product_upload_info_text($product_id) {
  189. return get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
  190. }
  191. public static function get_product_media_category($product_id) {
  192. return (int) get_post_meta($product_id, '_studiou_fpp_media_category', true);
  193. }
  194. /**
  195. * Get resolution limits for a product.
  196. * Returns array with min_width, min_height, max_width, max_height (0 = no limit).
  197. */
  198. public static function get_product_resolution_limits($product_id) {
  199. return array(
  200. 'min_width' => (int) get_post_meta($product_id, '_studiou_fpp_min_width', true),
  201. 'min_height' => (int) get_post_meta($product_id, '_studiou_fpp_min_height', true),
  202. 'max_width' => (int) get_post_meta($product_id, '_studiou_fpp_max_width', true),
  203. 'max_height' => (int) get_post_meta($product_id, '_studiou_fpp_max_height', true),
  204. );
  205. }
  206. }