// ========================= // AJAX توليد المقال - النسخة المحسنة للتنسيق والروابط // ========================= add_action('wp_ajax_generate_ai_article_ultimate', function(){ if (!current_user_can('manage_options')) wp_send_json_error(['msg' => 'غير مصرح']); if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'ai_nonce')) wp_send_json_error(['msg' => 'رمز غير صالح']); $topic = sanitize_text_field($_POST['topic'] ?? ''); $keywords = sanitize_text_field($_POST['keywords'] ?? ''); $compare_mode = intval($_POST['compare_mode'] ?? 0); $app1_name = sanitize_text_field($_POST['app1_name'] ?? ''); $app2_name = sanitize_text_field($_POST['app2_name'] ?? ''); $google_play_input = esc_url_raw($_POST['google_play'] ?? ''); $content_type = sanitize_text_field($_POST['content_type'] ?? 'app'); $custom_links_raw = wp_kses_post($_POST['custom_links'] ?? ''); $encrypted = get_option('ai_key_encrypted'); $api_key = $encrypted ? ai_decrypt_api_key($encrypted) : ''; $settings = ai_seo_get_settings(); if (empty($topic)) wp_send_json_error(['msg' => 'الموضوع مطلوب']); if (empty($api_key)) wp_send_json_error(['msg' => 'مفتاح API غير موجود']); // ============================================= // 0️⃣ المتغيرات العامة // ============================================= $used_urls = []; $used_anchors = []; // ============================================= // 🔍 جلب بيانات المقارنة (إذا اختار المستخدم) // ============================================= $comparison_data = []; if ($compare_mode == 1 && !empty($app1_name) && !empty($app2_name)) { $apps = [$app1_name, $app2_name]; foreach ($apps as $app_name) { $post = get_posts([ 's' => $app_name, 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'post' ]); if (!empty($post)) { $post_id = $post[0]->ID; $img_url = get_the_post_thumbnail_url($post_id, 'medium') ?: ''; $play_link = get_google_play_link_from_post($post_id); $comparison_data[$app_name] = [ 'name' => $app_name, 'img' => $img_url, 'play_link' => $play_link, 'post_id' => $post_id, 'post_url' => get_permalink($post_id) ]; if (!empty($play_link)) $used_urls[] = $play_link; if (!empty($post_id)) $used_urls[] = get_permalink($post_id); } else { $comparison_data[$app_name] = [ 'name' => $app_name, 'img' => '', 'play_link' => '', 'post_id' => 0, 'post_url' => '' ]; } } } // ============================================= // 🔍 جلب الروابط الداخلية الذكية - محسنة // ============================================= $keywords_array = array_filter(array_map('trim', explode(',', $keywords))); $main_keyword = $keywords_array[0] ?? $topic; $app_name_main = preg_replace('/^(تطبيق|لعبة|برنامج|تحميل|شرح|مراجعة)\s+/i', '', $topic); $app_name_main = trim($app_name_main); // استعلام محسن باستخدام WP_Query $args = [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 100, 'orderby' => 'relevance', 's' => $app_name_main, 'sentence' => true, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ]; $query = new WP_Query($args); $smart_links = []; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $post_url = get_permalink(); $post_title = get_the_title(); $post_cats = wp_get_post_categories($post_id, ['fields' => 'names']); // تجنب المنشورات المستخدمة مسبقاً if (in_array($post_url, $used_urls)) continue; // تحديد نوع المحتوى من التصنيفات $is_game = false; $is_app = false; foreach ($post_cats as $cat) { if (stripos($cat, 'لعبة') !== false || stripos($cat, 'game') !== false) $is_game = true; if (stripos($cat, 'تطبيق') !== false || stripos($cat, 'app') !== false) $is_app = true; } // إذا كان نوع المحتوى الحالي لعبة، نأخذ فقط المنشورات المصنفة كلعبة أو ليس لها تصنيف محدد if ($content_type === 'game' && $is_app && !$is_game) continue; if ($content_type === 'app' && $is_game && !$is_app) continue; // حساب درجة الصلة $relevance_score = 0; $title_lower = mb_strtolower($post_title); $content_lower = mb_strtolower(get_the_content()); foreach ([$main_keyword, $app_name_main] as $word) { if (mb_strpos($title_lower, mb_strtolower($word)) !== false) $relevance_score += 10; if (mb_strpos($content_lower, mb_strtolower($word)) !== false) $relevance_score += 3; } if ($relevance_score < 5) continue; // إنشاء اسم العرض $clean_name = preg_replace('/^(تحميل|تطبيق|لعبة|برنامج|أفضل|شرح|طريقة|كيفية|مراجعة|تنزيل|دليل)\s+/iu', '', $post_title); $clean_name = trim($clean_name); if (mb_strlen($clean_name) > 35) $clean_name = mb_substr($clean_name, 0, 32) . '...'; $type_icon = ($content_type === 'game') ? '🎮' : '📱'; $type_text = ($content_type === 'game') ? 'لعبة' : 'تطبيق'; $display_name = "{$type_icon} {$type_text}: {$clean_name}"; if (in_array($display_name, $used_anchors)) continue; $smart_links[] = [ 'display_name' => $display_name, 'url' => $post_url, 'score' => $relevance_score, 'img' => get_the_post_thumbnail_url($post_id, 'thumbnail') ?: '', 'post_id' => $post_id ]; $used_anchors[] = $display_name; $used_urls[] = $post_url; if (count($smart_links) >= 15) break; } wp_reset_postdata(); } // ترتيب حسب الصلة usort($smart_links, fn($a, $b) => $b['score'] <=> $a['score']); // ============================================= // 📌 الروابط السياقية (حسب الإعدادات) // ============================================= $contextual_links_text = ''; $contextual_links = []; if ($settings['enable_contextual_links'] && !empty($smart_links)) { $contextual_links = array_slice($smart_links, 0, $settings['contextual_links_count']); $contextual_links_text = "الروابط الداخلية المطلوب إضافتها ضمن النص (لا تكرر أي رابط):\n"; foreach ($contextual_links as $index => $link) { $contextual_links_text .= ($index + 1) . '. "' . $link['display_name'] . '" → ' . $link['url'] . "\n"; } $contextual_links_text .= "\nتعليمات: أضف هذه الروابط بشكل طبيعي في أماكن مناسبة داخل الفقرات، وليس في قائمة منفصلة."; } // ============================================= // 📦 قسم المشابهات (يتم عرضه في نهاية المقال) // ============================================= $related_section = ''; if ($settings['enable_related_section'] && !empty($smart_links)) { // استبعاد الروابط المستخدمة سياقياً $contextual_urls = array_map(function($link) { return $link['url']; }, $contextual_links); $filtered_related = array_filter($smart_links, function($link) use ($contextual_urls) { return !in_array($link['url'], $contextual_urls); }); $related_links = array_slice($filtered_related, 0, $settings['related_section_count']); if (!empty($related_links)) { $img_size = $settings['related_image_size']; $img_width = $img_size === 'small' ? '50px' : ($img_size === 'medium' ? '80px' : '120px'); $img_height = $img_width; if ($settings['related_section_style'] === 'grid') { $related_section = '

