PluginProbe
Plugin Groups / 3.0.0
Plugin Groups v3.0.0
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 3.0.0, at classes/class-utils.php

162 lines 3.4 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
24 $tag = strstr( $tag, ' ', false );
25 $tag = trim( $tag, '> ' );
26 $args = shortcode_parse_atts( $tag );
27 $return = array();
28 foreach ( $args as $key => $value ) {
29 if ( is_int( $key ) ) {
30 $return[ $value ] = 'true';
31 continue;
32 }
33 $return[ $key ] = $value;
34 }
35
36 return $return;
37 }
38
39 /**
40 * Check if an element type is a void elements.
41 *
42 * @param string $element The element to check.
43 *
44 * @return bool
45 */
46 public static function is_void_element( $element ) {
47
48 $void_elements = array(
49 'area',
50 'base',
51 'br',
52 'col',
53 'embed',
54 'hr',
55 'img',
56 'input',
57 'link',
58 'meta',
59 'param',
60 'source',
61 'track',
62 'wbr',
63 );
64
65 return in_array( strtolower( $element ), $void_elements, true );
66 }
67
68 /**
69 * Build an HTML tag.
70 *
71 * @param string $element The element to build.
72 * @param array $attributes The attributes for the tags.
73 * @param string $content The element content.
74 *
75 * @return string
76 */
77 public static function build_tag( $element, $attributes = array(), $content = '' ) {
78
79 $parts = array(
80 '<' . $element,
81 );
82 if ( ! empty( $attributes ) ) {
83 $parts[] = self::build_attributes( $attributes );
84 }
85 $suffix = null;
86 if ( self::is_void_element( $element ) ) {
87 $parts[] = '/>';
88 } else {
89 $parts[] = '>';
90 $suffix = $content . '</' . $element . '>';
91 }
92
93 return implode( ' ', $parts ) . $suffix;
94 }
95
96 /**
97 * Builds and sanitizes attributes for an HTML tag.
98 *
99 * @param array $attributes Array of key value attributes to build.
100 *
101 * @return string
102 */
103 public static function build_attributes( $attributes ) {
104
105 $parts = array();
106 foreach ( $attributes as $attribute => $value ) {
107 if ( is_array( $value ) ) {
108 if ( count( $value ) !== count( $value, COUNT_RECURSIVE ) ) {
109 $value = wp_json_encode( $value );
110 } else {
111 $value = implode( ' ', $value );
112 }
113 }
114 $parts[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
115 }
116
117 return implode( ' ', $parts );
118 }
119
120 /**
121 * Generate a new ID for a group.
122 *
123 * @return string
124 */
125 public static function generate_id() {
126
127 return uniqid( 'nd' );
128 }
129
130 /**
131 * Get a sanitized input text field.
132 *
133 * Reads directly from the superglobals rather than filter_input(), which has a
134 * long-standing PHP/SAPI bug where INPUT_POST/INPUT_GET can return NULL even
135 * though the superglobal has the value.
136 *
137 * @param int $type The type to get.
138 * @param string $var The value to get.
139 *
140 * @return mixed
141 */
142 public static function get_sanitized_text( $type, $var ) {
143
144 switch ( $type ) {
145 case INPUT_POST:
146 $value = isset( $_POST[ $var ] ) ? wp_unslash( $_POST[ $var ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing
147 break;
148 case INPUT_GET:
149 $value = isset( $_GET[ $var ] ) ? wp_unslash( $_GET[ $var ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
150 break;
151 case INPUT_SERVER:
152 $value = isset( $_SERVER[ $var ] ) ? wp_unslash( $_SERVER[ $var ] ) : null;
153 break;
154 default:
155 $value = null;
156 }
157
158 return null === $value ? null : sanitize_text_field( $value );
159 }
160
161 }
162