308 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			308 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
if (!defined('ABSPATH')) {
 | 
						|
    exit;
 | 
						|
}
 | 
						|
 | 
						|
class WPDD_Metaboxes {
 | 
						|
    
 | 
						|
    public static function init() {
 | 
						|
        add_action('add_meta_boxes', array(__CLASS__, 'add_meta_boxes'));
 | 
						|
        add_action('save_post_wpdd_product', array(__CLASS__, 'save_product_meta'), 10, 2);
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function add_meta_boxes() {
 | 
						|
        add_meta_box(
 | 
						|
            'wpdd_product_pricing',
 | 
						|
            __('Product Pricing', 'wp-digital-download'),
 | 
						|
            array(__CLASS__, 'render_pricing_metabox'),
 | 
						|
            'wpdd_product',
 | 
						|
            'side',
 | 
						|
            'high'
 | 
						|
        );
 | 
						|
        
 | 
						|
        add_meta_box(
 | 
						|
            'wpdd_product_files',
 | 
						|
            __('Downloadable Files', 'wp-digital-download'),
 | 
						|
            array(__CLASS__, 'render_files_metabox'),
 | 
						|
            'wpdd_product',
 | 
						|
            'normal',
 | 
						|
            'high'
 | 
						|
        );
 | 
						|
        
 | 
						|
        add_meta_box(
 | 
						|
            'wpdd_product_settings',
 | 
						|
            __('Product Settings', 'wp-digital-download'),
 | 
						|
            array(__CLASS__, 'render_settings_metabox'),
 | 
						|
            'wpdd_product',
 | 
						|
            'normal',
 | 
						|
            'default'
 | 
						|
        );
 | 
						|
        
 | 
						|
        add_meta_box(
 | 
						|
            'wpdd_product_stats',
 | 
						|
            __('Product Statistics', 'wp-digital-download'),
 | 
						|
            array(__CLASS__, 'render_stats_metabox'),
 | 
						|
            'wpdd_product',
 | 
						|
            'side',
 | 
						|
            'low'
 | 
						|
        );
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function render_pricing_metabox($post) {
 | 
						|
        wp_nonce_field('wpdd_save_product_meta', 'wpdd_product_meta_nonce');
 | 
						|
        
 | 
						|
        $price = get_post_meta($post->ID, '_wpdd_price', true);
 | 
						|
        $is_free = get_post_meta($post->ID, '_wpdd_is_free', true);
 | 
						|
        $sale_price = get_post_meta($post->ID, '_wpdd_sale_price', true);
 | 
						|
        ?>
 | 
						|
        <div class="wpdd-metabox-content">
 | 
						|
            <p>
 | 
						|
                <label>
 | 
						|
                    <input type="checkbox" name="wpdd_is_free" id="wpdd_is_free" value="1" <?php checked($is_free, '1'); ?> />
 | 
						|
                    <?php _e('This is a free product', 'wp-digital-download'); ?>
 | 
						|
                </label>
 | 
						|
            </p>
 | 
						|
            
 | 
						|
            <p class="wpdd-price-field">
 | 
						|
                <label for="wpdd_price"><?php _e('Regular Price', 'wp-digital-download'); ?> ($)</label>
 | 
						|
                <input type="number" id="wpdd_price" name="wpdd_price" value="<?php echo esc_attr($price); ?>" step="0.01" min="0" />
 | 
						|
            </p>
 | 
						|
            
 | 
						|
            <p class="wpdd-price-field">
 | 
						|
                <label for="wpdd_sale_price"><?php _e('Sale Price', 'wp-digital-download'); ?> ($)</label>
 | 
						|
                <input type="number" id="wpdd_sale_price" name="wpdd_sale_price" value="<?php echo esc_attr($sale_price); ?>" step="0.01" min="0" />
 | 
						|
                <span class="description"><?php _e('Leave blank for no sale', 'wp-digital-download'); ?></span>
 | 
						|
            </p>
 | 
						|
        </div>
 | 
						|
        <?php
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function render_files_metabox($post) {
 | 
						|
        $files = get_post_meta($post->ID, '_wpdd_files', true);
 | 
						|
        if (!is_array($files)) {
 | 
						|
            $files = array();
 | 
						|
        }
 | 
						|
        ?>
 | 
						|
        <div class="wpdd-files-container">
 | 
						|
            <div id="wpdd-files-list">
 | 
						|
                <?php if (!empty($files)) : ?>
 | 
						|
                    <?php foreach ($files as $index => $file) : ?>
 | 
						|
                        <div class="wpdd-file-item" data-index="<?php echo $index; ?>">
 | 
						|
                            <div class="wpdd-file-header">
 | 
						|
                                <span class="wpdd-file-handle dashicons dashicons-menu"></span>
 | 
						|
                                <input type="text" name="wpdd_files[<?php echo $index; ?>][name]" 
 | 
						|
                                       value="<?php echo esc_attr($file['name']); ?>" 
 | 
						|
                                       placeholder="<?php _e('File Name', 'wp-digital-download'); ?>" />
 | 
						|
                                <button type="button" class="button wpdd-remove-file">
 | 
						|
                                    <?php _e('Remove', 'wp-digital-download'); ?>
 | 
						|
                                </button>
 | 
						|
                            </div>
 | 
						|
                            <div class="wpdd-file-content">
 | 
						|
                                <div class="wpdd-file-url">
 | 
						|
                                    <input type="text" name="wpdd_files[<?php echo $index; ?>][url]" 
 | 
						|
                                           value="<?php echo esc_url($file['url']); ?>" 
 | 
						|
                                           placeholder="<?php _e('File URL', 'wp-digital-download'); ?>" 
 | 
						|
                                           class="wpdd-file-url-input" />
 | 
						|
                                    <button type="button" class="button wpdd-upload-file">
 | 
						|
                                        <?php _e('Upload File', 'wp-digital-download'); ?>
 | 
						|
                                    </button>
 | 
						|
                                </div>
 | 
						|
                                <input type="hidden" name="wpdd_files[<?php echo $index; ?>][id]" 
 | 
						|
                                       value="<?php echo esc_attr($file['id']); ?>" 
 | 
						|
                                       class="wpdd-file-id" />
 | 
						|
                            </div>
 | 
						|
                        </div>
 | 
						|
                    <?php endforeach; ?>
 | 
						|
                <?php endif; ?>
 | 
						|
            </div>
 | 
						|
            
 | 
						|
            <button type="button" id="wpdd-add-file" class="button button-primary">
 | 
						|
                <?php _e('Add File', 'wp-digital-download'); ?>
 | 
						|
            </button>
 | 
						|
            
 | 
						|
            <template id="wpdd-file-template">
 | 
						|
                <div class="wpdd-file-item" data-index="">
 | 
						|
                    <div class="wpdd-file-header">
 | 
						|
                        <span class="wpdd-file-handle dashicons dashicons-menu"></span>
 | 
						|
                        <input type="text" name="wpdd_files[INDEX][name]" 
 | 
						|
                               placeholder="<?php _e('File Name', 'wp-digital-download'); ?>" />
 | 
						|
                        <button type="button" class="button wpdd-remove-file">
 | 
						|
                            <?php _e('Remove', 'wp-digital-download'); ?>
 | 
						|
                        </button>
 | 
						|
                    </div>
 | 
						|
                    <div class="wpdd-file-content">
 | 
						|
                        <div class="wpdd-file-url">
 | 
						|
                            <input type="text" name="wpdd_files[INDEX][url]" 
 | 
						|
                                   placeholder="<?php _e('File URL', 'wp-digital-download'); ?>" 
 | 
						|
                                   class="wpdd-file-url-input" />
 | 
						|
                            <button type="button" class="button wpdd-upload-file">
 | 
						|
                                <?php _e('Upload File', 'wp-digital-download'); ?>
 | 
						|
                            </button>
 | 
						|
                        </div>
 | 
						|
                        <input type="hidden" name="wpdd_files[INDEX][id]" class="wpdd-file-id" />
 | 
						|
                    </div>
 | 
						|
                </div>
 | 
						|
            </template>
 | 
						|
        </div>
 | 
						|
        <?php
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function render_settings_metabox($post) {
 | 
						|
        $download_limit = get_post_meta($post->ID, '_wpdd_download_limit', true);
 | 
						|
        $download_expiry = get_post_meta($post->ID, '_wpdd_download_expiry', true);
 | 
						|
        $enable_watermark = get_post_meta($post->ID, '_wpdd_enable_watermark', true);
 | 
						|
        $watermark_text = get_post_meta($post->ID, '_wpdd_watermark_text', true);
 | 
						|
        ?>
 | 
						|
        <div class="wpdd-settings-grid">
 | 
						|
            <div class="wpdd-setting-group">
 | 
						|
                <h4><?php _e('Download Settings', 'wp-digital-download'); ?></h4>
 | 
						|
                
 | 
						|
                <p>
 | 
						|
                    <label for="wpdd_download_limit">
 | 
						|
                        <?php _e('Download Limit', 'wp-digital-download'); ?>
 | 
						|
                    </label>
 | 
						|
                    <input type="number" id="wpdd_download_limit" name="wpdd_download_limit" 
 | 
						|
                           value="<?php echo esc_attr($download_limit ?: 5); ?>" min="0" />
 | 
						|
                    <span class="description">
 | 
						|
                        <?php _e('Number of times a customer can download after purchase. 0 = unlimited', 'wp-digital-download'); ?>
 | 
						|
                    </span>
 | 
						|
                </p>
 | 
						|
                
 | 
						|
                <p>
 | 
						|
                    <label for="wpdd_download_expiry">
 | 
						|
                        <?php _e('Download Expiry (days)', 'wp-digital-download'); ?>
 | 
						|
                    </label>
 | 
						|
                    <input type="number" id="wpdd_download_expiry" name="wpdd_download_expiry" 
 | 
						|
                           value="<?php echo esc_attr($download_expiry ?: 30); ?>" min="0" />
 | 
						|
                    <span class="description">
 | 
						|
                        <?php _e('Number of days download link remains valid. 0 = never expires', 'wp-digital-download'); ?>
 | 
						|
                    </span>
 | 
						|
                </p>
 | 
						|
            </div>
 | 
						|
            
 | 
						|
            <div class="wpdd-setting-group">
 | 
						|
                <h4><?php _e('Watermark Settings', 'wp-digital-download'); ?></h4>
 | 
						|
                
 | 
						|
                <p>
 | 
						|
                    <label>
 | 
						|
                        <input type="checkbox" name="wpdd_enable_watermark" value="1" 
 | 
						|
                               <?php checked($enable_watermark, '1'); ?> />
 | 
						|
                        <?php _e('Enable watermarking for images and PDFs', 'wp-digital-download'); ?>
 | 
						|
                    </label>
 | 
						|
                </p>
 | 
						|
                
 | 
						|
                <p>
 | 
						|
                    <label for="wpdd_watermark_text">
 | 
						|
                        <?php _e('Watermark Text', 'wp-digital-download'); ?>
 | 
						|
                    </label>
 | 
						|
                    <input type="text" id="wpdd_watermark_text" name="wpdd_watermark_text" 
 | 
						|
                           value="<?php echo esc_attr($watermark_text); ?>" 
 | 
						|
                           placeholder="<?php _e('e.g., {customer_email} - {order_id}', 'wp-digital-download'); ?>" />
 | 
						|
                    <span class="description">
 | 
						|
                        <?php _e('Available placeholders: {customer_name}, {customer_email}, {order_id}, {date}', 'wp-digital-download'); ?>
 | 
						|
                    </span>
 | 
						|
                </p>
 | 
						|
            </div>
 | 
						|
        </div>
 | 
						|
        <?php
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function render_stats_metabox($post) {
 | 
						|
        global $wpdb;
 | 
						|
        
 | 
						|
        $total_sales = $wpdb->get_var($wpdb->prepare(
 | 
						|
            "SELECT COUNT(*) FROM {$wpdb->prefix}wpdd_orders 
 | 
						|
             WHERE product_id = %d AND status = 'completed'",
 | 
						|
            $post->ID
 | 
						|
        ));
 | 
						|
        
 | 
						|
        $total_revenue = $wpdb->get_var($wpdb->prepare(
 | 
						|
            "SELECT SUM(amount) FROM {$wpdb->prefix}wpdd_orders 
 | 
						|
             WHERE product_id = %d AND status = 'completed'",
 | 
						|
            $post->ID
 | 
						|
        ));
 | 
						|
        
 | 
						|
        $total_downloads = $wpdb->get_var($wpdb->prepare(
 | 
						|
            "SELECT COUNT(*) FROM {$wpdb->prefix}wpdd_downloads 
 | 
						|
             WHERE product_id = %d",
 | 
						|
            $post->ID
 | 
						|
        ));
 | 
						|
        ?>
 | 
						|
        <div class="wpdd-stats">
 | 
						|
            <p>
 | 
						|
                <strong><?php _e('Total Sales:', 'wp-digital-download'); ?></strong> 
 | 
						|
                <?php echo intval($total_sales); ?>
 | 
						|
            </p>
 | 
						|
            <p>
 | 
						|
                <strong><?php _e('Total Revenue:', 'wp-digital-download'); ?></strong> 
 | 
						|
                $<?php echo number_format(floatval($total_revenue), 2); ?>
 | 
						|
            </p>
 | 
						|
            <p>
 | 
						|
                <strong><?php _e('Total Downloads:', 'wp-digital-download'); ?></strong> 
 | 
						|
                <?php echo intval($total_downloads); ?>
 | 
						|
            </p>
 | 
						|
        </div>
 | 
						|
        <?php
 | 
						|
    }
 | 
						|
    
 | 
						|
    public static function save_product_meta($post_id, $post) {
 | 
						|
        if (!isset($_POST['wpdd_product_meta_nonce']) || 
 | 
						|
            !wp_verify_nonce($_POST['wpdd_product_meta_nonce'], 'wpdd_save_product_meta')) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (!current_user_can('edit_post', $post_id)) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        
 | 
						|
        update_post_meta($post_id, '_wpdd_is_free', 
 | 
						|
            isset($_POST['wpdd_is_free']) ? '1' : '0');
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_price'])) {
 | 
						|
            update_post_meta($post_id, '_wpdd_price', 
 | 
						|
                floatval($_POST['wpdd_price']));
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_sale_price'])) {
 | 
						|
            update_post_meta($post_id, '_wpdd_sale_price', 
 | 
						|
                floatval($_POST['wpdd_sale_price']));
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_files']) && is_array($_POST['wpdd_files'])) {
 | 
						|
            $files = array();
 | 
						|
            foreach ($_POST['wpdd_files'] as $file) {
 | 
						|
                if (!empty($file['url'])) {
 | 
						|
                    $files[] = array(
 | 
						|
                        'id' => sanitize_text_field($file['id']),
 | 
						|
                        'name' => sanitize_text_field($file['name']),
 | 
						|
                        'url' => esc_url_raw($file['url'])
 | 
						|
                    );
 | 
						|
                }
 | 
						|
            }
 | 
						|
            update_post_meta($post_id, '_wpdd_files', $files);
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_download_limit'])) {
 | 
						|
            update_post_meta($post_id, '_wpdd_download_limit', 
 | 
						|
                intval($_POST['wpdd_download_limit']));
 | 
						|
        }
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_download_expiry'])) {
 | 
						|
            update_post_meta($post_id, '_wpdd_download_expiry', 
 | 
						|
                intval($_POST['wpdd_download_expiry']));
 | 
						|
        }
 | 
						|
        
 | 
						|
        update_post_meta($post_id, '_wpdd_enable_watermark', 
 | 
						|
            isset($_POST['wpdd_enable_watermark']) ? '1' : '0');
 | 
						|
        
 | 
						|
        if (isset($_POST['wpdd_watermark_text'])) {
 | 
						|
            update_post_meta($post_id, '_wpdd_watermark_text', 
 | 
						|
                sanitize_text_field($_POST['wpdd_watermark_text']));
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |