Make WordPress Core

Changeset 45630


Ignore:
Timestamp:
07/12/2019 12:16:34 AM (5 years ago)
Author:
pento
Message:

Code Modernisation: Introduce the spread operator in wpdb::prepare().

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.

Props jrf.
See #47678.

File:
1 edited

Legend:

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

    r45611 r45630  
    12681268     *   %s (string)
    12691269     *
    1270      * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder.
    1271      *
    1272      * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes
    1273      * added by this function, so should be passed with appropriate quotes around them for your usage.
     1270     * All placeholders MUST be left unquoted in the query string. A corresponding argument
     1271     * MUST be passed for each placeholder.
     1272     *
     1273     * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s)
     1274     * will not have quotes added by this function, so should be passed with appropriate quotes around
     1275     * them for your usage.
    12741276     *
    12751277     * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example,
     
    12771279     * cannot be inserted directly in the query string. Also see wpdb::esc_like().
    12781280     *
    1279      * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination
    1280      * of the two is not supported.
     1281     * Arguments may be passed as individual arguments to the method, or as a single array containing
     1282     * all arguments. A combination of the two is not supported.
    12811283     *
    12821284     * Examples:
     
    12881290     *
    12891291     * @param string      $query   Query statement with sprintf()-like placeholders
    1290      * @param array|mixed $args    The array of variables to substitute into the query's placeholders if being called with an array of arguments,
    1291      *                             or the first variable to substitute into the query's placeholders if being called with individual arguments.
    1292      * @param mixed       ...$args further variables to substitute into the query's placeholders if being called wih individual arguments.
     1292     * @param array|mixed $args    The array of variables to substitute into the query's placeholders
     1293     *                             if being called with an array of arguments, or the first variable
     1294     *                             to substitute into the query's placeholders if being called with
     1295     *                             individual arguments.
     1296     * @param mixed       ...$args Further variables to substitute into the query's placeholders
     1297     *                             if being called with individual arguments.
    12931298     * @return string|void Sanitized query string, if there is a query to prepare.
    12941299     */
    1295     public function prepare( $query, $args ) {
     1300    public function prepare( $query, ...$args ) {
    12961301        if ( is_null( $query ) ) {
    12971302            return;
     
    13031308            _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );
    13041309        }
    1305 
    1306         $args = func_get_args();
    1307         array_shift( $args );
    13081310
    13091311        // If args were passed as an array (as in vsprintf), move them up.
Note: See TracChangeset for help on using the changeset viewer.