finished() ) { wp_die( esc_html__( 'This export is no longer available. Export again.', 'modulary-export' ), '', [ 'response' => 404, 'back_link' => true, ] ); } $request = $job->request(); $filename = sprintf( 'orders-%s.%s', wp_date( 'Y-m-d-His' ), Format::extension( $request->format ) ); /** * Filter the download filename. * * @param string $filename Default "orders-YYYY-MM-DD-HHMMSS.csv" (".xlsx" for Excel). * @param Request $request The export request. */ $filename = (string) apply_filters( 'modulary_export_filename', $filename, $request ); wc_set_time_limit( 0 ); while ( ob_get_level() ) { ob_end_clean(); } nocache_headers(); header( 'Content-Type: ' . Format::content_type( $request->format ) ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' ); header( 'Content-Length: ' . (string) filesize( $job->file() ) ); header( 'X-Content-Type-Options: nosniff' ); $stream = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $this->deliver( $job, $stream ); fclose( $stream ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } /** * Copy a finished job's file to a stream, remember it as the last export * and delete the job's files. * * @param Job $job Finished job of the current user. * @param resource $stream Writable stream. * @return int Bytes sent. * @throws JobException When the file is gone. */ public function deliver( Job $job, $stream ): int { $file = fopen( $job->file(), 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false === $file ) { throw JobException::gone(); } $sent = (int) stream_copy_to_stream( $file, $stream ); fclose( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions update_option( 'modulary_export_last_export', [ 'time' => time(), 'rows' => $job->rows(), 'request' => $job->request()->to_array(), ], false ); $job->delete(); return $sent; } /** * The file itself: the request's format, every matching order, complete. * * @param Request $request Export request. * @param resource $stream Writable stream. * @return int Rows written (excluding header). */ public function write_file( Request $request, $stream ): int { return ( new Exporter( $request ) )->write( Format::writer( $request, $stream ) ); } }