'You do not have permission to save this file.']); } if (!ai_tts_current_user_can_manage_tts_post($post_id)) { wp_send_json_error(['message' => 'You do not have permission to save this file.']); } // User Roles $user_roles = ai_tts_get_option('user_roles'); $user_roles_to_enable = ai_tts_get_option('user_roles_to_enable'); if($user_roles == 'specific' && !empty($user_roles_to_enable)) { $current_user = wp_get_current_user(); $current_user_roles = $current_user->roles; $intersect = array_intersect($current_user_roles, $user_roles_to_enable); if(empty($intersect)) { wp_send_json_error(['message' => 'You do not have permission to save this file.']); } } // Check nonce (accept both 'nonce' and fallback 'security', as ai_tts_verify_generate_request() does) $nonce_key = isset( $_POST['nonce'] ) ? 'nonce' : ( isset( $_POST['security'] ) ? 'security' : '' ); $raw_nonce = $nonce_key ? sanitize_text_field( wp_unslash( $_POST[ $nonce_key ] ) ) : ''; if (!$raw_nonce || !wp_verify_nonce($raw_nonce, 'ai_tts_nonce')) { wp_send_json_error(['message' => 'Nonce verification failed']); } // Convert post title to URL friendly string $post_title = $post_id ? get_post_field('post_title', $post_id) : ''; $post_title = sanitize_title($post_title); // Drop percent-encoded (non-Latin) characters: web servers decode the URL and would look for a different file name. $post_title = preg_replace('/%[0-9a-f]{2}/i', '', $post_title); $post_title = implode('-', array_slice(array_filter(explode('-', $post_title), 'strlen'), 0, 10)); // Get the file storage location $file_storage_location = ai_tts_get_option('file_storage_location'); // Generate a random string for the file name if($post_id) { $random_string = wp_generate_password(2, false); $file_name = 'post-' . $post_id . '-' . $random_string . ( '' !== $post_title ? '-' . $post_title : '' ) . '.mp3'; } else { $random_string = wp_generate_password(10, false); $file_name = 'admin-' . $random_string . '.mp3'; } // Save the MP3 response bytes directly. $mp3 = $response; // Check if folder exists, if not create it if (!file_exists(AI_TTS_UPLOAD_DIR) && !wp_mkdir_p(AI_TTS_UPLOAD_DIR)) { wp_send_json_error(['message' => 'There was an error creating the upload directory.']); } // Save the file to the uploads directory $file_path = AI_TTS_UPLOAD_DIR . $file_name; // Save the file to the uploads directory global $wp_filesystem; if (empty($wp_filesystem)) { require_once (ABSPATH . '/wp-admin/includes/file.php'); WP_Filesystem(); } if (empty($wp_filesystem) || !$wp_filesystem->put_contents($file_path, $mp3, FS_CHMOD_FILE)) { wp_send_json_error(['message' => 'There was an error saving the file.']); } // Get the file URL $file_url = content_url('/uploads/ai-text-to-speech/' . $file_name); // Allow modification of the file URL $file_url = apply_filters('modify_ai_tts_file_url', $file_url, $file_path, $file_name, $post_id); if (is_wp_error($file_url)) { if ( file_exists( $file_path ) ) { wp_delete_file( $file_path ); } wp_send_json_error(['message' => $file_url->get_error_message()]); } // Check if successful if (!$file_url) { wp_send_json_error(['message' => 'There was an error saving the file.']); } // Save the file URL to post meta if ($post_id) { update_post_meta($post_id, 'ai_tts_file_url', $file_url); update_post_meta($post_id, 'ai_tts_generated_at', current_time('mysql')); update_post_meta($post_id, 'ai_tts_location', $file_storage_location); if ($file_storage_location !== 'local') { update_post_meta($post_id, 'ai_tts_file_name', $file_name); } } // Return the file URL return $file_url; } /** * Record an admin-generated (no post) TTS file in the site option so it * appears on the TTS Files page. Keeps the 50 most recent entries and * removes older local files from disk when the limit is exceeded. */ function ai_tts_record_admin_generated_file( $file_url, $file_name, $storage = 'local', $voice_model = '', $voice = '', $voice_style = '' ) { $max_entries = 50; $entries = get_option( 'ai_tts_admin_generated_files', [] ); if ( ! is_array( $entries ) ) { $entries = []; } $max_id = 0; foreach ( $entries as $entry ) { if ( isset( $entry['id'] ) && intval( $entry['id'] ) > $max_id ) { $max_id = intval( $entry['id'] ); } } $entries[] = [ 'id' => $max_id + 1, 'file_url' => $file_url, 'file_name' => $file_name, 'storage' => $storage, 'generated_at' => current_time( 'mysql' ), 'voice_model' => $voice_model, 'voice' => $voice, 'voice_style' => $voice_style, ]; // Prune oldest entries beyond the limit, deleting their local files if ( count( $entries ) > $max_entries ) { $excess = array_splice( $entries, 0, count( $entries ) - $max_entries ); foreach ( $excess as $old ) { if ( ! empty( $old['file_name'] ) && ( ! isset( $old['storage'] ) || $old['storage'] === 'local' ) ) { $old_path = AI_TTS_UPLOAD_DIR . basename( $old['file_name'] ); if ( file_exists( $old_path ) ) { wp_delete_file( $old_path ); } } } } update_option( 'ai_tts_admin_generated_files', $entries ); } /** * Delete a single admin-generated file entry (by its stored id) and its * local file from disk. */ function ai_tts_delete_admin_generated_file( $admin_file_id ) { $admin_file_id = absint( $admin_file_id ); $entries = get_option( 'ai_tts_admin_generated_files', [] ); if ( ! is_array( $entries ) ) { return; } foreach ( $entries as $key => $entry ) { if ( isset( $entry['id'] ) && absint( $entry['id'] ) === $admin_file_id ) { if ( ! empty( $entry['file_name'] ) && ( ! isset( $entry['storage'] ) || $entry['storage'] === 'local' ) ) { $file_path = AI_TTS_UPLOAD_DIR . basename( $entry['file_name'] ); if ( file_exists( $file_path ) ) { wp_delete_file( $file_path ); } } unset( $entries[ $key ] ); break; } } update_option( 'ai_tts_admin_generated_files', array_values( $entries ) ); }