iterate( static function ( \WC_Subscription $subscription ) use ( $handle, &$rows, &$stopped, $deadline, $stream ) { // No escape character: quotes are only doubled, as RFC 4180 and spreadsheets expect. A // backslash escape would let a value such as \",=1+1 end its cell early and open a new one. fputcsv( $handle, Columns::row( $subscription ), ',', '"', '' ); $rows++; if ( 0 === $rows % 200 ) { if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } if ( $stream ) { if ( ob_get_level() ) { ob_flush(); } flush(); } } if ( microtime( true ) > $deadline ) { $stopped = true; return false; } return true; }, $cursor ); return array( 'rows' => $rows, 'done' => ! $stopped && ! empty( $cursor['done'] ), ); } /** * Streams a whole cohort to the HTTP response and exits. * * @param CohortSpec $spec Cohort. * @param string $file_name Download name. * @return void */ public static function send( CohortSpec $spec, $file_name ) { if ( function_exists( 'wc_set_time_limit' ) ) { wc_set_time_limit( 0 ); } nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $file_name ) . '"' ); header( 'X-Content-Type-Options: nosniff' ); $handle = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions $cursor = array(); self::write( $handle, $spec, true, $cursor, PHP_INT_MAX, true ); fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions } }