Adding more functionality

This commit is contained in:
2025-08-29 18:54:14 -07:00
parent 5aa0777fd3
commit 6d797ef686
17 changed files with 4237 additions and 85 deletions

View File

@@ -12,6 +12,15 @@ class WPDD_Metaboxes {
}
public static function add_meta_boxes() {
add_meta_box(
'wpdd_product_type',
__('Product Type', 'wp-digital-download'),
array(__CLASS__, 'render_product_type_metabox'),
'wpdd_product',
'side',
'high'
);
add_meta_box(
'wpdd_product_pricing',
__('Product Pricing', 'wp-digital-download'),
@@ -30,6 +39,15 @@ class WPDD_Metaboxes {
'high'
);
add_meta_box(
'wpdd_software_licensing',
__('Software Licensing', 'wp-digital-download'),
array(__CLASS__, 'render_software_licensing_metabox'),
'wpdd_product',
'normal',
'high'
);
add_meta_box(
'wpdd_product_settings',
__('Product Settings', 'wp-digital-download'),
@@ -78,6 +96,46 @@ class WPDD_Metaboxes {
<?php
}
public static function render_product_type_metabox($post) {
$product_type = get_post_meta($post->ID, '_wpdd_product_type', true) ?: 'digital_download';
?>
<p>
<label>
<input type="radio" name="wpdd_product_type" value="digital_download" <?php checked($product_type, 'digital_download'); ?> />
<?php _e('Digital Download', 'wp-digital-download'); ?>
</label>
<br>
<span class="description"><?php _e('Standard downloadable file (PDF, images, etc.)', 'wp-digital-download'); ?></span>
</p>
<p>
<label>
<input type="radio" name="wpdd_product_type" value="software_license" <?php checked($product_type, 'software_license'); ?> />
<?php _e('Software License', 'wp-digital-download'); ?>
</label>
<br>
<span class="description"><?php _e('WordPress plugin/theme with license key and automatic updates', 'wp-digital-download'); ?></span>
</p>
<script type="text/javascript">
jQuery(document).ready(function($) {
function toggleMetaboxes() {
var productType = $('input[name="wpdd_product_type"]:checked').val();
if (productType === 'software_license') {
$('#wpdd_software_licensing').show();
$('#wpdd_product_files').hide();
} else {
$('#wpdd_software_licensing').hide();
$('#wpdd_product_files').show();
}
}
$('input[name="wpdd_product_type"]').on('change', toggleMetaboxes);
toggleMetaboxes();
});
</script>
<?php
}
public static function render_files_metabox($post) {
$files = get_post_meta($post->ID, '_wpdd_files', true);
if (!is_array($files)) {
@@ -148,6 +206,101 @@ class WPDD_Metaboxes {
<?php
}
public static function render_software_licensing_metabox($post) {
$git_repository = get_post_meta($post->ID, '_wpdd_git_repository', true);
$git_username = get_post_meta($post->ID, '_wpdd_git_username', true);
$git_token = get_post_meta($post->ID, '_wpdd_git_token', true);
$webhook_passcode = get_post_meta($post->ID, '_wpdd_webhook_passcode', true);
$max_activations = get_post_meta($post->ID, '_wpdd_max_activations', true) ?: 1;
$license_duration = get_post_meta($post->ID, '_wpdd_license_duration', true);
$current_version = get_post_meta($post->ID, '_wpdd_current_version', true);
$min_wp_version = get_post_meta($post->ID, '_wpdd_min_wp_version', true);
$tested_wp_version = get_post_meta($post->ID, '_wpdd_tested_wp_version', true);
// Generate webhook passcode if not set
if (!$webhook_passcode) {
$webhook_passcode = wp_generate_password(32, false);
update_post_meta($post->ID, '_wpdd_webhook_passcode', $webhook_passcode);
}
$webhook_url = home_url("/wp-json/wpdd/v1/webhook/{$post->ID}/{$webhook_passcode}");
?>
<div class="wpdd-metabox-content">
<h4><?php _e('Repository Settings', 'wp-digital-download'); ?></h4>
<p>
<label for="wpdd_git_repository"><?php _e('Git Repository URL', 'wp-digital-download'); ?></label><br>
<input type="url" id="wpdd_git_repository" name="wpdd_git_repository" value="<?php echo esc_attr($git_repository); ?>" class="widefat" placeholder="https://github.com/username/repository" />
<span class="description"><?php _e('Full URL to your Git repository', 'wp-digital-download'); ?></span>
</p>
<p>
<label for="wpdd_git_username"><?php _e('Git Username (optional)', 'wp-digital-download'); ?></label><br>
<input type="text" id="wpdd_git_username" name="wpdd_git_username" value="<?php echo esc_attr($git_username); ?>" class="widefat" />
<span class="description"><?php _e('For private repositories', 'wp-digital-download'); ?></span>
</p>
<p>
<label for="wpdd_git_token"><?php _e('Git Access Token (optional)', 'wp-digital-download'); ?></label><br>
<input type="password" id="wpdd_git_token" name="wpdd_git_token" value="<?php echo esc_attr($git_token); ?>" class="widefat" />
<span class="description"><?php _e('Personal access token for private repositories', 'wp-digital-download'); ?></span>
</p>
<h4><?php _e('Webhook Configuration', 'wp-digital-download'); ?></h4>
<p>
<label><?php _e('Webhook URL', 'wp-digital-download'); ?></label><br>
<input type="text" value="<?php echo esc_attr($webhook_url); ?>" class="widefat" readonly onclick="this.select();" />
<span class="description"><?php _e('Add this URL to your repository webhook settings (GitHub, GitLab, etc.) to receive release notifications', 'wp-digital-download'); ?></span>
</p>
<p>
<button type="button" class="button" onclick="wpddRegenerateWebhookPasscode(<?php echo $post->ID; ?>)">
<?php _e('Regenerate Webhook URL', 'wp-digital-download'); ?>
</button>
</p>
<h4><?php _e('License Settings', 'wp-digital-download'); ?></h4>
<p>
<label for="wpdd_max_activations"><?php _e('Max Activations per License', 'wp-digital-download'); ?></label><br>
<input type="number" id="wpdd_max_activations" name="wpdd_max_activations" value="<?php echo esc_attr($max_activations); ?>" min="1" />
<span class="description"><?php _e('Number of sites where the license can be activated', 'wp-digital-download'); ?></span>
</p>
<p>
<label for="wpdd_license_duration"><?php _e('License Duration (days)', 'wp-digital-download'); ?></label><br>
<input type="number" id="wpdd_license_duration" name="wpdd_license_duration" value="<?php echo esc_attr($license_duration); ?>" min="0" />
<span class="description"><?php _e('Leave blank or 0 for lifetime licenses', 'wp-digital-download'); ?></span>
</p>
<h4><?php _e('Version Information', 'wp-digital-download'); ?></h4>
<p>
<label for="wpdd_current_version"><?php _e('Current Version', 'wp-digital-download'); ?></label><br>
<input type="text" id="wpdd_current_version" name="wpdd_current_version" value="<?php echo esc_attr($current_version); ?>" placeholder="1.0.0" />
</p>
<p>
<label for="wpdd_min_wp_version"><?php _e('Minimum WordPress Version', 'wp-digital-download'); ?></label><br>
<input type="text" id="wpdd_min_wp_version" name="wpdd_min_wp_version" value="<?php echo esc_attr($min_wp_version); ?>" placeholder="5.0" />
</p>
<p>
<label for="wpdd_tested_wp_version"><?php _e('Tested up to WordPress Version', 'wp-digital-download'); ?></label><br>
<input type="text" id="wpdd_tested_wp_version" name="wpdd_tested_wp_version" value="<?php echo esc_attr($tested_wp_version); ?>" placeholder="6.4" />
</p>
</div>
<script type="text/javascript">
function wpddRegenerateWebhookPasscode(productId) {
if (confirm('<?php _e('Are you sure? This will invalidate the existing webhook URL.', 'wp-digital-download'); ?>')) {
var newPasscode = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
jQuery('#wpdd_webhook_passcode').val(newPasscode);
var newUrl = '<?php echo home_url("/wp-json/wpdd/v1/webhook/"); ?>' + productId + '/' + newPasscode;
jQuery('input[readonly]').val(newUrl);
}
}
</script>
<?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);
@@ -304,5 +457,52 @@ class WPDD_Metaboxes {
update_post_meta($post_id, '_wpdd_watermark_text',
sanitize_text_field($_POST['wpdd_watermark_text']));
}
// Product type
if (isset($_POST['wpdd_product_type'])) {
update_post_meta($post_id, '_wpdd_product_type',
sanitize_text_field($_POST['wpdd_product_type']));
}
// Software licensing fields
if (isset($_POST['wpdd_git_repository'])) {
update_post_meta($post_id, '_wpdd_git_repository',
esc_url_raw($_POST['wpdd_git_repository']));
}
if (isset($_POST['wpdd_git_username'])) {
update_post_meta($post_id, '_wpdd_git_username',
sanitize_text_field($_POST['wpdd_git_username']));
}
if (isset($_POST['wpdd_git_token'])) {
update_post_meta($post_id, '_wpdd_git_token',
sanitize_text_field($_POST['wpdd_git_token']));
}
if (isset($_POST['wpdd_max_activations'])) {
update_post_meta($post_id, '_wpdd_max_activations',
intval($_POST['wpdd_max_activations']));
}
if (isset($_POST['wpdd_license_duration'])) {
update_post_meta($post_id, '_wpdd_license_duration',
intval($_POST['wpdd_license_duration']));
}
if (isset($_POST['wpdd_current_version'])) {
update_post_meta($post_id, '_wpdd_current_version',
sanitize_text_field($_POST['wpdd_current_version']));
}
if (isset($_POST['wpdd_min_wp_version'])) {
update_post_meta($post_id, '_wpdd_min_wp_version',
sanitize_text_field($_POST['wpdd_min_wp_version']));
}
if (isset($_POST['wpdd_tested_wp_version'])) {
update_post_meta($post_id, '_wpdd_tested_wp_version',
sanitize_text_field($_POST['wpdd_tested_wp_version']));
}
}
}