### Eclipse Workspace Patch 1.0
#P wordpress-trunk
|
|
|
530 | 530 | } |
531 | 531 | |
532 | 532 | /** |
533 | | * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. |
| 533 | * Mimicks a prepare of a SQL query for execution. |
| 534 | * |
| 535 | * The following placeholders can be used for values: |
| 536 | * |
| 537 | * %d (decimal number) |
| 538 | * %s (string) |
| 539 | * %% (%) |
| 540 | * |
| 541 | * Both %d and %s should be left unquoted in the query string. |
534 | 542 | * |
| 543 | * <code> |
| 544 | * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) |
| 545 | * </code> |
| 546 | * |
| 547 | * NOTE: This has nothing to do with prepared statements your database might support. |
| 548 | * |
| 549 | * More technical information: |
| 550 | * |
| 551 | * Uses sprintf()-like syntax. |
| 552 | * |
535 | 553 | * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). |
536 | 554 | * Does not support sign, padding, alignment, width or precision specifiers. |
537 | 555 | * Does not support argument numbering/swapping. |
538 | 556 | * |
539 | 557 | * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. |
540 | 558 | * |
541 | | * Both %d and %s should be left unquoted in the query string. |
542 | | * |
543 | | * <code> |
544 | | * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) |
545 | | * </code> |
546 | | * |
547 | 559 | * @link http://php.net/sprintf Description of syntax. |
548 | 560 | * @since 2.3.0 |
549 | 561 | * |
550 | | * @param string $query Query statement with sprintf()-like placeholders |
551 | | * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. |
552 | | * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. |
| 562 | * @param string $query Query statement with wpdb->prepare placeholders |
| 563 | * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called compareable to {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. |
| 564 | * @param mixed $args,... further variables to substitute. |
553 | 565 | * @return null|string Sanitized query string |
554 | 566 | */ |
555 | 567 | function prepare($query = null) { // ( $query, *$args ) |
… |
… |
|
560 | 572 | // If args were passed as an array (as in vsprintf), move them up |
561 | 573 | if ( isset($args[0]) && is_array($args[0]) ) |
562 | 574 | $args = $args[0]; |
563 | | $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it |
564 | | $query = str_replace('"%s"', '%s', $query); // doublequote unquoting |
565 | | $query = str_replace('%s', "'%s'", $query); // quote the strings |
| 575 | $query = preg_replace('|(?<!%)%s|', "'%s'", $query); //quote the strings, Avoiding escaped strings |
566 | 576 | array_walk($args, array(&$this, 'escape_by_ref')); |
567 | 577 | return @vsprintf($query, $args); |
568 | 578 | } |