Question

I have a method that creates a downloadable CSV on the fly when a button is clicked:

header( 'Content-type: text/csv' );
header( 'Cache-Control: no-store, no-cache' );
header( 'Content-Disposition: attachment; filename="email_list.csv"' );

$outstream = fopen( 'php://output', 'w' );

fputcsv( $outstream, $csv->header );

foreach ( $csv->rows as $row ) {
    fputcsv( $outstream, $row );
}

fclose( $outstream );

exit();

When I run the class file through PHP Code Sniffer I get the following warnings:

 63 | WARNING | File operations should use WP_Filesystem methods instead of
    |         | direct PHP filesystem calls. Found: fopen()
 71 | WARNING | File operations should use WP_Filesystem methods instead of
    |         | direct PHP filesystem calls. Found: fclose()

As I understand it WP_Filesystem is necessary when dealing with files on the server. I would like to know if it is necessary when creating downloadable files on the fly like my example above. If so, why?

Was it helpful?

Solution

No.

Keep in mind that the code sniffer is a limited utility operating on very simplistic parsing of the PHP code and has very little contextual knowledge. Therefor, you should use it as an advisory, a tool that should direct you to give a second thought to a part of your code, and not as some "source of truth" regarding code quality.

Once you have decided that your code is good, "mask" the error generation with one of the ways the sniffer provides, so it will not bugger you all the time and hide other problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top