// UI-Erweiterung der Post-Übersicht für Bulk-Aktionen add_filter('bulk_actions-edit-post', 'kupf_tta_bulk_actions'); add_filter('bulk_actions-edit-zeitungsartikel', 'kupf_tta_bulk_actions'); function kupf_tta_bulk_actions($actions) { $actions['generate_tts'] = 'Audioversion erstellen'; return $actions; } add_filter('handle_bulk_actions-edit-post', 'kupf_tta_handle_bulk_action', 10, 3); add_filter('handle_bulk_actions-edit-zeitungsartikel', 'kupf_tta_handle_bulk_action', 10, 3); function kupf_tta_handle_bulk_action($redirect_to, $doaction, $post_ids) { if ($doaction !== 'generate_tts') { return $redirect_to; } foreach ($post_ids as $post_id) { as_enqueue_async_action('kupf_tta_generate_audio_post', [$post_id, 'de-DE']); } return $redirect_to; } // Registrierung der Action Scheduler Hook add_action('kupf_tta_generate_audio_post', 'kupf_tta_generate_audio_post', 10, 2); // Audio-Datei-Erstellung function kupf_tta_generate_audio_post($post_id, $language) { $post = get_post($post_id); $title = strip_tags($post->post_title); $content = wp_strip_all_tags($post->post_content); $text_to_speak = $title . ". " . $content; $audio_content = kupf_tta_google_tts_request($text_to_speak, $language); if (!$audio_content) { return; } $year = get_the_date('Y', $post_id); $upload_dir = wp_upload_dir(); $dir_path = $upload_dir['basedir'] . "/$year/"; if (!file_exists($dir_path)) { wp_mkdir_p($dir_path); } $file_path = $dir_path . $$year = get_the_date('Y', $post_id); $upload_dir = wp_upload_dir(); $dir_path = $upload_dir['basedir'] . "/$year/"; if (!file_exists($dir_path)) { wp_mkdir_p($dir_path); } $file_path = $dir_path . $post_id . '-' . $language . '.mp3'; $audio_content = kupf_tta_google_tts_request($text, $language); if ($audio_content) { file_put_contents($file_path, $audio_content); } else { error_log("Failed to generate audio content for post ID: $post_id"); } // Google Cloud TTS API Integration function kupf_tta_google_tts_request($text, $language) { $api_key = 'YOUR_GOOGLE_CLOUD_API_KEY'; // Replace with a secure method to retrieve the API key $url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=$api_key"; $payload = [ 'input' => [ 'text' => $text ], 'voice' => [ 'languageCode' => $language, 'name' => 'de-DE-studio-C' ], 'audioConfig' => [ 'audioEncoding' => 'MP3' ] ]; $args = [ 'body' => json_encode($payload), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = wp_remote_post($url, $args); if (is_wp_error($response)) { error_log('TTS API Request Error: ' . $response->get_error_message()); return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['audioContent'])) { error_log('No audio content returned from TTS API.'); return null; } return base64_decode($body['audioContent']); }$year = get_the_date('Y', $post_id); $upload_dir = wp_upload_dir(); $dir_path = $upload_dir['basedir'] . "/$year/"; if (!file_exists($dir_path)) { wp_mkdir_p($dir_path); } $file_path = $dir_path . $post_id . '-' . $language . '.mp3'; $audio_content = kupf_tta_google_tts_request($text, $language); if ($audio_content) { file_put_contents($file_path, $audio_content); } else { error_log("Failed to generate audio content for post ID: $post_id"); } // Google Cloud TTS API Integration function kupf_tta_google_tts_request($text, $language) { $api_key = 'YOUR_GOOGLE_CLOUD_API_KEY'; // Replace with a secure method to retrieve the API key $url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=$api_key"; $payload = [ 'input' => [ 'text' => $text ], 'voice' => [ 'languageCode' => $language, 'name' => 'de-DE-studio-C' ], 'audioConfig' => [ 'audioEncoding' => 'MP3' ] ]; $args = [ 'body' => json_encode($payload), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = wp_remote_post($url, $args); if (is_wp_error($response)) { error_log('TTS API Request Error: ' . $response->get_error_message()); return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['audioContent'])) { error_log('No audio content returned from TTS API.'); return null; } return base64_decode($body['audioContent']); }$year = get_the_date('Y', $post_id); $upload_dir = wp_upload_dir(); $dir_path = $upload_dir['basedir'] . "/$year/"; if (!file_exists($dir_path)) { wp_mkdir_p($dir_path); } $file_path = $dir_path . $post_id . '-' . $language . '.mp3'; $audio_content = kupf_tta_google_tts_request($text, $language); if ($audio_content) { file_put_contents($file_path, $audio_content); } else { error_log("Failed to generate audio content for post ID: $post_id"); } // Google Cloud TTS API Integration function kupf_tta_google_tts_request($text, $language) { $api_key = 'YOUR_GOOGLE_CLOUD_API_KEY'; // Replace with a secure method to retrieve the API key $url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=$api_key"; $payload = [ 'input' => [ 'text' => $text ], 'voice' => [ 'languageCode' => $language, 'name' => 'de-DE-studio-C' ], 'audioConfig' => [ 'audioEncoding' => 'MP3' ] ]; $args = [ 'body' => json_encode($payload), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = wp_remote_post($url, $args); if (is_wp_error($response)) { error_log('TTS API Request Error: ' . $response->get_error_message()); return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['audioContent'])) { error_log('No audio content returned from TTS API.'); return null; } return base64_decode($body['audioContent']); }post_id . '-' . $language . '.mp3'; file_put_contents($file_path, $audio_content); } // Google Cloud TTS API Integration function kupf_tta_google_tts_request($text, $language) { $api_key = 'YOUR_GOOGLE_CLOUD_API_KEY'; // API-Key sicher speichern und abrufen! $url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=$api_key"; $payload = [ 'input' => [ 'text' => $text ], 'voice' => [ 'languageCode' => $language, 'name' => 'de-DE-studio-C' ], 'audioConfig' => [ 'audioEncoding' => 'MP3' ] ]; $args = [ 'body' => json_encode($payload), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = wp_remote_post($url, $args); if (is_wp_error($response)) { error_log('TTS API Request Error: ' . $response->get_error_message()); return null; } if (empty($body['audioContent'])) { error_log('No audio content returned from TTS API.'); return null; } $body = json_decode(wp_remote_retrieve_body($response), true); if (!isset($body['audioContent'])) { error_log('TTS API Response Error: ' . wp_remote_retrieve_body($response)); return null; } return base64_decode($body['audioContent']); }