From 89757f35cdec6f21deaa1140f5bd2eeaa582cd93 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Tue, 10 Nov 2026 13:28:27 -0400 Subject: [PATCH 1/3] feat(admin): add IDs to post dropdown labels --- classes/controllers/FrmSettingsController.php | 2 +- classes/helpers/FrmAppHelper.php | 41 +++++++- classes/models/FrmGatedContentAction.php | 2 +- .../misc/test_FrmPostDropdownLabels.php | 97 +++++++++++++++++++ 4 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/misc/test_FrmPostDropdownLabels.php diff --git a/classes/controllers/FrmSettingsController.php b/classes/controllers/FrmSettingsController.php index 6742d3c841..d47d062872 100644 --- a/classes/controllers/FrmSettingsController.php +++ b/classes/controllers/FrmSettingsController.php @@ -618,7 +618,7 @@ public static function page_search() { foreach ( $pages as $page ) { $results[] = array( 'value' => $page->ID, - 'label' => $page->post_title, + 'label' => FrmAppHelper::get_post_dropdown_label( $page ), ); } diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index fa9234fec0..1d5226d8cb 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1953,6 +1953,42 @@ public static function get_post_ids_and_titles( $post_type = 'page' ) { ); } + /** + * Format a page or View choice with a title and its ID. + * + * @since x.x + * + * @param object $post Post containing ID and post_title. + * @param bool|int $truncate Maximum title length, or false for the full title. + * + * @return string + */ + public static function get_post_dropdown_label( $post, $truncate = false ) { + $title = trim( $post->post_title ); + $title = '' === $title ? FrmFormsHelper::get_no_title_text() : $title; + + if ( $truncate ) { + $title = self::truncate( $title, $truncate ); + } + + /* translators: 1: Post title, 2: Post ID. */ + return sprintf( __( '%1$s (ID: %2$d)', 'formidable' ), $title, $post->ID ); + } + + /** + * Format a WordPress page dropdown label. + * + * @since x.x + * + * @param string $title Filtered page title. + * @param object $page Page containing ID and post_title. + * + * @return string + */ + public static function page_dropdown_label( $title, $page ) { + return self::get_post_dropdown_label( $page ); + } + /** * Renders an autocomplete page selection or a regular dropdown depending on * the total page count @@ -1977,7 +2013,8 @@ public static function maybe_autocomplete_pages_options( $args ) { $title = ''; if ( $selected ) { - $title = get_the_title( $selected ); + $post = get_post( $selected ); + $title = $post ? self::get_post_dropdown_label( $post ) : ''; } ?> @@ -2108,7 +2145,7 @@ public static function wp_pages_dropdown( $args = array(), $page_id = '', $trunc diff --git a/classes/models/FrmGatedContentAction.php b/classes/models/FrmGatedContentAction.php index 22a2c52ae5..80929de07d 100644 --- a/classes/models/FrmGatedContentAction.php +++ b/classes/models/FrmGatedContentAction.php @@ -246,7 +246,7 @@ public static function get_posts_autocomplete_source( $posts ) { static function ( $p ) { return array( 'value' => (string) $p->ID, - 'label' => $p->post_title, + 'label' => FrmAppHelper::get_post_dropdown_label( $p ), ); }, $posts diff --git a/tests/phpunit/misc/test_FrmPostDropdownLabels.php b/tests/phpunit/misc/test_FrmPostDropdownLabels.php new file mode 100644 index 0000000000..76b5d3fc70 --- /dev/null +++ b/tests/phpunit/misc/test_FrmPostDropdownLabels.php @@ -0,0 +1,97 @@ +alert(1)' ) as $title ) { + $post = (object) array( + 'ID' => 123, + 'post_title' => $title, + ); + $expected = '' === trim( $title ) ? FrmFormsHelper::get_no_title_text() : $title; + $this->assertSame( $expected . ' (ID: 123)', FrmAppHelper::get_post_dropdown_label( $post ) ); + $this->assertSame( $title, $post->post_title ); + } + } + + /** @return void */ + public function test_truncated_title_keeps_id() { + $post = (object) array( + 'ID' => 456, + 'post_title' => str_repeat( 'Long title ', 20 ), + ); + $label = FrmAppHelper::get_post_dropdown_label( $post, 30 ); + $this->assertStringContainsString( '(ID: 456)', $label ); + $this->assertLessThan( strlen( $post->post_title ), strlen( $label ) ); + } + + /** @return void */ + public function test_page_dropdown_preserves_selection_and_escapes_titles() { + $id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'A & B', + ) + ); + add_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ), 10, 2 ); + $html = wp_dropdown_pages( + array( + 'echo' => 0, + 'selected' => $id, + 'name' => 'destination', + 'id' => 'destination-id', + ) + ); + $this->assertStringContainsString( 'value="' . $id . '" selected="selected"', $html ); + $this->assertStringContainsString( 'A & B (ID: ' . $id . ')', $html ); + $this->assertStringContainsString( "id='destination-id'", $html ); + remove_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ), 10 ); + $this->assertFalse( has_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ) ) ); + } + + /** @return void */ + public function test_shared_page_dropdown_uses_no_title() { + $id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => '', + ) + ); + ob_start(); + FrmAppHelper::wp_pages_dropdown( + array( + 'field_name' => 'destination', + 'page_id' => $id, + ) + ); + $html = ob_get_clean(); + $this->assertStringContainsString( '(no title) (ID: ' . $id . ')', $html ); + $this->assertStringContainsString( 'value="' . $id . '"', $html ); + } + + /** @return void */ + public function test_gated_content_autocomplete_keeps_values() { + $source = json_decode( + FrmGatedContentAction::get_posts_autocomplete_source( + array( + (object) array( + 'ID' => 123, + 'post_title' => '', + ), + ) + ), + true + ); + $this->assertSame( + array( + array( + 'value' => '123', + 'label' => '(no title) (ID: 123)', + ), + ), + $source + ); + } +} From 3acba6b798f658020a2aa919f339c5a30a0da099 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 11 Nov 2026 10:48:43 -0400 Subject: [PATCH 2/3] Match page search on prefilled labels and IDs The page autocomplete now strips a trailing "(ID: n)" from the search term and matches an all-digit term against the post ID, so a prefilled label can be edited and a page can be found by ID. Keep the Private and Protected title prefix on the selected autocomplete value, and drop the unused page_dropdown_label() filter callback. Co-Authored-By: Claude Opus 5.5 --- classes/controllers/FrmSettingsController.php | 39 ++++++++++++--- classes/helpers/FrmAppHelper.php | 27 +++++------ .../misc/test_FrmPostDropdownLabels.php | 48 ++++++++++++++----- 3 files changed, 79 insertions(+), 35 deletions(-) diff --git a/classes/controllers/FrmSettingsController.php b/classes/controllers/FrmSettingsController.php index d47d062872..131883a601 100644 --- a/classes/controllers/FrmSettingsController.php +++ b/classes/controllers/FrmSettingsController.php @@ -596,17 +596,44 @@ public static function page_search() { FrmAppHelper::permission_check( 'frm_edit_forms' ); check_ajax_referer( 'frm_ajax', 'nonce' ); - global $wpdb; - $term = FrmAppHelper::get_param( 'term', '', 'get', 'sanitize_text_field' ); $post_type = FrmAppHelper::get_param( 'post_type', 'page', 'get', 'sanitize_text_field' ); + wp_send_json( self::get_page_search_results( $term, $post_type ) ); + } + + /** + * Find published posts for the page autocomplete by title or ID. + * + * A trailing " (ID: n)" is removed from the term so a prefilled label can be + * edited, and an all-digit term also matches the post ID. + * + * @since x.x + * + * @param string $term Search term typed in the autocomplete input. + * @param string $post_type Post type to search. + * + * @return array[] Autocomplete results with value and label keys. + */ + public static function get_page_search_results( $term, $post_type ) { + global $wpdb; + + $term = trim( (string) preg_replace( '/\s*\(ID: \d+\)\s*$/', '', $term ) ); $where = array( - 'post_status' => 'publish', - 'post_type' => $post_type, - 'post_title LIKE' => $term, + 'post_status' => 'publish', + 'post_type' => $post_type, ); + if ( preg_match( '/^\d+$/', $term ) ) { + $where[] = array( + 'or' => 1, + 'post_title LIKE' => $term, + 'ID' => (int) $term, + ); + } else { + $where['post_title LIKE'] = $term; + } + $atts = array( 'limit' => 25, 'order_by' => 'post_title', @@ -622,7 +649,7 @@ public static function page_search() { ); } - wp_send_json( $results ); + return $results; } /** diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 1d5226d8cb..77aedf7985 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1975,20 +1975,6 @@ public static function get_post_dropdown_label( $post, $truncate = false ) { return sprintf( __( '%1$s (ID: %2$d)', 'formidable' ), $title, $post->ID ); } - /** - * Format a WordPress page dropdown label. - * - * @since x.x - * - * @param string $title Filtered page title. - * @param object $page Page containing ID and post_title. - * - * @return string - */ - public static function page_dropdown_label( $title, $page ) { - return self::get_post_dropdown_label( $page ); - } - /** * Renders an autocomplete page selection or a regular dropdown depending on * the total page count @@ -2013,8 +1999,17 @@ public static function maybe_autocomplete_pages_options( $args ) { $title = ''; if ( $selected ) { - $post = get_post( $selected ); - $title = $post ? self::get_post_dropdown_label( $post ) : ''; + $post = get_post( $selected ); + + if ( $post ) { + // Use the displayed title so the Private and Protected prefixes are kept. + $title = self::get_post_dropdown_label( + (object) array( + 'ID' => $post->ID, + 'post_title' => get_the_title( $post ), + ) + ); + } } ?> diff --git a/tests/phpunit/misc/test_FrmPostDropdownLabels.php b/tests/phpunit/misc/test_FrmPostDropdownLabels.php index 76b5d3fc70..fc7c66ae73 100644 --- a/tests/phpunit/misc/test_FrmPostDropdownLabels.php +++ b/tests/phpunit/misc/test_FrmPostDropdownLabels.php @@ -26,28 +26,50 @@ public function test_truncated_title_keeps_id() { } /** @return void */ - public function test_page_dropdown_preserves_selection_and_escapes_titles() { + public function test_page_search_matches_prefilled_label_and_id() { $id = self::factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'publish', - 'post_title' => 'A & B', + 'post_title' => 'Saved Listings', ) ); - add_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ), 10, 2 ); - $html = wp_dropdown_pages( + + foreach ( array( 'Saved Listings', 'Saved Listings (ID: ' . $id . ')', 'Saved', (string) $id ) as $term ) { + $values = wp_list_pluck( FrmSettingsController::get_page_search_results( $term, 'page' ), 'value' ); + $this->assertContains( (string) $id, array_map( 'strval', $values ), 'Term: ' . $term ); + } + + $results = FrmSettingsController::get_page_search_results( 'Saved Listings', 'page' ); + $this->assertSame( 'Saved Listings (ID: ' . $id . ')', $results[0]['label'] ); + $this->assertSame( array(), FrmSettingsController::get_page_search_results( 'No such page', 'page' ) ); + } + + /** @return void */ + public function test_autocomplete_selected_value_keeps_private_prefix() { + $id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'private', + 'post_title' => 'Secret', + ) + ); + self::factory()->post->create_many( + 51, + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + ob_start(); + FrmAppHelper::maybe_autocomplete_pages_options( array( - 'echo' => 0, - 'selected' => $id, - 'name' => 'destination', - 'id' => 'destination-id', + 'field_name' => 'destination', + 'page_id' => $id, ) ); - $this->assertStringContainsString( 'value="' . $id . '" selected="selected"', $html ); - $this->assertStringContainsString( 'A & B (ID: ' . $id . ')', $html ); - $this->assertStringContainsString( "id='destination-id'", $html ); - remove_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ), 10 ); - $this->assertFalse( has_filter( 'list_pages', array( 'FrmAppHelper', 'page_dropdown_label' ) ) ); + $html = ob_get_clean(); + $this->assertStringContainsString( 'value="Private: Secret (ID: ' . $id . ')"', $html ); } /** @return void */ From 84cdf0f99ed19e1a45f3660f1753721baabbd2ac Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 11 Nov 2026 14:55:56 -0400 Subject: [PATCH 3/3] fix(admin): search partially edited page labels --- classes/controllers/FrmSettingsController.php | 2 +- tests/phpunit/misc/test_FrmPostDropdownLabels.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/classes/controllers/FrmSettingsController.php b/classes/controllers/FrmSettingsController.php index 131883a601..d11f8e04d5 100644 --- a/classes/controllers/FrmSettingsController.php +++ b/classes/controllers/FrmSettingsController.php @@ -618,7 +618,7 @@ public static function page_search() { public static function get_page_search_results( $term, $post_type ) { global $wpdb; - $term = trim( (string) preg_replace( '/\s*\(ID: \d+\)\s*$/', '', $term ) ); + $term = trim( (string) preg_replace( '/\s+\((?:I(?:D(?::\s*\d*)?)?)?\)?\s*$/', '', $term ) ); $where = array( 'post_status' => 'publish', 'post_type' => $post_type, diff --git a/tests/phpunit/misc/test_FrmPostDropdownLabels.php b/tests/phpunit/misc/test_FrmPostDropdownLabels.php index fc7c66ae73..4d90aa6d05 100644 --- a/tests/phpunit/misc/test_FrmPostDropdownLabels.php +++ b/tests/phpunit/misc/test_FrmPostDropdownLabels.php @@ -35,7 +35,19 @@ public function test_page_search_matches_prefilled_label_and_id() { ) ); - foreach ( array( 'Saved Listings', 'Saved Listings (ID: ' . $id . ')', 'Saved', (string) $id ) as $term ) { + $terms = array( + 'Saved Listings', + 'Saved Listings (ID: ' . $id . ')', + 'Saved Listings (ID: 6', + 'Saved Listings (ID: ', + 'Saved Listings (ID', + 'Saved Listings (I', + 'Saved Listings (', + 'Saved', + (string) $id, + ); + + foreach ( $terms as $term ) { $values = wp_list_pluck( FrmSettingsController::get_page_search_results( $term, 'page' ), 'value' ); $this->assertContains( (string) $id, array_map( 'strval', $values ), 'Term: ' . $term ); }