|
@@ -910,10 +910,26 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
|
|
|
|
|
foreach ($possible_taxonomies as $taxonomy) {
|
|
foreach ($possible_taxonomies as $taxonomy) {
|
|
|
if (taxonomy_exists($taxonomy)) {
|
|
if (taxonomy_exists($taxonomy)) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Detected media taxonomy: ' . $taxonomy);
|
|
|
return $taxonomy;
|
|
return $taxonomy;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Try to find any taxonomy registered for attachments
|
|
|
|
|
+ $attachment_taxonomies = get_object_taxonomies('attachment', 'names');
|
|
|
|
|
+ if (!empty($attachment_taxonomies)) {
|
|
|
|
|
+ // Filter out built-in taxonomies
|
|
|
|
|
+ $custom_taxonomies = array_filter($attachment_taxonomies, function($tax) {
|
|
|
|
|
+ return !in_array($tax, array('post_tag', 'category', 'post_format'));
|
|
|
|
|
+ });
|
|
|
|
|
+ if (!empty($custom_taxonomies)) {
|
|
|
|
|
+ $found = reset($custom_taxonomies);
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Auto-detected attachment taxonomy: ' . $found);
|
|
|
|
|
+ return $found;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: No media category taxonomy found. Registered attachment taxonomies: ' . implode(', ', $attachment_taxonomies ?? array()));
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -953,7 +969,10 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
|
|
|
|
|
$taxonomy = $this->get_media_category_taxonomy();
|
|
$taxonomy = $this->get_media_category_taxonomy();
|
|
|
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: get_media_from_categories called - taxonomy: ' . ($taxonomy ?: 'false') . ', category_ids: ' . implode(', ', $category_ids));
|
|
|
|
|
+
|
|
|
if (!$taxonomy || empty($category_ids)) {
|
|
if (!$taxonomy || empty($category_ids)) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: No taxonomy or empty category_ids, returning empty');
|
|
|
return array();
|
|
return array();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -970,11 +989,9 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
";
|
|
";
|
|
|
|
|
|
|
|
$params = array_merge($category_ids, array($taxonomy));
|
|
$params = array_merge($category_ids, array($taxonomy));
|
|
|
- $media = $wpdb->get_col($wpdb->prepare($sql, $params));
|
|
|
|
|
|
|
+ $media = $wpdb->get_col($wpdb->prepare($sql, ...$params));
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Found ' . count($media) . ' media files in media categories ' . implode(', ', $category_ids));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Query found ' . ($media ? count($media) : 0) . ' media files');
|
|
|
|
|
|
|
|
return $media ? array_map('intval', $media) : array();
|
|
return $media ? array_map('intval', $media) : array();
|
|
|
}
|
|
}
|
|
@@ -989,6 +1006,10 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
$deleted_count = 0;
|
|
$deleted_count = 0;
|
|
|
$failed_count = 0;
|
|
$failed_count = 0;
|
|
|
$failed_media = array();
|
|
$failed_media = array();
|
|
|
|
|
+ $affected_term_ids = array();
|
|
|
|
|
+ $taxonomy = $this->get_media_category_taxonomy();
|
|
|
|
|
+
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: delete_media_batch called with ' . count($media_ids) . ' media IDs');
|
|
|
|
|
|
|
|
foreach ($media_ids as $media_id) {
|
|
foreach ($media_ids as $media_id) {
|
|
|
try {
|
|
try {
|
|
@@ -997,37 +1018,46 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
if (!$attachment || $attachment->post_type !== 'attachment') {
|
|
if (!$attachment || $attachment->post_type !== 'attachment') {
|
|
|
$failed_count++;
|
|
$failed_count++;
|
|
|
$failed_media[] = $media_id;
|
|
$failed_media[] = $media_id;
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Media file ' . $media_id . ' not found or not an attachment');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Media file ' . $media_id . ' not found or not an attachment');
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Collect affected term IDs before deletion for count update
|
|
|
|
|
+ if ($taxonomy) {
|
|
|
|
|
+ $terms = wp_get_object_terms($media_id, $taxonomy, array('fields' => 'ids'));
|
|
|
|
|
+ if (!is_wp_error($terms)) {
|
|
|
|
|
+ $affected_term_ids = array_merge($affected_term_ids, $terms);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Force delete attachment and its files from disk
|
|
// Force delete attachment and its files from disk
|
|
|
$result = wp_delete_attachment($media_id, true);
|
|
$result = wp_delete_attachment($media_id, true);
|
|
|
|
|
|
|
|
if ($result) {
|
|
if ($result) {
|
|
|
$deleted_count++;
|
|
$deleted_count++;
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Successfully deleted media file ' . $media_id);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Successfully deleted media file ' . $media_id);
|
|
|
} else {
|
|
} else {
|
|
|
$failed_count++;
|
|
$failed_count++;
|
|
|
$failed_media[] = $media_id;
|
|
$failed_media[] = $media_id;
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Failed to delete media file ' . $media_id);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Failed to delete media file ' . $media_id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
$failed_count++;
|
|
$failed_count++;
|
|
|
$failed_media[] = $media_id;
|
|
$failed_media[] = $media_id;
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Exception deleting media file ' . $media_id . ': ' . $e->getMessage());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Exception deleting media file ' . $media_id . ': ' . $e->getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Force update term counts for affected categories
|
|
|
|
|
+ if ($taxonomy && !empty($affected_term_ids)) {
|
|
|
|
|
+ $affected_term_ids = array_unique($affected_term_ids);
|
|
|
|
|
+ wp_update_term_count_now($affected_term_ids, $taxonomy);
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Updated term counts for term IDs: ' . implode(', ', $affected_term_ids));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Batch result - deleted: ' . $deleted_count . ', failed: ' . $failed_count);
|
|
|
|
|
+
|
|
|
return array(
|
|
return array(
|
|
|
'deleted_count' => $deleted_count,
|
|
'deleted_count' => $deleted_count,
|
|
|
'failed_count' => $failed_count,
|
|
'failed_count' => $failed_count,
|
|
@@ -1048,43 +1078,47 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
$original_error_reporting = error_reporting();
|
|
$original_error_reporting = error_reporting();
|
|
|
error_reporting(0);
|
|
error_reporting(0);
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Clear media AJAX handler called');
|
|
|
|
|
- error_log('STUDIOU WC: POST data: ' . print_r($_POST, true));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Clear media AJAX handler called');
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: POST data: ' . print_r($_POST, true));
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) {
|
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Nonce verification failed');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('Security check failed', 'studiou-wc-product-cat-manage')
|
|
'message' => __('Security check failed', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Permission check failed');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')
|
|
'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_POST['media_categories'])) {
|
|
if (!isset($_POST['media_categories'])) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: media_categories not in POST data');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('Missing required parameters', 'studiou-wc-product-cat-manage')
|
|
'message' => __('Missing required parameters', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$category_ids = array_map('intval', $_POST['media_categories']);
|
|
$category_ids = array_map('intval', $_POST['media_categories']);
|
|
|
|
|
|
|
|
if (empty($category_ids)) {
|
|
if (empty($category_ids)) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Empty category_ids after intval mapping');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('Please select at least one category', 'studiou-wc-product-cat-manage')
|
|
'message' => __('Please select at least one category', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$media_ids = $this->get_media_from_categories($category_ids);
|
|
$media_ids = $this->get_media_from_categories($category_ids);
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Found ' . count($media_ids) . ' media files to delete');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Found ' . count($media_ids) . ' media files to delete from categories: ' . implode(', ', $category_ids));
|
|
|
|
|
|
|
|
ob_clean();
|
|
ob_clean();
|
|
|
|
|
|
|
@@ -1098,10 +1132,8 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
));
|
|
));
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Clear media operation failed - ' . $e->getMessage());
|
|
|
|
|
- error_log('STUDIOU WC: Exception trace: ' . $e->getTraceAsString());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Clear media operation failed - ' . $e->getMessage());
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Exception trace: ' . $e->getTraceAsString());
|
|
|
|
|
|
|
|
ob_clean();
|
|
ob_clean();
|
|
|
|
|
|
|
@@ -1130,41 +1162,40 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
$original_error_reporting = error_reporting();
|
|
$original_error_reporting = error_reporting();
|
|
|
error_reporting(0);
|
|
error_reporting(0);
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Clear media batch AJAX handler called');
|
|
|
|
|
- error_log('STUDIOU WC: POST data: ' . print_r($_POST, true));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Clear media batch AJAX handler called');
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) {
|
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Batch nonce verification failed');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('Security check failed', 'studiou-wc-product-cat-manage')
|
|
'message' => __('Security check failed', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
if (!current_user_can('manage_woocommerce')) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Batch permission check failed');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')
|
|
'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_POST['media_ids'])) {
|
|
if (!isset($_POST['media_ids'])) {
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: media_ids not in POST data');
|
|
|
wp_send_json_error(array(
|
|
wp_send_json_error(array(
|
|
|
'message' => __('Missing required parameters', 'studiou-wc-product-cat-manage')
|
|
'message' => __('Missing required parameters', 'studiou-wc-product-cat-manage')
|
|
|
));
|
|
));
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$media_ids = array_map('intval', $_POST['media_ids']);
|
|
$media_ids = array_map('intval', $_POST['media_ids']);
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Processing batch of ' . count($media_ids) . ' media files for deletion');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Processing batch of ' . count($media_ids) . ' media files for deletion');
|
|
|
|
|
|
|
|
$result = $this->delete_media_batch($media_ids);
|
|
$result = $this->delete_media_batch($media_ids);
|
|
|
|
|
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Batch media deletion completed - Deleted: ' . $result['deleted_count'] . ', Failed: ' . $result['failed_count']);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Batch completed - Deleted: ' . $result['deleted_count'] . ', Failed: ' . $result['failed_count']);
|
|
|
|
|
|
|
|
ob_clean();
|
|
ob_clean();
|
|
|
|
|
|
|
@@ -1175,10 +1206,8 @@ class Studiou_WC_Product_Cat_Manage_Manipulator {
|
|
|
));
|
|
));
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
- if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
|
|
|
- error_log('STUDIOU WC: Clear media batch operation failed - ' . $e->getMessage());
|
|
|
|
|
- error_log('STUDIOU WC: Exception trace: ' . $e->getTraceAsString());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Clear media batch operation failed - ' . $e->getMessage());
|
|
|
|
|
+ error_log('STUDIOU WC MEDIA: Exception trace: ' . $e->getTraceAsString());
|
|
|
|
|
|
|
|
ob_clean();
|
|
ob_clean();
|
|
|
|
|
|