Make WordPress Core


Ignore:
Timestamp:
12/02/2016 06:58:36 AM (8 years ago)
Author:
pento
Message:

REST API: Require the reassign parameter when deleting users.

When deleting a user through the WordPress admin, a specific decision is presented - whether to assign all of the user's posts to another user, or to delete all of the posts.

This change requires reassign as a parameter in the corresponding REST API endpoint, so that content isn't accidentally lost.

Merges [39426] to the 4.7 branch.

Props jeremyfelt.
Fixes #39000.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39401 r39427  
    9393                        'type'        => 'integer',
    9494                        'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ),
     95                        'required'    => true,
     96                        'sanitize_callback' => array( $this, 'check_reassign' ),
    9597                    ),
    9698                ),
     
    126128                        'type'        => 'integer',
    127129                        'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ),
     130                        'required'    => true,
     131                        'sanitize_callback' => array( $this, 'check_reassign' ),
    128132                    ),
    129133                ),
     
    131135            'schema' => array( $this, 'get_public_item_schema' ),
    132136        ));
     137    }
     138
     139    /**
     140     * Checks for a valid value for the reassign parameter when deleting users.
     141     *
     142     * The value can be an integer, 'false', false, or ''.
     143     *
     144     * @since 4.7.0
     145     *
     146     * @param int|bool        $value   The value passed to the reassign parameter.
     147     * @param WP_REST_Request $request Full details about the request.
     148     * @param string          $param   The parameter that is being sanitized.
     149     *
     150     * @return int|bool|WP_Error
     151     */
     152    public function check_reassign( $value, $request, $param ) {
     153        if ( is_numeric( $value ) ) {
     154            return $value;
     155        }
     156
     157        if ( empty( $value ) || false === $value || 'false' === $value ) {
     158            return false;
     159        }
     160
     161        return new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
    133162    }
    134163
     
    674703    public function delete_item( $request ) {
    675704        $id       = (int) $request['id'];
    676         $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null;
     705        $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] );
    677706        $force    = isset( $request['force'] ) ? (bool) $request['force'] : false;
    678707
Note: See TracChangeset for help on using the changeset viewer.