WordPress.org

Make WordPress Core

Ticket #47678: 47678-wpdb-prepare.patch

File 47678-wpdb-prepare.patch, 3.8 KB (added by jrf, 11 months ago)

Simplify & modernize wpdb::prepare(). Includes reformatting the documentation for line length/readibility.

  • src/wp-includes/wp-db.php

    From e78ca508542b707ea28809845b3782366864d3d2 Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Thu, 11 Jul 2019 12:36:35 +0200
    Subject: [PATCH] Simplify & modernize wpdb::prepare()
    
    Includes reformatting the documentation for line length/readibility.
    ---
     src/wp-includes/wp-db.php | 26 ++++++++++++++------------
     1 file changed, 14 insertions(+), 12 deletions(-)
    
    diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php
    index 3c41943e55..46851b14fb 100644
    a b class wpdb { 
    12671267         *   %f (float)
    12681268         *   %s (string)
    12691269         *
    1270          * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder.
     1270         * All placeholders MUST be left unquoted in the query string. A corresponding argument
     1271         * MUST be passed for each placeholder.
    12711272         *
    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.
     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,
    12761278         * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these
    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:
    12831285         *     $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) );
    class wpdb { 
    12871289         * @since 2.3.0
    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;
    12981303                }
    class wpdb { 
    13031308                        _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );
    13041309                }
    13051310
    1306                 $args = func_get_args();
    1307                 array_shift( $args );
    1308 
    13091311                // If args were passed as an array (as in vsprintf), move them up.
    13101312                $passed_as_array = false;
    13111313                if ( is_array( $args[0] ) && count( $args ) == 1 ) {