WC_Admin_List_Table_Products::build_title_match_case_clause │ private │ WC 11.1.0

Build the relevance expression that ranks title matches ahead of the remaining search results.

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

Хуков нет.

Возвращает

string. CASE expression, or an empty string when the term yields no title predicates.

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

// private - только в коде основоного (родительского) класса
$result = $this->build_title_match_case_clause( $search_term );
$search_term(строка) (обязательный)
Search term the product matcher already ran against.

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

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

Код WC_Admin_List_Table_Products::build_title_match_case_clause() WC 11.2.2

private function build_title_match_case_clause( $search_term ) {
	global $wpdb;

	// Group exactly as search_products() does, so ranking and matching agree on what an OR group is.
	// \s+or\s+ alone would not: \s also matches \x0B and \x0C, which wc_clean() leaves in place.
	$search_groups = stristr( $search_term, ' or ' ) ? preg_split( '/\s+or\s+/i', $search_term ) : array( $search_term );
	if ( ! $search_groups ) {
		return '';
	}

	$title_match_groups = array();
	foreach ( $search_groups as $search_group ) {
		$title_match_queries = array();
		foreach ( $this->parse_search_terms( $search_group ) as $title_search_term ) {
			$title_match_queries[] = $wpdb->prepare(
				"{$wpdb->posts}.post_title LIKE %s",
				'%' . $wpdb->esc_like( $title_search_term ) . '%'
			);
		}

		if ( $title_match_queries ) {
			$title_match_groups[] = '(' . implode( ' AND ', $title_match_queries ) . ')';
		}
	}

	if ( ! $title_match_groups ) {
		return '';
	}

	return 'CASE WHEN (' . implode( ' OR ', $title_match_groups ) . ') THEN 0 ELSE 1 END';
}