$max_size) { wp_send_json_error(esc_html__('Image size cannot exceed 5MB', 'ai-try-on-assistant')); } require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); $attachment_id = media_handle_upload('photo', 0); if (is_wp_error($attachment_id)) { wp_send_json_error($attachment_id->get_error_message()); } $user_id = get_current_user_id(); // Delete old photo $old_photo_id = get_user_meta($user_id, 'aitoa_profile_photo_id', true); if ($old_photo_id) { wp_delete_attachment($old_photo_id, true); } // Save new photo update_user_meta($user_id, 'aitoa_profile_photo_id', $attachment_id); $photo_url = wp_get_attachment_image_url($attachment_id, 'medium'); wp_send_json_success(array( 'photo_url' => $photo_url, 'message' => esc_html__('Photo uploaded successfully', 'ai-try-on-assistant') )); } public function delete_photo() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } if (!is_user_logged_in()) { wp_send_json_error(esc_html__('Please login first', 'ai-try-on-assistant')); } $user_id = get_current_user_id(); $photo_id = get_user_meta($user_id, 'aitoa_profile_photo_id', true); if ($photo_id) { wp_delete_attachment($photo_id, true); delete_user_meta($user_id, 'aitoa_profile_photo_id'); } wp_send_json_success(array( 'message' => esc_html__('Photo deleted', 'ai-try-on-assistant') )); } public function process_try_on() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } if (!is_user_logged_in()) { wp_send_json_error(esc_html__('Please login first', 'ai-try-on-assistant')); } $user_id = get_current_user_id(); $photo_id = get_user_meta($user_id, 'aitoa_profile_photo_id', true); if (!$photo_id) { wp_send_json_error(esc_html__('Please upload your photo first', 'ai-try-on-assistant')); } $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0; $image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; if (!$product_id || !$image_id) { wp_send_json_error(esc_html__('Invalid parameters', 'ai-try-on-assistant')); } // Use Gemini API to process try-on $gemini = new AITOA_Gemini(); $result = $gemini->process_try_on($photo_id, $image_id, $product_id); if (is_wp_error($result)) { wp_send_json_error($result->get_error_message()); } wp_send_json_success($result); } public function load_try_on_interface() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } if (!is_user_logged_in()) { wp_send_json_error(esc_html__('Please login first', 'ai-try-on-assistant')); } ob_start(); include AITOA_PLUGIN_PATH . 'public/templates/try-on-modal.php'; $html = ob_get_clean(); wp_send_json_success(array('html' => $html)); } public function get_image_data() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } $image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; if (!$image_id) { wp_send_json_error(esc_html__('Invalid image ID', 'ai-try-on-assistant')); } $image_url = wp_get_attachment_image_url($image_id, 'medium'); $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); if (!$image_url) { wp_send_json_error(esc_html__('Image not found', 'ai-try-on-assistant')); } wp_send_json_success(array( 'url' => $image_url, 'alt' => $image_alt ?: esc_html__('Product image', 'ai-try-on-assistant') )); } public function get_user_photo() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } if (!is_user_logged_in()) { wp_send_json_error(esc_html__('Please login first', 'ai-try-on-assistant')); } $user_id = get_current_user_id(); $photo_id = get_user_meta($user_id, 'aitoa_profile_photo_id', true); if (!$photo_id) { wp_send_json_error(esc_html__('User photo not found', 'ai-try-on-assistant')); } $photo_url = wp_get_attachment_image_url($photo_id, 'medium'); if (!$photo_url) { wp_send_json_error(esc_html__('Photo loading failed', 'ai-try-on-assistant')); } wp_send_json_success(array( 'url' => $photo_url, 'alt' => esc_html__('My photo', 'ai-try-on-assistant') )); } public function validate_image() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_admin_nonce')) { wp_die(esc_html__('Security check failed', 'ai-try-on-assistant')); } $image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; if (!$image_id) { wp_send_json_error(esc_html__('Invalid image ID', 'ai-try-on-assistant')); } $image_url = wp_get_attachment_image_url($image_id, 'thumbnail'); $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true); if (!$image_url) { wp_send_json_error(esc_html__('Image not found', 'ai-try-on-assistant')); } wp_send_json_success(array( 'url' => $image_url, 'alt' => $image_alt ?: esc_html__('Image', 'ai-try-on-assistant') )); } public function quick_save_try_on_settings() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'aitoa_save_product_meta')) { wp_send_json_error(esc_html__('Security check failed', 'ai-try-on-assistant')); } if (!current_user_can('edit_posts')) { wp_send_json_error(esc_html__('Permission denied', 'ai-try-on-assistant')); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; if (!$post_id || get_post_type($post_id) !== 'product') { wp_send_json_error(esc_html__('Invalid product ID', 'ai-try-on-assistant')); } // Save enable try-on setting $enable_try_on = isset($_POST['enable_try_on']) && $_POST['enable_try_on'] === 'yes' ? 'yes' : 'no'; update_post_meta($post_id, '_aitoa_enable_try_on', $enable_try_on); // Save try-on type if (isset($_POST['try_on_type'])) { $try_on_type = sanitize_text_field(wp_unslash($_POST['try_on_type'])); update_post_meta($post_id, '_aitoa_try_on_type', $try_on_type); } // Save tryable images $tryable_images = array(); if (isset($_POST['tryable_images']) && is_array($_POST['tryable_images'])) { $tryable_images = array_map('intval', $_POST['tryable_images']); } update_post_meta($post_id, '_aitoa_tryable_images', $tryable_images); wp_send_json_success(array( 'message' => esc_html__('Try-on settings saved successfully!', 'ai-try-on-assistant') )); } }