'_yoast_wpseo_title', 'description' => '_yoast_wpseo_metadesc', 'keyword' => '_yoast_wpseo_focuskw' ]; } elseif (is_plugin_active('seo-by-rank-math/rank-math.php')) { $seo_fields = [ 'title' => 'rank_math_title', 'description' => 'rank_math_description', 'keyword' => 'rank_math_focus_keyword' ]; } elseif (is_plugin_active('all-in-one-seo-pack/all_in_one_seo_pack.php')) { $seo_fields = [ 'title' => '_aioseo_title', 'description' => '_aioseo_description', 'keyword' => '_aioseo_keywords' ]; } return $seo_fields; } } if (!function_exists('ai_content_wizard_generate_seo_elements')) { /** * Генерирует SEO элементы для поста. * * @param int $post_id Идентификатор поста. * @param string $keyword Ключевое слово. * @param string $selected_language Выбранный язык. * * @return array Массив с результатом генерации и токенами [успех, input_tokens, output_tokens]. */ function ai_content_wizard_generate_seo_elements($post_id, $keyword, $selected_language) { // Получаем текущую модель и определяем принадлежность к OpenAI $text_model = ai_content_wizard_get_setting('ai_content_wizard_text_model', '') ?? ''; if (!is_string($text_model)) { $text_model = ''; } $text_model = trim($text_model); $is_openai_model = ai_content_wizard_is_openai_model($text_model); if (!$is_openai_model) { error_log('AI Content Wizard: No valid OpenAI model configured for SEO generation.'); return [false, 0, 0]; } // Подключаем файлы для генерации SEO элементов (только для OpenAI) $openai_file = AI_CONTENT_WIZARD_PATH . 'includes/text-generation/seo-generation/openai/openai_generate_seo_elements.php'; if (file_exists($openai_file)) { require_once $openai_file; } else { error_log('AI Content Wizard: OpenAI generate SEO elements file not found.'); return [false, 0, 0]; } // Определяем API-ключ на основе выбранной модели (только для OpenAI) $api_key = ai_content_wizard_get_setting('ai_content_wizard_api_key', ''); if (empty($api_key)) { error_log('AI Content Wizard: API key is empty for the selected model.'); return [false, 0, 0]; } // Проверка активного SEO плагина $seo_fields = ai_content_wizard_check_seo_plugin(); if (empty($seo_fields)) { error_log('AI Content Wizard: No active SEO plugin found.'); return [false, 0, 0]; } $input_tokens = 0; $output_tokens = 0; // Получение контента поста $article_content = get_post_field('post_content', $post_id); $article_content = is_string($article_content) ? $article_content : ''; // Получение категорий поста $categories = get_the_category($post_id); $category_id = !empty($categories) ? 'category-' . $categories[0]->term_id : null; $use_global_settings = is_null($category_id) ? 1 : ai_content_wizard_get_category_setting($category_id, 'use_global_settings', 1); // Выбор настроек формата $format = ($use_global_settings || is_null($category_id)) ? ai_content_wizard_get_setting('ai_content_wizard_format', 'post') : ai_content_wizard_get_category_setting($category_id, 'format', 'post'); $site_name = get_bloginfo('name'); $content_snippet = mb_substr($article_content, 0, 500); $language_full_name = get_full_language_name($selected_language); // Проверка включения генерации тегов $enable_tags_generation = ai_content_wizard_get_setting('ai_content_wizard_enable_tags_generation', 0); if ($enable_tags_generation) { if (function_exists('generate_seo_tags_openai')) { // Используем OpenAI для генерации тегов [$generated_tags, $tags_input_tokens, $tags_output_tokens] = generate_seo_tags_openai($content_snippet, $language_full_name, $api_key, $text_model); } else { error_log('AI Content Wizard: No valid OpenAI function defined for tag generation.'); $generated_tags = ''; $tags_input_tokens = 0; $tags_output_tokens = 0; } if (!empty($generated_tags)) { $generated_tags = str_replace(' – ', ', ', $generated_tags); $tags_array = array_map('trim', explode(',', $generated_tags)); $tags_array = array_filter($tags_array); wp_set_post_tags($post_id, $tags_array, true); $input_tokens += $tags_input_tokens; $output_tokens += $tags_output_tokens; } else { error_log('AI Content Wizard: Generated tags are empty.'); } } // Генерация SEO заголовка и описания через OpenAI if (function_exists('generate_seo_title_description_openai')) { [$seo_title, $seo_description, $title_input_tokens, $title_output_tokens, $desc_input_tokens, $desc_output_tokens] = generate_seo_title_description_openai($content_snippet, $format, $language_full_name, $keyword, $site_name, $api_key, $text_model); } else { error_log('AI Content Wizard: No valid OpenAI function defined for SEO title and description generation.'); return [false, 0, 0]; } $input_tokens += $title_input_tokens + $desc_input_tokens; $output_tokens += $title_output_tokens + $desc_output_tokens; // Улучшение SEO заголовка, если включено (только OpenAI) if (ai_content_wizard_get_setting('ai_content_wizard_enhanced_generation_quality', 0) == 1) { $keyword_in_seo_title = stripos($seo_title, $keyword) === 0; if (!$keyword_in_seo_title) { if (function_exists('refine_seo_title_openai')) { [$seo_title, $seo_title_input_tokens, $seo_title_output_tokens] = refine_seo_title_openai($seo_title, $keyword, $api_key, $text_model, $language_full_name); } else { error_log('AI Content Wizard: No valid OpenAI function defined for SEO title refinement.'); $seo_title_input_tokens = 0; $seo_title_output_tokens = 0; } $input_tokens += $seo_title_input_tokens; $output_tokens += $seo_title_output_tokens; } } // Улучшение SEO описания, если включено (только OpenAI) if (ai_content_wizard_get_setting('ai_content_wizard_enhanced_generation_quality', 0) == 1) { $keyword_in_seo_description = stripos($seo_description, $keyword) === 0; if (!$keyword_in_seo_description) { if (function_exists('refine_seo_description_openai')) { [$seo_description, $seo_desc_input_tokens, $seo_desc_output_tokens] = refine_seo_description_openai($seo_description, $keyword, $api_key, $text_model, $language_full_name); } else { error_log('AI Content Wizard: No valid OpenAI function defined for SEO description refinement.'); $seo_desc_input_tokens = 0; $seo_desc_output_tokens = 0; } $input_tokens += $seo_desc_input_tokens; $output_tokens += $seo_desc_output_tokens; } } // Сохранение обновленных SEO элементов update_post_meta($post_id, $seo_fields['title'], $seo_title); update_post_meta($post_id, $seo_fields['description'], $seo_description); if (isset($seo_fields['keyword'])) { update_post_meta($post_id, $seo_fields['keyword'], $keyword); } return [true, $input_tokens, $output_tokens]; } }