WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/15/2019 11:08:02 AM (2 months ago)
Author:
SergeyBiryukov
Message:

Code Modernisation: Introduce the spread operator in wp-includes/deprecated.php.

Rather than relying func_get_args() to retrieve arbitrary function arguments, we can now use the spread operator to assign them directly to a variable.

While these functions are deprecated, they can still get a minor performance boost in case they are being called.

Props jrf.
See #47678.

File:
1 edited

Legend:

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

    r45932 r46129  
    17991799 * @see _n()
    18001800 */
    1801 function __ngettext() {
     1801function __ngettext( ...$args ) {
    18021802    _deprecated_function( __FUNCTION__, '2.8.0', '_n()' );
    1803     $args = func_get_args();
    1804     return call_user_func_array('_n', $args);
     1803    return _n( ...$args );
    18051804}
    18061805
     
    18121811 * @see _n_noop()
    18131812 */
    1814 function __ngettext_noop() {
     1813function __ngettext_noop( ...$args ) {
    18151814    _deprecated_function( __FUNCTION__, '2.8.0', '_n_noop()' );
    1816     $args = func_get_args();
    1817     return call_user_func_array('_n_noop', $args);
     1815    return _n_noop( ...$args );
    18181816
    18191817}
     
    20772075    _deprecated_function( __FUNCTION__, '2.8.0', 'esc_html()' );
    20782076    if ( func_num_args() > 1 ) { // Maintain back-compat for people passing additional arguments.
    2079         $args = func_get_args();
    2080         return call_user_func_array( '_wp_specialchars', $args );
     2077        return _wp_specialchars( $string, $quote_style, $charset, $double_encode );
    20812078    } else {
    20822079        return esc_html( $string );
     
    21182115 * @param mixed      ...$params       Widget parameters.
    21192116 */
    2120 function register_sidebar_widget($name, $output_callback, $classname = '') {
     2117function register_sidebar_widget($name, $output_callback, $classname = '', ...$params) {
    21212118    _deprecated_function( __FUNCTION__, '2.8.0', 'wp_register_sidebar_widget()' );
    21222119    // Compat
    2123     if ( is_array($name) ) {
    2124         if ( count($name) == 3 )
    2125             $name = sprintf($name[0], $name[2]);
    2126         else
     2120    if ( is_array( $name ) ) {
     2121        if ( count( $name ) === 3 ) {
     2122            $name = sprintf( $name[0], $name[2] );
     2123        } else {
    21272124            $name = $name[0];
     2125        }
    21282126    }
    21292127
    2130     $id = sanitize_title($name);
     2128    $id      = sanitize_title( $name );
    21312129    $options = array();
    2132     if ( !empty($classname) && is_string($classname) )
     2130    if ( ! empty( $classname ) && is_string( $classname ) ) {
    21332131        $options['classname'] = $classname;
    2134     $params = array_slice(func_get_args(), 2);
    2135     $args = array($id, $name, $output_callback, $options);
    2136     if ( !empty($params) )
    2137         $args = array_merge($args, $params);
    2138 
    2139     call_user_func_array('wp_register_sidebar_widget', $args);
     2132    }
     2133
     2134    wp_register_sidebar_widget( $id, $name, $output_callback, $options, ...$params );
    21402135}
    21412136
     
    21682163 * @see wp_register_widget_control()
    21692164 *
    2170  * @param int|string $name Sidebar ID.
    2171  * @param callable $control_callback Widget control callback to display and process form.
    2172  * @param int $width Widget width.
    2173  * @param int $height Widget height.
    2174  */
    2175 function register_widget_control($name, $control_callback, $width = '', $height = '') {
     2165 * @param int|string $name             Sidebar ID.
     2166 * @param callable   $control_callback Widget control callback to display and process form.
     2167 * @param int        $width            Widget width.
     2168 * @param int        $height           Widget height.
     2169 * @param mixed      ...$params        Widget parameters.
     2170 */
     2171function register_widget_control($name, $control_callback, $width = '', $height = '', ...$params) {
    21762172    _deprecated_function( __FUNCTION__, '2.8.0', 'wp_register_widget_control()' );
    21772173    // Compat
    2178     if ( is_array($name) ) {
    2179         if ( count($name) == 3 )
    2180             $name = sprintf($name[0], $name[2]);
    2181         else
     2174    if ( is_array( $name ) ) {
     2175        if ( count( $name ) === 3 ) {
     2176            $name = sprintf( $name[0], $name[2] );
     2177        } else {
    21822178            $name = $name[0];
     2179        }
    21832180    }
    21842181
    2185     $id = sanitize_title($name);
     2182    $id      = sanitize_title( $name );
    21862183    $options = array();
    2187     if ( !empty($width) )
     2184    if ( ! empty( $width ) ) {
    21882185        $options['width'] = $width;
    2189     if ( !empty($height) )
     2186    }
     2187    if ( ! empty( $height ) ) {
    21902188        $options['height'] = $height;
    2191     $params = array_slice(func_get_args(), 4);
    2192     $args = array($id, $name, $control_callback, $options);
    2193     if ( !empty($params) )
    2194         $args = array_merge($args, $params);
    2195 
    2196     call_user_func_array('wp_register_widget_control', $args);
     2189    }
     2190
     2191    wp_register_widget_control( $id, $name, $control_callback, $options, ...$params );
    21972192}
    21982193
Note: See TracChangeset for help on using the changeset viewer.