'You do not have permission to delete 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 delete this file.']); } } // Check nonce (do not over-sanitize to avoid altering token) $raw_nonce = isset($_POST['nonce']) ? wp_unslash($_POST['nonce']) : ''; if (!$raw_nonce || !wp_verify_nonce($raw_nonce, 'ai_tts_nonce')) { wp_send_json_error(['message' => 'Nonce verification failed.']); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; if (!$post_id || !get_post($post_id) || !current_user_can('edit_post', $post_id)) { wp_send_json_error(['message' => 'You do not have permission to delete this file.']); } $result = ai_tts_delete_file_by_post($post_id); if (is_wp_error($result)) { wp_send_json_error(['message' => $result->get_error_message()]); } wp_send_json_success(['message' => 'File deleted successfully.']); } /** * Delete TTS file for a given post (reusable logic for AJAX and admin pages) * * @param int $post_id * @return true|WP_Error */ function ai_tts_delete_file_by_post($post_id) { if (!$post_id) { return new WP_Error('invalid_post', esc_html__('Invalid post ID.', 'ai-text-to-speech')); } // Ensure post exists $post = get_post($post_id); if (!$post) { return new WP_Error('invalid_post', esc_html__('Invalid post ID.', 'ai-text-to-speech')); } $file_storage_location = get_post_meta($post_id, 'ai_tts_location', true); $file_url = get_post_meta($post_id, 'ai_tts_file_url', true); $file_path = ''; if ($file_storage_location == 'local' || !$file_storage_location) { $file_path = str_replace(content_url(), WP_CONTENT_DIR, $file_url); // Harden: only allow deletes inside our uploads directory $uploads_dir = defined('AI_TTS_UPLOAD_DIR') ? realpath(AI_TTS_UPLOAD_DIR) : ''; $path_real = $file_path ? realpath($file_path) : ''; // Compare with a trailing separator so sibling folders like "ai-text-to-speech-backup" don't match. $uploads_prefix = $uploads_dir ? rtrim( $uploads_dir, '/\\' ) . DIRECTORY_SEPARATOR : ''; if ( $uploads_prefix && $path_real && 0 === strpos( $path_real, $uploads_prefix ) && file_exists( $path_real ) ) { wp_delete_file( $path_real ); } } if ($file_storage_location == 'wp_media') { $attachment_id = intval(get_post_meta($post_id, 'ai_tts_attachment_id', true)); if ($attachment_id > 0) { wp_delete_attachment($attachment_id, true); } } do_action('delete_ai_tts_file', $file_url, $file_path, $post_id); delete_post_meta($post_id, 'ai_tts_file_url'); delete_post_meta($post_id, 'ai_tts_file_name'); delete_post_meta($post_id, 'ai_tts_dropbox_file_path'); delete_post_meta($post_id, 'ai_tts_location'); delete_post_meta($post_id, 'ai_tts_attachment_id'); delete_post_meta($post_id, 'ai_tts_data'); delete_post_meta($post_id, 'ai_tts_generated_at'); delete_post_meta($post_id, 'ai_tts_intro_date'); delete_post_meta($post_id, 'ai_tts_intro_date_stale'); return true; }