Make WordPress Core

Ticket #16943: 16943.patch

File 16943.patch, 5.8 KB (added by hakre, 14 years ago)

add_query_arg() + friends

  • wp-includes/formatting.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress-trunk
     
    12451245        if ( is_array($value) ) {
    12461246                $value = array_map('stripslashes_deep', $value);
    12471247        } elseif ( is_object($value) ) {
    1248                 $vars = get_object_vars( $value );
    1249                 foreach ($vars as $key=>$data) {
    1250                         $value->{$key} = stripslashes_deep( $data );
     1248                foreach ( $value as &$data ) {
     1249                        $data = stripslashes_deep( $data );
    12511250                }
    12521251        } else {
    12531252                $value = stripslashes($value);
     
    12681267 * @return array|string $value The encoded array (or string from the callback).
    12691268 */
    12701269function urlencode_deep($value) {
    1271         $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
     1270        is_array( $value ) ? array_walk_recursive( $value, 'urlencode_ref' ) : $value = urlencode($value);
    12721271        return $value;
    12731272}
    12741273
    12751274/**
     1275 * map urlencode() onto a reference
     1276 *
     1277 * @since 3.2
     1278 * @param string $value to be urlencoded
     1279 */
     1280function urlencode_ref( &$value ) {
     1281        $value = urlencode( $value );
     1282}
     1283
     1284/**
    12761285 * Converts email addresses characters to HTML entities to block spam bots.
    12771286 *
    12781287 * @since 0.71
  • wp-includes/functions.php

     
    13771377 * using this function. You can also retrieve the full URL with query data.
    13781378 *
    13791379 * Adding a single key & value or an associative array. Setting a key value to
    1380  * emptystring removes the key. Omitting oldquery_or_uri uses the $_SERVER
    1381  * value.
     1380 * an empty string removes the key. Omitting oldquery_or_uri uses the
     1381 * $_SERVER['REQUEST_URI'] value.
    13821382 *
    13831383 * @since 1.5.0
    13841384 *
    1385  * @param mixed $param1 Either newkey or an associative_array
    1386  * @param mixed $param2 Either newvalue or oldquery or uri
    1387  * @param mixed $param3 Optional. Old query or uri
     1385 * @param array|string $param1 Either newkey or an associative_array
     1386 * @param string $param2 Either newvalue or Optional. Old query or uri
     1387 * @param string $param3 Optional. Old query or uri
    13881388 * @return string New URL query string.
    13891389 */
    13901390function add_query_arg() {
    1391         $ret = '';
    1392         if ( is_array( func_get_arg(0) ) ) {
    1393                 if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
    1394                         $uri = $_SERVER['REQUEST_URI'];
    1395                 else
    1396                         $uri = @func_get_arg( 1 );
    1397         } else {
    1398                 if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
    1399                         $uri = $_SERVER['REQUEST_URI'];
    1400                 else
    1401                         $uri = @func_get_arg( 2 );
     1391        $args = func_get_args();
     1392        $args = array_merge( $args , array_fill( 0, 3, false )); # all unset parameter are false
     1393
     1394        // if first param is not an array, it forms together with the second parameter
     1395        // the key=value pair. Every other argument shifts one to front then.
     1396        if ( ! is_array( $args[0] ) ) {
     1397                $key = array_shift( $args );
     1398                $args[0] = array( $key => $args[0] );
    14021399        }
    14031400
    1404         if ( $frag = strstr( $uri, '#' ) )
    1405                 $uri = substr( $uri, 0, -strlen( $frag ) );
    1406         else
    1407                 $frag = '';
     1401        // the last argument is either query or uri, default is $_SERVER['REQUEST_URI']
     1402        $uri = false === $args[1] ? $_SERVER['REQUEST_URI'] : $args[1];
    14081403
    1409         if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
    1410                 $protocol = $matches[0];
    1411                 $uri = substr( $uri, strlen( $protocol ) );
    1412         } else {
    1413                 $protocol = '';
    1414         }
     1404        // pop fragment off, defaults to empty fragment
     1405        if ( $fragment = (string) strstr( $uri, '#' ) )
     1406                $uri = substr( $uri, 0, -strlen( $fragment ) );
    14151407
     1408        // shift scheme off, defaults to empty scheme
     1409        if ( $scheme = (string) preg_replace( '|^(https?://)?(.*)$|i', '$1', $uri ) )
     1410                $uri = substr( $uri, strlen( $scheme ) );
     1411
     1412        // extract base and query from query or uri
    14161413        if ( strpos( $uri, '?' ) !== false ) {
    1417                 $parts = explode( '?', $uri, 2 );
    1418                 if ( 1 == count( $parts ) ) {
    1419                         $base = '?';
    1420                         $query = $parts[0];
    1421                 } else {
    1422                         $base = $parts[0] . '?';
    1423                         $query = $parts[1];
    1424                 }
    1425         } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
     1414                list( $base, $query ) = explode( '?', $uri, 2 );
     1415                $base .= '?';
     1416        } elseif ( $scheme || strpos( $uri, '=' ) === false ) {
    14261417                $base = $uri . '?';
    14271418                $query = '';
    14281419        } else {
     
    14321423
    14331424        wp_parse_str( $query, $qs );
    14341425        $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
    1435         if ( is_array( func_get_arg( 0 ) ) ) {
    1436                 $kayvees = func_get_arg( 0 );
    1437                 $qs = array_merge( $qs, $kayvees );
    1438         } else {
    1439                 $qs[func_get_arg( 0 )] = func_get_arg( 1 );
    1440         }
     1426        $qs = array_merge( $qs, $args[0] );
    14411427
    1442         foreach ( (array) $qs as $k => $v ) {
    1443                 if ( $v === false )
    1444                         unset( $qs[$k] );
    1445         }
     1428        // remove all entries === false
     1429        if ( $del = array_keys( $qs, false, true ) )
     1430                $qs = array_diff_key( $qs, array_flip( $del ) );
    14461431
    14471432        $ret = build_query( $qs );
    14481433        $ret = trim( $ret, '?' );
    14491434        $ret = preg_replace( '#=(&|$)#', '$1', $ret );
    1450         $ret = $protocol . $base . $ret . $frag;
     1435        $ret = $scheme . $base . $ret . $fragment;
    14511436        $ret = rtrim( $ret, '?' );
    14521437        return $ret;
    14531438}
     
    14581443 * @since 1.5.0
    14591444 *
    14601445 * @param string|array $key Query key or keys to remove.
    1461  * @param bool $query When false uses the $_SERVER value.
     1446 * @param bool $query Optional. When false uses the $_SERVER value.
    14621447 * @return string New URL query string.
    14631448 */
    1464 function remove_query_arg( $key, $query=false ) {
    1465         if ( is_array( $key ) ) { // removing multiple keys
    1466                 foreach ( $key as $k )
    1467                         $query = add_query_arg( $k, false, $query );
    1468                 return $query;
    1469         }
    1470         return add_query_arg( $key, false, $query );
     1449function remove_query_arg( $key, $query = false ) {
     1450        $key = (array) $key;
     1451        if ( $count = count( $key ) )
     1452                $key = array_combine( $key, array_fill( 0, $count, false ) );
     1453        return add_query_arg( $key, $query );
    14711454}
    14721455
    14731456/**