Skip to content

Commit

Permalink
Jetpack: Stats: Fix csv trailing newline handling (#39787)
Browse files Browse the repository at this point in the history
When #39665 switched from incorrectly using `str_getcsv( $csv, "\n" )`
to `explode()`, a minor difference between the two was unnoticed: the
former does not produce an empty element at the end of the result on a
trailing newline, while the latter does.

Add an `rtrim` on the data to ensure there isn't a trailing newline.
  • Loading branch information
anomiex authored Oct 16, 2024
1 parent e87ae9f commit 5b688b8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Fix bug in #39665 related to a trailing newline in the CSV data.


2 changes: 1 addition & 1 deletion projects/plugins/jetpack/modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ function stats_get_remote_csv( $url ) {
*/
function stats_str_getcsv( $csv ) {
// @todo Correctly handle embedded newlines. Note, despite claims online, `str_getcsv( $csv, "\n" )` does not actually work.
$lines = explode( "\n", $csv );
$lines = explode( "\n", rtrim( $csv, "\n" ) );
return array_map(
function ( $line ) {
// @todo When we drop support for PHP <7.4, consider passing empty-string for `$escape` here for better spec compatibility.
Expand Down

0 comments on commit 5b688b8

Please sign in to comment.