CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils
  • Test_CMB2_Utils_WIN

Functions

  • _tests_cmb2_manually_load_plugin
  • cmb2_ajax
  • cmb2_autoload_classes
  • cmb2_bootstrap
  • cmb2_dir
  • cmb2_do_oembed
  • cmb2_get_field
  • cmb2_get_field_value
  • cmb2_get_metabox
  • cmb2_get_metabox_form
  • cmb2_get_metabox_sanitized_values
  • cmb2_get_oembed
  • cmb2_get_option
  • cmb2_metabox_form
  • cmb2_options
  • cmb2_print_metabox_form
  • cmb2_update_option
  • cmb2_utils
  • date_create_from_format
  • date_timestamp_get
  • new_cmb2_box

Hooks

  • Hook Reference

Class CMB2_Utils

CMB2 Utilities

Direct known subclasses

Test_CMB2_Utils_WIN

Package: CMB2
Category: WordPress_Plugin
License: GPL-2.0+
Author: CMB2 team
Since: 1.1.0
Link: https://cmb2.io
Located at includes/CMB2_Utils.php

Methods summary

public static integer|false
# image_id_from_url( string $img_url )

Utility method that attempts to get an attachment's ID by it's url

Utility method that attempts to get an attachment's ID by it's url

Parameters

$img_url
Attachment url.

Returns

integer|false
Attachment ID or false

Since

1.0.0
public static array
# get_available_image_sizes( )

Utility method to get a combined list of default and custom registered image sizes

Utility method to get a combined list of default and custom registered image sizes

Returns

array
The image sizes

Since

2.2.4

Link

http://core.trac.wordpress.org/ticket/18947
public static false|string
# get_named_size( array|string $size )

Utility method to return the closest named size from an array of values

Utility method to return the closest named size from an array of values

Based off of WordPress's image_get_intermediate_size() If the size matches an existing size then it will be used. If there is no direct match, then the nearest image size larger than the specified size will be used. If nothing is found, then the function will return false. Uses get_available_image_sizes() to get all available sizes.

Parameters

$size
Image size. Accepts an array of width and height (in that order).

Returns

false|string
Named image size e.g. 'thumbnail'

Since

2.2.4
public static string
# timezone_offset( string $tzstring )

Utility method that returns time string offset by timezone

Utility method that returns time string offset by timezone

Parameters

$tzstring
Time string.

Returns

string
Offset time string

Since

1.0.0
public static string
# timezone_string( )

Utility method that returns a timezone string representing the default timezone for the site.

Utility method that returns a timezone string representing the default timezone for the site.

Roughly copied from WordPress, as get_option('timezone_string') will return an empty string if no value has been set on the options page. A timezone string is required by the wp_timezone_choice() used by the select_timezone field.

Returns

string
Timezone string

Since

1.0.0
public static integer
# make_valid_time_stamp( string|integer $string )

Returns a unix timestamp, first checking if value already is a timestamp.

Returns a unix timestamp, first checking if value already is a timestamp.

Parameters

$string
Possible timestamp string.

Returns

integer
Time stamp.

Since

2.0.0
public static boolean
# is_valid_date( mixed $date )

Determine if a value is a valid date.

Determine if a value is a valid date.

Parameters

$date
Value to check.

Returns

boolean
Whether value is a valid date

Since

2.9.1
public static boolean
# is_valid_time_stamp( mixed $timestamp )

Determine if a value is a valid timestamp

Determine if a value is a valid timestamp

Parameters

$timestamp
Value to check.

Returns

boolean
Whether value is a valid timestamp

Since

2.0.0
public static boolean
# isempty( mixed $value )

Checks if a value is 'empty'. Still accepts 0.

Checks if a value is 'empty'. Still accepts 0.

Parameters

$value
Value to check.

Returns

boolean
True or false

Since

2.0.0
public static boolean
# notempty( mixed $value )

Checks if a value is not 'empty'. 0 doesn't count as empty.

Checks if a value is not 'empty'. 0 doesn't count as empty.

Parameters

$value
Value to check.

Returns

boolean
True or false

Since

2.2.2
public static array
# filter_empty( mixed $value )

Filters out empty values (not including 0).

Filters out empty values (not including 0).

Parameters

$value
Value to check.

Returns

array
True or false.

Since

2.2.2
public static
# array_insert( array & $array, array $new, integer $position )

Insert a single array item inside another array at a set position

Insert a single array item inside another array at a set position

Parameters

$array
Array to modify. Is passed by reference, and no return is needed. Passed by reference.
$new
New array to insert.
$position
Position in the main array to insert the new array.

Since

2.0.2
public static string
# url( string $path = '' )

Defines the url which is used to load local resources. This may need to be filtered for local Window installations. If resources do not load, please check the wiki for details.

