Make WordPress Core


Ignore:
Timestamp:
04/20/2015 05:45:32 AM (9 years ago)
Author:
pento
Message:

Clean up some edge cases in sanitize_sql_orderby(). Merge of [32164] to the 4.1 branch.

Props vortfu, dd32.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1/src/wp-includes/formatting.php

    r30766 r32165  
    13101310
    13111311/**
    1312  * Ensures a string is a valid SQL order by clause.
    1313  *
    1314  * Accepts one or more columns, with or without ASC/DESC, and also accepts
    1315  * RAND().
     1312 * Ensures a string is a valid SQL 'order by' clause.
     1313 *
     1314 * Accepts one or more columns, with or without a sort order (ASC / DESC).
     1315 * e.g. 'column_1', 'column_1, column_2', 'column_1 ASC, column_2 DESC' etc.
     1316 *
     1317 * Also accepts 'RAND()'.
    13161318 *
    13171319 * @since 2.5.1
    13181320 *
    1319  * @param string $orderby Order by string to be checked.
    1320  * @return false|string Returns the order by clause if it is a match, false otherwise.
    1321  */
    1322 function sanitize_sql_orderby( $orderby ){
    1323     preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
    1324     if ( !$obmatches )
    1325         return false;
    1326     return $orderby;
     1321 * @param string $orderby Order by clause to be validated.
     1322 * @return string|bool Returns $orderby if valid, false otherwise.
     1323 */
     1324function sanitize_sql_orderby( $orderby ) {
     1325    if ( preg_match( '/^\s*(([a-z0-9_]+|`[a-z0-9_]+`)(\s+(ASC|DESC))?\s*(,\s*(?=[a-z0-9_`])|$))+$/i', $orderby ) || preg_match( '/^\s*RAND\(\s*\)\s*$/i', $orderby ) ) {
     1326        return $orderby;
     1327    }
     1328    return false;
    13271329}
    13281330
Note: See TracChangeset for help on using the changeset viewer.