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 { |
| 1267 | 1267 | * %f (float) |
| 1268 | 1268 | * %s (string) |
| 1269 | 1269 | * |
| 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. |
| 1271 | 1272 | * |
| 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. |
| 1274 | 1276 | * |
| 1275 | 1277 | * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, |
| 1276 | 1278 | * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these |
| 1277 | 1279 | * cannot be inserted directly in the query string. Also see wpdb::esc_like(). |
| 1278 | 1280 | * |
| 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. |
| 1281 | 1283 | * |
| 1282 | 1284 | * Examples: |
| 1283 | 1285 | * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); |
| … |
… |
class wpdb { |
| 1287 | 1289 | * @since 2.3.0 |
| 1288 | 1290 | * |
| 1289 | 1291 | * @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. |
| 1293 | 1298 | * @return string|void Sanitized query string, if there is a query to prepare. |
| 1294 | 1299 | */ |
| 1295 | | public function prepare( $query, $args ) { |
| | 1300 | public function prepare( $query, ...$args ) { |
| 1296 | 1301 | if ( is_null( $query ) ) { |
| 1297 | 1302 | return; |
| 1298 | 1303 | } |
| … |
… |
class wpdb { |
| 1303 | 1308 | _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' ); |
| 1304 | 1309 | } |
| 1305 | 1310 | |
| 1306 | | $args = func_get_args(); |
| 1307 | | array_shift( $args ); |
| 1308 | | |
| 1309 | 1311 | // If args were passed as an array (as in vsprintf), move them up. |
| 1310 | 1312 | $passed_as_array = false; |
| 1311 | 1313 | if ( is_array( $args[0] ) && count( $args ) == 1 ) { |