Enhance Subscriptions, Affiliates, and Software Distribution modules
This commit is contained in:
@@ -220,6 +220,7 @@ class ProductsController
|
||||
$category = $request->get_param('category');
|
||||
$type = $request->get_param('type');
|
||||
$stock_status = $request->get_param('stock_status');
|
||||
$software_enabled = $request->get_param('software_enabled');
|
||||
$orderby = $request->get_param('orderby') ?: 'date';
|
||||
$order = $request->get_param('order') ?: 'DESC';
|
||||
|
||||
@@ -266,11 +267,19 @@ class ProductsController
|
||||
|
||||
// Stock status filter
|
||||
if ($stock_status) {
|
||||
$args['meta_query'] = [
|
||||
[
|
||||
'key' => '_stock_status',
|
||||
'value' => $stock_status,
|
||||
],
|
||||
$args['meta_query'] = $args['meta_query'] ?? [];
|
||||
$args['meta_query'][] = [
|
||||
'key' => '_stock_status',
|
||||
'value' => $stock_status,
|
||||
];
|
||||
}
|
||||
|
||||
// Software enabled filter
|
||||
if ($software_enabled === 'true' || $software_enabled === '1') {
|
||||
$args['meta_query'] = $args['meta_query'] ?? [];
|
||||
$args['meta_query'][] = [
|
||||
'key' => '_woonoow_software_enabled',
|
||||
'value' => 'yes',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -660,6 +669,26 @@ class ProductsController
|
||||
update_post_meta($product->get_id(), '_woonoow_affiliate_commission_rate', self::sanitize_number($data['affiliate_commission_rate']));
|
||||
}
|
||||
|
||||
// Software meta
|
||||
if (isset($data['software_enabled'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_enabled', $data['software_enabled'] ? 'yes' : 'no');
|
||||
}
|
||||
if (isset($data['software_slug'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_slug', sanitize_title($data['software_slug']));
|
||||
}
|
||||
if (isset($data['software_wp_enabled'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_wp_enabled', $data['software_wp_enabled'] ? 'yes' : 'no');
|
||||
}
|
||||
if (isset($data['software_requires_wp'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_requires_wp', sanitize_text_field($data['software_requires_wp']));
|
||||
}
|
||||
if (isset($data['software_tested_wp'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_tested_wp', sanitize_text_field($data['software_tested_wp']));
|
||||
}
|
||||
if (isset($data['software_requires_php'])) {
|
||||
update_post_meta($product->get_id(), '_woonoow_software_requires_php', sanitize_text_field($data['software_requires_php']));
|
||||
}
|
||||
|
||||
// Allow plugins to perform additional updates (Level 1 compatibility)
|
||||
do_action('woonoow/product_updated', $product, $data, $request);
|
||||
|
||||
@@ -819,6 +848,10 @@ class ProductsController
|
||||
'permalink' => get_permalink($product->get_id()),
|
||||
'date_created' => $product->get_date_created() ? $product->get_date_created()->date('Y-m-d H:i:s') : '',
|
||||
'date_modified' => $product->get_date_modified() ? $product->get_date_modified()->date('Y-m-d H:i:s') : '',
|
||||
'software_enabled' => get_post_meta($product->get_id(), '_woonoow_software_enabled', true) === 'yes',
|
||||
'software_slug' => get_post_meta($product->get_id(), '_woonoow_software_slug', true),
|
||||
'software_current_version' => get_post_meta($product->get_id(), '_woonoow_software_current_version', true),
|
||||
'software_wp_enabled' => get_post_meta($product->get_id(), '_woonoow_software_wp_enabled', true) === 'yes',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -877,6 +910,14 @@ class ProductsController
|
||||
$data['affiliate_enabled'] = get_post_meta($product->get_id(), '_woonoow_affiliate_enabled', true) === 'yes';
|
||||
$data['affiliate_commission_rate'] = get_post_meta($product->get_id(), '_woonoow_affiliate_commission_rate', true) ?: '';
|
||||
|
||||
// Software fields
|
||||
$data['software_enabled'] = get_post_meta($product->get_id(), '_woonoow_software_enabled', true) === 'yes';
|
||||
$data['software_slug'] = get_post_meta($product->get_id(), '_woonoow_software_slug', true) ?: '';
|
||||
$data['software_wp_enabled'] = get_post_meta($product->get_id(), '_woonoow_software_wp_enabled', true) === 'yes';
|
||||
$data['software_requires_wp'] = get_post_meta($product->get_id(), '_woonoow_software_requires_wp', true) ?: '';
|
||||
$data['software_tested_wp'] = get_post_meta($product->get_id(), '_woonoow_software_tested_wp', true) ?: '';
|
||||
$data['software_requires_php'] = get_post_meta($product->get_id(), '_woonoow_software_requires_php', true) ?: '';
|
||||
|
||||
// Images array (URLs) for frontend - featured + gallery
|
||||
$images = [];
|
||||
$featured_image_id = $product->get_image_id();
|
||||
@@ -1078,6 +1119,10 @@ class ProductsController
|
||||
'image_url' => $image_url,
|
||||
'image' => $image_url, // For form compatibility
|
||||
'license_duration_days' => get_post_meta($variation->get_id(), '_license_duration_days', true) ?: '',
|
||||
'subscription_signup_fee' => get_post_meta($variation->get_id(), '_woonoow_subscription_signup_fee', true) ?: '',
|
||||
'subscription_trial_days' => get_post_meta($variation->get_id(), '_woonoow_subscription_trial_days', true) ?: '',
|
||||
'subscription_period' => get_post_meta($variation->get_id(), '_woonoow_subscription_period', true) ?: '',
|
||||
'subscription_interval' => get_post_meta($variation->get_id(), '_woonoow_subscription_interval', true) ?: '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1214,6 +1259,20 @@ class ProductsController
|
||||
update_post_meta($saved_id, '_license_duration_days', self::sanitize_number($var_data['license_duration_days']));
|
||||
}
|
||||
|
||||
// Save variation-level subscription fields
|
||||
if (isset($var_data['subscription_signup_fee'])) {
|
||||
update_post_meta($saved_id, '_woonoow_subscription_signup_fee', self::sanitize_number($var_data['subscription_signup_fee']));
|
||||
}
|
||||
if (isset($var_data['subscription_trial_days'])) {
|
||||
update_post_meta($saved_id, '_woonoow_subscription_trial_days', absint($var_data['subscription_trial_days']));
|
||||
}
|
||||
if (isset($var_data['subscription_period'])) {
|
||||
update_post_meta($saved_id, '_woonoow_subscription_period', sanitize_key($var_data['subscription_period']));
|
||||
}
|
||||
if (isset($var_data['subscription_interval'])) {
|
||||
update_post_meta($saved_id, '_woonoow_subscription_interval', absint($var_data['subscription_interval']));
|
||||
}
|
||||
|
||||
// Manually save attributes using direct database insert
|
||||
if (!empty($wc_attributes)) {
|
||||
global $wpdb;
|
||||
|
||||
@@ -90,6 +90,15 @@ class SoftwareController
|
||||
return current_user_can('manage_woocommerce');
|
||||
},
|
||||
]);
|
||||
|
||||
// Edit version
|
||||
register_rest_route($namespace, '/software/products/(?P<product_id>\d+)/versions/(?P<version_id>\d+)', [
|
||||
'methods' => 'PUT',
|
||||
'callback' => [__CLASS__, 'edit_version'],
|
||||
'permission_callback' => function () {
|
||||
return current_user_can('manage_woocommerce');
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +136,7 @@ class SoftwareController
|
||||
// Log the check (optional - for analytics)
|
||||
do_action('woonoow/software/update_check', $slug, $current_version, $site_url, $license_key);
|
||||
|
||||
$result = SoftwareManager::check_update($license_key, $slug, $current_version);
|
||||
$result = SoftwareManager::check_update($license_key, $slug, $current_version, $site_url);
|
||||
|
||||
$status_code = isset($result['success']) && $result['success'] === false ? 400 : 200;
|
||||
|
||||
@@ -242,7 +251,7 @@ class SoftwareController
|
||||
return [
|
||||
'version' => $v['version'],
|
||||
'release_date' => $v['release_date'],
|
||||
'changelog' => $v['changelog'],
|
||||
'changelog' => is_string($v['changelog']) && strpos(trim($v['changelog']), '{') === 0 ? json_decode($v['changelog'], true) : $v['changelog'],
|
||||
'download_count' => (int) $v['download_count'],
|
||||
];
|
||||
}, $versions),
|
||||
@@ -283,8 +292,29 @@ class SoftwareController
|
||||
$params = $request->get_json_params();
|
||||
|
||||
$version = sanitize_text_field($params['version'] ?? '');
|
||||
$changelog = wp_kses_post($params['changelog'] ?? '');
|
||||
$set_current = (bool) ($params['set_current'] ?? true);
|
||||
|
||||
$raw_changelog = $params['changelog'] ?? [];
|
||||
$changelog_data = [];
|
||||
if (is_array($raw_changelog)) {
|
||||
$changelog_data = [
|
||||
'narrative' => wp_kses_post($raw_changelog['narrative'] ?? ''),
|
||||
'points' => []
|
||||
];
|
||||
if (isset($raw_changelog['points']) && is_array($raw_changelog['points'])) {
|
||||
foreach ($raw_changelog['points'] as $pt) {
|
||||
if (isset($pt['type']) && isset($pt['text'])) {
|
||||
$changelog_data['points'][] = [
|
||||
'type' => sanitize_text_field($pt['type']),
|
||||
'text' => sanitize_text_field($pt['text']),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$changelog_data = ['narrative' => sanitize_textarea_field($raw_changelog), 'points' => []];
|
||||
}
|
||||
$changelog = wp_json_encode($changelog_data);
|
||||
|
||||
if (empty($version)) {
|
||||
return new WP_REST_Response([
|
||||
@@ -318,4 +348,62 @@ class SoftwareController
|
||||
'message' => __('Version added successfully', 'woonoow'),
|
||||
], 201);
|
||||
}
|
||||
/**
|
||||
* Edit an existing version
|
||||
*/
|
||||
public static function edit_version(WP_REST_Request $request)
|
||||
{
|
||||
$product_id = $request->get_param('product_id');
|
||||
$version_id = $request->get_param('version_id');
|
||||
$version = sanitize_text_field($request->get_param('version'));
|
||||
$changelog = $request->get_param('changelog');
|
||||
$set_current = filter_var($request->get_param('set_current'), FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
// If changelog is an array/object, encode it to JSON
|
||||
if (is_array($changelog) || is_object($changelog)) {
|
||||
if (isset($changelog['narrative'])) {
|
||||
$changelog['narrative'] = sanitize_textarea_field($changelog['narrative']);
|
||||
}
|
||||
if (isset($changelog['points']) && is_array($changelog['points'])) {
|
||||
foreach ($changelog['points'] as &$point) {
|
||||
$point['type'] = sanitize_text_field($point['type'] ?? '');
|
||||
$point['text'] = sanitize_text_field($point['text'] ?? '');
|
||||
}
|
||||
}
|
||||
$changelog = wp_json_encode($changelog);
|
||||
} else {
|
||||
$changelog = wp_kses_post($changelog);
|
||||
}
|
||||
|
||||
if (empty($version)) {
|
||||
return new WP_REST_Response([
|
||||
'success' => false,
|
||||
'error' => 'missing_version',
|
||||
'message' => __('Version number is required', 'woonoow'),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$product = wc_get_product($product_id);
|
||||
if (!$product) {
|
||||
return new WP_REST_Response([
|
||||
'success' => false,
|
||||
'error' => 'product_not_found',
|
||||
], 404);
|
||||
}
|
||||
|
||||
$result = SoftwareManager::update_version($version_id, $product_id, $version, $changelog, $set_current);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return new WP_REST_Response([
|
||||
'success' => false,
|
||||
'error' => $result->get_error_code(),
|
||||
'message' => $result->get_error_message(),
|
||||
], 400);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'success' => true,
|
||||
'message' => __('Version updated successfully', 'woonoow'),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user