diff --git a/src/wp-includes/class-wp-widget.php b/src/wp-includes/class-wp-widget.php index 114328d499..0681d3715c 100644 --- a/src/wp-includes/class-wp-widget.php +++ b/src/wp-includes/class-wp-widget.php @@ -177,11 +177,17 @@ class WP_Widget { * * This function should be used in form() methods to create name attributes for fields to be saved by update() * + * @since 4.4.0 Array format field names are now accepted. + * * @param string $field_name Field name * @return string Name attribute for $field_name */ public function get_field_name($field_name) { - return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; + if ( false === $pos = strpos( $field_name, '[' ) ) { + return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; + } else { + return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) ); + } } /** @@ -190,6 +196,8 @@ class WP_Widget { * This function should be used in form() methods to create id attributes * for fields to be saved by {@see WP_Widget::update()}. * + * @since 4.4.0 Array format field IDs are now accepted. + * * @since 2.8.0 * @access public * @@ -197,7 +205,7 @@ class WP_Widget { * @return string ID attribute for `$field_name`. */ public function get_field_id( $field_name ) { - return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name; + return 'widget-' . $this->id_base . '-' . $this->number . '-' . trim( str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name ), '-' ); } /** diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 0086518b77..3aa6e73dd3 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -157,20 +157,104 @@ class Tests_Widgets extends WP_UnitTestCase { /** * @see WP_Widget::get_field_name() + * @dataProvider data_wp_widget_get_field_name + * */ - function test_wp_widget_get_field_name() { + function test_wp_widget_get_field_name( $expected, $value_to_test ) { $widget = new WP_Widget( 'foo', 'Foo' ); $widget->_set( 2 ); - $this->assertEquals( 'widget-foo[2][title]', $widget->get_field_name( 'title' ) ); + $this->assertEquals( $expected, $widget->get_field_name( $value_to_test ) ); + } + + /** + * Data provider. + * + * Passes the expected field name and the value to test. + * + * @since 4.4.0 + * + * @return array { + * @type array { + * @type string $expected The expected field id to be returned. + * @type string $value_to_test The value being passed to the get_field_name method. + * } + * } + */ + function data_wp_widget_get_field_name( ) { + + return array( + array( + 'widget-foo[2][title]', + 'title', + ), + array( + 'widget-foo[2][posttypes][]', + 'posttypes[]', + ), + array( + 'widget-foo[2][posttypes][4]', + 'posttypes[4]', + ), + array( + 'widget-foo[2][posttypes][4][]', + 'posttypes[4][]', + ), + array( + 'widget-foo[2][posttypes][4][][6]', + 'posttypes[4][][6]', + ), + ); } /** * @see WP_Widget::get_field_id() + * @dataProvider data_wp_widget_get_field_id + * */ - function test_wp_widget_get_field_id() { + function test_wp_widget_get_field_id( $expected, $value_to_test ) { $widget = new WP_Widget( 'foo', 'Foo' ); $widget->_set( 2 ); - $this->assertEquals( 'widget-foo-2-title', $widget->get_field_id( 'title' ) ); + $this->assertEquals( $expected, $widget->get_field_id( $value_to_test ) ); + } + + + /** + * Data provider. + * + * Passes the expected field id and the value to be used in the tests. + * + * @since 4.4.0 + * + * @return array { + * @type array { + * @type string $expected The expected field id to be returned. + * @type string $value_to_test The value being passed to the get_field_id method. + * } + * } + */ + function data_wp_widget_get_field_id() { + return array( + array( + 'widget-foo-2-title', + 'title', + ), + array( + 'widget-foo-2-posttypes', + 'posttypes[]', + ), + array( + 'widget-foo-2-posttypes-4', + 'posttypes[4]', + ), + array( + 'widget-foo-2-posttypes-4', + 'posttypes[4][]', + ), + array( + 'widget-foo-2-posttypes-4-6', + 'posttypes[4][][6]', + ), + ); } /**