$label ) { $checked = ( $current_val === $key ) ? 'checked' : ''; $class = in_array( $key, $available_models ) ? '' : 'unavailable'; echo ''; // Используем wp_kses для разрешения тега echo ''; } } /* ------------------ Main Settings Page ------------------ */ function ai_content_wizard_api_settings_page() { // Если была изменена модель изображений, показываем уведомление if ( get_transient( 'ai_content_wizard_image_model_changed' ) ) { echo '

' . esc_html__( 'You have changed the image model. Settings have been automatically updated.', 'ai-content-wizard' ) . '

'; delete_transient( 'ai_content_wizard_image_model_changed' ); } // Получаем текущие настройки $api_key = ai_content_wizard_get_setting( 'ai_content_wizard_api_key', '' ); $anthropic_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_anthropic_api_key', '' ); $deepai_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_deepai_api_key', '' ); $deepseek_api_key = ai_content_wizard_get_setting( 'deepseek_api_key', '' ); $stable_diffusion_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_stable_diffusion_api_key', '' ); $bing_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_bing_api_key', '' ); $google_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_google_api_key', '' ); $google_cse_id = ai_content_wizard_get_setting( 'ai_content_wizard_google_cse_id', '' ); $rss_feed_url = ai_content_wizard_get_setting( 'ai_content_wizard_rss_feed_url', '' ); $text_model = ai_content_wizard_get_setting( 'ai_content_wizard_text_model', 'gpt-4o-mini' ); $image_model = ai_content_wizard_get_setting( 'ai_content_wizard_image_model', 'dall-e-3' ); $models = ai_content_wizard_get_setting( 'ai_content_wizard_available_models', array() ); $fine_tuned_models = ai_content_wizard_get_setting( 'ai_content_wizard_fine_tuned_models', array() ); if ( ! is_array( $models ) ) { $models = array(); } if ( ! is_array( $fine_tuned_models ) ) { $fine_tuned_models = array(); } // Обработка сброса ключей API if ( isset( $_POST['reset_api_key'] ) && $_POST['reset_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_api_key', '' ); $api_key = ''; } if ( isset( $_POST['reset_anthropic_api_key'] ) && $_POST['reset_anthropic_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_anthropic_api_key', '' ); $anthropic_api_key = ''; } if ( isset( $_POST['reset_deepseek_api_key'] ) && $_POST['reset_deepseek_api_key'] === '1' ) { ai_content_wizard_update_setting( 'deepseek_api_key', '' ); $deepseek_api_key = ''; } if ( isset( $_POST['reset_deepai_api_key'] ) && $_POST['reset_deepai_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_deepai_api_key', '' ); $deepai_api_key = ''; } if ( isset( $_POST['reset_stable_diffusion_api_key'] ) && $_POST['reset_stable_diffusion_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_stable_diffusion_api_key', '' ); $stable_diffusion_api_key = ''; } if ( isset( $_POST['reset_bing_api_key'] ) && $_POST['reset_bing_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_bing_api_key', '' ); $bing_api_key = ''; } if ( isset( $_POST['reset_google_api_key'] ) && $_POST['reset_google_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_google_api_key', '' ); $google_api_key = ''; } if ( isset( $_POST['reset_google_cse_id'] ) && $_POST['reset_google_cse_id'] === '1' ) { ai_content_wizard_update_setting( 'ai_content_wizard_google_cse_id', '' ); $google_cse_id = ''; } // Новые сбросы для Ideogram if ( isset( $_POST['reset_ideogram_api_key'] ) && $_POST['reset_ideogram_api_key'] === '1' ) { ai_content_wizard_update_setting( 'ideogram_api_key', '' ); } // Обработка сохранения формы и валидация ключей API $modalToShow = ''; // переменная для показа модального окна (будет установлена ниже) if ( $_SERVER['REQUEST_METHOD'] === 'POST' && ( isset( $_POST['ai_content_wizard_api_key'] ) || isset( $_POST['ai_content_wizard_anthropic_api_key'] ) ) ) { $is_reset = ( ( isset( $_POST['reset_api_key'] ) && $_POST['reset_api_key'] === '1' ) || ( isset( $_POST['reset_anthropic_api_key'] ) && $_POST['reset_anthropic_api_key'] === '1' ) || ( isset( $_POST['reset_deepai_api_key'] ) && $_POST['reset_deepai_api_key'] === '1' ) || ( isset( $_POST['reset_stable_diffusion_api_key'] ) && $_POST['reset_stable_diffusion_api_key'] === '1' ) || ( isset( $_POST['reset_bing_api_key'] ) && $_POST['reset_bing_api_key'] === '1' ) || ( isset( $_POST['reset_google_api_key'] ) && $_POST['reset_google_api_key'] === '1' ) || ( isset( $_POST['reset_google_cse_id'] ) && $_POST['reset_google_cse_id'] === '1' ) ); $temp_api_key = sanitize_text_field( $_POST['ai_content_wizard_api_key'] ); $temp_anthropic_api_key = sanitize_text_field( $_POST['ai_content_wizard_anthropic_api_key'] ); $temp_deepseek_api_key = sanitize_text_field( $_POST['deepseek_api_key'] ); $selected_text_model = sanitize_text_field( $_POST['ai_content_wizard_text_model'] ); $image_model_in = sanitize_text_field( $_POST['ai_content_wizard_image_model'] ); $bing_api_key_in = sanitize_text_field( $_POST['ai_content_wizard_bing_api_key'] ); $google_api_key_in = sanitize_text_field( $_POST['ai_content_wizard_google_api_key'] ); $google_cse_id_in = sanitize_text_field( $_POST['ai_content_wizard_google_cse_id'] ); $rss_feed_url_in = sanitize_text_field( $_POST['ai_content_wizard_rss_feed_url'] ); $deepai_api_key_in = sanitize_text_field( $_POST['ai_content_wizard_deepai_api_key'] ); $stable_diffusion_api_key_in = sanitize_text_field( $_POST['ai_content_wizard_stable_diffusion_api_key'] ); $ideogram_api_key = sanitize_text_field( $_POST['ideogram_api_key'] ); $is_valid_key = true; $provider_fail_type = 'generic'; $selected_text_provider = sanitize_text_field( $_POST['text_provider'] ); if ( ! $is_reset ) { if ( $selected_text_provider === 'openai' ) { if ( ! is_valid_openai_api_key( $temp_api_key, $selected_text_model ) ) { $is_valid_key = false; $provider_fail_type = 'openai'; } } elseif ( $selected_text_provider === 'anthropic' ) { if ( ! is_valid_anthropic_api_key( $temp_anthropic_api_key, $selected_text_model ) ) { $is_valid_key = false; $provider_fail_type = 'anthropic'; } } elseif ( $selected_text_provider === 'deepseek' ) { if ( ! is_valid_deepseek_api_key( $temp_deepseek_api_key, $selected_text_model ) ) { $is_valid_key = false; $provider_fail_type = 'deepseek'; } } else { $is_valid_key = false; $provider_fail_type = 'generic'; } } if ( $is_valid_key ) { ai_content_wizard_update_setting( 'ai_content_wizard_api_key', $temp_api_key ); ai_content_wizard_update_setting( 'ai_content_wizard_anthropic_api_key', $temp_anthropic_api_key ); ai_content_wizard_update_setting( 'deepseek_api_key', $temp_deepseek_api_key ); ai_content_wizard_update_setting( 'ai_content_wizard_text_model', $selected_text_model ); if ( $selected_text_provider === 'deepseek' ) { ai_content_wizard_update_setting( 'deepseek_api_key', $temp_deepseek_api_key ); } $old_image_model = ai_content_wizard_get_setting( 'ai_content_wizard_image_model', 'dall-e-3' ); ai_content_wizard_update_setting( 'ai_content_wizard_image_model', $image_model_in ); ai_content_wizard_update_image_model_parameters( $image_model_in ); if ( $old_image_model !== $image_model_in ) { set_transient( 'ai_content_wizard_image_model_changed', true, 5 * MINUTE_IN_SECONDS ); } ai_content_wizard_update_setting( 'ai_content_wizard_bing_api_key', $bing_api_key_in ); ai_content_wizard_update_setting( 'ai_content_wizard_google_api_key', $google_api_key_in ); ai_content_wizard_update_setting( 'ai_content_wizard_google_cse_id', $google_cse_id_in ); ai_content_wizard_update_setting( 'ai_content_wizard_rss_feed_url', $rss_feed_url_in ); ai_content_wizard_update_setting( 'ai_content_wizard_deepai_api_key', $deepai_api_key_in ); ai_content_wizard_update_setting( 'ai_content_wizard_stable_diffusion_api_key', $stable_diffusion_api_key_in ); ai_content_wizard_update_setting( 'ideogram_api_key', $ideogram_api_key ); $modalToShow = 'success'; } else { switch ( $provider_fail_type ) { case 'openai': $modalToShow = 'openai'; break; case 'anthropic': $modalToShow = 'anthropic'; break; case 'deepseek': $modalToShow = 'deepseek'; break; default: $modalToShow = 'generic'; break; } } // Обновляем локальные переменные после сохранения $api_key = ai_content_wizard_get_setting( 'ai_content_wizard_api_key', '' ); $anthropic_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_anthropic_api_key', '' ); $deepseek_api_key = ai_content_wizard_get_setting( 'deepseek_api_key', '' ); $stable_diffusion_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_stable_diffusion_api_key', '' ); $bing_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_bing_api_key', '' ); $google_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_google_api_key', '' ); $google_cse_id = ai_content_wizard_get_setting( 'ai_content_wizard_google_cse_id', '' ); $rss_feed_url = ai_content_wizard_get_setting( 'ai_content_wizard_rss_feed_url', '' ); $text_model = ai_content_wizard_get_setting( 'ai_content_wizard_text_model', 'gpt-4o-mini' ); $image_model = ai_content_wizard_get_setting( 'ai_content_wizard_image_model', 'dall-e-3' ); } // Если нажата кнопка "Show available models" для OpenAI if ( isset( $_POST['aiw_update_openai_models'] ) ) { $api_key_temp = sanitize_text_field( $_POST['ai_content_wizard_api_key'] ?? $api_key ); $openai_models = aiw_retrieve_openai_models( $api_key_temp ); if ( ! empty( $openai_models ) ) { ai_content_wizard_update_setting( 'ai_content_wizard_available_models', $openai_models ); echo ""; $models = $openai_models; } else { echo ""; } } // Old update model names functionality if ( isset( $_POST['ai_content_wizard_update_models'] ) ) { update_model_names(); $models = ai_content_wizard_get_setting( 'ai_content_wizard_available_models', array() ); } // Выводим разметку настроек echo '
'; echo '

