Util

Utility functions

Tags
author

Barn2 Plugins support@barn2.com

license

GPL-3.0

copyright

Barn2 Media Ltd

Table of Contents

add_inline_script_params()  : mixed
Helper function to replace wp_localize_script
admin_has_wholesale_pricing()  : bool
Determine if the administrator views the default wholesale role pricing.
create_page()  : int
Create a page and store the ID in an option.
get_category_visibility()  : Category_Visibility
Gets the category visibility for a term ID.
get_current_user_wholesale_role_object()  : false|Wholesale_Role
Get a Wholesale_Role object for the current user.
get_default_role()  : string
Returns the default role option
get_login_redirect_url()  : string
Get the login redirect URL.
get_product_categories()  : array<string|int, mixed>
Retrieve an array of all product category IDs.
get_product_id_for_protection_check()  : int
Retrieve the product ID for protection check.
get_registration_redirect_url()  : string|false
Get the registration redirect URL.
get_the_category_visibility()  : array<string|int, Category_Visiblity>
Gets the Category_Visiblity objects for a WP_Post.
get_user_object()  : mixed
Return a user object
get_user_registration_fields()  : array<string|int, mixed>
Retrieve all the registration fields for a specific user
get_wholesale_login_url()  : string|false
Returns the permalink for the login page
get_wholesale_role_for_user()  : false|string
Retrieves the wholesale role for a user.
get_wholesale_roles()  : array<string|int, mixed>
Retrives all the wholesale role names (key)
get_wholesale_store_url()  : string|false
Returns the permalink for the store page
has_multiple_wholesale_roles()  : bool
Determines if the SITE has more than one wholesale role setup.
has_valid_license()  : bool
Determine whether the plugin has a valid license
is_between_actions()  : bool
Determine whether the execution is happening between two actions (and the first action has already completed)
is_json_media_type()  : mixed
Determine if media request type is JSON.
is_moderation_required()  : bool
Checks if user requires admin moderation
is_price_hidden()  : bool
Checks if the price of a product should be hidden
is_product_on_sale()  : mixed
is_protected()  : bool|string
Determines whether the supplied category or categories are protected.
is_rest_json_request()  : bool
Same as wp_is_json_request(), only available in 5.0
is_shop_administrator()  : bool
Determine if a user is a shop administrator.
is_wc_frontend_page()  : mixed
Check if we are on a WC frontend page.
is_wholesale_login_page()  : bool
Check if we are on wholesale login page
is_wholesale_store_page()  : bool
Check if we are on wholesale store page
is_wholesale_user()  : bool
Determins if the user is a wholesale user
shall_apply_wholesale_pricing()  : bool
Whether wholesale pricing shall be applied to the website for the user currently being logged in
site_uses_wc_shop_page()  : bool
Check if the current wholesale store page is the WC shop page
some_roles_have_sale_pricing()  : bool
Determine whether at least one wholesale role has sale pricing active
to_category_visibilities()  : array<string|int, Category_Visiblity>
Convers an array of WP_Terms to an array of Category_Visibility.
get_category_visibility_cache()  : array<string|int, mixed>
Get the category visiblity cache.
update_category_visibility_cache()  : mixed
Updates the category visiblity cache.

Methods

add_inline_script_params()

Helper function to replace wp_localize_script

public static add_inline_script_params(string $script_handle, string $variable_name, array<string|int, mixed> $script_params) : mixed
Parameters
$script_handle : string
$variable_name : string
$script_params : array<string|int, mixed>
Tags
Return values
mixed

admin_has_wholesale_pricing()

Determine if the administrator views the default wholesale role pricing.

public static admin_has_wholesale_pricing([int|WP_User $user = null ]) : bool
Parameters
$user : int|WP_User = null
Tags
Return values
bool

create_page()

Create a page and store the ID in an option.

public static create_page(mixed $slug[, string $option = '' ][, string $page_title = '' ][, string $page_content = '' ], int $post_parent) : int

