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.
Built from https://develop.svn.wordpress.org/trunk@51070


git-svn-id: http://core.svn.wordpress.org/trunk@50679 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-06-04 10:47:58 +00:00
parent cd4b3a357d
commit 4e2a6681a0
2 changed files with 42 additions and 12 deletions

View File

@ -152,7 +152,7 @@ class WP_Widget {
* @since 2.8.0 * @since 2.8.0
* *
* @param string $id_base Optional. Base ID for the widget, lowercase and unique. If left empty, * @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 string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
* information on accepted arguments. Default empty array. * information on accepted arguments. Default empty array.
@ -188,7 +188,7 @@ class WP_Widget {
* @see WP_Widget::__construct() * @see WP_Widget::__construct()
* *
* @param string $id_base Optional. Base ID for the widget, lowercase and unique. If left empty, * @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 string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
* information on accepted arguments. Default empty array. * information on accepted arguments. Default empty array.
@ -209,16 +209,20 @@ class WP_Widget {
* @since 2.8.0 * @since 2.8.0
* @since 4.4.0 Array format field names are now accepted. * @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 ) {
$pos = strpos( $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 { } 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;
} }
/** /**
@ -234,7 +238,10 @@ 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 . '-' . 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;
} }
/** /**
@ -466,6 +473,7 @@ class WP_Widget {
* @param WP_Widget $widget The current widget instance. * @param WP_Widget $widget The current widget instance.
*/ */
$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this ); $instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
if ( false !== $instance ) { if ( false !== $instance ) {
$all_instances[ $number ] = $instance; $all_instances[ $number ] = $instance;
} }
@ -521,6 +529,7 @@ class WP_Widget {
$instance = apply_filters( 'widget_form_callback', $instance, $this ); $instance = apply_filters( 'widget_form_callback', $instance, $this );
$return = null; $return = null;
if ( false !== $instance ) { if ( false !== $instance ) {
$return = $this->form( $instance ); $return = $this->form( $instance );
@ -542,6 +551,7 @@ class WP_Widget {
*/ */
do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) ); do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) );
} }
return $return; return $return;
} }
@ -554,9 +564,28 @@ class WP_Widget {
* compared to other instances of the same class. Default -1. * compared to other instances of the same class. Default -1.
*/ */
public function _register_one( $number = -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 ) ); wp_register_sidebar_widget(
_register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) ); $this->id,
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) ); $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 )
);
} }
/** /**
@ -601,6 +630,7 @@ class WP_Widget {
} }
unset( $settings['_multiwidget'], $settings['__i__'] ); unset( $settings['_multiwidget'], $settings['__i__'] );
return $settings; return $settings;
} }
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.8-alpha-51069'; $wp_version = '5.8-alpha-51070';
/** /**
* 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.