| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_FPP_Product {
- public function __construct() {
- // Add product data tab
- add_filter('woocommerce_product_data_tabs', array($this, 'add_product_data_tab'));
- add_action('woocommerce_product_data_panels', array($this, 'add_product_data_panel'));
- add_action('woocommerce_process_product_meta', array($this, 'save_product_meta'));
- // AJAX for adding new media category from product edit page
- add_action('wp_ajax_studiou_wcfpp_add_media_category', array($this, 'ajax_add_media_category'));
- }
- public function add_product_data_tab($tabs) {
- $tabs['studiou_fpp'] = array(
- 'label' => __('Free Photo', 'studiou-wc-free-photo-product'),
- 'target' => 'studiou_fpp_product_data',
- 'class' => array('show_if_variable'),
- 'priority' => 80,
- );
- return $tabs;
- }
- public function add_product_data_panel() {
- global $post;
- $product_id = $post->ID;
- $enabled = get_post_meta($product_id, '_studiou_fpp_enabled', true);
- $media_category = get_post_meta($product_id, '_studiou_fpp_media_category', true);
- $max_file_size = get_post_meta($product_id, '_studiou_fpp_max_file_size', true);
- $max_uploads = get_post_meta($product_id, '_studiou_fpp_max_uploads', true);
- $upload_info_text = get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
- $min_width = get_post_meta($product_id, '_studiou_fpp_min_width', true);
- $min_height = get_post_meta($product_id, '_studiou_fpp_min_height', true);
- $max_width = get_post_meta($product_id, '_studiou_fpp_max_width', true);
- $max_height = get_post_meta($product_id, '_studiou_fpp_max_height', true);
- if (empty($max_file_size)) {
- $max_file_size = 50;
- }
- $categories = get_terms(array(
- 'taxonomy' => 'studiou_media_category',
- 'hide_empty' => false,
- ));
- if (is_wp_error($categories)) {
- $categories = array();
- }
- ?>
- <div id="studiou_fpp_product_data" class="panel woocommerce_options_panel">
- <div class="options_group">
- <?php
- woocommerce_wp_checkbox(array(
- 'id' => '_studiou_fpp_enabled',
- 'label' => __('Enable Free Photo', 'studiou-wc-free-photo-product'),
- 'description' => __('Enable custom photo upload for this product', 'studiou-wc-free-photo-product'),
- 'value' => $enabled,
- ));
- ?>
- </div>
- <div class="options_group">
- <p class="form-field _studiou_fpp_media_category_field">
- <label for="_studiou_fpp_media_category"><?php esc_html_e('Media Category', 'studiou-wc-free-photo-product'); ?></label>
- <select id="_studiou_fpp_media_category" name="_studiou_fpp_media_category" class="select short">
- <option value=""><?php esc_html_e('— Select category —', 'studiou-wc-free-photo-product'); ?></option>
- <?php foreach ($categories as $cat) : ?>
- <option value="<?php echo esc_attr($cat->term_id); ?>" <?php selected($media_category, $cat->term_id); ?>>
- <?php echo esc_html($cat->name); ?>
- </option>
- <?php endforeach; ?>
- </select>
- <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>
- <?php echo wc_help_tip(__('Uploaded files will be assigned to this media category.', 'studiou-wc-free-photo-product')); ?>
- </p>
- <div class="studiou-fpp-new-media-cat-form" style="display:none;">
- <p class="form-field">
- <label for="studiou_fpp_new_cat_name"><?php esc_html_e('New Category Name', 'studiou-wc-free-photo-product'); ?></label>
- <input type="text" id="studiou_fpp_new_cat_name" class="short" />
- <button type="button" class="button studiou-fpp-save-media-cat-btn"><?php esc_html_e('Add', 'studiou-wc-free-photo-product'); ?></button>
- <button type="button" class="button studiou-fpp-cancel-media-cat-btn"><?php esc_html_e('Cancel', 'studiou-wc-free-photo-product'); ?></button>
- </p>
- </div>
- </div>
- <div class="options_group">
- <?php
- woocommerce_wp_text_input(array(
- 'id' => '_studiou_fpp_max_file_size',
- 'label' => __('Max File Size (MB)', 'studiou-wc-free-photo-product'),
- 'description' => __('Maximum allowed file size for uploads in megabytes.', 'studiou-wc-free-photo-product'),
- 'desc_tip' => true,
- 'type' => 'number',
- 'custom_attributes' => array(
- 'step' => '1',
- 'min' => '1',
- 'max' => '500',
- ),
- 'value' => $max_file_size,
- ));
- woocommerce_wp_text_input(array(
- 'id' => '_studiou_fpp_max_uploads',
- 'label' => __('Max Uploads', 'studiou-wc-free-photo-product'),
- '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'),
- 'desc_tip' => true,
- 'type' => 'number',
- 'custom_attributes' => array(
- 'step' => '1',
- 'min' => '0',
- ),
- 'value' => $max_uploads,
- ));
- ?>
- </div>
- <div class="options_group">
- <p class="form-field">
- <label><?php esc_html_e('Min Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
- <span class="wrap">
- <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'); ?>" />
- ×
- <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'); ?>" />
- </span>
- <?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')); ?>
- </p>
- <p class="form-field">
- <label><?php esc_html_e('Max Image Resolution (px)', 'studiou-wc-free-photo-product'); ?></label>
- <span class="wrap">
- <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'); ?>" />
- ×
- <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'); ?>" />
- </span>
- <?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')); ?>
- </p>
- </div>
- <div class="options_group">
- <?php
- woocommerce_wp_textarea_input(array(
- 'id' => '_studiou_fpp_upload_info_text',
- 'label' => __('Upload Info Text', 'studiou-wc-free-photo-product'),
- 'description' => __('Custom text displayed above the upload area on the product page. Leave empty for default text.', 'studiou-wc-free-photo-product'),
- 'desc_tip' => true,
- 'value' => $upload_info_text,
- 'style' => 'height:80px;',
- ));
- ?>
- </div>
- <div class="options_group">
- <p class="form-field">
- <label><?php esc_html_e('Shortcode', 'studiou-wc-free-photo-product'); ?></label>
- <code id="studiou-fpp-shortcode-display">[studiou_free_photo_product id="<?php echo esc_attr($product_id); ?>"]</code>
- <?php echo wc_help_tip(__('Use this shortcode to display this product with photo upload on any page.', 'studiou-wc-free-photo-product')); ?>
- </p>
- </div>
- </div>
- <?php
- }
- public function save_product_meta($product_id) {
- $enabled = isset($_POST['_studiou_fpp_enabled']) ? 'yes' : 'no';
- update_post_meta($product_id, '_studiou_fpp_enabled', $enabled);
- if (isset($_POST['_studiou_fpp_media_category'])) {
- update_post_meta($product_id, '_studiou_fpp_media_category', absint($_POST['_studiou_fpp_media_category']));
- }
- if (isset($_POST['_studiou_fpp_max_file_size'])) {
- $max_size = absint($_POST['_studiou_fpp_max_file_size']);
- if ($max_size < 1) $max_size = 1;
- if ($max_size > 500) $max_size = 500;
- update_post_meta($product_id, '_studiou_fpp_max_file_size', $max_size);
- }
- $max_uploads = isset($_POST['_studiou_fpp_max_uploads']) ? absint($_POST['_studiou_fpp_max_uploads']) : 0;
- update_post_meta($product_id, '_studiou_fpp_max_uploads', $max_uploads);
- if (isset($_POST['_studiou_fpp_upload_info_text'])) {
- update_post_meta($product_id, '_studiou_fpp_upload_info_text', sanitize_textarea_field($_POST['_studiou_fpp_upload_info_text']));
- }
- // Resolution limits (0 or empty = no limit)
- $res_fields = array('_studiou_fpp_min_width', '_studiou_fpp_min_height', '_studiou_fpp_max_width', '_studiou_fpp_max_height');
- foreach ($res_fields as $field) {
- $val = isset($_POST[$field]) ? absint($_POST[$field]) : 0;
- update_post_meta($product_id, $field, $val ?: '');
- }
- }
- public function ajax_add_media_category() {
- check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
- if (!current_user_can('manage_woocommerce')) {
- wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
- return;
- }
- $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
- if (empty($name)) {
- wp_send_json_error(array('message' => __('Category name is required.', 'studiou-wc-free-photo-product')));
- return;
- }
- $term = wp_insert_term($name, 'studiou_media_category');
- if (is_wp_error($term)) {
- wp_send_json_error(array('message' => $term->get_error_message()));
- return;
- }
- wp_send_json_success(array(
- 'term_id' => $term['term_id'],
- 'name' => $name,
- ));
- }
- public static function is_fpp_product($product_id) {
- return get_post_meta($product_id, '_studiou_fpp_enabled', true) === 'yes';
- }
- public static function get_product_max_file_size($product_id) {
- $size = get_post_meta($product_id, '_studiou_fpp_max_file_size', true);
- return !empty($size) ? (int) $size : 50;
- }
- /**
- * Max simultaneous uploads per batch for this product. 0 = unlimited.
- */
- public static function get_product_max_uploads($product_id) {
- $val = get_post_meta($product_id, '_studiou_fpp_max_uploads', true);
- return (int) $val;
- }
- public static function get_product_upload_info_text($product_id) {
- return get_post_meta($product_id, '_studiou_fpp_upload_info_text', true);
- }
- public static function get_product_media_category($product_id) {
- return (int) get_post_meta($product_id, '_studiou_fpp_media_category', true);
- }
- /**
- * Get resolution limits for a product.
- * Returns array with min_width, min_height, max_width, max_height (0 = no limit).
- */
- public static function get_product_resolution_limits($product_id) {
- return array(
- 'min_width' => (int) get_post_meta($product_id, '_studiou_fpp_min_width', true),
- 'min_height' => (int) get_post_meta($product_id, '_studiou_fpp_min_height', true),
- 'max_width' => (int) get_post_meta($product_id, '_studiou_fpp_max_width', true),
- 'max_height' => (int) get_post_meta($product_id, '_studiou_fpp_max_height', true),
- );
- }
- }
|