| @@ -1,7 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Generates an WP Admin form. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace WP_Stream; |
| 3 | 9 | |
| 10 | +/** | |
| 11 | + * Class - Form_Generator | |
| 12 | + */ | |
| 4 | 13 | class Form_Generator { |
| 5 | 14 | |
| 6 | 15 | /** |
| 7 | 16 | * List of all registered fields. |
| @@ -63,17 +72,20 @@ | ||
| 63 | 72 | * |
| 64 | 73 | * @return string |
| 65 | 74 | */ |
| 66 | 75 | 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 | + $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 | + ); | |
| 76 | 88 | |
| 77 | 89 | $output = ''; |
| 78 | 90 | switch ( $field_type ) { |
| 79 | 91 | case 'text': |
| @@ -94,9 +106,9 @@ | ||
| 94 | 106 | break; |
| 95 | 107 | case 'select': |
| 96 | 108 | $current_value = $args['value']; |
| 97 | 109 | |
| 98 | - $output = sprintf( | |
| 110 | + $output = sprintf( | |
| 99 | 111 | '<select name="%1$s" class="%2$s" id="%1$s">', |
| 100 | 112 | esc_attr( $args['name'] ), |
| 101 | 113 | esc_attr( $args['classes'] ) |
| 102 | 114 | ); |
| @@ -114,9 +126,9 @@ | ||
| 114 | 126 | case 'select2': |
| 115 | 127 | $values = array(); |
| 116 | 128 | |
| 117 | 129 | $multiple = ( $args['multiple'] ) ? 'multiple ' : ''; |
| 118 | - $output = sprintf( | |
| 130 | + $output = sprintf( | |
| 119 | 131 | '<select name="%1$s" id="%1$s" class="select2-select %2$s" %3$s%4$s>', |
| 120 | 132 | esc_attr( $args['name'] ), |
| 121 | 133 | esc_attr( $args['classes'] ), |
| 122 | 134 | $this->prepare_data_attributes_string( $args['data'] ), |
| @@ -127,37 +139,40 @@ | ||
| 127 | 139 | $output .= '<option value=""></option>'; |
| 128 | 140 | } |
| 129 | 141 | |
| 130 | 142 | 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'] ) ) { | |
| 143 | + $parent = wp_parse_args( | |
| 144 | + $parent, | |
| 145 | + array( | |
| 146 | + 'value' => '', | |
| 147 | + 'text' => '', | |
| 148 | + 'children' => array(), | |
| 149 | + ) | |
| 150 | + ); | |
| 151 | + if ( empty( $parent['value'] ) ) { | |
| 137 | 152 | continue; |
| 138 | 153 | } |
| 139 | 154 | if ( is_array( $args['value'] ) ) { |
| 140 | - $selected = selected( in_array( $parent['id'], $args['value'], true ), true, false ); | |
| 155 | + $selected = selected( in_array( $parent['value'], $args['value'], true ), true, false ); | |
| 141 | 156 | } else { |
| 142 | - $selected = selected( $args['value'], $parent['id'], false ); | |
| 157 | + $selected = selected( $args['value'], $parent['value'], false ); | |
| 143 | 158 | } |
| 144 | - $output .= sprintf( | |
| 159 | + $output .= sprintf( | |
| 145 | 160 | '<option class="parent" value="%1$s" %3$s>%2$s</option>', |
| 146 | - $parent['id'], | |
| 161 | + $parent['value'], | |
| 147 | 162 | $parent['text'], |
| 148 | 163 | $selected |
| 149 | 164 | ); |
| 150 | - $values[] = $parent['id']; | |
| 165 | + $values[] = $parent['value']; | |
| 151 | 166 | if ( ! empty( $parent['children'] ) ) { |
| 152 | 167 | foreach ( $parent['children'] as $child ) { |
| 153 | - $output .= sprintf( | |
| 168 | + $output .= sprintf( | |
| 154 | 169 | '<option class="child" value="%1$s" %3$s>%2$s</option>', |
| 155 | - $child['id'], | |
| 170 | + $child['value'], | |
| 156 | 171 | $child['text'], |
| 157 | - selected( $args['value'], $child['id'], false ) | |
| 172 | + selected( $args['value'], $child['value'], false ) | |
| 158 | 173 | ); |
| 159 | - $values[] = $child['id']; | |
| 174 | + $values[] = $child['value']; | |
| 160 | 175 | } |
| 161 | 176 | $output .= '</optgroup>'; |
| 162 | 177 | } |
| 163 | 178 | } |
| @@ -187,9 +202,9 @@ | ||
| 187 | 202 | $output = apply_filters( 'wp_stream_form_render_field', $output, $field_type, $args ); |
| 188 | 203 | break; |
| 189 | 204 | } |
| 190 | 205 | |
| 191 | - $output .= ! empty( $args['description'] ) ? wp_kses_post( sprintf( '<p class="description">%s</p>', $args['description'] ) ) : null; | |
| 206 | + $output .= ! empty( $args['description'] ) ? sprintf( '<p class="description">%s</p>', $args['description'] ) : null; | |
| 192 | 207 | |
| 193 | 208 | return $output; |
| 194 | 209 | } |
| 195 | 210 | |
| @@ -201,9 +216,9 @@ | ||
| 201 | 216 | */ |
| 202 | 217 | public function prepare_data_attributes_string( $data ) { |
| 203 | 218 | $output = ''; |
| 204 | 219 | foreach ( $data as $key => $value ) { |
| 205 | - $key = 'data-' . esc_attr( $key ); | |
| 220 | + $key = 'data-' . esc_attr( $key ); | |
| 206 | 221 | $output .= $key . '="' . esc_attr( $value ) . '" '; |
| 207 | 222 | } |
| 208 | 223 | return $output; |
| 209 | 224 | } |