Util

Utilities

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
associate_attachment()  : mixed
Associated attachment to post.
count_expired_posts()  : int
Count expired posts
create_page()  : int
Create a page and store the ID in an option. (adapted from WooCommerce)
disassociate_attachment()  : mixed
Disassociate attachment to post.
get_document_display_fields()  : array<string|int, mixed>
Retrieve the single document display option.
get_document_fields()  : array<string|int, mixed>
Retrive the Document post type fields.
get_eligible_auto_thumbnail_documents()  : array<string|int, mixed>
Get the eligible documents for auto thumbnail
get_the_fake_linked_term_names()  : string
Retrieve a linked list of terms (for Author and File Types).
is_acf_active()  : bool
Check if the Advanced Custom Fields plugin is active.
is_ept_active()  : bool
Check if the Easy Post Types plugin is active.
is_expired_document()  : bool
Is expired document
normalize_user_arguments()  : array<string|int, mixed>
Normalize user arguments provided to shortcode.
read_more_link()  : string
Get a read more link with a space before it.
round()  : float
Round a number using the built-in `round` function, but unless the value to round is numeric (a number or a string that can be parsed as a number), apply 'floatval' first to it (so it will convert it to 0 in most cases).
sanitize_array_recursive()  : mixed
Sanitize array recursively.
str_ends_with()  : bool
Determins if a string ends with another string
string_to_bool()  : bool
Converts a string (e.g. 'yes' or 'no') to a bool.

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>
Return values
mixed

associate_attachment()

Associated attachment to post.

public static associate_attachment(mixed $attachment_id, mixed $post_id) : mixed
Parameters
$attachment_id : mixed
$post_id : mixed
Return values
mixed

count_expired_posts()

Count expired posts

public static count_expired_posts() : int
Return values
int

create_page()

Create a page and store the ID in an option. (adapted from WooCommerce)

public static create_page(mixed $slug[, string $option = '' ][, string $page_title = '' ][, string $page_content = '' ], int $post_parent) : int
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.

Return values
int

page ID.

disassociate_attachment()

Disassociate attachment to post.

public static disassociate_attachment(mixed $attachment_id) : mixed
Parameters
$attachment_id : mixed
Return values
mixed

get_document_display_fields()

Retrieve the single document display option.

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

get_document_fields()

Retrive the Document post type fields.

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

get_eligible_auto_thumbnail_documents()

Get the eligible documents for auto thumbnail

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

get_the_fake_linked_term_names()

Retrieve a linked list of terms (for Author and File Types).

public static get_the_fake_linked_term_names(WP_Post $post, string $taxonomy[, string $sep = ', ' ]) : string
Parameters
$post : WP_Post
$taxonomy : string
$sep : string = ', '
Return values
string

is_acf_active()

Check if the Advanced Custom Fields plugin is active.

public static is_acf_active() : bool
Return values
bool

is_ept_active()

Check if the Easy Post Types plugin is active.

public static is_ept_active() : bool
Return values
bool

is_expired_document()

Is expired document

public static is_expired_document(WP_Post $post) : bool
Parameters
$post : WP_Post
Return values
bool

normalize_user_arguments()

Normalize user arguments provided to shortcode.

public static normalize_user_arguments(array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>
Tags
deprecated
Return values
array<string|int, mixed>

Get a read more link with a space before it.

public static read_more_link(string $path[, bool $new_tab = true ]) : string
Parameters
$path : string

The relative path to the link (relative to barn2.com).

$new_tab : bool = true

Whether to open the link in a new tab.

Return values
string

The read more link.

round()

Round a number using the built-in `round` function, but unless the value to round is numeric (a number or a string that can be parsed as a number), apply 'floatval' first to it (so it will convert it to 0 in most cases).

public static round(mixed $val, int $precision[, int $mode = PHP_ROUND_HALF_UP ]) : float

This is needed because in PHP 7 applying round to a non-numeric value returns 0, but in PHP 8 it throws an error. Specifically, in WooCommerce we have a few places where round('') is often executed.

Parameters
$val : mixed

The value to round.

$precision : int

The optional number of decimal digits to round to.

$mode : int = PHP_ROUND_HALF_UP

A constant to specify the mode in which rounding occurs.

Return values
float

The value rounded to the given precision as a float, or the supplied default value.

sanitize_array_recursive()

Sanitize array recursively.

public static sanitize_array_recursive(mixed $array) : mixed
Parameters
$array : mixed
Return values
mixed

str_ends_with()

Determins if a string ends with another string

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

string_to_bool()

Converts a string (e.g. 'yes' or 'no') to a bool.

public static string_to_bool(string|bool $string) : bool
Parameters
$string : string|bool

String to convert. If a bool is passed it will be returned as-is.

Return values
bool

Search results