# plugin-groups/2.0.6/classes/class-utils.php

Plugin Groups, version 2.0.6. 130 lines.

- Page: https://pluginprobe.com/plugins/plugin-groups/2.0.6/code/classes/class-utils.php
- Raw: https://pluginprobe.com/plugins/plugin-groups/2.0.6/raw/classes/class-utils.php
- Modified: 2021-09-04T09:36:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/plugin-groups/2.0.6/code/classes/class-utils.php#L10-L20`.

```php
<?php
/**
 * Utils for Plugin Groups.
 *
 * @package plugin_groups
 */

namespace Plugin_Groups;

/**
 * Class Plugin_Groups_Utils
 */
class Utils {

	/**
	 * Get all the attributes from an HTML tag.
	 *
	 * @param string $tag HTML tag to get attributes from.
	 *
	 * @return array
	 */
	public static function get_tag_attributes( $tag ) {

		$tag    = strstr( $tag, ' ', false );
		$tag    = trim( $tag, '> ' );
		$args   = shortcode_parse_atts( $tag );
		$return = array();
		foreach ( $args as $key => $value ) {
			if ( is_int( $key ) ) {
				$return[ $value ] = 'true';
				continue;
			}
			$return[ $key ] = $value;
		}

		return $return;
	}

	/**
	 * Check if an element type is a void elements.
	 *
	 * @param string $element The element to check.
	 *
	 * @return bool
	 */
	public static function is_void_element( $element ) {

		$void_elements = array(
			'area',
			'base',
			'br',
			'col',
			'embed',
			'hr',
			'img',
			'input',
			'link',
			'meta',
			'param',
			'source',
			'track',
			'wbr',
		);

		return in_array( strtolower( $element ), $void_elements, true );
	}

	/**
	 * Build an HTML tag.
	 *
	 * @param string $element    The element to build.
	 * @param array  $attributes The attributes for the tags.
	 * @param string $content    The element content.
	 *
	 * @return string
	 */
	public static function build_tag( $element, $attributes = array(), $content = '' ) {

		$parts = array(
			'<' . $element,
		);
		if ( ! empty( $attributes ) ) {
			$parts[] = self::build_attributes( $attributes );
		}
		$suffix = null;
		if ( self::is_void_element( $element ) ) {
			$parts[] = '/>';
		} else {
			$parts[] = '>';
			$suffix  = $content . '</' . $element . '>';
		}

		return implode( ' ', $parts ) . $suffix;
	}

	/**
	 * Builds and sanitizes attributes for an HTML tag.
	 *
	 * @param array $attributes Array of key value attributes to build.
	 *
	 * @return string
	 */
	public static function build_attributes( $attributes ) {

		$parts = array();
		foreach ( $attributes as $attribute => $value ) {
			if ( is_array( $value ) ) {
				if ( count( $value ) !== count( $value, COUNT_RECURSIVE ) ) {
					$value = wp_json_encode( $value );
				} else {
					$value = implode( ' ', $value );
				}
			}
			$parts[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
		}

		return implode( ' ', $parts );
	}

	/**
	 * Generate a new ID for a group.
	 *
	 * @return string
	 */
	public static function generate_id() {

		return uniqid( 'nd' );
	}
}

```
