Query

Class Query

Handles query-related operations to filter products and return the results.

Tags
author

Barn2 Plugins [email protected]

license

GPL-3.0

copyright

Barn2 Media Ltd

Table of Contents

$filters  : array<string|int, Filter>
Filters associated with this query.
$found_product_ids  : array<string|int, mixed>
The found product IDs for the query.
$integrations  : array<string|int, mixed>
Integrations (shortcode attributes) for customizing the query.
$is_preflight  : bool
Whether this query is a preflight request.
$is_reset_request  : bool
Whether this query is a reset request.
$no_results  : bool
Whether the query has no results.
$order_by  : string|null
The order by for the query.
$paged  : int
The current page number.
$parameters  : array<string|int, mixed>
The parameters for the filters.
$possible_choices  : array<string|int, mixed>
The possible choices for the filters.
$query_args  : array<string|int, mixed>
Additional WP_Query arguments for constraining the base product query.
__construct()  : mixed
Query constructor.
from_parameters()  : Query
Create a Query instance from parameters.
get_filters()  : array<string|int, Filter>
Get the filters associated with this query.
get_filters_with_search_query()  : array<string|int, Filter>
Get the filters with a search query.
get_integration()  : mixed
Get a specific integration value.
get_integrations()  : array<string|int, mixed>
Get the integrations.
get_order_by()  : string|null
Get the order by for the query.
get_paged()  : int
Get the current page number.
get_parameters()  : array<string|int, mixed>|null
Get the parameters for the filters.
get_possible_choices()  : array<string, array<string, string|string[]>>
Get the possible choices for the filters.
get_product_ids()  : array<string|int, mixed>
Get the found product IDs.
get_queriable_filters()  : array<string|int, Filter>
Get the queriable filters that have a search query.
get_query_args()  : array<string|int, mixed>
Get the query arguments.
get_results()  : array<string|int, mixed>
Get the results for the query.
has_no_results()  : bool
Check if the query has no results.
has_search_filter()  : bool
Check if the query has a search filter.
is_preflight()  : bool
Check if this query is a preflight request.
is_reset_request()  : bool
Check if this query is a reset request.
maybe_insert_search_query()  : void
Maybe insert the search query into the WP_Query.
prepare_results()  : void
Prepare the results for the query.
refresh()  : $this
Refresh the query results after query arguments or filters have been modified.
set_filters()  : $this
Set the filters associated with this query.
set_integrations()  : $this
Set the integrations.
set_is_preflight()  : $this
Set whether this query is a preflight request.
set_is_reset_request()  : $this
Set whether this query is a reset request.
set_no_results()  : self
Set whether the query has no results.
set_order_by()  : $this
Set the order by for the query.
set_paged()  : self
Set the current page number.
set_parameters()  : $this
Set the parameters for the filters.
set_parameters_to_filters()  : $this
Set the parameters to the filters.
set_possible_choices()  : $this
Set the possible choices for the filters.
set_query_args()  : $this
Set the query arguments.
apply_query_args()  : void
Apply query arguments to constrain the base WP_Query.
apply_sorting_to_product_ids()  : void
Apply sorting to the found product IDs based on the order_by parameter.
collect_possible_choices()  : void
Collect the possible choices for the filters.
generate_cache_key_for_query_args()  : string
Generate a unique cache key for query args and filter parameters.
get_active_filters_list()  : array<string|int, mixed>
Get the active filters list.
get_all_products()  : array<string|int, mixed>
Get all products that could potentially be shown using WooCommerce's query logic.
get_default_order_by()  : string
Get the default order by value when none is provided.
get_matching_posts()  : array<string|int, mixed>
Get the posts that match the filters.
get_post_ids_from_query_args()  : array<string|int, mixed>
Get post IDs from query args constraints.
get_posts_per_page()  : int
Get the number of posts per page based on integrations.
get_search_query()  : string|null
Get the search query from the first filter of the type Search.
has_active_filters()  : bool
Check if there are any active filters in the current query.
load_post_ids_from_query_args()  : array<string|int, mixed>
Load post IDs from query arguments.
normalize_array_for_cache()  : array<string|int, mixed>
Normalize an array for consistent cache key generation.
sanitize_integrations()  : array<string|int, mixed>
Sanitize and validate integrations.
sanitize_query_args()  : array<string|int, mixed>
Sanitize and validate query arguments.

