Skip to content

Commit 794a136

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51070 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 176da85 commit 794a136

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/wp-includes/class-wp-widget.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function form( $instance ) {
152152
* @since 2.8.0
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
158158
* information on accepted arguments. Default empty array.
@@ -188,7 +188,7 @@ public function __construct( $id_base, $name, $widget_options = array(), $contro
188188
* @see WP_Widget::__construct()
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
194194
* information on accepted arguments. Default empty array.
@@ -209,16 +209,20 @@ public function WP_Widget( $id_base, $name, $widget_options = array(), $control_
209209
* @since 2.8.0
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( '[' ) );
222+
$field_name = '[' . $field_name . ']';
221223
}
224+
225+
return 'widget-' . $this->id_base . '[' . $this->number . ']' . $field_name;
222226
}
223227

224228
/**
@@ -234,7 +238,10 @@ public function get_field_name( $field_name ) {
234238
* @return string ID attribute for `$field_name`.
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

240247
/**
@@ -466,6 +473,7 @@ public function update_callback( $deprecated = 1 ) {
466473
* @param WP_Widget $widget The current widget instance.
467474
*/
468475
$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
476+
469477
if ( false !== $instance ) {
470478
$all_instances[ $number ] = $instance;
471479
}
@@ -521,6 +529,7 @@ public function form_callback( $widget_args = 1 ) {
521529
$instance = apply_filters( 'widget_form_callback', $instance, $this );
522530

523531
$return = null;
532+
524533
if ( false !== $instance ) {
525534
$return = $this->form( $instance );
526535

@@ -542,6 +551,7 @@ public function form_callback( $widget_args = 1 ) {
542551
*/
543552
do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) );
544553
}
554+
545555
return $return;
546556
}
547557

@@ -554,9 +564,28 @@ public function form_callback( $widget_args = 1 ) {
554564
* compared to other instances of the same class. Default -1.
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

562591
/**
@@ -601,6 +630,7 @@ public function get_settings() {
601630
}
602631

603632
unset( $settings['_multiwidget'], $settings['__i__'] );
633+
604634
return $settings;
605635
}
606636
}

0 commit comments

Comments
 (0)