HEX
Server: LiteSpeed
System: Linux premium127.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
User: luxbsolr (925)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: /home/luxbsolr/cardsord.store/wp-content/plugins/Csv_Uploader/test_image_handling.php
<?php
/**
 * Test image handling improvements
 */

// Simulate WordPress functions
function __($text, $domain = '') { return $text; }
function apply_filters($hook, $value) { return $value; }
function wc_get_product_id_by_sku($sku) { return false; }

// Include classes
require_once 'includes/class-fcpi-product-mapper.php';

echo "=== Testing Image Field Mapping Improvements ===\n\n";

$mapper = new FCPI_Product_Mapper();

// Test your CSV sample data
$csv_row = array(
    'Name' => 'Yu-Gi-Oh - 2025 Mega-Pack Bundle',
    'Images' => 'https://cdn.shopify.com/s/files/1/0514/4854/5477/files/websiteimages-2025-09-08T152046.500_b25b5125-fde7-490b-bf52-799fe695cd29.png?v=1757376884'
);

echo "Test CSV Row:\n";
echo "- Name: " . $csv_row['Name'] . "\n";
echo "- Images: " . $csv_row['Images'] . "\n\n";

// Set up mappings (like the AJAX handler would do)
$wc_fields = $mapper->get_wc_fields();
$mapper->set_mapping('Name', $wc_fields['name']['wc_field']); // post_title
$mapper->set_mapping('Images', $wc_fields['images']['wc_field']); // _product_image_gallery

echo "Field Mappings:\n";
foreach ($mapper->get_mappings() as $csv_field => $mapping) {
    echo "- '{$csv_field}' => '{$mapping['wc_field']}'\n";
}

// Map the data
$mapped_data = $mapper->map_row_data($csv_row);

echo "\nMapped Product Data:\n";
foreach ($mapped_data as $field => $value) {
    if (is_array($value)) {
        echo "- {$field}: " . json_encode($value) . "\n";
    } else {
        echo "- {$field}: '{$value}'\n";
    }
}

// Check image transformation
echo "\nImage Transformation Analysis:\n";
$images_field = $wc_fields['images'];
echo "- Images field config: " . json_encode($images_field) . "\n";

// Test transformation logic
$raw_image_url = $csv_row['Images'];
echo "- Raw image URL: {$raw_image_url}\n";

// Apply the transformation (like mapper does)
$transformed_images = array_filter(array_map('trim', preg_split('/[,;|]/', $raw_image_url)));
echo "- Transformed images array: " . json_encode($transformed_images) . "\n";
echo "- Number of images: " . count($transformed_images) . "\n";

// Check what the batch processor will receive
if (isset($mapped_data['_product_image_gallery'])) {
    $gallery_data = $mapped_data['_product_image_gallery'];
    echo "\nBatch Processor Will Receive:\n";
    echo "- _product_image_gallery: " . json_encode($gallery_data) . "\n";
    echo "- Is array: " . (is_array($gallery_data) ? 'Yes' : 'No') . "\n";
    if (is_array($gallery_data)) {
        echo "- Count: " . count($gallery_data) . "\n";
        echo "- Single image: " . (count($gallery_data) == 1 ? 'Yes (will be used as featured image)' : 'No') . "\n";
    }
}

echo "\n=== Expected Behavior ===\n";
echo "✅ Single image URL should be detected\n";
echo "✅ Will be set as featured image (post thumbnail)\n";
echo "✅ No gallery images will be set\n";
echo "✅ Debug logs will show: 'Set single gallery image as featured'\n";

// Test multiple images
echo "\n=== Testing Multiple Images ===\n";
$multi_image_csv = array(
    'Name' => 'Test Product',
    'Images' => 'https://example.com/image1.jpg,https://example.com/image2.jpg,https://example.com/image3.jpg'
);

$mapper->set_mapping('Images', '_product_image_gallery');
$multi_mapped = $mapper->map_row_data($multi_image_csv);

echo "Multiple images input: " . $multi_image_csv['Images'] . "\n";
echo "Mapped data: " . json_encode($multi_mapped['_product_image_gallery']) . "\n";
echo "Count: " . count($multi_mapped['_product_image_gallery']) . "\n";
echo "Expected: First image becomes featured, rest become gallery\n";
?>