Util

Utility class with static methods used throughout the plugin.

Tags
author

Barn2 Plugins support@barn2.com

license

GPL-3.0

copyright

Barn2 Media Ltd

Table of Contents

convert_array_to_dropdown_options()  : array<string|int, mixed>
Convert an array of options to a format suitable for a dropdown.
discount_exists()  : bool
Determine if a discount exists.
get_bulk_table_locations()  : array<string|int, mixed>
Get the list of locations where the bulk table can be displayed.
get_cart_products_ids()  : array<string|int, mixed>
Get the list of product IDs in the cart.
get_coupon_identifier()  : string
Generate an identifier slug used for setting up coupon data.
get_currency_data()  : array<string|int, mixed>
Get currency data about the store currency.
get_current_user_role()  : string
Get the 1st role of the currently logged in user.
get_product_text_allowed_html()  : array<string|int, mixed>
Get the list of allowed HTML tags for the discount text.
get_roles()  : array<string|int, mixed>
Returns a list of all registered user roles on the site.
get_selected_variations_by_specific_product()  : array<string|int, mixed>
Get selected variations by specific product id
get_selected_variations_id_flatten()  : array<string|int, mixed>
Get flatten array of id from specific product specific variations
prices_include_tax()  : bool
Determine whether prices are entered including tax.
product_has_categories()  : bool
Determine if a product has at least one of the given categories.
should_escape_discount_text()  : bool
Determine if the discount text should be escaped.
should_show_sale_price()  : bool
Helper function to determine if the sale price should be shown.
string_ends_with()  : bool
Determine if a string ends with a given suffix.
string_starts_with()  : bool
Determine if a string starts with a given prefix.

Methods

convert_array_to_dropdown_options()

Convert an array of options to a format suitable for a dropdown.

public static convert_array_to_dropdown_options(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
$options : array<string|int, mixed>

The list of options.

Return values
array<string|int, mixed>

The formatted list of options.

discount_exists()

Determine if a discount exists.

public static discount_exists(int $discount_id) : bool
Parameters
$discount_id : int

The discount ID.

Return values
bool

get_bulk_table_locations()

Get the list of locations where the bulk table can be displayed.

public static get_bulk_table_locations() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_cart_products_ids()

Get the list of product IDs in the cart.

public static get_cart_products_ids(WC_Cart $cart) : array<string|int, mixed>
Parameters
$cart : WC_Cart

The cart object.

Return values
array<string|int, mixed>

The list of product IDs.

get_coupon_identifier()

Generate an identifier slug used for setting up coupon data.

public static get_coupon_identifier(Discount $discount) : string
Parameters
$discount : Discount
Return values
string

get_currency_data()

Get currency data about the store currency.

public static get_currency_data() : array<string|int, mixed>

For use in JS.

Return values
array<string|int, mixed>

get_current_user_role()

Get the 1st role of the currently logged in user.

public static get_current_user_role([int|bool $user_id = false ][, bool $check_guest_role = false ]) : string

If a user ID is provided, get the 1st role of that user. If the user is a guest, return 'guest'.

If $user_id === 0 and $check_guest_role is true, return 'guest'. If $user_id === 0 and $check_guest_role is false, return an empty string.

Parameters
$user_id : int|bool = false

The user ID. If not provided, the current user is used. Default false. If 0 and $check_guest_role is true, return 'guest' but if 0 and $check_guest_role is false, return an empty string.

$check_guest_role : bool = false

Whether to check if the user is a guest. Default false.

Return values
string

The user role or 'guest' if the user is a guest or an empty string if $user_id === 0.

get_product_text_allowed_html()

Get the list of allowed HTML tags for the discount text.

public static get_product_text_allowed_html() : array<string|int, mixed>

Default to the list of allowed HTML tags for posts.

Return values
array<string|int, mixed>

get_roles()

Returns a list of all registered user roles on the site.

public static get_roles([bool $dropdown = false ][, bool $with_guest_role = false ]) : array<string|int, mixed>
Parameters
$dropdown : bool = false

Whether to format the list for use in a dropdown.

$with_guest_role : bool = false

Whether to include the guest role in the list.

Return values
array<string|int, mixed>

The list of roles.

get_selected_variations_by_specific_product()

Get selected variations by specific product id

public static get_selected_variations_by_specific_product(int $product_id, array<string|int, mixed> $variations) : array<string|int, mixed>
Parameters
$product_id : int
$variations : array<string|int, mixed>

Product variations

Return values
array<string|int, mixed>

$variations_id

get_selected_variations_id_flatten()

Get flatten array of id from specific product specific variations

public static get_selected_variations_id_flatten(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
$data : array<string|int, mixed>
Return values
array<string|int, mixed>

prices_include_tax()

Determine whether prices are entered including tax.

public static prices_include_tax() : bool
Return values
bool

product_has_categories()

Determine if a product has at least one of the given categories.

public static product_has_categories(int $product_id, array<string|int, mixed> $categories) : bool
Parameters
$product_id : int

The product ID.

$categories : array<string|int, mixed>

The list of category IDs.

Return values
bool

should_escape_discount_text()

Determine if the discount text should be escaped.

public static should_escape_discount_text() : bool
Return values
bool

should_show_sale_price()

Helper function to determine if the sale price should be shown.

public static should_show_sale_price([bool|WC_Product $product = false ]) : bool
Parameters
$product : bool|WC_Product = false

The product.

Return values
bool

Whether to show the sale price.

string_ends_with()

Determine if a string ends with a given suffix.

public static string_ends_with(string $haystack, string $needle) : bool
Parameters
$haystack : string
$needle : string
Return values
bool

string_starts_with()

Determine if a string starts with a given prefix.

public static string_starts_with(string $string, string $prefix) : bool
Parameters
$string : string
$prefix : string
Return values
bool

Search results