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