فوم اصلاح مدل Active آکات 225 میلی لیتر
Categories: بهداشتی, سالنی و کارآموزی
Tag: فوم اصلاح مدل Active آکات 225 میلی لیتر
private function process_images($product_id, $images) { if (empty($images) || !is_array($images)) { error_log("Cosmetic Importer: No images to process"); return; } error_log("Cosmetic Importer: Starting image processing for " . count($images) . " images"); $gallery_ids = array(); $success_count = 0; foreach (array_slice($images, 0, 5) as $index => $image_url) { if ($success_count >= 3) { error_log("Cosmetic Importer: Reached maximum image limit (3)"); break; } error_log("Cosmetic Importer: Processing image " . ($index + 1) . ": " . $image_url); $attachment_id = $this->upload_image_from_url($image_url); if ($attachment_id) { $gallery_ids[] = $attachment_id; $success_count++; error_log("Cosmetic Importer: ✅ Image uploaded successfully. ID: " . $attachment_id); } else { error_log("Cosmetic Importer: ❌ Failed to upload image: " . $image_url); } } error_log("Cosmetic Importer: Total successful uploads: " . count($gallery_ids)); if (!empty($gallery_ids)) { // تنظیم تصویر شاخص $featured_result = set_post_thumbnail($product_id, $gallery_ids[0]); if ($featured_result) { error_log("Cosmetic Importer: ✅ Featured image set successfully: " . $gallery_ids[0]); } else { error_log("Cosmetic Importer: ❌ Failed to set featured image: " . $gallery_ids[0]); } // تنظیم گالری تصاویر if (count($gallery_ids) > 1) { $gallery_ids_without_featured = array_slice($gallery_ids, 1); $gallery_string = implode(',', $gallery_ids_without_featured); $gallery_result = update_post_meta($product_id, '_product_image_gallery', $gallery_string); if ($gallery_result) { error_log("Cosmetic Importer: ✅ Gallery images set successfully: " . $gallery_string); } else { error_log("Cosmetic Importer: ❌ Failed to set gallery images"); } } error_log("Cosmetic Importer: 🎉 Image processing completed successfully"); } else { error_log("Cosmetic Importer: 💥 No images were uploaded successfully"); } } private function upload_image_from_url($image_url) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); error_log("Cosmetic Importer: 🚀 Starting upload for: " . $image_url); // بررسی URL تصویر if (!filter_var($image_url, FILTER_VALIDATE_URL)) { error_log("Cosmetic Importer: Invalid URL: " . $image_url); return false; } // بررسی پسوند فایل $file_extension = pathinfo(parse_url($image_url, PHP_URL_PATH), PATHINFO_EXTENSION); $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif', 'webp'); if (!in_array(strtolower($file_extension), $allowed_extensions)) { error_log("Cosmetic Importer: Invalid file extension: " . $file_extension); return false; } // تنظیم timeout add_filter('http_request_timeout', function() { return 30; }); // تنظیم user agent برای برخی سرورها add_filter('http_headers_useragent', function() { return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'; }); try { // دانلود تصویر error_log("Cosmetic Importer: Downloading image..."); $tmp_file = download_url($image_url); if (is_wp_error($tmp_file)) { error_log("Cosmetic Importer: Download failed: " . $tmp_file->get_error_message()); return false; } error_log("Cosmetic Importer: Download successful. Temp file: " . $tmp_file); // بررسی وجود فایل if (!file_exists($tmp_file)) { error_log("Cosmetic Importer: Temp file does not exist"); return false; } // بررسی سایز فایل $file_size = filesize($tmp_file); if ($file_size === 0 || $file_size === false) { error_log("Cosmetic Importer: File is empty or invalid"); @unlink($tmp_file); return false; } error_log("Cosmetic Importer: File size: " . $file_size . " bytes"); // آمادهسازی نام فایل $filename = basename($image_url); $filename = sanitize_file_name($filename); // اگر نام فایل مشکل داشت، یک نام جدید ایجاد کن if (empty($filename) || !preg_match('/\.(jpg|jpeg|png|gif|webp)$/i', $filename)) { $filename = 'product-image-' . time() . '-' . rand(1000, 9999) . '.jpg'; error_log("Cosmetic Importer: Generated new filename: " . $filename); } $file_array = array( 'name' => $filename, 'tmp_name' => $tmp_file, 'error' => 0, 'size' => $file_size ); // آپلود به کتابخانه رسانه error_log("Cosmetic Importer: Uploading to media library..."); $attachment_id = media_handle_sideload($file_array, 0); // پاکسازی فایل موقت (حتی اگر آپلود موفق بود) if (file_exists($tmp_file)) { @unlink($tmp_file); } if (is_wp_error($attachment_id)) { error_log("Cosmetic Importer: Media handle sideload failed: " . $attachment_id->get_error_message()); return false; } // تولید متاسایزها error_log("Cosmetic Importer: Generating image sizes..."); $attach_data = wp_generate_attachment_metadata($attachment_id, get_attached_file($attachment_id)); wp_update_attachment_metadata($attachment_id, $attach_data); error_log("Cosmetic Importer: ✅ Image upload completed successfully. ID: " . $attachment_id); return $attachment_id; } catch (Exception $e) { error_log("Cosmetic Importer: Exception during image upload: " . $e->getMessage()); // پاکسازی فایل موقت در صورت خطا if (isset($tmp_file) && file_exists($tmp_file)) { @unlink($tmp_file); } return false; } }
هنوز حساب کاربری ندارید؟
ایجاد حساب کاربری
نقد و بررسیها
هنوز بررسیای ثبت نشده است.