Utility functions

link Classnames utils

Conditionally join classnames together.
Function takes any number of arguments and returns a string containing space-separated class names.

Example one:

/**
 * @param mixed ...$args Arguments.
 * @return string Classnames
 */
use function T2\Utils\classnames;

$class_names = 'foo bar baz';
$class_names = classnames( 't2-block', $class_names );

Example two:

/**
 * @param mixed ...$args Arguments.
 * @return string Classnames
 */
use function T2\Utils\classnames;

$color = 'red';
$size = 'large';
$is_active = true;
$is_disabled = false;

$class_names = classnames(
    'button',
    [
        'button--color' => $color,
        'button--size' => $size,
        'button--active' => $is_active,
        'button--disabled' => $is_disabled,
    ],
    'button--icon',
    [
        'icon',
        'icon--close',
        [
            'icon__inner',
            'icon__inner--small' => $size === 'small',
        ],
	]
);

echo $class_names;

// Output: button button--color-red button--size-large button--active button--icon icon icon--close icon__inner icon__inner--small

link Attributes utils

Creates a valid id attribute based on a string.

Example usage:

/**
 * @param string $string String to create id from.
 */
use function T2\Utils\id_attribute;

function render( array $attributes, string $content ): string {
	$args = [
		'title'               => $attributes['title'],
	];

	$button_id   = $args['id'] ?? id_attribute( $args['title'] );
	$panel_id    = id_attribute( 'accordion-panel' );

	// Rest of the code...
}

link Array to attrs

Build attribute strings from associative array.
This function takes an array of HTML attributes and their values, sanitizes them to prevent malicious input, and returns a string of properly formatted HTML attribute-value pairs.

Example usage:

/**
 * @param array $attributes Attributes.
 */
use function T2\Utils\array_to_attrs;

$my_array = [
	'aria-label' => 'my-label',
	'class'      => 'custom-class',
	'data-foo'   => 'bar',
	'data-baz'   => 'qux',
	'disabled'   => true,
];

echo array_to_attrs($attributes);
// Output: aria-label="my-label" class="custom-class" data-foo="bar" data-baz="qux" disabled="1"

// Omit false values by setting second argument to true. (E.g. disabled="false" would give an unexpected result)
echo array_to_attrs( [
	'type'     => 'button',
	'class'    => 'wp-element-button',
	'disabled' => false,
	'data-id'  => 1,
], true );
// Output: type="button" class="wp-element-button" data-id="1"

link Array to style

Build a style attribute string from an associative array.
This function takes an array of CSS properties and their values, and returns a properly formatted CSS string.
The returned CSS string is escaped with esc_attr() by default, but this can be disabled.

Example usage:

/**
 * @param array $styles Css styles.
 */
use function T2\Utils\array_to_style;

$styles = [
	'color'   => '#fff',
	'padding' => (string) 0,
	'margin'  => '2rem',
];

echo array_to_style( $styles );
// Output: color: #fff; padding: 0; margin: 2rem;

link Array merge values

The function takes any number of arrays as input parameters and returns a new array that contains only the unique values from all the input arrays.

Example usage:

/**
 * @param array ...$arrays Arrays to merge.
 */
use function T2\Utils\array_merge_values;

$array1 = [ "apple", "banana", "orange", "peach" ];
$array2 = [ "orange", "peach", "grapefruit", "mango" ];
$array3 = [ "mango", "papaya", "banana", "kiwi" ];

$result = array_merge_values( $array1, $array2, $array3 );
print_r($result);
// Output: Array ( [0] => apple [1] => banana [2] => orange [3] => peach [4] => grapefruit [5] => mango [6] => papaya [7] => kiwi )

link String to array

The function takes a string and returns an array with non-empty and unique entries.
The string separator is a comma by default, but can be any character.

Example usage:

/**
 * @param mixed  $value A string to be converted.
 * @param string $separator The string separator.
 */
use function T2\Utils\string_to_array;

print_r( string_to_array( 'left, center, right' ) );
// Output: Array ( [0] => left [1] => center [2] => right )

print_r( string_to_array( 'left, center, right, left, ' ) );
// Output: Array ( [0] => left [1] => center [2] => right )

print_r( string_to_array( 'left|center|right', '|' ) );
// Output: Array ( [0] => left [1] => center [2] => right )

link Settings Page helper class

With the T2\Utils\Settings_Page helper class, creating a WordPress admin settings page
is fast and easy. First, you register a new settings page, and then you add option fields to it.
Option fields are grouped into sections.

link Example usage:

add_action( 'admin_menu', function (): void {
	$page = \T2\Utils\Settings_Page::register_page( 'dekode-starter-theme', [
		'page_title'      => __( 'Starter Theme settings', 'your-text-domain' ),
		'menu_title'      => __( 'Starter Theme', 'your-text-domain' ),
		'parent_slug'     => 'options-general.php', // This is the default value.
		'capability'      => 'manage_options',      // This is the default value.
		'plugin_basename' => plugin_basename( __FILE__ ), // Adds a plugin action link to the settings page.
	] );

	$page->add_section( [
		'section'     => 'your_prefix_section_01',
		'label'       => __( 'Add a section with add_section()', 'your-text-domain' ),
		'description' => __( 'This is a section description.', 'your-text-domain' ),
	] );

	$page->add_number( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_01',
		'label'       => __( 'add_number()', 'your-text-domain' ),
		'default'     => 123,
		'description' => __( 'This is a number field.', 'your-text-domain' ),
	] );

	$page->add_text( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_02',
		'label'       => __( 'add_text()', 'your-text-domain' ),
		'default'     => __( 'The default value', 'your-text-domain' ),
		'description' => __( 'This is a text field.', 'your-text-domain' ),
	] );

	$page->add_password( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_03',
		'label'       => __( 'add_password()', 'your-text-domain' ),
		'default'     => 'something',
		'description' => __( 'This is a password field.', 'your-text-domain' ),
	] );

	$page->add_radio( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_04',
		'terms'       => [ 'hello' => 'Hello', 'world' => 'World' ],
		'default'     => 'world',
		'label'       => __( 'add_radio()', 'your-text-domain' ),
		'description' => __( 'These are radio buttons.', 'your-text-domain' ),
	] );

	$page->add_checkbox( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_05',
		'default'     => 1,
		'label'       => __( 'add_checkbox()', 'your-text-domain' ),
		'description' => __( 'This is a checkbox field. Values are stored a 1 or 0 (not true or false)', 'your-text-domain' ),
	] );

	$page->add_multibox( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_06',
		'terms'       => [ 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3' ],
		'default'     => [ 'option1', 'option3' ],
		'label'       => __( 'add_multibox()', 'your-text-domain' ),
		'description' => __( 'Select none, one or multiple options.', 'your-text-domain' ),
	] );

	$page->add_dropdown( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_07',
		'terms'       => [ 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3' ],
		'default'     => 'option2',
		'label'       => __( 'add_dropdown()', 'your-text-domain' ),
		'description' => __( 'This is a select field.', 'your-text-domain' ),
	] );

	$page->add_list( [
		'section'     => 'your_prefix_section_01',
		'option'      => 'your_prefix_option_08',
		'default'     => [ 'List item 1', 'List item 2', 'List item 3', 'List item 4', 'List item 5' ],
		'label'       => __( 'add_list()', 'your-text-domain' ),
		'description' => [
			__( 'This fields lets you add, edit and remove list items, i.e. urls.', 'your-text-domain' ),
			__( 'The list items are stored in an array.', 'your-text-domain' ),
		]
	] );
});

link Setting page

Settings Page screenshot