' . esc_html__( 'AI Content Wizard - API Settings', 'ai-content-wizard' ) . '

'; ?>

×

×

  • 1 OpenAI API Keys to verify or generate your key.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
  • 2
  • 3 video tutorial for a step-by-step guide.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>

×

  • 1 Anthropic Console to generate your API Key.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
  • 2
  • 3

×

  • 1 DeepSeek API Keys to generate your API Key.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
  • 2
×

  • 1
  • 2
×

×

  • 1 OpenAI Dashboard.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
  • 2

×

  • OpenAI Dashboard. Models include GPT-3.5, GPT-4, etc.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
  • Anthropic Console. Models include Claude variants.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>
×

OpenAI Dashboard. Make sure your account has a linked payment method.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>

×

Anthropic Console. A valid payment method is required.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>

×

×

×

×

×

DeepAI.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>

×

Stability AI.', 'ai-content-wizard' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ); ?>

×

×

'Bearer ' . $api_key, 'Content-Type' => 'application/json' ); $response = wp_remote_get('https://api.openai.com/v1/models', array( 'headers' => $headers, 'timeout' => 30 )); if(is_wp_error($response)){ error_log('OpenAI model fetch error: ' . $response->get_error_message()); return array(); } $code = wp_remote_retrieve_response_code($response); if($code !== 200){ error_log('OpenAI model fetch error code: ' . $code); return array(); } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if(!isset($data['data'])) return array(); $allowed_models = array( 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4', 'gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini', 'gpt-4.5-preview', 'o1-preview', 'o1-mini', 'o1', 'o3-mini' ); $result = array(); foreach($data['data'] as $model_info){ if(!empty($model_info['id']) && in_array($model_info['id'], $allowed_models)){ $result[] = $model_info['id']; } } return array_unique($result); } /* ------------------ update_model_names (old functionality) ------------------ */ function update_model_names(){ $models = ai_content_wizard_get_setting('ai_content_wizard_available_models', array()); $api_key = ai_content_wizard_get_setting('ai_content_wizard_api_key'); $models_updated = false; foreach($models as $index => $model_id){ if(strpos($model_id, 'ftjob-') === 0){ error_log("Checking model with ID: $model_id"); $model_name = get_model_name_by_job_id($model_id, $api_key); if(!empty($model_name) && $model_name !== $model_id){ $models[$index] = $model_name; $models_updated = true; error_log("Updated model ID $model_id to name $model_name"); } } } if($models_updated){ ai_content_wizard_update_setting('ai_content_wizard_available_models', $models); error_log("Model names have been updated."); } } function get_model_name_by_job_id($job_id, $api_key){ // Stub – here you can implement a real request to OpenAI for obtaining the model name by job ID. return ''; } /** * Other helper functions (e.g. aiwizard_encrypt_data, aiwizard_send_data_to_api, etc.) can be left as-is. */ function aiwizard_encrypt_data($data, $key) { if (is_array($data)) { $data = wp_json_encode($data); } $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv); return base64_encode($encrypted . '::' . $iv); } function aiwizard_change_plugin_status_on_server($site_url, $action) { $key = 'ai-content-wizard'; $data = array('site_url' => $site_url, 'action' => $action); $json_data = wp_json_encode(array( 'data' => aiwizard_encrypt_data($data, $key) )); $response = wp_remote_post('https://aiwizard.buyreadysite.com/wp-json/my-stats-plugin/v1/change-status', array( 'body' => $json_data, 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json') )); if (is_wp_error($response)) { error_log('Error making POST request: ' . $response->get_error_message()); } else { $body = wp_remote_retrieve_body($response); $data = json_decode($body); if (isset($data->code) && $data->code === "Success") { return true; } } return false; } function aiwizard_send_data_to_api($data, $extended_data = null) { $key = 'ai-content-wizard'; $json_data = wp_json_encode(array( 'data' => aiwizard_encrypt_data($data, $key) )); $response = wp_remote_post('https://aiwizard.buyreadysite.com/wp-json/my-stats-plugin/v1/stats', array( 'body' => $json_data, 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json') )); if (is_wp_error($response)) { error_log('Error making POST request: ' . $response->get_error_message()); } } function aiwizard_send_content_plan_to_api($data) { $key = 'ai-content-wizard'; $json_data = wp_json_encode(array( 'content_plan_data' => aiwizard_encrypt_data($data, $key) )); $response = wp_remote_post('https://aiwizard.buyreadysite.com/wp-json/my-stats-plugin/v1/content-plan', array( 'body' => $json_data, 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json') )); if (is_wp_error($response)) { error_log('Error making POST request: ' . $response->get_error_message()); return false; } $body = wp_remote_retrieve_body($response); $response_data = json_decode($body); if (json_last_error() !== JSON_ERROR_NONE) { error_log("JSON decode error: " . json_last_error_msg()); return false; } if (!is_object($response_data) || !property_exists($response_data, 'code')) { error_log("Invalid response format: " . $body); return false; } if ($response_data->code === "Success") { return true; } return false; }