Skip to content

Commit

Permalink
Coding Standards: Simplify the logic in WP_Widget::get_field_name()
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
SergeyBiryukov committed Jun 4, 2021
1 parent 176da85 commit 794a136
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/wp-includes/class-wp-widget.php
Expand Up @@ -152,7 +152,7 @@ public function form( $instance ) {
* @since 2.8.0
*
* @param string $id_base Optional. Base ID for the widget, lowercase and unique. If left empty,
* a portion of the widget's class name will be used. Has to be unique.
* a portion of the widget's PHP class name will be used. Has to be unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
* information on accepted arguments. Default empty array.
Expand Down Expand Up @@ -188,7 +188,7 @@ public function __construct( $id_base, $name, $widget_options = array(), $contro
* @see WP_Widget::__construct()
*
* @param string $id_base Optional. Base ID for the widget, lowercase and unique. If left empty,
* a portion of the widget's class name will be used. Has to be unique.
* a portion of the widget's PHP class name will be used. Has to be unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
* information on accepted arguments. Default empty array.
Expand All @@ -209,16 +209,20 @@ public function WP_Widget( $id_base, $name, $widget_options = array(), $control_
* @since 2.8.0
* @since 4.4.0 Array format field names are now accepted.
*
* @param string $field_name Field name
* @return string Name attribute for $field_name
* @param string $field_name Field name.
* @return string Name attribute for `$field_name`.
*/
public function get_field_name( $field_name ) {
$pos = strpos( $field_name, '[' );
if ( false === $pos ) {
return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';

if ( false !== $pos ) {
// Replace the first occurrence of '[' with ']['.
$field_name = '[' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
} else {
return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
$field_name = '[' . $field_name . ']';
}

return 'widget-' . $this->id_base . '[' . $this->number . ']' . $field_name;
}

/**
Expand All @@ -234,7 +238,10 @@ public function get_field_name( $field_name ) {
* @return string ID attribute for `$field_name`.
*/
public function get_field_id( $field_name ) {
return 'widget-' . $this->id_base . '-' . $this->number . '-' . trim( str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name ), '-' );
$field_name = str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name );
$field_name = trim( $field_name, '-' );

return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
}

/**
Expand Down Expand Up @@ -466,6 +473,7 @@ public function update_callback( $deprecated = 1 ) {
* @param WP_Widget $widget The current widget instance.
*/
$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );

if ( false !== $instance ) {
$all_instances[ $number ] = $instance;
}
Expand Down Expand Up @@ -521,6 +529,7 @@ public function form_callback( $widget_args = 1 ) {
$instance = apply_filters( 'widget_form_callback', $instance, $this );

$return = null;

if ( false !== $instance ) {
$return = $this->form( $instance );

Expand All @@ -542,6 +551,7 @@ public function form_callback( $widget_args = 1 ) {
*/
do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) );
}

return $return;
}

Expand All @@ -554,9 +564,28 @@ public function form_callback( $widget_args = 1 ) {
* compared to other instances of the same class. Default -1.
*/
public function _register_one( $number = -1 ) {
wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
_register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
wp_register_sidebar_widget(
$this->id,
$this->name,
$this->_get_display_callback(),
$this->widget_options,
array( 'number' => $number )
);

_register_widget_update_callback(
$this->id_base,
$this->_get_update_callback(),
$this->control_options,
array( 'number' => -1 )
);

_register_widget_form_callback(
$this->id,
$this->name,
$this->_get_form_callback(),
$this->control_options,
array( 'number' => $number )
);
}

/**
Expand Down Expand Up @@ -601,6 +630,7 @@ public function get_settings() {
}

unset( $settings['_multiwidget'], $settings['__i__'] );

return $settings;
}
}

0 comments on commit 794a136

Please sign in to comment.