PluginProbe
Plugin Groups / 2.0.1
Plugin Groups v2.0.1
trunk 1.0.3 1.1.0 1.2.1 1.2.2 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.9 3.0.0
plugin-groups / classes / class-utils.php

class-utils.php in Plugin Groups 2.0.1, at classes/class-utils.php

118 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utils for Plugin Groups.
4 *
5 * @package plugin_groups
6 */
7
8 namespace Plugin_Groups;
9
10 /**
11 * Class Plugin_Groups_Utils
12 */
13 class Utils {
14
15 /**
16 * Get all the attributes from an HTML tag.
17 *
18 * @param string $tag HTML tag to get attributes from.
19 *
20 * @return array
21 */
22 public static function get_tag_attributes( $tag ) {
23 $tag = strstr( $tag, ' ', false );
24 $tag = trim( $tag, '> ' );
25 $args = shortcode_parse_atts( $tag );
26 $return = array();
27 foreach ( $args as $key => $value ) {
28 if ( is_int( $key ) ) {
29 $return[ $value ] = 'true';
30 continue;
31 }
32 $return[ $key ] = $value;
33 }
34
35 return $return;
36 }
37
38 /**
39 * Check if an element type is a void elements.
40 *
41 * @param string $element The element to check.
42 *
43 * @return bool
44 */
45 public static function is_void_element( $element ) {
46 $void_elements = array(
47 'area',
48 'base',
49 'br',
50 'col',
51 'embed',
52 'hr',
53 'img',
54 'input',
55 'link',
56 'meta',
57 'param',
58 'source',
59 'track',
60 'wbr',
61 );
62
63 return in_array( strtolower( $element ), $void_elements, true );
64 }
65
66 /**
67 * Build an HTML tag.
68 *
69 * @param string $element The element to build.
70 * @param array $attributes The attributes for the tags.
71 * @param string $content The element content.
72 *
73 * @return string
74 */
75 public static function build_tag( $element, $attributes = array(), $content = '' ) {
76
77 $parts = array(
78 '<' . $element,
79 );
80 if ( ! empty( $attributes ) ) {
81 $parts[] = self::build_attributes( $attributes );
82 }
83 $suffix = null;
84 if ( self::is_void_element( $element ) ) {
85 $parts[] = '/>';
86 } else {
87 $parts[] = '>';
88 $suffix = $content . '</' . $element . '>';
89 }
90
91 return implode( ' ', $parts ) . $suffix;
92 }
93
94 /**
95 * Builds and sanitizes attributes for an HTML tag.
96 *
97 * @param array $attributes Array of key value attributes to build.
98 *
99 * @return string
100 */
101 public static function build_attributes( $attributes ) {
102 $parts = array();
103 foreach ( $attributes as $attribute => $value ) {
104 if ( is_array( $value ) ) {
105 if ( count( $value ) !== count( $value, COUNT_RECURSIVE ) ) {
106 $value = wp_json_encode( $value );
107 } else {
108 $value = implode( ' ', $value );
109 }
110 }
111 $parts[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
112 }
113
114 return implode( ' ', $parts );
115
116 }
117 }
118