From 5b688b8ec9ecaac1ca5b1bf719829e85149f18fa Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 16 Oct 2024 10:35:54 -0400 Subject: [PATCH] Jetpack: Stats: Fix csv trailing newline handling (#39787) 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. --- .../jetpack/changelog/fix-stats-explode-trailing-newline | 5 +++++ projects/plugins/jetpack/modules/stats.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 projects/plugins/jetpack/changelog/fix-stats-explode-trailing-newline diff --git a/projects/plugins/jetpack/changelog/fix-stats-explode-trailing-newline b/projects/plugins/jetpack/changelog/fix-stats-explode-trailing-newline new file mode 100644 index 0000000000000..591df46c8f0a5 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-stats-explode-trailing-newline @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Fix bug in #39665 related to a trailing newline in the CSV data. + + diff --git a/projects/plugins/jetpack/modules/stats.php b/projects/plugins/jetpack/modules/stats.php index e306bc9d180e9..cf12fe07c5ee9 100644 --- a/projects/plugins/jetpack/modules/stats.php +++ b/projects/plugins/jetpack/modules/stats.php @@ -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.