Make WordPress Core

Changeset 51070


Ignore:
Timestamp:
06/04/2021 10:46:22 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Simplify the logic in WP_Widget::get_field_name() and ::get_field_id().

Includes minor code layout fixes for better readability.

Follow-up to [41292], [50953], [50961].

Props 5ubliminal, solarissmoke, tamlyn, jdgrimes, jorbin, stevenkword, drebbits.web, westonruter, jipmoors, justinahinon, helen, lukecarbis, Mte90, hellofromTonya, SergeyBiryukov.
See #16773, #52627.

File:
1 edited

Legend:

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

    r50961 r51070  
    153153     *
    154154     * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
    155      *                                a portion of the widget's class name will be used. Has to be unique.
     155     *                                a portion of the widget's PHP class name will be used. Has to be unique.
    156156     * @param string $name            Name for the widget displayed on the configuration page.
    157157     * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
     
    189189     *
    190190     * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
    191      *                                a portion of the widget's class name will be used. Has to be unique.
     191     *                                a portion of the widget's PHP class name will be used. Has to be unique.
    192192     * @param string $name            Name for the widget displayed on the configuration page.
    193193     * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
     
    210210     * @since 4.4.0 Array format field names are now accepted.
    211211     *
    212      * @param string $field_name Field name
    213      * @return string Name attribute for $field_name
     212     * @param string $field_name Field name.
     213     * @return string Name attribute for `$field_name`.
    214214     */
    215215    public function get_field_name( $field_name ) {
    216216        $pos = strpos( $field_name, '[' );
    217         if ( false === $pos ) {
    218             return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
     217
     218        if ( false !== $pos ) {
     219            // Replace the first occurrence of '[' with ']['.
     220            $field_name = '[' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
    219221        } else {
    220             return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
    221         }
     222            $field_name = '[' . $field_name . ']';
     223        }
     224
     225        return 'widget-' . $this->id_base . '[' . $this->number . ']' . $field_name;
    222226    }
    223227
     
    235239     */
    236240    public function get_field_id( $field_name ) {
    237         return 'widget-' . $this->id_base . '-' . $this->number . '-' . trim( str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name ), '-' );
     241        $field_name = str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name );
     242        $field_name = trim( $field_name, '-' );
     243
     244        return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
    238245    }
    239246
     
    467474                 */
    468475                $instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
     476
    469477                if ( false !== $instance ) {
    470478                    $all_instances[ $number ] = $instance;
     
    522530
    523531        $return = null;
     532
    524533        if ( false !== $instance ) {
    525534            $return = $this->form( $instance );
     
    543552            do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) );
    544553        }
     554
    545555        return $return;
    546556    }
     
    555565     */
    556566    public function _register_one( $number = -1 ) {
    557         wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
    558         _register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
    559         _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
     567        wp_register_sidebar_widget(
     568            $this->id,
     569            $this->name,
     570            $this->_get_display_callback(),
     571            $this->widget_options,
     572            array( 'number' => $number )
     573        );
     574
     575        _register_widget_update_callback(
     576            $this->id_base,
     577            $this->_get_update_callback(),
     578            $this->control_options,
     579            array( 'number' => -1 )
     580        );
     581
     582        _register_widget_form_callback(
     583            $this->id,
     584            $this->name,
     585            $this->_get_form_callback(),
     586            $this->control_options,
     587            array( 'number' => $number )
     588        );
    560589    }
    561590
     
    602631
    603632        unset( $settings['_multiwidget'], $settings['__i__'] );
     633
    604634        return $settings;
    605635    }
Note: See TracChangeset for help on using the changeset viewer.