api_key ) ? $api_key_data[0]->api_key : ''; if ( empty( $get_all_data ) ) { log_openai_error( 'No active prompts found.' ); } elseif ( empty( $api_key ) ) { log_openai_error( 'No API key configured for model: ' . $get_ai_model ); } if ( $get_all_data && $api_key ) { foreach ( $get_all_data as $cron_data ) { if ( $cron_data->prompt ) { $response = call_openai_api( $api_key, $cron_data->prompt, $get_ai_model, $cron_data->post_language ); $ai_title = call_openai_api_for_title( $api_key, $cron_data->prompt, $get_ai_model, $cron_data->post_language ); if ( $response && $ai_title ) { log_openai_error( 'Post Created: ' . $ai_title ); $term_id = $cron_data->taxonomy_id ? $cron_data->taxonomy_id : ''; $term = $term_id ? get_term( $term_id ) : ''; $taxonomy = $term ? $term->taxonomy : ''; $status = $cron_data->status ? $cron_data->status : 'publish'; $new_post_id = wp_insert_post( array( 'post_title' => $ai_title, 'post_content' => $response, 'post_status' => $status, 'post_type' => $cron_data->post_type, ) ); update_post_meta( $new_post_id, '_yoast_wpseo_title', $ai_title ); update_post_meta( $new_post_id, 'rank_math_title', $ai_title ); update_post_meta( $new_post_id, '_aioseop_title', $ai_title ); if ( $new_post_id && $taxonomy && $term_id ) { wp_set_object_terms( $new_post_id, intval( $term_id ), $taxonomy ); } $insert_details = AIMSAICS_Content_Scheduler_Db::insert_promp_details( $cron_data->id, $new_post_id ); } } } } } /** * Call the OpenAI or Gemini API to generate post content. * * @param string $api_key The API key. * @param string $prompt The content prompt. * @param string $api_model The AI model to use. * @param string $language_code The language code. * * @return string|null Generated HTML content or null on failure. */ function call_openai_api( $api_key, $prompt, $api_model, $language_code ) { $language = Locale::getDisplayLanguage( $language_code, 'en' ); $get_gpt_model = get_option( 'aimsaisc_gpwa_gpt_model', 'gpt-4o-mini' ); $get_gemini_model = get_option( 'aimsaisc_gpwa_gemini_model', 'gemini-1.5-flash' ); if ( 'gpt' === $api_model ) { $url = 'https://api.openai.com/v1/chat/completions'; $data = array( 'model' => $get_gpt_model, 'messages' => array( array( 'role' => 'system', 'content' => 'You are a helpful assistant.', ), array( 'role' => 'user', 'content' => "Write a unique, detailed blog post in {$language} language:\n\n{$prompt}", ), ), 'max_tokens' => 4000, 'temperature' => 0.7, ); $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ); } elseif ( 'gemini' === $api_model ) { $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $get_gemini_model . ':generateContent?key=' . $api_key; $data = array( 'contents' => array( array( 'parts' => array( array( 'text' => 'Write a blog post in ' . $language . ' with at least 10,000 characters. Format the response using valid HTML tags (e.g.

,

,