Changeset 34780
- Timestamp:
- 10/02/2015 08:23:54 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-widget.php
r34560 r34780 178 178 * This function should be used in form() methods to create name attributes for fields to be saved by update() 179 179 * 180 * @since 4.4.0 Array format field names are now accepted. 181 * 180 182 * @param string $field_name Field name 181 183 * @return string Name attribute for $field_name 182 184 */ 183 185 public function get_field_name($field_name) { 184 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; 186 if ( false === $pos = strpos( $field_name, '[' ) ) { 187 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; 188 } else { 189 return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) ); 190 } 185 191 } 186 192 … … 191 197 * for fields to be saved by {@see WP_Widget::update()}. 192 198 * 199 * @since 4.4.0 Array format field IDs are now accepted. 200 * 193 201 * @since 2.8.0 194 202 * @access public … … 198 206 */ 199 207 public function get_field_id( $field_name ) { 200 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;208 return 'widget-' . $this->id_base . '-' . $this->number . '-' . trim( str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name ), '-' ); 201 209 } 202 210 -
trunk/tests/phpunit/tests/widgets.php
r34563 r34780 158 158 /** 159 159 * @see WP_Widget::get_field_name() 160 */ 161 function test_wp_widget_get_field_name() { 160 * @dataProvider data_wp_widget_get_field_name 161 * 162 */ 163 function test_wp_widget_get_field_name( $expected, $value_to_test ) { 162 164 $widget = new WP_Widget( 'foo', 'Foo' ); 163 165 $widget->_set( 2 ); 164 $this->assertEquals( 'widget-foo[2][title]', $widget->get_field_name( 'title' ) ); 166 $this->assertEquals( $expected, $widget->get_field_name( $value_to_test ) ); 167 } 168 169 /** 170 * Data provider. 171 * 172 * Passes the expected field name and the value to test. 173 * 174 * @since 4.4.0 175 * 176 * @return array { 177 * @type array { 178 * @type string $expected The expected field id to be returned. 179 * @type string $value_to_test The value being passed to the get_field_name method. 180 * } 181 * } 182 */ 183 function data_wp_widget_get_field_name( ) { 184 185 return array( 186 array( 187 'widget-foo[2][title]', 188 'title', 189 ), 190 array( 191 'widget-foo[2][posttypes][]', 192 'posttypes[]', 193 ), 194 array( 195 'widget-foo[2][posttypes][4]', 196 'posttypes[4]', 197 ), 198 array( 199 'widget-foo[2][posttypes][4][]', 200 'posttypes[4][]', 201 ), 202 array( 203 'widget-foo[2][posttypes][4][][6]', 204 'posttypes[4][][6]', 205 ), 206 ); 165 207 } 166 208 167 209 /** 168 210 * @see WP_Widget::get_field_id() 169 */ 170 function test_wp_widget_get_field_id() { 211 * @dataProvider data_wp_widget_get_field_id 212 * 213 */ 214 function test_wp_widget_get_field_id( $expected, $value_to_test ) { 171 215 $widget = new WP_Widget( 'foo', 'Foo' ); 172 216 $widget->_set( 2 ); 173 $this->assertEquals( 'widget-foo-2-title', $widget->get_field_id( 'title' ) ); 217 $this->assertEquals( $expected, $widget->get_field_id( $value_to_test ) ); 218 } 219 220 221 /** 222 * Data provider. 223 * 224 * Passes the expected field id and the value to be used in the tests. 225 * 226 * @since 4.4.0 227 * 228 * @return array { 229 * @type array { 230 * @type string $expected The expected field id to be returned. 231 * @type string $value_to_test The value being passed to the get_field_id method. 232 * } 233 * } 234 */ 235 function data_wp_widget_get_field_id() { 236 return array( 237 array( 238 'widget-foo-2-title', 239 'title', 240 ), 241 array( 242 'widget-foo-2-posttypes', 243 'posttypes[]', 244 ), 245 array( 246 'widget-foo-2-posttypes-4', 247 'posttypes[4]', 248 ), 249 array( 250 'widget-foo-2-posttypes-4', 251 'posttypes[4][]', 252 ), 253 array( 254 'widget-foo-2-posttypes-4-6', 255 'posttypes[4][][6]', 256 ), 257 ); 174 258 } 175 259
Note: See TracChangeset
for help on using the changeset viewer.