Fix version upload handling and add better error logging

- Fix VersionManager::createVersion() to handle null attachment_id properly
- Add product type validation in AJAX version handler
- Add database error logging for debugging
- Improve meta box visibility logic for licensed products
- Add known bug note to CLAUDE.md

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 22:02:51 +01:00
parent 319dfe357a
commit 08aa7dae35
3 changed files with 57 additions and 25 deletions

View File

@@ -46,14 +46,23 @@ final class VersionAdminController
*/
public function addVersionsMetaBox(): void
{
add_meta_box(
'wc_licensed_product_versions',
__('Product Versions', 'wc-licensed-product'),
[$this, 'renderVersionsMetaBox'],
'product',
'normal',
'high'
);
global $post;
// Only add meta box for licensed products or new products
if ($post && $post->post_type === 'product') {
$product = wc_get_product($post->ID);
// Show for licensed products or new products (where type might be selected later)
if (!$product || $product->is_type('licensed') || $post->post_status === 'auto-draft') {
add_meta_box(
'wc_licensed_product_versions',
__('Product Versions', 'wc-licensed-product'),
[$this, 'renderVersionsMetaBox'],
'product',
'normal',
'high'
);
}
}
}
/**
@@ -251,6 +260,16 @@ final class VersionAdminController
wp_send_json_error(['message' => __('This version already exists.', 'wc-licensed-product')]);
}
// Verify product exists and is of type licensed
$product = wc_get_product($productId);
if (!$product) {
wp_send_json_error(['message' => __('Product not found.', 'wc-licensed-product')]);
}
if (!$product->is_type('licensed')) {
wp_send_json_error(['message' => __('This product is not a licensed product.', 'wc-licensed-product')]);
}
$newVersion = $this->versionManager->createVersion(
$productId,
$version,
@@ -260,7 +279,12 @@ final class VersionAdminController
);
if (!$newVersion) {
wp_send_json_error(['message' => __('Failed to create version.', 'wc-licensed-product')]);
global $wpdb;
$errorMessage = __('Failed to create version.', 'wc-licensed-product');
if (!empty($wpdb->last_error)) {
error_log('WC Licensed Product: DB error - ' . $wpdb->last_error);
}
wp_send_json_error(['message' => $errorMessage]);
}
wp_send_json_success([

View File

@@ -105,23 +105,31 @@ class VersionManager
$parsed = ProductVersion::parseVersion($version);
$tableName = Installer::getVersionsTable();
$result = $wpdb->insert(
$tableName,
[
'product_id' => $productId,
'version' => $version,
'major_version' => $parsed['major'],
'minor_version' => $parsed['minor'],
'patch_version' => $parsed['patch'],
'release_notes' => $releaseNotes,
'download_url' => $downloadUrl,
'attachment_id' => $attachmentId,
'is_active' => 1,
],
['%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%d']
);
// Build data and formats arrays, handling null values properly
$data = [
'product_id' => $productId,
'version' => $version,
'major_version' => $parsed['major'],
'minor_version' => $parsed['minor'],
'patch_version' => $parsed['patch'],
'release_notes' => $releaseNotes,
'download_url' => $downloadUrl,
'is_active' => 1,
];
$formats = ['%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d'];
// Only include attachment_id if it's set
if ($attachmentId !== null && $attachmentId > 0) {
$data['attachment_id'] = $attachmentId;
$formats[] = '%d';
}
$result = $wpdb->insert($tableName, $data, $formats);
if ($result === false) {
// Log error for debugging
error_log('WC Licensed Product: Failed to create version - ' . $wpdb->last_error);
return null;
}