Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions classes/controllers/FrmSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 ),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should fix. The label now carries (ID: n), but the query above it still only matches post_title. I ran the same FrmDb::get_results( $wpdb->posts, array( ..., 'post_title LIKE' => $term ), ... ) against a published page titled Saved Listings (ID 24):

  • term Saved Listings → 1 row
  • term Saved Listings (ID: 24) → 0 rows
  • term 24 → 0 rows

So the pre-filled value (FrmAppHelper::maybe_autocomplete_pages_options()) cannot be refined without losing every match, and the ID that this PR is adding cannot be used to find the page. Fix: strip a trailing (ID: n) from the term and also match an all-digit term against ID. Sketch:

$term = preg_replace( '/\s*\(ID: \d+\)\s*$/', '', $term );

and extend $where with an ID OR clause when ctype_digit( $term ). Add a test that calls page_search() (or the query) with a prefilled label and with a numeric ID.

);
}

wp_send_json( $results );
return $results;
}

/**
Expand Down
36 changes: 34 additions & 2 deletions classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ),
)
);
}
}

?>
Expand Down Expand Up @@ -2087,7 +2119,7 @@ public static function wp_pages_dropdown( $args = array(), $page_id = '', $trunc
<option value=""><?php echo esc_html( $args['placeholder'] ); ?></option>
<?php foreach ( $pages as $page ) { ?>
<option value="<?php echo esc_attr( $page->ID ); ?>" <?php selected( $selected, $page->ID ); ?>>
<?php echo esc_html( $args['truncate'] ? self::truncate( $page->post_title, $args['truncate'] ) : $page->post_title ); ?>
<?php echo esc_html( self::get_post_dropdown_label( $page, $args['truncate'] ) ); ?>
</option>
<?php } ?>
</select>
Expand Down
2 changes: 1 addition & 1 deletion classes/models/FrmGatedContentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
131 changes: 131 additions & 0 deletions tests/phpunit/misc/test_FrmPostDropdownLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/** Verify consistent page and View choices without changing stored IDs. */
class test_FrmPostDropdownLabels extends FrmUnitTest {
/** @return void */
public function test_titles_and_ids() {
foreach ( array( '', ' ', 'Saved Listings', '0', 'A & B <script>alert(1)</script>' ) 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 ) );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertSame( $title, $post->post_title );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

}
}

/** @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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertLessThan( strlen( $post->post_title ), strlen( $label ) );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertLessThan()


The method you are trying to call is not defined, which can result in a fatal error.

}

/** @return void */
public function test_page_search_matches_prefilled_label_and_id() {
$id = self::factory()->post->create(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined static method test_FrmPostDropdownLabels::factory()


Invalid call to a static method. This would lead to a run time error.

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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertContains()


The method you are trying to call is not defined, which can result in a fatal error.

}

$results = FrmSettingsController::get_page_search_results( 'Saved Listings', 'page' );
$this->assertSame( 'Saved Listings (ID: ' . $id . ')', $results[0]['label'] );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertSame( array(), FrmSettingsController::get_page_search_results( 'No such page', 'page' ) );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

}

/** @return void */
public function test_autocomplete_selected_value_keeps_private_prefix() {
$id = self::factory()->post->create(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined static method test_FrmPostDropdownLabels::factory()


Invalid call to a static method. This would lead to a run time error.

array(
'post_type' => 'page',
'post_status' => 'private',
'post_title' => 'Secret',
)
);
self::factory()->post->create_many(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined static method test_FrmPostDropdownLabels::factory()


Invalid call to a static method. This would lead to a run time error.

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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

}

/** @return void */
public function test_shared_page_dropdown_uses_no_title() {
$id = self::factory()->post->create(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined static method test_FrmPostDropdownLabels::factory()


Invalid call to a static method. This would lead to a run time error.

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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertStringContainsString( 'value="' . $id . '"', $html );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

}

/** @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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmPostDropdownLabels::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

array(
array(
'value' => '123',
'label' => '(no title) (ID: 123)',
),
),
$source
);
}
}
Loading