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

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