This is a copy of wc_create_page so we can do WP CLI activations without WooCommerce

Parameters
$slug : mixed

Slug for the new page.

$option : string = ''

Option name to store the page's ID.

$page_title : string = ''

(default: '') Title for the new page.

$page_content : string = ''

(default: '') Content for the new page.

$post_parent : int

(default: 0) Parent for the new page.

Tags
Return values
int

page ID.

get_category_visibility()

Gets the category visibility for a term ID.

public static get_category_visibility(int|string $term_id) : Category_Visibility
Parameters
$term_id : int|string
Tags
Return values
Category_Visibility

get_current_user_wholesale_role_object()

Get a Wholesale_Role object for the current user.

public static get_current_user_wholesale_role_object() : false|Wholesale_Role
Tags
Return values
false|Wholesale_Role

get_default_role()

Returns the default role option

public static get_default_role() : string
Tags
Return values
string

get_login_redirect_url()

Get the login redirect URL.

public static get_login_redirect_url() : string
Tags
Return values
string

get_product_categories()

Retrieve an array of all product category IDs.

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

get_product_id_for_protection_check()

Retrieve the product ID for protection check.

public static get_product_id_for_protection_check(WC_Product $product) : int
Parameters
$product : WC_Product
Tags
Return values
int

get_registration_redirect_url()

Get the registration redirect URL.

public static get_registration_redirect_url() : string|false
Tags
Return values
string|false

get_the_category_visibility()

Gets the Category_Visiblity objects for a WP_Post.

public static get_the_category_visibility([WP_Post $post = null ]) : array<string|int, Category_Visiblity>
Parameters
$post : WP_Post = null
Tags
Return values
array<string|int, Category_Visiblity>

get_user_object()

Return a user object

public static get_user_object([int|WP_User $user = null ]) : mixed
Parameters
$user : int|WP_User = null

Either a user object or a user ID. Default to current user.

Tags
Return values
mixed

get_user_registration_fields()

Retrieve all the registration fields for a specific user

public static get_user_registration_fields(int $user_id) : array<string|int, mixed>
Parameters
$user_id : int
Tags
Return values
array<string|int, mixed>

$user_fields

get_wholesale_login_url()

Returns the permalink for the login page

public static get_wholesale_login_url() : string|false
Tags
Return values
string|false

get_wholesale_role_for_user()

Retrieves the wholesale role for a user.

public static get_wholesale_role_for_user([int|WP_User $user = null ]) : false|string
Parameters
$user : int|WP_User = null
Tags
Return values
false|string

get_wholesale_roles()

Retrives all the wholesale role names (key)

public static get_wholesale_roles([string $set = 'all' ]) : array<string|int, mixed>
Parameters
$set : string = 'all'

'all' or 'editable'

Tags
Return values
array<string|int, mixed>

get_wholesale_store_url()

Returns the permalink for the store page

public static get_wholesale_store_url() : string|false
Tags
Return values
string|false

has_multiple_wholesale_roles()

Determines if the SITE has more than one wholesale role setup.

public static has_multiple_wholesale_roles() : bool
Tags
Return values
bool

has_valid_license()

Determine whether the plugin has a valid license

public static has_valid_license() : bool
Tags
Return values
bool

is_between_actions()

Determine whether the execution is happening between two actions (and the first action has already completed)

public static is_between_actions(string $hook1, string $hook2) : bool
Parameters
$hook1 : string

The first action hook

$hook2 : string

The second action hook

Tags
Return values
bool

is_json_media_type()

Determine if media request type is JSON.

public static is_json_media_type(mixed $media_type) : mixed
Parameters
$media_type : mixed
Tags
Return values
mixed

is_moderation_required()

Checks if user requires admin moderation

public static is_moderation_required([int|WP_User $user = null ]) : bool
Parameters
$user : int|WP_User = null
Tags
Return values
bool

