diff --git a/classes/controllers/FrmSettingsController.php b/classes/controllers/FrmSettingsController.php
index 6742d3c841..d11f8e04d5 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+\((?:I(?:D(?::\s*\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',
@@ -618,11 +645,11 @@ 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 ),
);
}
- wp_send_json( $results );
+ return $results;
}
/**
diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php
index a2e0e432c2..b3ef586c8b 100644
--- a/classes/helpers/FrmAppHelper.php
+++ b/classes/helpers/FrmAppHelper.php
@@ -1932,6 +1932,28 @@ 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 );
+ }
+
/**
* Renders an autocomplete page selection or a regular dropdown depending on
* the total page count
@@ -1956,7 +1978,17 @@ public static function maybe_autocomplete_pages_options( $args ) {
$title = '';
if ( $selected ) {
- $title = get_the_title( $selected );
+ $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 ),
+ )
+ );
+ }
}
?>
@@ -2087,7 +2119,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..4d90aa6d05
--- /dev/null
+++ b/tests/phpunit/misc/test_FrmPostDropdownLabels.php
@@ -0,0 +1,131 @@
+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_search_matches_prefilled_label_and_id() {
+ $id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_status' => 'publish',
+ 'post_title' => 'Saved Listings',
+ )
+ );
+
+ $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 );
+ }
+
+ $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(
+ 'field_name' => 'destination',
+ 'page_id' => $id,
+ )
+ );
+ $html = ob_get_clean();
+ $this->assertStringContainsString( 'value="Private: Secret (ID: ' . $id . ')"', $html );
+ }
+
+ /** @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
+ );
+ }
+}