// ========================= // 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 = '
| الميزة | ' . esc_html($app1_name) . ' | ' . esc_html($app2_name) . ' |
|---|---|---|
| سهولة الاستخدام | ★★★★★ | ★★★★☆ |
| الأداء والسرعة | ★★★★★ | ★★★★☆ |
| جودة المحتوى | ★★★★★ | ★★★★★ |
| التحديثات والدعم | ★★★★☆ | ★★★★★ |
| السعر (إن وجد) | مجاني / مدفوع | مجاني / مدفوع |
...
. - لا تترك فراغات أو علامات غير مغلقة. - لا تستخدم