Enhance Subscriptions, Affiliates, and Software Distribution modules

This commit is contained in:
Dwindi Ramadhana
2026-06-03 21:24:03 +07:00
parent f8c733832e
commit 21ece27b9b
9 changed files with 803 additions and 96 deletions

View File

@@ -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);
}
}