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

209 lines 5.3 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 ( is_array( $args['value'] ) ) {
137 $selected = selected( in_array( $parent['value'], $args['value'], true ), true, false );
138 } else {
139 $selected = selected( $args['value'], $parent['value'], false );
140 }
141 $output .= sprintf(
142 '<option class="parent" value="%1$s" %3$s>%2$s</option>',
143 $parent['value'],
144 $parent['text'],
145 $selected
146 );
147 $values[] = $parent['value'];
148 if ( ! empty( $parent['children'] ) ) {
149 foreach ( $parent['children'] as $child ) {
150 $output .= sprintf(
151 '<option class="child" value="%1$s" %3$s>%2$s</option>',
152 $child['value'],
153 $child['text'],
154 selected( $args['value'], $child['value'], false )
155 );
156 $values[] = $child['value'];
157 }
158 $output .= '</optgroup>';
159 }
160 }
161
162 $selected_values = explode( ',', $args['value'] );
163
164 foreach ( $selected_values as $selected_value ) {
165 if ( ! empty( $selected_value ) && ! in_array( $selected_value, $values, true ) ) {
166 $output .= sprintf(
167 '<option value="%1$s" %2$s>%1$s</option>',
168 $selected_value,
169 selected( true, true, false )
170 );
171 }
172 }
173
174 $output .= '</select>';
175 break;
176 case 'checkbox':
177 $output = sprintf(
178 '<input type="checkbox" name="%1$s" id="%1$s" value="1" %3$s>%2$s',
179 $args['name'],
180 $args['text'],
181 checked( $args['value'], true, false )
182 );
183 break;
184 default:
185 $output = apply_filters( 'wp_stream_form_render_field', $output, $field_type, $args );
186 break;
187 }
188
189 $output .= ! empty( $args['description'] ) ? wp_kses_post( sprintf( '<p class="description">%s</p>', $args['description'] ) ) : null;
190
191 return $output;
192 }
193
194 /**
195 * Prepares string with HTML data attributes
196 *
197 * @param $data array List of key/value data pairs to prepare.
198 * @return string
199 */
200 public function prepare_data_attributes_string( $data ) {
201 $output = '';
202 foreach ( $data as $key => $value ) {
203 $key = 'data-' . esc_attr( $key );
204 $output .= $key . '="' . esc_attr( $value ) . '" ';
205 }
206 return $output;
207 }
208 }
209