Make WordPress Core


Ignore:
Timestamp:
10/02/2015 08:23:54 PM (11 years ago)
Author:
johnbillion
Message:

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

Fixes #12133
Props ch1902, welcher

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-widget.php

    r34560 r34780  
    178178     * This function should be used in form() methods to create name attributes for fields to be saved by update()
    179179     *
     180     * @since 4.4.0 Array format field names are now accepted.
     181     *
    180182     * @param string $field_name Field name
    181183     * @return string Name attribute for $field_name
    182184     */
    183185    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        }
    185191    }
    186192
     
    191197     * for fields to be saved by {@see WP_Widget::update()}.
    192198     *
     199     * @since 4.4.0 Array format field IDs are now accepted.
     200     *
    193201     * @since 2.8.0
    194202     * @access public
     
    198206     */
    199207    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 ), '-' );
    201209    }
    202210
Note: See TracChangeset for help on using the changeset viewer.