🔗 ' . ($content_type === 'game' ? 'ألعاب مشابهة قد تهمك' : 'تطبيقات مشابهة قد تهمك') . '

'; } else { $related_section = '

🔗 ' . ($content_type === 'game' ? 'ألعاب مشابهة قد تهمك' : 'تطبيقات مشابهة قد تهمك') . '

'; } } } // ============================================= // 📸 قسم المقارنة (إذا كان المستخدم يريد مقارنة اثنين) // ============================================= $comparison_section = ''; if ($compare_mode == 1 && !empty($comparison_data)) { $appA = $comparison_data[$app1_name]; $appB = $comparison_data[$app2_name]; $comparison_section = '

📊 مقارنة شاملة: ' . esc_html($app1_name) . ' vs ' . esc_html($app2_name) . '

' . (!empty($appA['img']) ? '' . esc_attr($appA['name']) . '' : '') . '

' . esc_html($appA['name']) . '

' . (!empty($appA['play_link']) ? '📱 تحميل الآن' : '') . '
' . (!empty($appB['img']) ? '' . esc_attr($appB['name']) . '' : '') . '

' . esc_html($appB['name']) . '

' . (!empty($appB['play_link']) ? '📱 تحميل الآن' : '') . '

⚖️ جدول المقارنة

الميزة' . esc_html($app1_name) . '' . esc_html($app2_name) . '
سهولة الاستخدام★★★★★★★★★☆
الأداء والسرعة★★★★★★★★★☆
جودة المحتوى★★★★★★★★★★
التحديثات والدعم★★★★☆★★★★★
السعر (إن وجد)مجاني / مدفوعمجاني / مدفوع
'; } // ============================================= // 📲 خطوات التثبيت // ============================================= $install_steps = ''; if ($settings['enable_install_steps'] && !empty($google_play_input) && strpos($google_play_input, 'play.google.com') !== false) { $app_display_name = preg_replace('/^(تطبيق|لعبة|برنامج|تحميل|شرح)\s+/i', '', $topic); $app_display_name = trim($app_display_name); $install_steps = '

