PluginProbe
Stream – Activity Log & Audit Trail / 4.0.0
Stream – Activity Log & Audit Trail v4.0.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-form-generator.php

class-form-generator.php in Stream – Activity Log & Audit Trail 4.0.0, at classes/class-form-generator.php

226 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Generates an WP Admin form.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Form_Generator
12 */
13 class Form_Generator {
14
15 /**
16 * List of all registered fields.
17 *
18 * @var array
19 */
20 public $fields = array();
21
22 /**
23 * Adds a new field to the form.
24 *
25 * @param string $field_type The type of field being added.
26 * @param array $args Options for the field. See render_field().
27 * @return void
28 */
29 public function add_field( $field_type, $args ) {
30 $this->fields[] = array(
31 'type' => $field_type,
32 'args' => $args,
33 );
34 }
35
36 /**
37 * Renders all fields currently registered.
38 *
39 * @return string
40 */
41 public function render_fields() {
42 $output = '';
43 foreach ( $this->fields as $data ) {
44 $output .= $this->render_field( $data['type'], $data['args'] );
45 }
46 return $output;
47 }
48
49 /**
50 * Renders all fields currently registered as a table.
51 *
52 * @return string
53 */
54 public function render_fields_table() {
55 $output = '<table class="form-table">';
56 foreach ( $this->fields as $data ) {
57 $title = ( array_key_exists( 'title', $data['args'] ) ) ? $data['args']['title'] : '';
58
59 $output .= '<tr><th>' . $title . '</th><td>';
60 $output .= $this->render_field( $data['type'], $data['args'] );
61 $output .= '</td><tr>';
62 }
63 $output .= '</table>';
64 return $output;
65 }
66
67 /**
68 * Renders a single field.
69 *
70 * @param string $field_type The type of field being rendered.
71 * @param array $args The options for the field type.
72 *
73 * @return string
74 */
75 public function render_field( $field_type, $args ) {
76 $args = wp_parse_args(
77 $args,
78 array(
79 'name' => '',
80 'value' => '',
81 'options' => array(),
82 'description' => '',
83 'classes' => '',
84 'data' => array(),
85 'multiple' => false,
86 )
87 );
88
89 $output = '';
90 switch ( $field_type ) {
91 case 'text':
92 $output = sprintf(
93 '<input type="text" name="%1$s" id="%1$s" class="%2$s" value="%3$s" />',
94 esc_attr( $args['name'] ),
95 esc_attr( $args['classes'] ),
96 esc_attr( $args['value'] )
97 );
98 break;
99 case 'hidden':
100 $output = sprintf(
101 '<input type="hidden" name="%1$s" id="%1$s" class="%2$s" value="%3$s" />',
102 esc_attr( $args['name'] ),
103 esc_attr( $args['classes'] ),
104 esc_attr( $args['value'] )
105 );
106 break;
107 case 'select':
108 $current_value = $args['value'];
109
110 $output = sprintf(
111 '<select name="%1$s" class="%2$s" id="%1$s">',
112 esc_attr( $args['name'] ),
113 esc_attr( $args['classes'] )
114 );
115
116 foreach ( $args['options'] as $value => $label ) {
117 $output .= sprintf(
118 '<option value="%1$s" %2$s>%3$s</option>',
119 esc_attr( $value ),
120 selected( $value === $current_value, true, false ),
121 esc_html( $label )
122 );
123 }
124 $output .= '</select>';
125 break;
126 case 'select2':
127 $values = array();
128
129 $multiple = ( $args['multiple'] ) ? 'multiple ' : '';
130 $output = sprintf(
131 '<select name="%1$s" id="%1$s" class="select2-select %2$s" %3$s%4$s>',
132 esc_attr( $args['name'] ),
133 esc_attr( $args['classes'] ),
134 $this->prepare_data_attributes_string( $args['data'] ),
135 $multiple
136 );
137
138 if ( array_key_exists( 'placeholder', $args['data'] ) && ! $multiple ) {
139 $output .= '<option value=""></option>';
140 }
141
142 foreach ( $args['options'] as $parent ) {
143 $parent = wp_parse_args(
144 $parent,
145 array(
146 'value' => '',
147 'text' => '',
148 'children' => array(),
149 )
150 );
151 if ( empty( $parent['value'] ) ) {
152 continue;
153 }
154 if ( is_array( $args['value'] ) ) {
155 $selected = selected( in_array( $parent['value'], $args['value'], true ), true, false );
156 } else {
157 $selected = selected( $args['value'], $parent['value'], false );
158 }
159 $output .= sprintf(
160 '<option class="parent" value="%1$s" %3$s>%2$s</option>',
161 $parent['value'],
162 $parent['text'],
163 $selected
164 );
165 $values[] = $parent['value'];
166 if ( ! empty( $parent['children'] ) ) {
167 foreach ( $parent['children'] as $child ) {
168 $output .= sprintf(
169 '<option class="child" value="%1$s" %3$s>%2$s</option>',
170 $child['value'],
171 $child['text'],
172 selected( $args['value'], $child['value'], false )
173 );
174 $values[] = $child['value'];
175 }
176 $output .= '</optgroup>';
177 }
178 }
179
180 $selected_values = explode( ',', $args['value'] );
181 foreach ( $selected_values as $selected_value ) {
182 if ( ! empty( $selected_value ) && ! in_array( $selected_value, array_map( 'strval', $values ), true ) ) {
183 $output .= sprintf(
184 '<option value="%1$s" %2$s>%1$s</option>',
185 $selected_value,
186 selected( true, true, false )
187 );
188 }
189 }
190
191 $output .= '</select>';
192 break;
193 case 'checkbox':
194 $output = sprintf(
195 '<input type="checkbox" name="%1$s" id="%1$s" value="1" %3$s>%2$s',
196 $args['name'],
197 $args['text'],
198 checked( $args['value'], true, false )
199 );
200 break;
201 default:
202 $output = apply_filters( 'wp_stream_form_render_field', $output, $field_type, $args );
203 break;
204 }
205
206 $output .= ! empty( $args['description'] ) ? sprintf( '<p class="description">%s</p>', $args['description'] ) : null;
207
208 return $output;
209 }
210
211 /**
212 * Prepares string with HTML data attributes
213 *
214 * @param array $data List of key/value data pairs to prepare.
215 * @return string
216 */
217 public function prepare_data_attributes_string( $data ) {
218 $output = '';
219 foreach ( $data as $key => $value ) {
220 $key = 'data-' . esc_attr( $key );
221 $output .= $key . '="' . esc_attr( $value ) . '" ';
222 }
223 return $output;
224 }
225 }
226