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-admin/includes/post.php

    r23094 r23416  
    150150function edit_post( $post_data = null ) {
    151151
    152     if ( empty($post_data) )
    153         $post_data = &$_POST;
     152    if ( empty( $post_data ) )
     153        $post_data = wp_unslash( $_POST );
    154154
    155155    // Clear out any data in internal vars.
     
    229229        if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
    230230            $image_alt = get_post_meta( $post_ID, '_wp_attachment_image_alt', true );
    231             if ( $image_alt != stripslashes( $post_data['_wp_attachment_image_alt'] ) ) {
    232                 $image_alt = wp_strip_all_tags( stripslashes( $post_data['_wp_attachment_image_alt'] ), true );
    233                 // update_meta expects slashed
    234                 update_post_meta( $post_ID, '_wp_attachment_image_alt', addslashes( $image_alt ) );
     231            if ( $image_alt != $post_data['_wp_attachment_image_alt'] ) {
     232                $image_alt = wp_strip_all_tags( $post_data['_wp_attachment_image_alt'], true );
     233                wp_update_post_meta( $post_ID, '_wp_attachment_image_alt', $image_alt );
    235234            }
    236235        }
     
    242241    add_meta( $post_ID );
    243242
    244     update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
     243    wp_update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    245244
    246245    wp_update_post( $post_data );
     
    423422    $post_title = '';
    424423    if ( !empty( $_REQUEST['post_title'] ) )
    425         $post_title = esc_html( stripslashes( $_REQUEST['post_title'] ));
     424        $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
    426425
    427426    $post_content = '';
    428427    if ( !empty( $_REQUEST['content'] ) )
    429         $post_content = esc_html( stripslashes( $_REQUEST['content'] ));
     428        $post_content = esc_html( wp_unslash( $_REQUEST['content'] ));
    430429
    431430    $post_excerpt = '';
    432431    if ( !empty( $_REQUEST['excerpt'] ) )
    433         $post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
     432        $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ));
    434433
    435434    if ( $create_in_db ) {
     
    480479    global $wpdb;
    481480
    482     $post_title = stripslashes( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    483     $post_content = stripslashes( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    484     $post_date = stripslashes( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
     481    $post_title = sanitize_post_field( 'post_title', $title, 0, 'db' );
     482    $post_content = sanitize_post_field( 'post_content', $content, 0, 'db' );
     483    $post_date = sanitize_post_field( 'post_date', $date, 0, 'db' );
    485484
    486485    $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
     
    560559
    561560    // Create the post.
    562     $post_ID = wp_insert_post( $_POST );
     561    $post_ID = wp_insert_post( wp_unslash( $_POST ) );
    563562    if ( is_wp_error( $post_ID ) )
    564563        return $post_ID;
     
    569568    add_meta( $post_ID );
    570569
    571     add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
     570    wp_add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    572571
    573572    // Now that we have an ID we can fix any attachment anchor hrefs
     
    613612    $post_ID = (int) $post_ID;
    614613
    615     $metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
    616     $metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
    617     $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
     614    $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
     615    $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
     616    $metavalue = isset($_POST['metavalue']) ? wp_unslash( trim( $_POST['metavalue'] ) ) : '';
    618617    if ( is_string( $metavalue ) )
    619618        $metavalue = trim( $metavalue );
     
    632631            return false;
    633632
    634         $metakey = esc_sql( $metakey );
    635 
    636         return add_post_meta( $post_ID, $metakey, $metavalue );
     633        return wp_add_post_meta( $post_ID, $metakey, $metavalue );
    637634    }
    638635
     
    707704 *
    708705 * @param unknown_type $meta_id
    709  * @param unknown_type $meta_key Expect Slashed
    710  * @param unknown_type $meta_value Expect Slashed
     706 * @param unknown_type $meta_key
     707 * @param unknown_type $meta_value
    711708 * @return unknown
    712709 */
    713710function update_meta( $meta_id, $meta_key, $meta_value ) {
    714     $meta_key = stripslashes( $meta_key );
    715     $meta_value = stripslashes_deep( $meta_value );
    716 
    717711    return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
    718712}
     
    768762    if ( $replace ) {
    769763        $post['post_content'] = $content;
    770         // Escape data pulled from DB.
    771         $post = add_magic_quotes($post);
    772764
    773765        return wp_update_post($post);
     
    11801172    $lock = "$now:$user_id";
    11811173
    1182     update_post_meta( $post->ID, '_edit_lock', $lock );
     1174    wp_update_post_meta( $post->ID, '_edit_lock', $lock );
    11831175    return array( $now, $user_id );
    11841176}
     
    12311223    // Only store one autosave. If there is already an autosave, overwrite it.
    12321224    if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
    1233         $new_autosave = _wp_post_revision_fields( $_POST, true );
     1225        $new_autosave = _wp_post_revision_fields( wp_unslash( $_POST ), true );
    12341226        $new_autosave['ID'] = $old_autosave->ID;
    12351227        $new_autosave['post_author'] = get_current_user_id();
     
    12381230
    12391231    // _wp_put_post_revision() expects unescaped.
    1240     $_POST = stripslashes_deep($_POST);
     1232    $_POST = wp_unslash( $_POST );
    12411233
    12421234    // Otherwise create the new autosave as a special post revision
Note: See TracChangeset for help on using the changeset viewer.