Properties

$filters

Filters associated with this query.

protected array<string|int, Filter> $filters

The filters associated with this query.

$found_product_ids

The found product IDs for the query.

protected array<string|int, mixed> $found_product_ids = []

The found product IDs for the query.

$integrations

Integrations (shortcode attributes) for customizing the query.

protected array<string|int, mixed> $integrations = []

Array of integrations/shortcode attributes.

$is_preflight

Whether this query is a preflight request.

protected bool $is_preflight = false

$is_reset_request

Whether this query is a reset request.

protected bool $is_reset_request = false

$no_results

Whether the query has no results.

protected bool $no_results = false

$order_by

The order by for the query.

protected string|null $order_by

The order by for the query.

$paged

The current page number.

protected int $paged = 1

$parameters

The parameters for the filters.

protected array<string|int, mixed> $parameters

These are the parameters that are sent in the request.

The parameters for the filters.

$possible_choices

The possible choices for the filters.

protected array<string|int, mixed> $possible_choices = []

These are the values that are retrieved from the database.

The possible choices for the filters.

$query_args

Additional WP_Query arguments for constraining the base product query.

protected array<string|int, mixed> $query_args = []

WP_Query arguments to apply to the base query.

Methods

__construct()

Query constructor.

public __construct(array<string|int, Filter> $filters[, array<string|int, mixed> $parameters = [] ][, string|null $order_by = null ][, bool $is_preflight = false ][, int $paged = 1 ][, array<string|int, mixed> $query_args = [] ][, array<string|int, mixed> $integrations = [] ][, bool $is_reset_request = false ]) : mixed
Parameters
$filters : array<string|int, Filter>

The filters associated with this query.

$parameters : array<string|int, mixed> = []

The parameters for the filters.

$order_by : string|null = null

The order by for the query.

$is_preflight : bool = false

Whether this query is a preflight request.

$paged : int = 1

The current page number.

$query_args : array<string|int, mixed> = []

Additional WP_Query arguments for constraining the base query.

$integrations : array<string|int, mixed> = []

Integrations (shortcode attributes) for customizing the query.

$is_reset_request : bool = false

Whether this query is a reset request.

Return values
mixed —

from_parameters()

Create a Query instance from parameters.

public static from_parameters([array<string|int, mixed> $parameters = [] ][, string|null $order_by = null ][, bool $is_preflight = false ][, int $paged = 1 ][, array<string|int, mixed> $query_args = [] ][, array<string|int, mixed> $integrations = [] ][, bool $is_reset_request = false ]) : Query

This static method takes an array of parameters and creates Filter instances based on the parameter keys (slugs), then creates and returns a Query instance.

Parameters
$parameters : array<string|int, mixed> = []

The parameters for the filters (slug => value pairs).

$order_by : string|null = null

The order by for the query.

$is_preflight : bool = false

Whether this query is a preflight request.

$paged : int = 1

The current page number.

$query_args : array<string|int, mixed> = []

Additional WP_Query arguments for constraining the base query.

$integrations : array<string|int, mixed> = []

Integrations (shortcode attributes) for customizing the query.

$is_reset_request : bool = false

Whether this query is a reset request.

Return values
Query —

The created Query instance.

get_filters()

Get the filters associated with this query.

public get_filters() : array<string|int, Filter>
Return values
array<string|int, Filter> —

The filters.

get_filters_with_search_query()

Get the filters with a search query.

public get_filters_with_search_query() : array<string|int, Filter>
Return values
array<string|int, Filter> —

The filters with a search query.

get_integration()

Get a specific integration value.

public get_integration(string $key[, mixed $default = null ]) : mixed
Parameters
$key : string

The integration key.

$default : mixed = null

The default value if key doesn't exist.

