mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Introduce support for array format field names in WP_Widget::get_field_name() and WP_Widget::get_field_id().
Fixes #12133 Props ch1902, welcher git-svn-id: https://develop.svn.wordpress.org/trunk@34780 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d9031e0d51
commit
48300f62b3
@ -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 ), '-' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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]',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user