📲 خطوات تحميل وتثبيت ' . esc_html($app_display_name) . '

  1. 📱 افتح متجر Google Play - ابحث عن أيقونة المتجر على هاتفك الأندرويد
  2. 🔍 ابحث عن ' . esc_html($app_display_name) . ' - استخدم شريط البحث واكتب اسم ' . ($content_type === 'game' ? 'اللعبة' : 'التطبيق') . '
  3. ✅ اختر ' . ($content_type === 'game' ? 'اللعبة' : 'التطبيق') . ' الرسمي - تأكد من أن المطور هو الناشر الرسمي
  4. 📥 اضغط على زر "تثبيت" - انتظر حتى اكتمال التحميل والتثبيت
  5. 🚀 افتح ' . ($content_type === 'game' ? 'اللعبة' : 'التطبيق') . ' - بعد التثبيت، يمكنك البدء فوراً
📱 تحميل ' . esc_html($app_display_name) . ' من Google Play الآن
'; } // الروابط المخصصة $custom_links_html = ''; if (!empty($custom_links_raw)) { $custom_links_html = ''; } // ============================================= // 🧠 بناء الـ Prompt الديناميكي (محسن للتنسيق) // ============================================= $type_text = ($content_type === 'game') ? 'اللعبة' : 'التطبيق'; $type_name = ($content_type === 'game') ? 'لعبة' : 'تطبيق'; // هيكل المقال الأساسي $structures = [ 'standard' => [ 'title_hook' => 'مقدمة ممتدة تبدأ بقصة قصيرة أو موقف شخصي', 'sections' => [ "ما هو {$type_text} {$app_name_main}؟", "أهم المميزات التي جذبتني في {$type_text}", "عيوب قد تواجهك (بصراحة)", "تجربتي الشخصية مع {$type_text}", "نصائح من قلب التجربة", "الأسئلة الشائعة", "الخلاصة: هل أنصح بـ {$type_text}؟" ] ], 'comparative' => [ 'title_hook' => 'مقدمة مقارنة بين ' . $type_text . ' والمنافسين', 'sections' => [ "لماذا تحتاج {$type_text} {$app_name_main}؟", "نظرة شاملة على الميزات", "نقاط الضعف التي قد لا يخبرك بها أحد", "جدول مقارنة تقني شامل", "تجارب المستخدمين الحقيقية", "نصائح لتحقيق أقصى استفادة", "قرار الشراء: نعم أم لا؟" ] ] ]; $random_structure = $structures[array_rand($structures)]; $sections_list = array_map(function($section) use ($app_name_main, $main_keyword) { $section = str_replace('{app_name}', $app_name_main, $section); if (strpos($section, $main_keyword) === false) { $section .= " – دليل {$main_keyword}"; } return $section; }, $random_structure['sections']); $headings_html = "

" . implode("

\n

", $sections_list) . "

"; // تعليمات التنسيق الصارمة $formatting_instructions = " **تعليمات تنسيق HTML صارمة (يجب الالتزام بها):** - اكتب المقال كاملاً داخل . - استخدم

مرة واحدة فقط للعنوان الرئيسي. - استخدم

لكل قسم رئيسي. - استخدم

للعناوين الفرعية داخل الأقسام. - كل فقرة يجب أن تكون داخل

...

. - لا تترك فراغات أو علامات غير مغلقة. - لا تستخدم
إلا في حالات الضرورة القصوى. - لا تضيف أي علامات