Return values
mixed —

The integration value or default.

get_integrations()

Get the integrations.

public get_integrations() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The integrations array.

get_order_by()

Get the order by for the query.

public get_order_by() : string|null
Return values
string|null —

The order by for the query.

get_paged()

Get the current page number.

public get_paged() : int
Return values
int —

The current page number.

get_parameters()

Get the parameters for the filters.

public get_parameters() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —

The parameters for the filters.

get_possible_choices()

Get the possible choices for the filters.

public get_possible_choices() : array<string, array<string, string|string[]>>
Return values
array<string, array<string, string|string[]>> —

The possible choices for the filters.

get_product_ids()

Get the found product IDs.

public get_product_ids() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The found product IDs.

get_queriable_filters()

Get the queriable filters that have a search query.

public get_queriable_filters() : array<string|int, Filter>
Return values
array<string|int, Filter> —

The queriable filters.

get_query_args()

Get the query arguments.

public get_query_args() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The query arguments.

get_results()

Get the results for the query.

public get_results() : array<string|int, mixed>
Return values
array<string|int, mixed> —

has_no_results()

Check if the query has no results.

public has_no_results() : bool
Return values
bool —

Whether the query has no results.

has_search_filter()

Check if the query has a search filter.

public has_search_filter() : bool
Return values
bool —

Whether the query has a search filter.

is_preflight()

Check if this query is a preflight request.

public is_preflight() : bool
Return values
bool —

Whether this query is a preflight request.

is_reset_request()

Check if this query is a reset request.

public is_reset_request() : bool
Return values
bool —

Whether this query is a reset request.

maybe_insert_search_query()

Maybe insert the search query into the WP_Query.

public maybe_insert_search_query(WP_Query &$wp_query) : void
Parameters
$wp_query : WP_Query

The WP_Query object.

Return values
void —

prepare_results()

Prepare the results for the query.

public prepare_results() : void
Return values
void —

refresh()

Refresh the query results after query arguments or filters have been modified.

public refresh() : $this

This method resets the internal state and recalculates all results, possible choices, and other derived data based on the current filters, parameters, and query arguments.

Return values
$this —

set_filters()

Set the filters associated with this query.

public set_filters(array<string|int, Filter> $filters) : $this
Parameters
$filters : array<string|int, Filter>

The filters to set.

Return values
$this —

set_integrations()

Set the integrations.

public set_integrations(array<string|int, mixed> $integrations) : $this
Parameters
$integrations : array<string|int, mixed>

The integrations to set.

Return values
$this —

set_is_preflight()

Set whether this query is a preflight request.

public set_is_preflight(bool $is_preflight) : $this
Parameters
$is_preflight : bool

Whether this query is a preflight request.

Return values
$this —

set_is_reset_request()

Set whether this query is a reset request.

public set_is_reset_request(bool $is_reset_request) : $this
Parameters
$is_reset_request : bool

Whether this query is a reset request.

Return values
$this —

set_no_results()

Set whether the query has no results.

public set_no_results(bool $no_results) : self
Parameters
$no_results : bool

Whether the query has no results.

Return values
self —

set_order_by()

Set the order by for the query.

public set_order_by(string $order_by) : $this
Parameters
$order_by : string

The order by to set.

Return values
$this —

set_paged()

Set the current page number.

public set_paged(int $paged) : self
Parameters
$paged : int

The page number to set.

Return values
self —

set_parameters()

Set the parameters for the filters.

public set_parameters(array<string, string|string[]> $parameters) : $this
Parameters
$parameters : array<string, string|string[]>

The parameters to set.

Return values
$this —

set_parameters_to_filters()

Set the parameters to the filters.

public set_parameters_to_filters() : $this
Return values
$this —

set_possible_choices()

Set the possible choices for the filters.

public set_possible_choices(array<string, array<string, string|string[]>> $possible_choices) : $this
Parameters
$possible_choices : array<string, array<string, string|string[]>>

The possible choices to set.

Return values
$this —

set_query_args()

Set the query arguments.

