| @@ -1,16 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Generates an WP Admin form. | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace WP_Stream; |
| 9 | 3 | |
| 10 | -/** | |
| 11 | - * Class - Form_Generator | |
| 12 | - */ | |
| 13 | 4 | class Form_Generator { |
| 14 | 5 | |
| 15 | 6 | /** |
| 16 | 7 | * List of all registered fields. |
| @@ -35,43 +26,45 @@ | ||
| 35 | 26 | |
| 36 | 27 | /** |
| 37 | 28 | * Renders all fields currently registered. |
| 38 | 29 | * |
| 39 | - * @return void | |
| 30 | + * @return string | |
| 40 | 31 | */ |
| 41 | 32 | public function render_fields() { |
| 33 | + $output = ''; | |
| 42 | 34 | foreach ( $this->fields as $data ) { |
| 43 | - $this->render_field( $data['type'], $data['args'] ); | |
| 35 | + $output .= $this->render_field( $data['type'], $data['args'] ); | |
| 44 | 36 | } |
| 37 | + return $output; | |
| 45 | 38 | } |
| 46 | 39 | |
| 47 | 40 | /** |
| 48 | 41 | * Renders all fields currently registered as a table. |
| 49 | 42 | * |
| 50 | - * @return void | |
| 43 | + * @return string | |
| 51 | 44 | */ |
| 52 | 45 | public function render_fields_table() { |
| 53 | - echo '<table class="form-table">'; | |
| 46 | + $output = '<table class="form-table">'; | |
| 54 | 47 | foreach ( $this->fields as $data ) { |
| 55 | 48 | $title = ( array_key_exists( 'title', $data['args'] ) ) ? $data['args']['title'] : ''; |
| 56 | 49 | |
| 57 | - printf( '<tr><th>%s</th><td>', esc_html( $title ) ); | |
| 58 | - $this->render_field( $data['type'], $data['args'] ); | |
| 59 | - echo '</td><tr>'; | |
| 50 | + $output .= '<tr><th>' . $title . '</th><td>'; | |
| 51 | + $output .= $this->render_field( $data['type'], $data['args'] ); | |
| 52 | + $output .= '</td><tr>'; | |
| 60 | 53 | } |
| 61 | - echo '</table>'; | |
| 54 | + $output .= '</table>'; | |
| 55 | + return $output; | |
| 62 | 56 | } |
| 63 | 57 | |
| 64 | 58 | /** |
| 65 | - * Renders or returns a single field. | |
| 59 | + * Renders a single field. | |
| 66 | 60 | * |
| 67 | - * @param string $field_type The type of field being rendered. | |
| 68 | - * @param array $args The options for the field type. | |
| 69 | - * @param bool $echo_output Whether to echo the output or return it. | |
| 61 | + * @param string $field_type The type of field being rendered. | |
| 62 | + * @param array $args The options for the field type. | |
| 70 | 63 | * |
| 71 | - * @return string|void | |
| 64 | + * @return string | |
| 72 | 65 | */ |
| 73 | - public function render_field( $field_type, $args, $echo_output = true ) { | |
| 66 | + public function render_field( $field_type, $args ) { | |
| 74 | 67 | $args = wp_parse_args( |
| 75 | 68 | $args, |
| 76 | 69 | array( |
| 77 | 70 | 'name' => '', |
| @@ -123,14 +116,14 @@ | ||
| 123 | 116 | break; |
| 124 | 117 | case 'select2': |
| 125 | 118 | $values = array(); |
| 126 | 119 | |
| 127 | - $multiple = ( $args['multiple'] ) ? ' multiple' : ''; | |
| 120 | + $multiple = ( $args['multiple'] ) ? 'multiple ' : ''; | |
| 128 | 121 | $output = sprintf( |
| 129 | 122 | '<select name="%1$s" id="%1$s" class="select2-select %2$s" %3$s%4$s>', |
| 130 | 123 | esc_attr( $args['name'] ), |
| 131 | 124 | esc_attr( $args['classes'] ), |
| 132 | - $this->prepare_data_attributes_string( $args['data'] ), // The data attributes are escaped in the function. | |
| 125 | + $this->prepare_data_attributes_string( $args['data'] ), | |
| 133 | 126 | $multiple |
| 134 | 127 | ); |
| 135 | 128 | |
| 136 | 129 | if ( array_key_exists( 'placeholder', $args['data'] ) && ! $multiple ) { |
| @@ -154,21 +147,21 @@ | ||
| 154 | 147 | } else { |
| 155 | 148 | $selected = selected( $args['value'], $parent['value'], false ); |
| 156 | 149 | } |
| 157 | 150 | $output .= sprintf( |
| 158 | - '<option class="parent" value="%1$s" %2$s>%3$s</option>', | |
| 159 | - esc_attr( $parent['value'] ), | |
| 160 | - $selected, | |
| 161 | - esc_html( $parent['text'] ) | |
| 151 | + '<option class="parent" value="%1$s" %3$s>%2$s</option>', | |
| 152 | + $parent['value'], | |
| 153 | + $parent['text'], | |
| 154 | + $selected | |
| 162 | 155 | ); |
| 163 | 156 | $values[] = $parent['value']; |
| 164 | 157 | if ( ! empty( $parent['children'] ) ) { |
| 165 | 158 | foreach ( $parent['children'] as $child ) { |
| 166 | 159 | $output .= sprintf( |
| 167 | - '<option class="child" value="%1$s" %2$s>%3$s</option>', | |
| 168 | - esc_attr( $child['value'] ), | |
| 169 | - selected( $args['value'], $child['value'], false ), | |
| 170 | - esc_html( $child['text'] ) | |
| 160 | + '<option class="child" value="%1$s" %3$s>%2$s</option>', | |
| 161 | + $child['value'], | |
| 162 | + $child['text'], | |
| 163 | + selected( $args['value'], $child['value'], false ) | |
| 171 | 164 | ); |
| 172 | 165 | $values[] = $child['value']; |
| 173 | 166 | } |
| 174 | 167 | $output .= '</optgroup>'; |
| @@ -178,11 +171,11 @@ | ||
| 178 | 171 | $selected_values = explode( ',', $args['value'] ); |
| 179 | 172 | foreach ( $selected_values as $selected_value ) { |
| 180 | 173 | if ( ! empty( $selected_value ) && ! in_array( $selected_value, array_map( 'strval', $values ), true ) ) { |
| 181 | 174 | $output .= sprintf( |
| 182 | - '<option value="%1$s" selected="selected">%2$s</option>', | |
| 183 | - esc_attr( $selected_value ), | |
| 184 | - esc_html( $selected_value ) | |
| 175 | + '<option value="%1$s" %2$s>%1$s</option>', | |
| 176 | + $selected_value, | |
| 177 | + selected( true, true, false ) | |
| 185 | 178 | ); |
| 186 | 179 | } |
| 187 | 180 | } |
| 188 | 181 | |
| @@ -189,12 +182,12 @@ | ||
| 189 | 182 | $output .= '</select>'; |
| 190 | 183 | break; |
| 191 | 184 | case 'checkbox': |
| 192 | 185 | $output = sprintf( |
| 193 | - '<input type="checkbox" name="%1$s" id="%1$s" value="1" %2$s>%3$s', | |
| 194 | - esc_attr( $args['name'] ), | |
| 195 | - checked( $args['value'], true, false ), | |
| 196 | - esc_html( $args['text'] ) | |
| 186 | + '<input type="checkbox" name="%1$s" id="%1$s" value="1" %3$s>%2$s', | |
| 187 | + $args['name'], | |
| 188 | + $args['text'], | |
| 189 | + checked( $args['value'], true, false ) | |
| 197 | 190 | ); |
| 198 | 191 | break; |
| 199 | 192 | default: |
| 200 | 193 | $output = apply_filters( 'wp_stream_form_render_field', $output, $field_type, $args ); |
| @@ -200,17 +193,11 @@ | ||
| 200 | 193 | $output = apply_filters( 'wp_stream_form_render_field', $output, $field_type, $args ); |
| 201 | 194 | break; |
| 202 | 195 | } |
| 203 | 196 | |
| 204 | - if ( ! empty( $args['description'] ) ) { | |
| 205 | - $output .= sprintf( '<p class="description">%s</p>', esc_html( $args['description'] ) ); | |
| 206 | - } | |
| 197 | + $output .= ! empty( $args['description'] ) ? sprintf( '<p class="description">%s</p>', $args['description'] ) : null; | |
| 207 | 198 | |
| 208 | - if ( ! $echo_output ) { | |
| 209 | - return $output; | |
| 210 | - } | |
| 211 | - | |
| 212 | - echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 199 | + return $output; | |
| 213 | 200 | } |
| 214 | 201 | |
| 215 | 202 | /** |
| 216 | 203 | * Prepares string with HTML data attributes |
| @@ -220,13 +207,10 @@ | ||
| 220 | 207 | */ |
| 221 | 208 | public function prepare_data_attributes_string( $data ) { |
| 222 | 209 | $output = ''; |
| 223 | 210 | foreach ( $data as $key => $value ) { |
| 224 | - $output .= sprintf( | |
| 225 | - 'data-%s="%s" ', | |
| 226 | - esc_attr( $key ), | |
| 227 | - esc_attr( $value ) | |
| 228 | - ); | |
| 211 | + $key = 'data-' . esc_attr( $key ); | |
| 212 | + $output .= $key . '="' . esc_attr( $value ) . '" '; | |
| 229 | 213 | } |
| 230 | 214 | return $output; |
| 231 | 215 | } |
| 232 | 216 | } |