Automattic\WooCommerce\Blocks\BlockTypes

ProductCollectionNoResults::get_current_url_without_filters()protectedWC 1.0

Get current URL without filter query parameters which will be used for the "clear any filters" link.

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

Хуков нет.

Возвращает

null. Ничего (null).

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_current_url_without_filters();

Код ProductCollectionNoResults::get_current_url_without_filters() WC 9.3.3

protected function get_current_url_without_filters() {
	$protocol = is_ssl() ? 'https' : 'http';

	// Check the existence and sanitize HTTP_HOST and REQUEST_URI in the $_SERVER superglobal.
	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	$http_host = isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '';
	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
	$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';

	// Sanitize the host and URI.
	$http_host   = sanitize_text_field( $http_host );
	$request_uri = esc_url_raw( $request_uri );

	// Construct the full URL.
	$current_url = $protocol . '://' . $http_host . $request_uri;

	// Parse the URL to extract the query string.
	$parsed_url   = wp_parse_url( $current_url );
	$query_string = isset( $parsed_url['query'] ) ? $parsed_url['query'] : '';

	// Convert the query string into an associative array.
	parse_str( $query_string, $query_params );

	// Remove the filter query parameters.
	$params_to_remove = array( 'min_price', 'max_price', 'rating_filter', 'filter_', 'query_type_' );
	foreach ( $query_params as $key => $value ) {
		foreach ( $params_to_remove as $param ) {
			if ( strpos( $key, $param ) === 0 ) {
				unset( $query_params[ $key ] );
				break;
			}
		}
	}

	// Rebuild the query string without the removed parameters.
	$new_query_string = http_build_query( $query_params );

	// Reconstruct the URL.
	$new_url  = $parsed_url['scheme'] . '://' . $parsed_url['host'];
	$new_url .= isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
	$new_url .= $new_query_string ? '?' . $new_query_string : '';

	return $new_url;
}