Introduce support for array format field names in `WP_Widget::get_field_name()` and `WP_Widget::get_field_id()`.

Fixes #12133
Props ch1902, welcher

Built from https://develop.svn.wordpress.org/trunk@34780


git-svn-id: http://core.svn.wordpress.org/trunk@34745 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2015-10-02 20:24:24 +00:00
parent dba5a73119
commit 7707dbf862
2 changed files with 11 additions and 3 deletions

View File

@ -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() * 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 * @param string $field_name Field name
* @return string Name attribute for $field_name * @return string Name attribute for $field_name
*/ */
public function get_field_name($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 * This function should be used in form() methods to create id attributes
* for fields to be saved by {@see WP_Widget::update()}. * 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 * @since 2.8.0
* @access public * @access public
* *
@ -197,7 +205,7 @@ class WP_Widget {
* @return string ID attribute for `$field_name`. * @return string ID attribute for `$field_name`.
*/ */
public function get_field_id( $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 ), '-' );
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-34779'; $wp_version = '4.4-alpha-34780';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.