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.
,
' . nl2br( trim( $paragraph ) ) . '
'; } return $formatted_content; } /** * Call the AI API to generate a blog post title. * * @param string $api_key The API key. * @param string $prompt The prompt to create a title from. * @param string $api_model The AI model. * @param string $language_code The language code. * * @return string|null The generated title. */ function call_openai_api_for_title( $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' => 'Create a different engaging blog post title in ' . $language . ' for the topic: ' . $prompt . '. Return only one concise and engaging title.', ), ), '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' => 'Create a different engaging blog post title in ' . $language . ' for the topic: ' . $prompt . '. Return only one concise and engaging title.', ), ), ), ), ); $headers = array( 'Content-Type' => 'application/json' ); } else { log_openai_error( 'Unsupported model: ' . $api_model ); return null; } $response = wp_remote_post( $url, array( 'headers' => $headers, 'body' => wp_json_encode( $data ), 'timeout' => 60, ) ); if ( is_wp_error( $response ) ) { log_openai_error( "API error for prompt ({$prompt}): " . $response->get_error_message() ); return null; } $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); $content = ''; if ( 0 === strpos( $api_model, 'gpt' ) ) { $content = $response_data['choices'][0]['message']['content'] ?? null; } elseif ( 0 === strpos( $api_model, 'gemini' ) ) { $content = $response_data['candidates'][0]['content']['parts'][0]['text'] ?? null; } return $content; } /** * Log API errors to a file using WP_Filesystem. * * @param string $message Error message to log. */ function log_openai_error( $message ) { global $wp_filesystem; if ( ! function_exists( 'request_filesystem_credentials' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! $wp_filesystem ) { WP_Filesystem(); } $upload_dir = wp_upload_dir(); $plugin_dir = trailingslashit( $upload_dir['basedir'] ) . 'aims-ai-scheduler'; $log_file = $plugin_dir . '/logs.txt'; if ( ! $wp_filesystem->is_dir( $plugin_dir ) ) { $wp_filesystem->mkdir( $plugin_dir ); } if ( ! $wp_filesystem->exists( $log_file ) ) { $wp_filesystem->put_contents( $log_file, '', FS_CHMOD_FILE ); } $error_message = gmdate( 'Y-m-d H:i:s' ) . ' - ' . $message . "\n"; $existing_content = $wp_filesystem->exists( $log_file ) ? $wp_filesystem->get_contents( $log_file ) : ''; $wp_filesystem->put_contents( $log_file, $existing_content . $error_message, FS_CHMOD_FILE ); }