PluginProbe
Stream – Activity Log & Audit Trail / 3.1
Stream – Activity Log & Audit Trail v3.1
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 3.1, at classes/class-form-generator.php

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