From 4e2a6681a0e2e0bca4c5cded480ae7827cd995a3 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 4 Jun 2021 10:47:58 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-widget.php | 52 ++++++++++++++++++++++++++------- wp-includes/version.php | 2 +- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/wp-includes/class-wp-widget.php b/wp-includes/class-wp-widget.php index 71381d35e6..fa206e7d35 100644 --- a/wp-includes/class-wp-widget.php +++ b/wp-includes/class-wp-widget.php @@ -152,7 +152,7 @@ class WP_Widget { * @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. @@ -188,7 +188,7 @@ class WP_Widget { * @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. @@ -209,16 +209,20 @@ class WP_Widget { * @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; } /** @@ -234,7 +238,10 @@ class WP_Widget { * @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; } /** @@ -466,6 +473,7 @@ class WP_Widget { * @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; } @@ -521,6 +529,7 @@ class WP_Widget { $instance = apply_filters( 'widget_form_callback', $instance, $this ); $return = null; + if ( false !== $instance ) { $return = $this->form( $instance ); @@ -542,6 +551,7 @@ class WP_Widget { */ do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) ); } + return $return; } @@ -554,9 +564,28 @@ class WP_Widget { * 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 ) + ); } /** @@ -601,6 +630,7 @@ class WP_Widget { } unset( $settings['_multiwidget'], $settings['__i__'] ); + return $settings; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 894ba0fb42..8dd752e1f2 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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.