From 9ad0586f8302ac51023892af17f638e3ad94afc2 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Thu, 12 Nov 2026 13:06:06 -0400 Subject: [PATCH 1/3] fix(entries): preserve CSV export sort --- classes/controllers/FrmXMLController.php | 7 +- classes/helpers/FrmCSVExportHelper.php | 20 ++-- classes/helpers/FrmEntriesListHelper.php | 39 ++++--- classes/models/FrmEntry.php | 23 ++++ tests/phpunit/misc/test_FrmCSVExportSort.php | 107 +++++++++++++++++++ 5 files changed, 175 insertions(+), 21 deletions(-) create mode 100644 tests/phpunit/misc/test_FrmCSVExportSort.php diff --git a/classes/controllers/FrmXMLController.php b/classes/controllers/FrmXMLController.php index 20903fd2aa..61125a0b0b 100644 --- a/classes/controllers/FrmXMLController.php +++ b/classes/controllers/FrmXMLController.php @@ -706,8 +706,6 @@ public static function csv( $form_id = false, $search = '', $fid = '' ) { wp_raise_memory_limit(); } - global $wpdb; - $form = FrmForm::getOne( $form_id ); if ( ! $form ) { @@ -742,7 +740,10 @@ public static function csv( $form_id = false, $search = '', $fid = '' ) { */ $query = apply_filters( 'frm_csv_where', $query, compact( 'form_id', 'search', 'fid', 'item_id' ) ); - $entry_ids = FrmDb::get_col( $wpdb->prefix . 'frm_items it', $query ); + $orderby = FrmAppHelper::get_param( 'orderby', 'id', 'get', 'sanitize_title' ); + $order = FrmAppHelper::get_param( 'order', 'ASC', 'get', 'sanitize_title' ); + $order_by = FrmEntriesListHelper::get_sort_query( $orderby, $order ); + $entry_ids = FrmEntry::get_sorted_ids( $query, $order_by ); unset( $query ); if ( $entry_ids ) { diff --git a/classes/helpers/FrmCSVExportHelper.php b/classes/helpers/FrmCSVExportHelper.php index eb50ed5e08..429ea38f6e 100644 --- a/classes/helpers/FrmCSVExportHelper.php +++ b/classes/helpers/FrmCSVExportHelper.php @@ -506,7 +506,7 @@ private static function prepare_next_csv_rows( $next_set ) { 'parent_item_id' => $next_set, ); // Order by parent_item_id so children will be first. - $order_by = ' ORDER BY parent_item_id DESC'; + $order_by = ' ORDER BY parent_item_id DESC, id ASC'; } else { // When Pro is not installed, only query for direct ID matches only as we do not expect parent item id // Matches and the more simplified query is faster. @@ -518,15 +518,23 @@ private static function prepare_next_csv_rows( $next_set ) { $entries = FrmEntry::getAll( $where, $order_by, '', true, false ); + // Collect repeater values before writing parent rows in the requested order. foreach ( $entries as $entry ) { + if ( $entry->form_id === self::$form_id ) { + continue; + } + self::$entry = $entry; - unset( $entry ); + self::add_repeat_field_values_to_csv( $entries ); + } - if ( self::$entry->form_id === self::$form_id ) { - self::prepare_csv_row(); - } else { - self::add_repeat_field_values_to_csv( $entries ); + foreach ( $next_set as $entry_id ) { + if ( ! isset( $entries[ $entry_id ] ) || $entries[ $entry_id ]->form_id !== self::$form_id ) { + continue; } + + self::$entry = $entries[ $entry_id ]; + self::prepare_csv_row(); } } diff --git a/classes/helpers/FrmEntriesListHelper.php b/classes/helpers/FrmEntriesListHelper.php index adc63095d2..b1f539dc87 100644 --- a/classes/helpers/FrmEntriesListHelper.php +++ b/classes/helpers/FrmEntriesListHelper.php @@ -103,21 +103,36 @@ protected function get_entry_items( &$s_query, &$join_form_in_query ) { * @return string */ protected function get_order_by() { - $orderby = self::get_param( - array( - 'param' => 'orderby', - 'default' => 'id', - ) - ); - $order = self::get_param( - array( - 'param' => 'order', - 'default' => 'DESC', - ) - ); + $sort = self::get_sort_parameters(); + return self::get_sort_query( $sort['orderby'], $sort['order'] ); + } + /** + * Get the effective entries list sort before leaving the current admin screen. + * + * @since x.x + * + * @return array + */ + public static function get_sort_parameters() { + $orderby = FrmAppHelper::get_param( 'orderby', 'id', 'request', 'sanitize_title' ); + $order = FrmAppHelper::get_param( 'order', 'DESC', 'request', 'sanitize_title' ); FrmAppController::apply_saved_sort_preference( $orderby, $order ); + return compact( 'orderby', 'order' ); + } + + /** + * Build the same sort clause for the entries list and its CSV export. + * + * @since x.x + * + * @param string $orderby Entry column or field meta alias. + * @param string $order Sort direction. + * + * @return string + */ + public static function get_sort_query( $orderby, $order ) { if ( str_contains( $orderby, 'meta' ) ) { $order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) ); diff --git a/classes/models/FrmEntry.php b/classes/models/FrmEntry.php index bb86e42b6a..03b10df298 100644 --- a/classes/models/FrmEntry.php +++ b/classes/models/FrmEntry.php @@ -709,6 +709,29 @@ public static function getAll( $where, $order_by = '', $limit = '', $meta = fals return $entries; } + /** + * Fetch entry IDs in list order without loading every entry and its values. + * + * @since x.x + * + * @param array|string $where Entry query conditions. + * @param string $order_by Sort clause for an entry column or field meta alias. + * + * @return array + */ + public static function get_sorted_ids( $where, $order_by ) { + global $wpdb; + + $fields = 'it.id'; + $order_by = FrmDb::esc_order( $order_by ); + + if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) { + $fields .= self::sort_by_field( $order_matches[1] ); + } + + return FrmDb::get_col( $wpdb->prefix . 'frm_items it', $where, $fields, array( 'order_by' => $order_by ) ); + } + /** * @param int|string $field_id * diff --git a/tests/phpunit/misc/test_FrmCSVExportSort.php b/tests/phpunit/misc/test_FrmCSVExportSort.php new file mode 100644 index 0000000000..8a37b76ae1 --- /dev/null +++ b/tests/phpunit/misc/test_FrmCSVExportSort.php @@ -0,0 +1,107 @@ +factory->form->create_and_get(); + $ids = $this->factory->entry->create_many( 25, $this->factory->field->generate_entry_array( $form ) ); + $sort = FrmEntriesListHelper::get_sort_query( 'id', 'DESC' ); + $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); + + $this->assertSame( array_reverse( $ids ), $this->exported_ids( $form, $sorted_ids ), 'Descending order should survive every CSV batch.' ); + } + + public function test_numeric_field_export_uses_numeric_order() { + $form = $this->factory->form->create_and_get(); + $field_id = $this->factory->field->create( + array( + 'form_id' => $form->id, + 'type' => 'number', + ) + ); + $ids = array(); + + foreach ( array( '10', '2', '30' ) as $value ) { + $ids[] = $this->factory->entry->create( + array( + 'form_id' => $form->id, + 'item_meta' => array( $field_id => $value ), + ) + ); + } + + $sort = FrmEntriesListHelper::get_sort_query( 'meta_' . $field_id, 'ASC' ); + $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); + $this->assertSame( array( $ids[1], $ids[0], $ids[2] ), $this->exported_ids( $form, $sorted_ids ), 'Numeric sorting should place 2 before 10.' ); + } + + public function test_text_field_export_uses_field_order() { + $form = $this->factory->form->create_and_get(); + $field_id = $this->factory->field->create( + array( + 'form_id' => $form->id, + 'type' => 'text', + ) + ); + $ids = array(); + + foreach ( array( 'Pear', 'Apple', 'Orange' ) as $value ) { + $ids[] = $this->factory->entry->create( + array( + 'form_id' => $form->id, + 'item_meta' => array( $field_id => $value ), + ) + ); + } + + $sort = FrmEntriesListHelper::get_sort_query( 'meta_' . $field_id, 'DESC' ); + $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); + $this->assertSame( array( $ids[0], $ids[2], $ids[1] ), $this->exported_ids( $form, $sorted_ids ), 'Text sorting should follow the selected direction.' ); + } + + public function test_export_preserves_the_supplied_id_sequence() { + $form = $this->factory->form->create_and_get(); + $ids = $this->factory->entry->create_many( 25, $this->factory->field->generate_entry_array( $form ) ); + $ids = array_merge( array_slice( $ids, 12 ), array_slice( $ids, 0, 12 ) ); + + $this->assertSame( $ids, $this->exported_ids( $form, $ids ), 'CSV batches should preserve arbitrary caller order.' ); + } + + /** + * @param object $form Form being exported. + * @param array $ids Ordered entry IDs. + * + * @return array + */ + private function exported_ids( $form, $ids ) { + $path = FrmCSVExportHelper::generate_csv( + array( + 'mode' => 'file', + 'form' => $form, + 'entry_ids' => $ids, + 'form_cols' => FrmField::get_all_for_form( $form->id, '', 'include' ), + ) + ); + WP_Filesystem(); + global $wp_filesystem; + $lines = preg_split( '/\r\n|\n|\r/', trim( $wp_filesystem->get_contents( $path ) ) ); + $headings = str_getcsv( array_shift( $lines ), ',', '"', '' ); + $id_column = array_search( 'ID', $headings, true ); + $rows = array(); + + foreach ( $lines as $line ) { + $row = str_getcsv( $line, ',', '"', '' ); + $rows[] = (int) $row[ $id_column ]; + } + + wp_delete_file( $path ); + return $rows; + } +} From db30375a9dff91b65eed7ae7368c7e808f99f5b5 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Thu, 12 Nov 2026 14:23:57 -0400 Subject: [PATCH 2/3] test(csv): cover requested export sorting --- tests/phpunit/misc/test_FrmCSVExportSort.php | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/phpunit/misc/test_FrmCSVExportSort.php b/tests/phpunit/misc/test_FrmCSVExportSort.php index 8a37b76ae1..5af274300b 100644 --- a/tests/phpunit/misc/test_FrmCSVExportSort.php +++ b/tests/phpunit/misc/test_FrmCSVExportSort.php @@ -74,6 +74,73 @@ public function test_export_preserves_the_supplied_id_sequence() { $this->assertSame( $ids, $this->exported_ids( $form, $ids ), 'CSV batches should preserve arbitrary caller order.' ); } + /** + * @covers FrmXMLController::csv + * + * @runInSeparateProcess + * + * @preserveGlobalState disabled + */ + public function test_controller_reads_the_requested_sort() { + $this->set_current_user_to_1(); + $form = $this->factory->form->create_and_get(); + $field_id = $this->factory->field->create( + array( + 'form_id' => $form->id, + 'type' => 'number', + ) + ); + $ids = array(); + + foreach ( array( '10', '2', '30' ) as $value ) { + $ids[] = $this->factory->entry->create( + array( + 'form_id' => $form->id, + 'item_meta' => array( $field_id => $value ), + ) + ); + } + + $get = $_GET; + $request = $_REQUEST; + $_GET = array( + 'form' => $form->id, + 'orderby' => 'meta_' . $field_id, + 'order' => 'DESC', + ); + $_REQUEST = $_GET; + $handler = static function () { + return static function () { + throw new RuntimeException( 'CSV complete.' ); + }; + }; + add_filter( 'wp_die_handler', $handler ); + ob_start(); + + try { + FrmXMLController::csv(); + } catch ( RuntimeException $exception ) { + $this->assertSame( 'CSV complete.', $exception->getMessage(), 'The CSV controller should finish through wp_die.' ); + } finally { + $csv = ob_get_clean(); + remove_filter( 'wp_die_handler', $handler ); + $_GET = $get; + $_REQUEST = $request; + } + + $lines = preg_split( '/\r\n|\n|\r/', trim( $csv ) ); + $headings = str_getcsv( array_shift( $lines ), ',', '"', '' ); + $id_column = array_search( 'ID', $headings, true ); + $exported = array(); + + foreach ( $lines as $line ) { + $row = str_getcsv( $line, ',', '"', '' ); + $exported[] = (int) $row[ $id_column ]; + } + + $this->assertSame( array( $ids[2], $ids[0], $ids[1] ), $exported, 'The controller should read both sort parameters from the URL.' ); + } + /** * @param object $form Form being exported. * @param array $ids Ordered entry IDs. From 3c79cff45d9c142c9f6b1b1eda3608703778f884 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Mon, 16 Nov 2026 14:48:16 -0400 Subject: [PATCH 3/3] Test that frm_csv_where conditions on the items table alias still filter the sorted CSV export Co-Authored-By: Claude Opus 5.5 --- tests/phpunit/misc/test_FrmCSVExportSort.php | 80 ++++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/misc/test_FrmCSVExportSort.php b/tests/phpunit/misc/test_FrmCSVExportSort.php index 5af274300b..abb08f8e80 100644 --- a/tests/phpunit/misc/test_FrmCSVExportSort.php +++ b/tests/phpunit/misc/test_FrmCSVExportSort.php @@ -101,14 +101,80 @@ public function test_controller_reads_the_requested_sort() { ); } + $exported = $this->exported_ids_from_controller( + array( + 'form' => $form->id, + 'orderby' => 'meta_' . $field_id, + 'order' => 'DESC', + ) + ); + + $this->assertSame( array( $ids[2], $ids[0], $ids[1] ), $exported, 'The controller should read both sort parameters from the URL.' ); + } + + /** + * @covers FrmXMLController::csv + * + * @runInSeparateProcess + * + * @preserveGlobalState disabled + */ + public function test_controller_applies_items_table_conditions_with_the_sort() { + $this->set_current_user_to_1(); + $form = $this->factory->form->create_and_get(); + $field_id = $this->factory->field->create( + array( + 'form_id' => $form->id, + 'type' => 'number', + ) + ); + $ids = array(); + + foreach ( array( '10', '2', '30', '5' ) as $index => $value ) { + $ids[] = $this->factory->entry->create( + array( + 'form_id' => $form->id, + 'item_meta' => array( $field_id => $value ), + 'is_draft' => 2 === $index ? 1 : 0, + ) + ); + } + + $excluded = $ids[3]; + $filter = static function ( $query ) use ( $excluded ) { + $query['it.is_draft'] = 0; + $query['it.id !'] = $excluded; + return $query; + }; + add_filter( 'frm_csv_where', $filter ); + + try { + $exported = $this->exported_ids_from_controller( + array( + 'form' => $form->id, + 'orderby' => 'meta_' . $field_id, + 'order' => 'DESC', + ) + ); + } finally { + remove_filter( 'frm_csv_where', $filter ); + } + + $this->assertSame( array( $ids[0], $ids[1] ), $exported, 'Conditions on the aliased items table should filter the sorted export.' ); + } + + /** + * Run the CSV controller with the given URL parameters and return the exported entry IDs in order. + * + * @param array $params Query string parameters. + * + * @return array + */ + private function exported_ids_from_controller( $params ) { $get = $_GET; $request = $_REQUEST; - $_GET = array( - 'form' => $form->id, - 'orderby' => 'meta_' . $field_id, - 'order' => 'DESC', - ); - $_REQUEST = $_GET; + $_GET = $params; + $_REQUEST = $params; $handler = static function () { return static function () { throw new RuntimeException( 'CSV complete.' ); @@ -138,7 +204,7 @@ public function test_controller_reads_the_requested_sort() { $exported[] = (int) $row[ $id_column ]; } - $this->assertSame( array( $ids[2], $ids[0], $ids[1] ), $exported, 'The controller should read both sort parameters from the URL.' ); + return $exported; } /**