public set_query_args(array<string|int, mixed> $query_args) : $this
Parameters
$query_args : array<string|int, mixed>

The query arguments to set.

Return values
$this —

apply_query_args()

Apply query arguments to constrain the base WP_Query.

private apply_query_args(WP_Query $query) : void
Parameters
$query : WP_Query

The query to modify.

Return values
void —

apply_sorting_to_product_ids()

Apply sorting to the found product IDs based on the order_by parameter.

private apply_sorting_to_product_ids() : void
Return values
void —

collect_possible_choices()

Collect the possible choices for the filters.

private collect_possible_choices([array<string|int, mixed> $posts = [] ]) : void
Parameters
$posts : array<string|int, mixed> = []

The posts to collect the possible choices from.

Return values
void —

generate_cache_key_for_query_args()

Generate a unique cache key for query args and filter parameters.

private static generate_cache_key_for_query_args(array<string|int, mixed> $query_args, Query $query) : string

Includes query args, filter parameters, and taxonomy/page context to ensure uniqueness across different contexts.

Parameters
$query_args : array<string|int, mixed>

The query arguments.

$query : Query

The query object.

Return values
string —

The cache key.

get_active_filters_list()

Get the active filters list.

private get_active_filters_list() : array<string|int, mixed>

This returns an array of filters that are currently active and their selected values.

Values may be an array if the filter is allows multiple selections.

Return values
array<string|int, mixed> —

The active filters list.

get_all_products()

Get all products that could potentially be shown using WooCommerce's query logic.

private get_all_products() : array<string|int, mixed>
Return values
array<string|int, mixed> —

Array of product IDs.

get_default_order_by()

Get the default order by value when none is provided.

private get_default_order_by() : string
Return values
string —

get_matching_posts()

Get the posts that match the filters.

private get_matching_posts() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The posts that match the filters.

get_post_ids_from_query_args()

Get post IDs from query args constraints.

private static get_post_ids_from_query_args(array<string|int, mixed> $query_args, Query $query) : array<string|int, mixed>
Parameters
$query_args : array<string|int, mixed>

The query arguments to apply.

$query : Query

The query object.

Return values
array<string|int, mixed> —

Array of post IDs that match the query constraints.

get_posts_per_page()

Get the number of posts per page based on integrations.

private get_posts_per_page() : int
Return values
int —

Number of posts per page.

get_search_query()

Get the search query from the first filter of the type Search.

private get_search_query() : string|null
Return values
string|null —

The search query or null if not found.

has_active_filters()

Check if there are any active filters in the current query.

private has_active_filters() : bool

An active filter is one that has been applied by the user and has a search query/value set. This is determined by checking if there are any queriable filters, which are filters that have search parameters set.

Return values
bool —

True if there are active filters, false otherwise.

load_post_ids_from_query_args()

Load post IDs from query arguments.

private static load_post_ids_from_query_args(array<string|int, mixed> $query_args) : array<string|int, mixed>
Parameters
$query_args : array<string|int, mixed>

The query arguments to apply.

Return values
array<string|int, mixed> —

Array of post IDs that match the query constraints.

normalize_array_for_cache()

Normalize an array for consistent cache key generation.

private static normalize_array_for_cache(array<string|int, mixed> $data) : array<string|int, mixed>

Recursively sorts array keys and normalizes values to ensure the same data produces the same hash regardless of key order.

Parameters
$data : array<string|int, mixed>

The array to normalize.

Return values
array<string|int, mixed> —

The normalized array.

sanitize_integrations()

Sanitize and validate integrations.

private sanitize_integrations(array<string|int, mixed> $integrations) : array<string|int, mixed>
Parameters
$integrations : array<string|int, mixed>

Raw integrations.

Return values
array<string|int, mixed> —

Sanitized integrations.

sanitize_query_args()

Sanitize and validate query arguments.

private sanitize_query_args(array<string|int, mixed> $query_args) : array<string|int, mixed>
Parameters
$query_args : array<string|int, mixed>

Raw query arguments.

Return values
array<string|int, mixed> —

Sanitized query arguments.

Search results