Make WordPress Core


Ignore:
Timestamp:
02/14/2013 10:51:06 PM (12 years ago)
Author:
ryan
Message:

Change all core API to expect unslashed rather than slashed arguments.

The exceptions to this are update_post_meta() and add_post_meta() which are often used by plugins in POST handlers and will continue accepting slashed data for now.

Introduce wp_upate_post_meta() and wp_add_post_meta() as unslashed alternatives to update_post_meta() and add_post_meta(). These functions could become methods in WP_Post so don't use them too heavily yet.

Remove all escape() calls from wp_xmlrpc_server. Now that core expects unslashed data this is no longer needed.

Remove addslashes(), addslashes_gpc(), add_magic_quotes() calls on data being prepared for handoff to core functions that until now expected slashed data. Adding slashes in no longer necessary.

Introduce wp_unslash() and use to it remove slashes from GPCS data before using it in core API. Almost every instance of stripslashes() in core should now be wp_unslash(). In the future (a release or three) when GPCS is no longer slashed, wp_unslash() will stop stripping slashes and simply return what is passed. At this point wp_unslash() calls can be removed from core.

Introduce wp_slash() for slashing GPCS data. This will also turn into a noop once GPCS is no longer slashed. wp_slash() should almost never be used. It is mainly of use in unit tests.

Plugins should use wp_unslash() on data being passed to core API.

Plugins should no longer slash data being passed to core. So when you get_post() and then wp_insert_post() the post data from get_post() no longer needs addslashes(). Most plugins were not bothering with this. They will magically start doing the right thing. Unfortunately, those few souls who did it properly will now have to avoid calling addslashes() for 3.6 and newer.

Use wp_kses_post() and wp_kses_data(), which expect unslashed data, instead of wp_filter_post_kses() and wp_filter_kses(), which expect slashed data. Filters are no longer passed slashed data.

Remove many no longer necessary calls to $wpdb->escape() and esc_sql().

In wp_get_referer() and wp_get_original_referer(), return unslashed data.

Remove old stripslashes() calls from WP_Widget::update() handlers. These haven't been necessary since WP_Widget.

Switch several queries over to prepare().

Expect something to break.

Props alexkingorg
see #21767

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r23368 r23416  
    17171717 */
    17181718function wp_rel_nofollow( $text ) {
    1719     // This is a pre save filter, so text is already escaped.
    1720     $text = stripslashes($text);
    17211719    $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
    1722     $text = esc_sql($text);
    17231720    return $text;
    17241721}
     
    33433340    return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
    33443341}
     3342
     3343/**
     3344 * Conditionally add slashes to a string or array of strings. When GPCS
     3345 * slashing is turned on, slashes are added. When GPCS slashing is turned off,
     3346 * slashes are not added.
     3347 *
     3348 * This should be used when preparing data for core API that deal directly with GPCS data.
     3349 * Outside of unit tests, this should be rare. At a future date GPCS will no longer
     3350 * be slashed and this function will noop. Do not use it in situations where adding slashes
     3351 * is always required regardless of whether GPCS is slashed.
     3352 *
     3353 * @since 3.6.0
     3354 *
     3355 * @param string|array $value String or array of strings to slash.
     3356 * @return string|array Slashed $value
     3357 */
     3358function wp_slash( $value ) {
     3359    if ( is_array( $value ) ) {
     3360        foreach ( $value as $k => $v ) {
     3361            if ( is_array( $v ) ) {
     3362                $value[$k] = wp_slash( $v );
     3363            } else {
     3364                $value[$k] = addslashes( $v );
     3365            }
     3366        }
     3367    } else {
     3368        $value = addslashes( $value );
     3369    }
     3370
     3371    return $value;
     3372}
     3373
     3374/**
     3375 * Conditionally removes slashes from a string or array of strings. When GPCS
     3376 * slashing is turned on, slashes are stripped. When GPCS slashing is turned off,
     3377 * slashes are not stripped.
     3378 *
     3379 * This should be used for GPCS data before passing it along to core API. At a future
     3380 * date GPCS will no longer be slashed and this function will noop. Do not use it
     3381 * in situations where slash stripping is always required regardless of whether GPCS
     3382 * is slashed.
     3383 *
     3384 * @since 3.6.0
     3385 *
     3386 * @param string|array $value String or array of strings to unslash.
     3387 * @return string|array Unslashed $value
     3388 */
     3389function wp_unslash( $value ) {
     3390    return stripslashes_deep( $value );
     3391}
Note: See TracChangeset for help on using the changeset viewer.