Automattic\WooCommerce\Internal\Utilities
DatabaseUtil::sanitise_boolean_fts_search_term
Sanitize FTS Search params to remove relevancy operators for performance, and add partial matches. Useful when the sorting is already happening based on some other conditions, so relevancy calculation is not needed.
Method of the class: DatabaseUtil{}
No Hooks.
Returns
String. Sanitized search term.
Usage
$DatabaseUtil = new DatabaseUtil(); $DatabaseUtil->sanitise_boolean_fts_search_term( $param ): string;
- $param(string) (required)
- Search term.
Changelog
| Since 9.4.0 | Introduced. |
DatabaseUtil::sanitise_boolean_fts_search_term() DatabaseUtil::sanitise boolean fts search term code WC 10.5.0
public function sanitise_boolean_fts_search_term( string $param ): string {
// Remove any operator to prevent incorrect query and fatals, such as search starting with `++`. We can allow this in the future if we have proper validation for FTS search operators.
// Space is allowed to provide multiple words.
$sanitized_param = preg_replace( '/[^\p{L}\p{N}_]+/u', ' ', $param );
if ( $sanitized_param !== $param ) {
$param = str_replace( '"', '', $param );
return '"' . $param . '"';
}
// Split the search phrase into words so that we can add operators when needed.
$words = explode( ' ', $param );
$sanitized_words = array();
foreach ( $words as $word ) {
// Add `*` as suffix to every term so that partial matches happens.
$word = $word . '*';
$sanitized_words[] = $word;
}
return implode( ' ', $sanitized_words );
}