is_price_hidden()

Checks if the price of a product should be hidden

public static is_price_hidden([WC_Product $product = null ]) : bool

If the product parameter is missing, then return the global setting.

Parameters
$product : WC_Product = null

The product being considered

Tags
since
2.0.0

Added $product parameter to support category-based 'Hide price until login' option

Return values
bool

is_product_on_sale()

public static is_product_on_sale(mixed $onsale, mixed $product) : mixed
Parameters
$onsale : mixed
$product : mixed
Tags
Return values
mixed

is_protected()

Determines whether the supplied category or categories are protected.

public static is_protected(array<string|int, mixed>|Category_Visibility $categories) : bool|string

Checks the supplied categories including all ancestors of those categories (if any). Returns one of the following values:

  • 'wholesale_only' - One or more categories is protected to wholesale users only
  • 'public_only' - One or more categories is protected to specific users
  • false - All categories are public or at least one category has been 'unlocked' (see below).

The function will return false (i.e. not protected) if all categories including ancestors are public.

It also returns false if at least one protected category has been unlocked.

The function will always return false (i.e. unlocked) if at least one category is unlocked, regardless of the other categories supplied, even if the other categories are protected.

If two or more protected categories are found, or if one protected category has multiple types of protection, the function will return the first type of protection found, in the following order of precedence: wholesale_only, public_only. This can be controlled using the 'wcwp_category_protection_priority_order' filter.

Parameters
$categories : array<string|int, mixed>|Category_Visibility

The category or array of Category_Visibility objects to check.

Tags
Return values
bool|string

false if not protected, otherwise 'wholesale_only' or 'public_only',

is_rest_json_request()

Same as wp_is_json_request(), only available in 5.0

public static is_rest_json_request() : bool
Tags
Return values
bool

is_shop_administrator()

Determine if a user is a shop administrator.

public static is_shop_administrator([int|WP_User $user = null ]) : bool
Parameters
$user : int|WP_User = null
Tags
Return values
bool

is_wc_frontend_page()

Check if we are on a WC frontend page.

public static is_wc_frontend_page() : mixed
Tags
Return values
mixed

is_wholesale_login_page()

Check if we are on wholesale login page

public static is_wholesale_login_page() : bool
Tags
Return values
bool

is_wholesale_store_page()

Check if we are on wholesale store page

public static is_wholesale_store_page() : bool
Tags
Return values
bool

is_wholesale_user()

Determins if the user is a wholesale user

public static is_wholesale_user([int|WP_User $user = null ]) : bool
Parameters
$user : int|WP_User = null
Tags
Return values
bool

shall_apply_wholesale_pricing()

Whether wholesale pricing shall be applied to the website for the user currently being logged in

public static shall_apply_wholesale_pricing() : bool
Tags
Return values
bool

site_uses_wc_shop_page()

Check if the current wholesale store page is the WC shop page

public static site_uses_wc_shop_page() : bool
Tags
Return values
bool

some_roles_have_sale_pricing()

Determine whether at least one wholesale role has sale pricing active

public static some_roles_have_sale_pricing() : bool

Every time a role is updated, the wcwp_roles_with_sale_pricing option is updated with an array containing all the role that have sale pricing active.

Tags
Return values
bool

to_category_visibilities()

Convers an array of WP_Terms to an array of Category_Visibility.

public static to_category_visibilities(array<string|int, WP_Term$terms) : array<string|int, Category_Visiblity>
Parameters
$terms : array<string|int, WP_Term>
Tags
Return values
array<string|int, Category_Visiblity>

get_category_visibility_cache()

Get the category visiblity cache.

private static get_category_visibility_cache() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

update_category_visibility_cache()

Updates the category visiblity cache.

private static update_category_visibility_cache(array<string|int, Category_Visibility$term_visibilities) : mixed
Parameters
$term_visibilities : array<string|int, Category_Visibility>
Tags
Return values
mixed

Search results