active = get_option($this->active_option_name, true); if ($this->active=='true') { $this->init(); } add_action('wp_ajax_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); } public function update_active_option_callback() { check_ajax_referer('aiautotool_nonce', 'security'); if (!current_user_can('manage_options')) { wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403)); } if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active,null, 'no'); print_r($active); } wp_die(); } function init(){ add_action('admin_init', array($this, 'handle_create_users_submission')); } public function register_settings() { register_setting('auto_generate_username_settings', $this->user_count_option, array( 'type' => 'integer', 'sanitize_callback' => 'absint', )); add_settings_section('auto_generate_username_section', esc_html__('Create user random','ai-auto-tool'), array($this, 'section_callback'), 'auto-generate-username-settings'); add_settings_field('user_count', esc_html__('Number user:','ai-auto-tool'), array($this, 'user_count_callback'), 'auto-generate-username-settings', 'auto_generate_username_section'); } public function section_callback() { echo esc_html__('Create user random','ai-auto-tool'); } public function user_count_callback() { $count = get_option($this->user_count_option, 1); echo ''; } public function render_setting() { $ai_available = class_exists('AIAutotool_AI_Helper') && AIAutotool_AI_Helper::is_available(); ?>

get_website_context(); // Try to generate names using AI $ai_result = $this->generate_ai_usernames($quantity, $context); $ai_names = array(); $use_ai = false; if ($ai_result['success'] && !empty($ai_result['names'])) { $ai_names = $ai_result['names']; $use_ai = true; } // Create users for ($i = 0; $i < $quantity; $i++) { $first_name = ''; $last_name = ''; // Use AI-generated names if available if ($use_ai && isset($ai_names[$i])) { $first_name = $ai_names[$i]['first_name']; $last_name = $ai_names[$i]['last_name']; $username = $this->create_username_from_name($first_name, $last_name, $domain); } else { // Fallback to original method $username = $this->gen_authorname($i + 1); } $user_id = username_exists($username); if (!$user_id && email_exists($username . '@' . $domain) == false) { // Get author details (will use AI names if provided) $author_data = $this->get_author_profile_data($i + 1, $first_name, $last_name); // Create user $user_id = wp_create_user($username, $password, $username . '@' . $domain); // Update user with complete profile wp_update_user(array( 'ID' => $user_id, 'role' => 'author', 'first_name' => $author_data['first_name'], 'last_name' => $author_data['last_name'], 'display_name' => $author_data['display_name'], 'description' => $author_data['bio'] )); // Add additional user meta update_user_meta($user_id, 'user_url', $author_data['website']); update_user_meta($user_id, 'nickname', $author_data['nickname']); $created_users[] = $username . ($use_ai ? ' ✨' : ''); } } set_transient('created_users_list', $created_users, 60); // Redirect after submission $redirect_url = admin_url('admin.php?page=ai_auto_tool&success=true'); wp_redirect($redirect_url); exit; } } private function gen_authorname($index) { $names = array( 'John', 'Jane', 'Michael', 'Emma', 'Samuel', 'Olivia', 'William', 'Ava', 'James', 'Isabella', 'Liam', 'Sophia', 'Logan', 'Mia', 'Benjamin', 'Amelia', 'Lucas', 'Evelyn', 'Oliver', 'Ella', 'Noah', 'Charlotte', 'Sebastian', 'Grace', 'Alexander', 'Harper', 'Ethan', 'Stella', 'Daniel', 'Scarlett', 'Henry', 'Lily', 'Jackson', 'Chloe', 'Elijah', 'Piper', 'Carter', 'Zoe', 'Aiden', 'Nora' ); $middle_names = array( 'Smith', 'Doe', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor', 'Anderson', 'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Garcia', 'Martinez', 'Robinson', 'Clark', 'Rodriguez', 'Lewis', 'Lee', 'Walker', 'Hall', 'Allen', 'Young', 'Hernandez', 'King', 'Wright', 'Lopez', 'Hill', 'Scott', 'Green', 'Adams', 'Baker', 'Evans', 'Fisher' ); $username = $names[$index % count($names)] . '' . $middle_names[$index % count($middle_names)] ; return sanitize_user($username); } /** * Get website context for generating relevant author profiles */ private function get_website_context() { $site_title = get_bloginfo('name'); $site_description = get_bloginfo('description'); $locale = get_locale(); // Get main categories $categories = get_categories(array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hide_empty' => false )); $category_names = array(); foreach ($categories as $category) { if ($category->name != 'Uncategorized' && $category->name != 'Chưa phân loại') { $category_names[] = strtolower($category->name); } } return array( 'title' => $site_title, 'description' => $site_description, 'locale' => $locale, 'categories' => $category_names, 'is_vietnamese' => (strpos($locale, 'vi') !== false) ); } /** * Generate usernames using AI based on website context */ private function generate_ai_usernames($quantity, $context) { // Check if AI is available if (!class_exists('AIAutotool_AI_Helper') || !AIAutotool_AI_Helper::is_available()) { return array('success' => false, 'fallback' => true); } $language = $context['is_vietnamese'] ? 'Vietnamese' : 'English'; $categories_text = !empty($context['categories']) ? implode(', ', $context['categories']) : 'general content'; // Create AI prompt if ($context['is_vietnamese']) { $prompt = "Bạn là chuyên gia tạo tên tác giả chuyên nghiệp cho website. Website: {$context['title']} Mô tả: {$context['description']} Chủ đề: {$categories_text} Hãy tạo {$quantity} tên tác giả (author) phù hợp với nội dung website này. Yêu cầu: 1. Tên phải tự nhiên, dễ nhớ, chuyên nghiệp 2. Phù hợp với chủ đề website 3. Sử dụng tên Việt Nam thực tế (họ và tên) 4. Mỗi tên trên một dòng 5. Định dạng: Họ Tên (ví dụ: Nguyễn Văn An) 6. Không đánh số, không thêm ký tự đặc biệt 7. Tránh trùng lặp Chỉ trả về danh sách tên, mỗi tên một dòng:"; } else { $prompt = "You are an expert at creating professional author names for websites. Website: {$context['title']} Description: {$context['description']} Topics: {$categories_text} Generate {$quantity} professional author names that match this website's content. Requirements: 1. Names must be natural, memorable, and professional 2. Appropriate for the website's topic 3. Use realistic English names (first and last name) 4. One name per line 5. Format: FirstName LastName (e.g., John Smith) 6. No numbering, no special characters 7. No duplicates Return only the list of names, one per line:"; } // Call AI $result = AIAutotool_AI_Helper::smart_generate($prompt, array( 'temperature' => 0.8, 'max_tokens' => 500 )); if (!$result['success']) { return array('success' => false, 'fallback' => true); } // Parse AI response $names = $this->parse_ai_names_response($result['content'], $context['is_vietnamese']); if (empty($names)) { return array('success' => false, 'fallback' => true); } return array( 'success' => true, 'names' => $names ); } /** * Parse AI response to extract names */ private function parse_ai_names_response($response, $is_vietnamese) { $lines = explode("\n", trim($response)); $names = array(); foreach ($lines as $line) { $line = trim($line); // Skip empty lines and headers if (empty($line) || strpos($line, ':') !== false || strpos($line, 'Author') !== false || strpos($line, 'Tác giả') !== false || strlen($line) < 3) { continue; } // Remove numbering (1. 2. - * etc) $line = preg_replace('/^[\d\-\*\.\)\s]+/', '', $line); $line = trim($line); if (empty($line)) { continue; } // Validate name format $parts = explode(' ', $line); if (count($parts) >= 2) { $names[] = array( 'full_name' => $line, 'first_name' => $is_vietnamese ? end($parts) : $parts[0], 'last_name' => $is_vietnamese ? $parts[0] : end($parts) ); } } return $names; } /** * Generate username from full name */ private function create_username_from_name($first_name, $last_name, $domain) { // Remove Vietnamese accents $first = $this->remove_vietnamese_accents($first_name); $last = $this->remove_vietnamese_accents($last_name); // Create username $username = sanitize_user(strtolower($first . $last), true); // Check if exists and add number if needed $original_username = $username; $counter = 1; while (username_exists($username) || email_exists($username . '@' . $domain)) { $username = $original_username . $counter; $counter++; } return $username; } /** * Remove Vietnamese accents from string */ private function remove_vietnamese_accents($str) { $accents = array( 'à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ', 'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 'đ', 'è', 'é', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ', 'ì', 'í', 'ỉ', 'ĩ', 'ị', 'ò', 'ó', 'ỏ', 'õ', 'ọ', 'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ', 'ù', 'ú', 'ủ', 'ũ', 'ụ', 'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự', 'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ', 'À', 'Á', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ằ', 'Ắ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ầ', 'Ấ', 'Ẩ', 'Ẫ', 'Ậ', 'Đ', 'È', 'É', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ề', 'Ế', 'Ể', 'Ễ', 'Ệ', 'Ì', 'Í', 'Ỉ', 'Ĩ', 'Ị', 'Ò', 'Ó', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ồ', 'Ố', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ờ', 'Ớ', 'Ở', 'Ỡ', 'Ợ', 'Ù', 'Ú', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ừ', 'Ứ', 'Ử', 'Ữ', 'Ự', 'Ỳ', 'Ý', 'Ỷ', 'Ỹ', 'Ỵ' ); $no_accents = array( 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'y', 'y', 'y', 'y', 'y', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'I', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'Y', 'Y', 'Y', 'Y', 'Y' ); return str_replace($accents, $no_accents, $str); } /** * Determine professions based on website categories and content */ private function get_contextual_professions($context) { $category_string = implode(' ', $context['categories']); $all_content = strtolower($context['title'] . ' ' . $context['description'] . ' ' . $category_string); // Vietnamese professions $professions_vi = array( 'technology' => array('Chuyên gia Công nghệ', 'Nhà phát triển Web', 'Kỹ sư Phần mềm', 'Chuyên gia IT'), 'business' => array('Chuyên gia Kinh doanh', 'Nhà tư vấn Doanh nghiệp', 'Chiến lược gia Marketing', 'Chuyên gia Quản trị'), 'travel' => array('Blogger Du lịch', 'Nhà văn Du lịch', 'Chuyên gia Khám phá', 'Hướng dẫn viên Du lịch'), 'food' => array('Blogger Ẩm thực', 'Nhà phê bình Ẩm thực', 'Chuyên gia Nấu ăn', 'Đầu bếp'), 'health' => array('Chuyên gia Sức khỏe', 'Huấn luyện viên Thể hình', 'Chuyên gia Dinh dưỡng', 'Bác sĩ'), 'fashion' => array('Blogger Thời trang', 'Stylist', 'Nhà thiết kế Thời trang', 'Chuyên gia Xu hướng'), 'education' => array('Giảng viên', 'Chuyên gia Giáo dục', 'Nhà nghiên cứu', 'Tác giả Giáo dục'), 'finance' => array('Chuyên gia Tài chính', 'Nhà phân tích Đầu tư', 'Cố vấn Tài chính', 'Chuyên gia Kế toán'), 'marketing' => array('Chuyên gia Marketing', 'Chuyên gia SEO', 'Nhà chiến lược Nội dung', 'Chuyên gia Truyền thông'), 'default' => array('Biên tập viên', 'Nhà văn Nội dung', 'Blogger', 'Nhà sáng tạo Nội dung', 'Tác giả') ); // English professions $professions_en = array( 'technology' => array('Technology Expert', 'Web Developer', 'Software Engineer', 'IT Specialist', 'Tech Blogger'), 'business' => array('Business Expert', 'Business Consultant', 'Marketing Strategist', 'Management Specialist'), 'travel' => array('Travel Blogger', 'Travel Writer', 'Adventure Specialist', 'Travel Guide'), 'food' => array('Food Blogger', 'Food Critic', 'Culinary Expert', 'Chef'), 'health' => array('Health Expert', 'Fitness Coach', 'Nutrition Specialist', 'Wellness Consultant'), 'fashion' => array('Fashion Blogger', 'Stylist', 'Fashion Designer', 'Trend Specialist'), 'education' => array('Educator', 'Education Expert', 'Researcher', 'Educational Writer'), 'finance' => array('Finance Expert', 'Investment Analyst', 'Financial Advisor', 'Accounting Specialist'), 'marketing' => array('Marketing Expert', 'SEO Specialist', 'Content Strategist', 'Digital Marketing Consultant'), 'default' => array('Content Writer', 'Editor', 'Blogger', 'Content Creator', 'Author') ); $professions = $context['is_vietnamese'] ? $professions_vi : $professions_en; // Detect category based on keywords $keywords = array( 'technology' => array('tech', 'công nghệ', 'web', 'app', 'software', 'phần mềm', 'digital', 'ai', 'code', 'lập trình'), 'business' => array('business', 'kinh doanh', 'doanh nghiệp', 'startup', 'commerce', 'thương mại'), 'travel' => array('travel', 'du lịch', 'tour', 'khách sạn', 'hotel', 'trip', 'vacation'), 'food' => array('food', 'ẩm thực', 'recipe', 'cooking', 'nấu ăn', 'món ăn', 'restaurant', 'nhà hàng'), 'health' => array('health', 'sức khỏe', 'fitness', 'gym', 'yoga', 'medical', 'y tế', 'sport', 'thể thao'), 'fashion' => array('fashion', 'thời trang', 'style', 'beauty', 'làm đẹp', 'clothing', 'quần áo'), 'education' => array('education', 'giáo dục', 'học', 'course', 'training', 'đào tạo', 'school', 'trường'), 'finance' => array('finance', 'tài chính', 'investment', 'đầu tư', 'money', 'tiền', 'bank', 'ngân hàng'), 'marketing' => array('marketing', 'seo', 'content', 'social', 'advertising', 'quảng cáo', 'media') ); // Find matching category foreach ($keywords as $category => $words) { foreach ($words as $word) { if (strpos($all_content, $word) !== false) { return $professions[$category]; } } } return $professions['default']; } /** * Generate complete author profile data */ private function get_author_profile_data($index, $ai_first_name = '', $ai_last_name = '') { // Get website context $context = $this->get_website_context(); $is_vietnamese = $context['is_vietnamese']; // Use AI-generated names if provided, otherwise use default names if (!empty($ai_first_name) && !empty($ai_last_name)) { $first_name = $ai_first_name; $last_name = $ai_last_name; } else { // Names based on language if ($is_vietnamese) { $first_names = array( 'Minh', 'Hương', 'Tuấn', 'Lan', 'Hải', 'Mai', 'Dũng', 'Linh', 'Quân', 'Thu', 'Nam', 'Hoa', 'Bình', 'Nga', 'Trung', 'Hằng', 'Khoa', 'Trang', 'Long', 'Phương', 'Đức', 'Anh', 'Thảo', 'Hùng', 'Nhung', 'Tú', 'Thư', 'Kiên', 'Tâm', 'Hiếu', 'Vy', 'Toàn', 'Thanh', 'My', 'Khánh', 'Châu', 'Sơn', 'Dương', 'Hoàng', 'Yến' ); $last_names = array( 'Nguyễn', 'Trần', 'Lê', 'Phạm', 'Hoàng', 'Huỳnh', 'Phan', 'Vũ', 'Võ', 'Đặng', 'Bùi', 'Đỗ', 'Hồ', 'Ngô', 'Dương', 'Lý', 'Đinh', 'Đào', 'Tô', 'Mai' ); } else { $first_names = array( 'John', 'Jane', 'Michael', 'Emma', 'Samuel', 'Olivia', 'William', 'Ava', 'James', 'Isabella', 'Liam', 'Sophia', 'Logan', 'Mia', 'Benjamin', 'Amelia', 'Lucas', 'Evelyn', 'Oliver', 'Ella', 'Noah', 'Charlotte', 'Sebastian', 'Grace', 'Alexander', 'Harper', 'Ethan', 'Stella', 'Daniel', 'Scarlett', 'Henry', 'Lily', 'Jackson', 'Chloe', 'Elijah', 'Piper', 'Carter', 'Zoe', 'Aiden', 'Nora' ); $last_names = array( 'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez', 'Taylor', 'Anderson', 'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Lee', 'Walker' ); } // Select based on index $first_name = $first_names[$index % count($first_names)]; $last_name = $last_names[$index % count($last_names)]; } // Get contextual professions $professions = $this->get_contextual_professions($context); // Specialties based on categories $specialties = !empty($context['categories']) ? $context['categories'] : ($is_vietnamese ? array('nội dung số', 'viết lách', 'sáng tạo nội dung', 'truyền thông') : array('digital content', 'writing', 'content creation', 'media')); $interests = $is_vietnamese ? array('đọc sách', 'du lịch', 'nhiếp ảnh', 'âm nhạc', 'viết lách', 'công nghệ', 'nghệ thuật', 'thể thao', 'nấu ăn', 'học hỏi', 'sáng tạo') : array('reading', 'traveling', 'photography', 'music', 'writing', 'technology', 'art', 'sports', 'cooking', 'learning', 'innovation'); $profession = $professions[$index % count($professions)]; $specialty = $specialties[$index % count($specialties)]; $interest1 = $interests[$index % count($interests)]; $interest2 = $interests[($index + 3) % count($interests)]; // Generate professional bio based on language if ($is_vietnamese) { $bio_templates = array( "Xin chào! Tôi là {first_name} {last_name}, một {profession} với niềm đam mê về {specialty}. Tôi yêu thích chia sẻ kiến thức và giúp đỡ mọi người thành công. Khi không làm việc, bạn có thể thấy tôi {interest1} hoặc {interest2}.", "{first_name} {last_name} là một {profession} tận tâm chuyên về {specialty}. Với nhiều năm kinh nghiệm trong lĩnh vực này, tôi mang đến những hiểu biết sâu sắc và kiến thức thực tiễn. Ngoài công việc, tôi thích {interest1} và {interest2}.", "Chào mừng! Tôi là {first_name} {last_name}, {profession} của bạn. Chuyên môn của tôi nằm ở {specialty} và tôi cam kết tạo ra nội dung tạo nên sự khác biệt. Trong thời gian rảnh, tôi đam mê {interest1} và {interest2}.", "Là một {profession}, {first_name} {last_name} tập trung vào việc cung cấp nội dung chất lượng cao về {specialty}. Tôi tin vào sức mạnh của từ ngữ để truyền cảm hứng và giáo dục. Sở thích của tôi bao gồm {interest1} và {interest2}.", "{first_name} {last_name} đây! Tôi là một {profession} chuyên về {specialty}. Sứ mệnh của tôi là tạo ra nội dung hấp dẫn và có giá trị cho độc giả. Bên ngoài thế giới số, tôi thích {interest1} và khám phá ý tưởng mới qua {interest2}." ); } else { $bio_templates = array( "Hi, I'm {first_name} {last_name}, a passionate {profession} with extensive experience in {specialty}. I love sharing insights and helping others succeed. When I'm not working, you can find me {interest1} or {interest2}.", "{first_name} {last_name} is a dedicated {profession} specializing in {specialty}. With years of experience in the field, I bring valuable insights and practical knowledge to every piece of content. Outside of work, I enjoy {interest1} and {interest2}.", "Welcome! I'm {first_name} {last_name}, your friendly {profession}. My expertise lies in {specialty}, and I'm committed to creating content that makes a difference. In my spare time, I'm passionate about {interest1} and {interest2}.", "As a {profession}, {first_name} {last_name} focuses on delivering high-quality content centered around {specialty}. I believe in the power of words to inspire and educate. My hobbies include {interest1} and {interest2}.", "{first_name} {last_name} here! I'm a {profession} who specializes in {specialty}. My mission is to create engaging, informative content that resonates with readers. Outside the digital world, I enjoy {interest1} and exploring new ideas through {interest2}." ); } $bio = $bio_templates[$index % count($bio_templates)]; $bio = str_replace( array('{first_name}', '{last_name}', '{profession}', '{specialty}', '{interest1}', '{interest2}'), array($first_name, $last_name, $profession, $specialty, $interest1, $interest2), $bio ); // Display name format based on language $display_name = $is_vietnamese ? $last_name . ' ' . $first_name : $first_name . ' ' . $last_name; return array( 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, 'nickname' => $first_name . $last_name, 'bio' => $bio, 'website' => get_site_url() ); } public function render_tab_setting() { if ($this->active=='true') { echo ''; } } public function render_feature() { // Load feature descriptions require_once plugin_dir_path(dirname(__FILE__)) . 'lib/feature-descriptions.php'; $autoToolBox = new AutoToolBox( esc_html__('Random User Generator','ai-auto-tool'), AiAutoTool_Feature_Descriptions::get('random_user'), "https://doc.aiautotool.com/features/random-user", $this->active_option_name, $this->active, plugins_url('../../images/logo.svg', __FILE__) ); echo ($autoToolBox->generateHTML()); } public function auto_generate_username($user_id) { $user_count = get_option($this->user_count_option, 1); for ($i = 0; $i < $user_count; $i++) { $user_info = get_userdata($user_id); if (empty($user_info->user_login)) { $username = $this->generate_username($user_info->user_email); wp_update_user(array('ID' => $user_id, 'user_login' => $username)); } } } private function generate_username($email) { $username = strstr($email, '@', true); $username_exists = username_exists($username); $i = 1; while ($username_exists) { $new_username = $username . $i; $username_exists = username_exists($new_username); $i++; } return $new_username; } } // End of aiautotool_AutoGenerateUsername class } // End of class_exists check