@@ -30,27 +30,34 @@ class Boldgrid_Backup_File {
3030 * @param int $filesize File size (optional).
3131 */
3232 public static function send_file ( $ filepath , $ filesize = null ) {
33- WP_Filesystem ();
34- global $ wp_filesystem ;
33+ $ core = apply_filters ( 'boldgrid_backup_get_core ' , null );
34+ $ log = new Boldgrid_Backup_Admin_Log ( $ core );
35+ $ log ->init ( 'backup-download.log ' );
36+ $ log ->add ( 'Initializing send_file() method... ' );
3537
36- if ( empty ( $ filepath ) || ! $ wp_filesystem ->exists ( $ filepath ) ) {
38+ if ( empty ( $ filepath ) || ! $ core ->wp_filesystem ->exists ( $ filepath ) ) {
39+ $ log ->add ( 'Invalid filepath. ' );
3740 wp_redirect ( get_site_url (), 404 );
3841 }
3942
4043 $ filename = basename ( $ filepath );
4144
4245 if ( empty ( $ filesize ) ) {
43- $ filesize = $ wp_filesystem ->size ( $ filepath );
46+ $ filesize = $ core -> wp_filesystem ->size ( $ filepath );
4447 }
4548
4649 // Send header.
50+ $ log ->add ( 'Sending headers... ' );
4751 header ( 'Content-Disposition: attachment; filename=" ' . $ filename . '" ' );
4852 header ( 'Content-Transfer-Encoding: binary ' );
4953 header ( 'Content-Type: binary/octet-stream ' );
5054 header ( 'Content-Length: ' . $ filesize );
5155
5256 // Check and flush output buffer if needed.
53- if ( 0 !== ob_get_level () ) {
57+ $ ob_level = ob_get_level ();
58+ $ log ->add ( 'Output buffering level: ' . $ ob_level );
59+ if ( 0 !== $ ob_level ) {
60+ $ log ->add ( 'Calling ob_end_flush(). ' );
5461 ob_end_flush ();
5562 }
5663
@@ -78,17 +85,36 @@ public static function send_file( $filepath, $filesize = null ) {
7885 // If we can't open the file, abort.
7986 $ handle = fopen ( $ filepath , 'rb ' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
8087 if ( false === $ handle ) {
88+ $ log ->add ( 'Invalid handle. fopen failed. ' );
8189 wp_die ();
8290 }
8391
8492 // Loop through the file and send it 1MB at a time.
93+ $ buffer_size = 1024 * 1024 ;
94+ $ log ->add ( 'Beginnig to send file... Buffer size: ' . size_format ( $ buffer_size , 2 ) );
8595 while ( ! feof ( $ handle ) ) {
86- $ buffer = fread ( $ handle , 1024 * 1024 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
96+ $ log ->add ( 'Reading buffer... ' );
97+ $ time_start = microtime ( true );
98+ $ buffer = fread ( $ handle , $ buffer_size ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
99+ $ duration = microtime ( true ) - $ time_start ;
100+ $ log ->add ( 'Buffer read in ' . round ( $ duration , 4 ) . ' seconds. ' );
101+
102+ $ log ->add ( 'Sending buffer... ' );
103+ $ time_start = microtime ( true );
87104 echo $ buffer ; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
105+ $ duration = microtime ( true ) - $ time_start ;
106+ $ log ->add ( 'Buffer sent in ' . round ( $ duration , 4 ) . ' seconds. ' );
88107 }
108+ $ log ->add ( 'Finished sending file. ' );
89109
90- fclose ( $ handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
110+ $ log ->add ( 'Closing file... ' );
111+ if ( fclose ( $ handle ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
112+ $ log ->add ( 'File closed successfully. ' );
113+ } else {
114+ $ log ->add ( 'Error closing file. ' );
115+ }
91116
117+ $ log ->add ( 'send_file() method complete. Ending with wp_die(). ' );
92118 wp_die ();
93119 }
94120}
0 commit comments