[wp-hackers] small wp-db.php code question

Dion Hulse (dd32) wordpress at dd32.id.au
Tue Sep 18 08:04:44 UTC 2012


As the comment above it explains, no, it's not a bug.

The first element is poped off as it's the $query variable passed in, thats
not needed.

The then first element is expanded if it contains the data, Eg, the
following 2 lines are identical in result:
$wpdb->prepare( "...", 'A String', 42 );
$wpdb->prepare( "...", array( 'A String', 42 ) );

As a result of this, the following is not a valid way to call prepare:
$wpdb->prepare( "...", array( 'A String', 42 ), "another variable" );

On 17 September 2012 23:56, Simon Courtenage <courtenage at gmail.com> wrote:

> Dear wp-hackers,
>
> I'm going through the WP code line-by-line as part of a new project and
> have come across the code for the prepare function in wp-db.php.  This
> function
> can take a variable number of arguments (up to 3 according to the comments
> and usages).  However, I think there is an issue with the 3rd argument,
> when present, being over-written by the code.  The prepare() code is
>
> //////
> function prepare( $query = null ) { // ( $query, *$args )
>         if ( is_null( $query ) )
>             return;
>
>         $args = func_get_args();
>         array_shift( $args );
>         // If args were passed as an array (as in vsprintf), move them up
>         if ( isset( $args[0] ) && is_array($args[0]) )
>             $args = $args[0]; // QUESTION
>         $query = str_replace( "'%s'", '%s', $query ); // in case someone
> mistakenly already singlequoted it
>         $query = str_replace( '"%s"', '%s', $query ); // doublequote
> unquoting
>         $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the
> strings, avoiding escaped strings like %%s
>         array_walk( $args, array( &$this, 'escape_by_ref' ) );
>         return @vsprintf( $query, $args );
>     }
> //////
>
> The issue lies with the line I've commented with 'QUESTION'.  This
> overwrites the arg array obtained from func_get_args() with the second
> argument (after the first was popped from the array) - hence the 3rd arg is
> lost.  Have I missed something / is this intended behaviour / is this a
> bug?
>
> Thanks for reading my first post to this list!
>
> Regards
>
> Simon Courtenage
>
> --
>
>
> Join me on msgmash <http://www.msgmash.com>!
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list