Helpers
in package
Table of Contents
Methods
- arrayToString() : mixed
- blogExists() : bool
- Checks whether a site with the given blog ID exists in a multisite environment.
- buildBrandFilter() : string
- Build a TypeSense-compatible filter string for the brand field based on configuration.
- cleanSpecialCharacters() : string
- Cleans special characters from a string while preserving readable characters including Cyrillic, Asian characters, and other Unicode characters.
- decodeJsonlToArray() : array<string|int, mixed>
- Decodes a JSONL (JSON Lines) string into an array of JSON objects.
- extractTsSearchHits() : array<string|int, mixed>
- Extracts the 'document' key from each search result item in the provided array.
- flushCacheByLocales() : void
- Flushes WP Rocket cache for subsites in a WordPress multisite installation based on their locale.
- getAllAvailableLocales() : array<string|int, mixed>
- Retrieves all available locales from all sites.
- getAllCountriesForLocale() : array<string|int, mixed>
- Retrieves all available "country_of_sale" values for a given locale across all sites in a multisite network.
- getAttributeFirstValue() : string
- Retrieves the first value of a specified attribute from a product.
- getDurationMiliSecString() : mixed
- getDurationString() : mixed
- getFacetValues() : array<string|int, mixed>
- Extracts facet values from a Typesense search result.
- getFirstNonEmptyAttribute() : string|array<string|int, mixed>
- Retrieves the first non-empty value from a list of possible attribute keys in a product.
- getImportInfo() : array<string|int, mixed>
- Processes import results and returns statistics about successful and failed imports.
- getProductCardsFromHits() : array<string|int, mixed>
- Transforms search result hits into an array of ProductCard objects.
- getProductSlug() : string
- Generates a URL slug for a product based on its category and ID.
- getSiteIdByOptions() : int|null
- Retrieve the site ID (blog ID) for a multisite installation based on the given locale, country, and optionally brand.
- getSiteIdsByOptions() : array<string|int, int>
- Retrieve all site IDs (blog IDs) from a multisite installation that match the given locale, country, and/or brand.
- getTranslatedPimObject() : stdClass
- Retrieves the object that matches the specified language.
- isBrandValidForFilter() : bool
- Check if the brand of the given product passes the configured brand filter.
- isNonEmpty() : bool
- Determines whether a given value is considered "non-empty".
- parseColorString() : mixed
- transformAttributeId() : mixed
- transformProductId() : mixed
- updateFacetCountsIndexes() : array<string|int, mixed>
- Updates the facet count indexes and optionally sets the counts to zero.
- validateStringArray() : bool
- Validates whether the given array consists solely of strings.
- flushRocketCacheForCurrentSite() : void
- Flushes WP Rocket cache for the current site by deleting its cache folder from the filesystem.
- recursiveRmdir() : void
- Recursively deletes a directory and its contents.
Methods
arrayToString()
public
static arrayToString(string $separator, array<string|int, mixed> $array) : mixed
Parameters
- $separator : string
- $array : array<string|int, mixed>
blogExists()
Checks whether a site with the given blog ID exists in a multisite environment.
public
static blogExists(int $blogId) : bool
Uses the WordPress get_sites() function to determine if a site matching the provided
blog ID is registered. Returns false if the function is unavailable (e.g., not in a multisite context).
Parameters
- $blogId : int
-
The blog ID of the site to check.
Return values
bool —True if the site exists, false otherwise.
buildBrandFilter()
Build a TypeSense-compatible filter string for the brand field based on configuration.
public
static buildBrandFilter([int|null $blogId = null ]) : string
Retrieves the brand filter mode ('included' or 'excluded') and the list of brands from the config
for the given blog ID. Returns a string suitable to be appended to a TypeSense filter_by query.
Parameters
- $blogId : int|null = null
-
The blog ID used to retrieve brand filter configuration. If null, default config is used.
Return values
string —The filter string to append to a TypeSense query, or an empty string if no filter applies.
cleanSpecialCharacters()
Cleans special characters from a string while preserving readable characters including Cyrillic, Asian characters, and other Unicode characters.
public
static cleanSpecialCharacters(string $text) : string
Parameters
- $text : string
-
The input string to be cleaned
Return values
string —The cleaned string with preserved Unicode characters
decodeJsonlToArray()
Decodes a JSONL (JSON Lines) string into an array of JSON objects.
public
static decodeJsonlToArray(string $jsonl) : array<string|int, mixed>
Parameters
- $jsonl : string
-
A string containing JSON Lines, where each line is a JSON object.
Tags
Return values
array<string|int, mixed> —An array of decoded JSON objects.
extractTsSearchHits()
Extracts the 'document' key from each search result item in the provided array.
public
static extractTsSearchHits(array<string|int, mixed> $data) : array<string|int, mixed>
This function is used to process the search results returned from a TypeSense query. It iterates over each item in the input array, checks if the 'document' key exists, and if so, adds it to the $result array. The resulting array will have the same keys as the input array but will only contain the 'document' data.
Parameters
- $data : array<string|int, mixed>
-
The array of search results from TypeSense.
Return values
array<string|int, mixed> —An array containing only the 'document' elements from the search results.
flushCacheByLocales()
Flushes WP Rocket cache for subsites in a WordPress multisite installation based on their locale.
public
static flushCacheByLocales([array<string|int, mixed> $target_locales = [] ]) : void
If specific locales are provided (e.g., ['en', 'de']), only subsites that return these locales via ConfigProvider::getLocale() will have their cache flushed. If no locales are provided (empty array), the cache will be flushed for all subsites.
Requirements:
- Each subsite must have the ConfigProvider class with a static getLocale() method.
- Cache is stored in the WP Rocket cache directory (e.g., web/app/cache/wp-rocket).
Parameters
- $target_locales : array<string|int, mixed> = []
-
An array of locale codes (e.g., ['en', 'de']). If empty, all subsites will be flushed.
getAllAvailableLocales()
Retrieves all available locales from all sites.
public
static getAllAvailableLocales() : array<string|int, mixed>
This method fetches the locale setting from each site and returns a unique list of locales.
Return values
array<string|int, mixed> —List of unique locales available across all sites.
getAllCountriesForLocale()
Retrieves all available "country_of_sale" values for a given locale across all sites in a multisite network.
public
static getAllCountriesForLocale(string $locale) : array<string|int, mixed>
This function fetches the "site_locale" and "country_of_sale" options from all sites, then filters the results to include only the countries associated with the specified locale.
Parameters
- $locale : string
-
The locale to filter by.
Return values
array<string|int, mixed> —A unique list of country codes available for the given locale.
getAttributeFirstValue()
Retrieves the first value of a specified attribute from a product.
public
static getAttributeFirstValue(array<string|int, mixed> $product, string $attributeName) : string
This function checks if the 'value' key of the specified attribute in the product's attributes array is an array. If it is an array, the function returns the first element of that array. Otherwise, it returns the 'value' key directly.
This function is useful for handling attributes returned by TypeSense, where the 'value' key can contain either a single value or an array of values.
Parameters
- $product : array<string|int, mixed>
-
The product array containing the attributes.
- $attributeName : string
-
The name of the attribute to retrieve the value from.
Return values
string —The attribute value, or an empty string if the attribute or value is not found.
getDurationMiliSecString()
public
static getDurationMiliSecString(DateTime $startTime, DateTime $endTime) : mixed
Parameters
- $startTime : DateTime
- $endTime : DateTime
getDurationString()
public
static getDurationString(DateTime $startTime, DateTime $endTime) : mixed
Parameters
- $startTime : DateTime
- $endTime : DateTime
getFacetValues()
Extracts facet values from a Typesense search result.
public
static getFacetValues(array<string|int, mixed> $tsSearchResult) : array<string|int, mixed>
This function takes the result of a search query in Typesense and extracts the facet values
from the facet_counts section of the result. Facets are categories or attributes used for
filtering search results, and this function organizes them into an associative array.
Parameters
- $tsSearchResult : array<string|int, mixed>
-
The result array from a Typesense search query, containing facet counts.
Return values
array<string|int, mixed> —An associative array where the keys are facet field names and the values are arrays of facet values. If no facets are found, an empty array is returned.
getFirstNonEmptyAttribute()
Retrieves the first non-empty value from a list of possible attribute keys in a product.
public
static getFirstNonEmptyAttribute(array<string|int, mixed>|Product $product, array<string|int, mixed> $attributeKeys, string $returnType) : string|array<string|int, mixed>
This function loops through an array of attribute keys and checks if each key exists in the product's attributes and has a non-empty value. It returns either the first value as a string or the entire value array, depending on the specified return type.
Parameters
- $product : array<string|int, mixed>|Product
-
An associative array representing the product, which contains the attributes.
- $attributeKeys : array<string|int, mixed>
-
An array of possible keys to search for in the product's attributes.
- $returnType : string
-
Specifies the return type: 'string' to return the first value as a string, or 'array' to return the entire value array.
Return values
string|array<string|int, mixed> —The first non-empty attribute value found as a string or array, or an empty string/array if none is found.
getImportInfo()
Processes import results and returns statistics about successful and failed imports.
public
static getImportInfo(array<string|int, mixed> $importInfo, string $collectionName) : array<string|int, mixed>
Logs any errors that occurred during the import process.
Parameters
- $importInfo : array<string|int, mixed>
-
Array containing import results
- $collectionName : string
-
Name of the collection being imported
Return values
array<string|int, mixed> —Array with counts of successful and failed imports
getProductCardsFromHits()
Transforms search result hits into an array of ProductCard objects.
public
static getProductCardsFromHits(array<string|int, mixed> $searchResult) : array<string|int, mixed>
This static function processes the given search results, specifically the 'hits' array, and constructs ProductCard instances for each hit. It retrieves the product ID and associated card data from each document within the hits. If any errors occur during this process, they are logged for debugging purposes.
Parameters
- $searchResult : array<string|int, mixed>
-
The search results containing 'hits' to be processed.
Return values
array<string|int, mixed> —An array of ProductCard objects created from the search hits.
getProductSlug()
Generates a URL slug for a product based on its category and ID.
public
static getProductSlug(array<string|int, mixed> $product) : string
This function constructs a URL-friendly slug for a product by first extracting the product's ID and the category attribute from the product array. The category is converted into a URL-safe format using the Str::slug() method, which replaces spaces and special characters with hyphens.
The resulting slug is structured as "/category/product_id".
Parameters
- $product : array<string|int, mixed>
-
An associative array representing the product, containing its attributes.
Return values
string —A URL slug in the format "/category/product_id".
getSiteIdByOptions()
Retrieve the site ID (blog ID) for a multisite installation based on the given locale, country, and optionally brand.
public
static getSiteIdByOptions(string $locale, string $country[, string|null $brand = null ]) : int|null
This function iterates through all registered sites in a WordPress multisite network,
checking their ACF options (options_wsc_locale, options_wsc_country_of_sale, and optionally options_wsc_brand)
to find a match. If a site matches the provided criteria, its blog ID is returned.
Parameters
- $locale : string
-
The desired locale (e.g. 'en', 'de').
- $country : string
-
The country code (e.g. 'UK', 'DE').
- $brand : string|null = null
-
Optional brand identifier. If null, the brand is ignored during the search.
Return values
int|null —The matching site’s blog ID, or null if not found.
getSiteIdsByOptions()
Retrieve all site IDs (blog IDs) from a multisite installation that match the given locale, country, and/or brand.
public
static getSiteIdsByOptions([string|null $locale = null ][, string|null $country = null ][, string|null $brand = null ]) : array<string|int, int>
This function allows partial filtering — any combination of locale, country, and brand can be used. If a parameter is null, it is ignored during the filtering.
Parameters
- $locale : string|null = null
-
Optional locale (e.g. 'en', 'de').
- $country : string|null = null
-
Optional country code (e.g. 'UK', 'DE').
- $brand : string|null = null
-
Optional brand identifier.
Return values
array<string|int, int> —List of matching site blog IDs.
getTranslatedPimObject()
Retrieves the object that matches the specified language.
public
static getTranslatedPimObject(array<string|int, mixed> $objects, string $lang) : stdClass
This method searches through an array of objects and returns the parameter that corresponds to the provided language (locale). If no match is found, it returns the first object in the array by default.
Parameters
- $objects : array<string|int, mixed>
-
An array of traslated PIM objects, each having a 'locale' property.
- $lang : string
-
The language (locale) to search for within the objects.
Return values
stdClass —The parameter that matches the specified language, or the first parameter if no match is found.
isBrandValidForFilter()
Check if the brand of the given product passes the configured brand filter.
public
static isBrandValidForFilter(Product $product[, int|null $blogId = null ]) : bool
The brand filter is based on mode ('included' or 'excluded') and a list of brands defined in the config for the provided blog ID.
Parameters
- $product : Product
-
The product instance to check.
- $blogId : int|null = null
-
The blog ID used to load brand filter settings.
Return values
bool —True if the product's brand is valid according to the filter, false otherwise.
isNonEmpty()
Determines whether a given value is considered "non-empty".
public
static isNonEmpty(mixed $value) : bool
This method provides an extended and more context-aware interpretation of "non-empty" beyond PHP's built-in empty() function. It handles various data types including null, strings, arrays, objects, numbers, booleans, and traversables.
Parameters
- $value : mixed
-
The value to evaluate.
Return values
bool —True if the value is considered non-empty, false otherwise.
parseColorString()
public
static parseColorString(mixed $colorString) : mixed
Parameters
- $colorString : mixed
transformAttributeId()
public
static transformAttributeId(string $id) : mixed
Parameters
- $id : string
transformProductId()
public
static transformProductId(string $id) : mixed
Parameters
- $id : string
updateFacetCountsIndexes()
Updates the facet count indexes and optionally sets the counts to zero.
public
static updateFacetCountsIndexes(array<string|int, mixed> $facetCounts[, bool $setCountZero = false ]) : array<string|int, mixed>
This method reindexes the facet counts based on their field names. If the
$setCountZero parameter is true, it sets all counts to zero for each facet.
Parameters
- $facetCounts : array<string|int, mixed>
-
The array of facet counts to update.
- $setCountZero : bool = false
-
Whether to set all facet counts to zero (default is false).
Return values
array<string|int, mixed> —The updated facet counts with reindexed field names and optional zero counts.
validateStringArray()
Validates whether the given array consists solely of strings.
public
static validateStringArray(mixed $array) : bool
Parameters
- $array : mixed
-
The input to be validated.
Return values
bool —Returns true if the input is an array of strings, false otherwise.
flushRocketCacheForCurrentSite()
Flushes WP Rocket cache for the current site by deleting its cache folder from the filesystem.
private
static flushRocketCacheForCurrentSite() : void
recursiveRmdir()
Recursively deletes a directory and its contents.
private
static recursiveRmdir(string $dir) : void
Parameters
- $dir : string