prefix . 'ai_content_wizard_settings'; $post_logs_table = $wpdb->prefix . 'ai_content_wizard_posts_logs'; $plan_logs_table = $wpdb->prefix . 'ai_content_wizard_content_plan_logs'; $missing = false; if ( $wpdb->get_var( "SHOW TABLES LIKE '$settings_table'" ) != $settings_table ) { $missing = true; } if ( $wpdb->get_var( "SHOW TABLES LIKE '$post_logs_table'" ) != $post_logs_table ) { $missing = true; } if ( $wpdb->get_var( "SHOW TABLES LIKE '$plan_logs_table'" ) != $plan_logs_table ) { $missing = true; } if ( $missing ) { ai_content_wizard_on_activation(); } } /** * Основная функция отображения страницы мастера настройки плагина. */ function ai_content_wizard_wizard_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Access Denied', 'ai-content-wizard' ) ); } // Запускаем буферизацию вывода ob_start(); // Если передан параметр manual=1 – завершаем мастер и переходим в настройки API if ( isset( $_GET['manual'] ) && $_GET['manual'] == '1' ) { update_option( 'ai_content_wizard_wizard_completed', 1 ); wp_safe_redirect( esc_url( admin_url( 'admin.php?page=ai_content_wizard_api_settings' ) ) ); exit; } // Если передан параметр finish_wizard=1 – завершаем мастер и возвращаемся на главную страницу плагина if ( isset( $_GET['finish_wizard'] ) && $_GET['finish_wizard'] == '1' ) { update_option( 'ai_content_wizard_wizard_completed', 1 ); wp_safe_redirect( esc_url( admin_url( 'admin.php?page=ai_content_wizard' ) ) ); exit; } // Определяем шаги мастера $steps = array( 'welcome', 'system_check', 'text_provider', 'image_provider', 'notifications', 'finish' ); $current_step = isset( $_REQUEST['ai_content_wizard_wizard_step'] ) ? sanitize_text_field( $_REQUEST['ai_content_wizard_wizard_step'] ) : 'welcome'; $completed_steps = get_option( 'ai_content_wizard_completed_steps', array() ); // Обработка кнопки Skip – пропускаем текущий шаг if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['skip_step'] ) ) { $current_step = ai_content_wizard_get_next_step( $current_step, $steps ); } // Обработка отправки формы (если не нажата кнопка Back и не Skip) if ( $_SERVER['REQUEST_METHOD'] === 'POST' && ! isset( $_POST['back_step'] ) && ! isset( $_POST['skip_step'] ) ) { $error_occured = false; // Флаг ошибки switch ( $current_step ) { case 'system_check': ai_content_wizard_check_and_create_tables(); break; case 'text_provider': $text_provider = sanitize_text_field( $_POST['text_provider'] ?? '' ); $error = ''; $modal_to_show = ''; if ( empty( $text_provider ) ) { $error = __( 'Please select a text provider.', 'ai-content-wizard' ); $modal_to_show = 'showGenericErrorModal'; } else { // Проверяем API-ключ для выбранного провайдера if ( $text_provider === 'openai' ) { $openai_key = sanitize_text_field( $_POST['ai_content_wizard_api_key'] ?? '' ); $selected_model = sanitize_text_field( $_POST['text_model_selector'] ?? 'gpt-4o-mini' ); if ( empty( $openai_key ) ) { $error = __( 'OpenAI API key is required.', 'ai-content-wizard' ); $modal_to_show = 'showGenericErrorModal'; } elseif ( ! is_valid_openai_api_key( $openai_key, $selected_model ) ) { $error = __( 'The OpenAI API key is invalid.', 'ai-content-wizard' ); $modal_to_show = 'showOpenAIErrorModal'; } } elseif ( $text_provider === 'anthropic' ) { $anthropic_key = sanitize_text_field( $_POST['ai_content_wizard_anthropic_api_key'] ?? '' ); $selected_model = sanitize_text_field( $_POST['text_model_selector'] ?? 'claude-3-5-sonnet-20241022' ); if ( empty( $anthropic_key ) ) { $error = __( 'Anthropic API key is required.', 'ai-content-wizard' ); $modal_to_show = 'showGenericErrorModal'; } elseif ( ! is_valid_anthropic_api_key( $anthropic_key, $selected_model ) ) { $error = __( 'The Anthropic API key is invalid.', 'ai-content-wizard' ); $modal_to_show = 'showAnthropicErrorModal'; } } elseif ( $text_provider === 'deepseek' ) { $deepseek_key = sanitize_text_field( $_POST['deepseek_api_key'] ?? '' ); $selected_model = sanitize_text_field( $_POST['text_model_selector'] ?? 'gpt-4o-mini' ); if ( empty( $deepseek_key ) ) { $error = __( 'DeepSeek API key is required.', 'ai-content-wizard' ); $modal_to_show = 'showGenericErrorModal'; } elseif ( ! is_valid_deepseek_api_key( $deepseek_key, $selected_model ) ) { $error = __( 'The DeepSeek API key is invalid.', 'ai-content-wizard' ); $modal_to_show = 'showDeepSeekErrorModal'; } } } // Проверяем, что модель выбрана if ( empty( $_POST['text_model_selector'] ) ) { $error = __( 'Please select a text model.', 'ai-content-wizard' ); $modal_to_show = 'showGenericErrorModal'; } if ( ! empty( $error ) ) { $error_occured = true; // Показываем соответствующее модальное окно echo ''; break; } // Сохраняем настройки провайдера и модели ai_content_wizard_update_setting( 'ai_content_wizard_text_provider', $text_provider ); if ( $text_provider === 'openai' ) { ai_content_wizard_update_setting( 'ai_content_wizard_api_key', $openai_key ); } elseif ( $text_provider === 'anthropic' ) { ai_content_wizard_update_setting( 'ai_content_wizard_anthropic_api_key', $anthropic_key ); } elseif ( $text_provider === 'deepseek' ) { ai_content_wizard_update_setting( 'deepseek_api_key', $deepseek_key ); } ai_content_wizard_update_setting( 'ai_content_wizard_text_model', sanitize_text_field( $_POST['text_model_selector'] ) ); break; case 'image_provider': $img_provider = sanitize_text_field( $_POST['image_provider'] ?? 'dalle' ); ai_content_wizard_update_setting( 'ai_content_wizard_image_provider', $img_provider ); // Если для провайдера требуется API‑ключ – проверяем его наличие if ( $img_provider === 'deepai' ) { $img_api_key = sanitize_text_field( $_POST['ai_content_wizard_deepai_api_key'] ?? '' ); if ( empty( $img_api_key ) ) { $error = __( 'DeepAI API key is required for DeepAI provider.', 'ai-content-wizard' ); } else { ai_content_wizard_update_setting( 'ai_content_wizard_deepai_api_key', $img_api_key ); } } elseif ( $img_provider === 'stablediffusion' ) { $img_api_key = sanitize_text_field( $_POST['ai_content_wizard_stable_diffusion_api_key'] ?? '' ); if ( empty( $img_api_key ) ) { $error = __( 'Stable Diffusion API key is required for Stable Diffusion provider.', 'ai-content-wizard' ); } else { ai_content_wizard_update_setting( 'ai_content_wizard_stable_diffusion_api_key', $img_api_key ); } } elseif ( $img_provider === 'ideogram' ) { $img_api_key = sanitize_text_field( $_POST['ideogram_api_key'] ?? '' ); if ( empty( $img_api_key ) ) { $error = __( 'Ideogram API key is required for Ideogram provider.', 'ai-content-wizard' ); } else { ai_content_wizard_update_setting( 'ideogram_api_key', $img_api_key ); } } // Проверяем, что модель выбрана if ( empty( $_POST['image_model_selector'] ) ) { $error = __( 'Please select an image model.', 'ai-content-wizard' ); } if ( ! empty( $error ) ) { $error_occured = true; echo ''; break; } ai_content_wizard_update_setting( 'ai_content_wizard_image_model', sanitize_text_field( $_POST['image_model_selector'] ) ); ai_content_wizard_update_image_model_parameters( sanitize_text_field( $_POST['image_model_selector'] ) ); break; case 'notifications': $enable_email = isset( $_POST['enable_email_notifications'] ) ? 1 : 0; ai_content_wizard_update_setting( 'ai_content_wizard_enable_email_notifications', $enable_email ); if ( isset( $_POST['email_address'] ) ) { ai_content_wizard_update_setting( 'ai_content_wizard_email_address', sanitize_email( $_POST['email_address'] ) ); } $enable_tg = isset( $_POST['enable_telegram_notifications'] ) ? 1 : 0; ai_content_wizard_update_setting( 'ai_content_wizard_enable_telegram_notifications', $enable_tg ); if ( isset( $_POST['telegram_bot_token'] ) ) { ai_content_wizard_update_setting( 'ai_content_wizard_telegram_bot_token', sanitize_text_field( $_POST['telegram_bot_token'] ) ); } if ( isset( $_POST['telegram_chat_id'] ) ) { ai_content_wizard_update_setting( 'ai_content_wizard_telegram_chat_id', sanitize_text_field( $_POST['telegram_chat_id'] ) ); } break; case 'finish': // Сохраняем дефолтные настройки ai_content_wizard_update_setting('ai_content_wizard_enable_image_generation', 1); ai_content_wizard_update_setting('ai_content_wizard_enable_additional_images', 1); ai_content_wizard_update_setting('ai_content_wizard_additional_images_count', 2); ai_content_wizard_update_setting('ai_content_wizard_enable_toc_generation', 1); ai_content_wizard_update_setting('ai_content_wizard_enable_tags_generation', 1); update_option( 'ai_content_wizard_wizard_completed', 1 ); if ( ! headers_sent() ) { ob_end_clean(); wp_safe_redirect( esc_url( admin_url( 'admin.php?page=ai_content_wizard_settings' ) ) ); exit; } else { echo ''; exit; } break; } if ( ! $error_occured && $current_step !== 'finish' && ! isset( $_POST['back_step'] ) ) { $completed_steps = get_option( 'ai_content_wizard_completed_steps', array() ); if ( ! in_array( $current_step, $completed_steps ) ) { $completed_steps[] = $current_step; update_option( 'ai_content_wizard_completed_steps', $completed_steps ); } $current_step = ai_content_wizard_get_next_step( $current_step, $steps ); } } ?>