url_to_path( $audio_url ); if ( ! $this->file_exists( $file_path ) ) { accessibility_audio_tts_debug_log( '[AudioFileDeleter] Delete by URL: File not found at path derived from URL: ' . $file_path ); return false; } $result = $this->remove_file_from_disk( $file_path ); $this->log_deletion_result( $file_path, $result, 'URL' ); return $result; } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( '[AudioFileDeleter] Exception in delete_audio_file (URL): ' . $e->getMessage() ); return false; } } /** * Delete audio file from disk given its absolute server path. * * @param string $full_file_path Absolute server path to the audio file. * @return bool True if deleted successfully, false otherwise. */ public function delete_file_by_path( string $full_file_path ): bool { try { if ( empty( $full_file_path ) ) { accessibility_audio_tts_debug_log( '[AudioFileDeleter] Delete by Path: File path is empty.' ); return false; } if ( ! $this->file_exists( $full_file_path ) ) { accessibility_audio_tts_debug_log( '[AudioFileDeleter] Delete by Path: File not found at: ' . $full_file_path ); return false; } $result = $this->remove_file_from_disk( $full_file_path ); $this->log_deletion_result( $full_file_path, $result, 'Path' ); return $result; } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( '[AudioFileDeleter] Exception in delete_file_by_path: ' . $e->getMessage() ); return false; } } /** * Get detailed information about audio file. * * @param string $audio_url URL of the audio file. * @return array|null File information or null if file doesn't exist. */ public function get_file_info( $audio_url ) { $file_path = $this->url_to_path( $audio_url ); if ( ! $this->file_exists( $file_path ) ) { return null; } return array( 'url' => $audio_url, 'path' => $file_path, 'size' => filesize( $file_path ), 'size_formatted' => size_format( filesize( $file_path ) ), 'modified' => filemtime( $file_path ), 'modified_formatted' => $this->format_file_date( filemtime( $file_path ) ), ); } /** * Check if file exists on disk. * * @param string $file_path Absolute file path. * @return bool True if accessible. */ public function file_exists( $file_path ) { return File_Validation_Helper::is_file_accessible( $file_path ); } /** * Get file size in bytes. * * @param string $audio_url Audio file URL. * @return int|false File size in bytes or false if file doesn't exist. */ public function get_file_size( $audio_url ) { $file_path = $this->url_to_path( $audio_url ); if ( ! $this->file_exists( $file_path ) ) { return false; } return filesize( $file_path ); } /** * Convert audio URL to file path. * * @param string $audio_url Audio file URL. * @return string Absolute file path. */ private function url_to_path( $audio_url ) { return File_Validation_Helper::url_to_path( $audio_url ); } /** * Remove file from disk. * * @param string $file_path Absolute file path. * @return bool True on success. */ private function remove_file_from_disk( $file_path ) { return File_Validation_Helper::safe_remove_file( $file_path ); } /** * Log deletion operation result. * * @param string $file_path Absolute file path. * @param bool $result Operation result. * @param string $method Method of deletion (URL or Path). * @return void */ private function log_deletion_result( $file_path, $result, $method = 'Path' ) { $status = $result ? 'success' : 'failed'; accessibility_audio_tts_debug_log( '[AudioFileDeleter] Deletion via ' . $method . ' for file: ' . $file_path . ' (result: ' . $status . ')' ); } /** * Format file modification date. * * @param int $timestamp Unix timestamp. * @return string Localized date string. */ private function format_file_date( $timestamp ) { $date_format = get_option( 'date_format' ); $time_format = get_option( 'time_format' ); return date_i18n( $date_format . ' ' . $time_format, $timestamp ); } }