Automattic\WooCommerce\Internal\Utilities

DatabaseUtil::sanitise_boolean_fts_search_term()publicWC 9.4.0

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.

Метод класса: DatabaseUtil{}

Хуков нет.

Возвращает

Строку. Sanitized search term.

Использование

$DatabaseUtil = new DatabaseUtil();
$DatabaseUtil->sanitise_boolean_fts_search_term( $param ): string;
$param(строка) (обязательный)
Search term.

Список изменений

С версии 9.4.0 Введена.

Код DatabaseUtil::sanitise_boolean_fts_search_term() WC 9.7.1

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