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

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