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.

Built from https://develop.svn.wordpress.org/trunk@45630


git-svn-id: http://core.svn.wordpress.org/trunk@45441 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2019-07-12 00:17:57 +00:00
parent 2fe355cdd0
commit 4d3a84aa9f
2 changed files with 15 additions and 13 deletions

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.3-alpha-45629'; $wp_version = '5.3-alpha-45630';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -1267,17 +1267,19 @@ class wpdb {
* %f (float) * %f (float)
* %s (string) * %s (string)
* *
* All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder. * All placeholders MUST be left unquoted in the query string. A corresponding argument
* MUST be passed for each placeholder.
* *
* For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s)
* added by this function, so should be passed with appropriate quotes around them for your usage. * will not have quotes added by this function, so should be passed with appropriate quotes around
* them for your usage.
* *
* Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example,
* to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these
* cannot be inserted directly in the query string. Also see wpdb::esc_like(). * cannot be inserted directly in the query string. Also see wpdb::esc_like().
* *
* Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination * Arguments may be passed as individual arguments to the method, or as a single array containing
* of the two is not supported. * all arguments. A combination of the two is not supported.
* *
* Examples: * Examples:
* $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) );
@ -1287,12 +1289,15 @@ class wpdb {
* @since 2.3.0 * @since 2.3.0
* *
* @param string $query Query statement with sprintf()-like placeholders * @param string $query Query statement with sprintf()-like placeholders
* @param array|mixed $args The array of variables to substitute into the query's placeholders if being called with an array of arguments, * @param array|mixed $args The array of variables to substitute into the query's placeholders
* or the first variable to substitute into the query's placeholders if being called with individual arguments. * if being called with an array of arguments, or the first variable
* @param mixed ...$args further variables to substitute into the query's placeholders if being called wih individual arguments. * to substitute into the query's placeholders if being called with
* individual arguments.
* @param mixed ...$args Further variables to substitute into the query's placeholders
* if being called with individual arguments.
* @return string|void Sanitized query string, if there is a query to prepare. * @return string|void Sanitized query string, if there is a query to prepare.
*/ */
public function prepare( $query, $args ) { public function prepare( $query, ...$args ) {
if ( is_null( $query ) ) { if ( is_null( $query ) ) {
return; return;
} }
@ -1303,9 +1308,6 @@ class wpdb {
_doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' ); _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );
} }
$args = func_get_args();
array_shift( $args );
// If args were passed as an array (as in vsprintf), move them up. // If args were passed as an array (as in vsprintf), move them up.
$passed_as_array = false; $passed_as_array = false;
if ( is_array( $args[0] ) && count( $args ) == 1 ) { if ( is_array( $args[0] ) && count( $args ) == 1 ) {