filepath = $filepath; $this->filename = basename( $filepath ); $this->callback = $callback; $this->length = file_exists( $filepath ) ? filesize( $filepath ) : 0; $this->capability = $capability; } /** * @inheritdoc */ public function download() { if ( ! current_user_can( $this->capability ) ) { wp_die( 'Cheating Uh?' ); } $this->perform_download_callback(); } /** * @inheritdoc */ public function clean_ob() { $level = ob_get_level(); if ( $level ) { for ( $i = 0; $i < $level; $i ++ ) { ob_end_clean(); } } return $this; } /** * @inheritdoc */ public function filepath() { return $this->filepath; } /** * @inheritdoc */ public function headers() { $mime = MimeTypeExtractor::fromFilePath( $this->filepath ); $level = ob_get_level(); if ( $level ) { for ( $i = 0; $i < $level; $i ++ ) { ob_end_clean(); } } // phpcs:ignore @set_time_limit( 300 ); nocache_headers(); // Set headers. header( 'Content-Description: File Transfer' ); header( "Content-Type: {$mime}" ); header( "Content-Disposition: attachment; filename={$this->filename}" ); header( 'Content-Transfer-Encoding: ' . self::$encoding ); header( "Content-Length: {$this->length}" ); return $this; } /** * Perform the Download * * Note: The callback must call `die` it self. * * @return void */ private function perform_download_callback() { call_user_func( $this->callback, $this ); } }