Defines the url which is used to load local resources. This may need to be filtered for local Window installations. If resources do not load, please check the wiki for details.

Parameters

$path
URL path.

Returns

string
URL to CMB2 resources

Since

1.0.1
public static string
# get_url_from_dir( string $dir )

Converts a system path to a URL

Converts a system path to a URL

Parameters

$dir
Directory path to convert.

Returns

string
Converted URL.

Since

2.2.2
protected static string
# get_normalized_abspath( )

Get the normalized absolute path defined by WordPress.

Get the normalized absolute path defined by WordPress.

Returns

string
Normalized absolute path.

Since

2.2.6
protected static string
# normalize_path( string $path )

wp_normalize_path wrapper for back-compat. Normalize a filesystem path.

wp_normalize_path wrapper for back-compat. Normalize a filesystem path.

On windows systems, replaces backslashes with forward slashes and forces upper-case drive letters. Allows for two leading slashes for Windows network shares, but ensures that all other duplicate slashes are reduced to a single.

Parameters

$path
Path to normalize.

Returns

string
Normalized path.

Since

2.2.0
public static mixed
# get_timestamp_from_value( string $value, string $date_format )

Get timestamp from text date

Get timestamp from text date

Parameters

$value
Date value.
$date_format
Expected date format.

Returns

mixed
Unix timestamp representing the date.

Since

2.2.0
public static string
# php_to_js_dateformat( string $format )

Takes a php date() format string and returns a string formatted to suit for the date/time pickers It will work only with the following subset of date() options:

Takes a php date() format string and returns a string formatted to suit for the date/time pickers It will work only with the following subset of date() options:

Formats: d, l, j, z, m, F, n, y, and Y.

A slight effort is made to deal with escaped characters.

Other options are ignored, because they would either bring compatibility problems between PHP and JS, or bring even more translation troubles.

Parameters

$format
PHP date format.

Returns

string
reformatted string

Since

2.2.0
public static string
# wrap_escaped_chars( string $value )

Helper function for CMB_Utils::php_to_js_dateformat().

Helper function for CMB_Utils::php_to_js_dateformat().

Parameters

$value
Value to wrap/escape.

Returns

string
Modified value

Since

2.2.0
public static
# log_if_debug( string $function, integer $line, mixed $msg, mixed $debug = null )

Send to debug.log if WP_DEBUG is defined and true

Send to debug.log if WP_DEBUG is defined and true

Parameters

$function
Function name.
$line
Line number.
$msg
Message to output.
$debug
Variable to print_r.

Since

2.2.0
public static string|false
# get_file_ext( string $file )

Determine a file's extension

Determine a file's extension

Parameters

$file
File url.

Returns

string|false
File extension or false

Since

1.0.0
public static string
# get_file_name_from_path( string $value )

Get the file name from a url

Get the file name from a url

Parameters

$value
File url or path.

Returns

string
File name

Since

2.0.0
public static boolean
# wp_at_least( string $version )

Check if WP version is at least $version.

Check if WP version is at least $version.

Parameters

$version
WP version string to compare.

Returns

boolean
Result of comparison check.

Since

2.2.2
public static string
# concat_attrs( array $attrs, array $attr_exclude = array() )

Combines attributes into a string for a form element.

Combines attributes into a string for a form element.

Parameters

$attrs
Attributes to concatenate.
$attr_exclude
Attributes that should NOT be concatenated.

Returns

string
String of attributes for form element.

Since

1.1.0
public static boolean
# is_data_attribute( string $att )

Check if given attribute is a data attribute.

Check if given attribute is a data attribute.

Parameters

$att
HTML attribute.

Returns

boolean

Since

2.2.5
public static array
# ensure_array( mixed $value, array $default = array() )

Ensures value is an array.

Ensures value is an array.

Parameters

$value
Value to ensure is array.
$default
Default array. Defaults to empty array.

Returns

array
The array.

Since

2.2.3
public static mixed
# normalize_if_numeric( mixed $value )

If number is numeric, normalize it with floatval or intval, depending on if decimal is found.

If number is numeric, normalize it with floatval or intval, depending on if decimal is found.

Parameters

$value
Value to normalize (if numeric).

Returns

mixed
Possibly normalized value.

Since

2.2.6
public static string
# generate_hash( string $string )

Generates a 12 character unique hash from a string.

Generates a 12 character unique hash from a string.

Parameters

$string
String to create a hash from.

Returns

string

Since

2.4.0

Properties summary

protected static string $ABSPATH

The WordPress ABSPATH constant.

The WordPress ABSPATH constant.

Since

2.2.3
# CMB2_Utils::$ABSPATH
protected static string $url

The url which is used to load local resources.

The url which is used to load local resources.

Since

2.0.0
# ''
CMB2 Documentation API documentation generated by ApiGen