[wp-trac] [WordPress Trac] #56662: WP_REST_Users_Controller::update_item(): 'rest_user_invalid_id ' error will never be thrown as `$user` will never be falsey.

WordPress Trac noreply at wordpress.org
Mon Sep 26 22:36:49 UTC 2022


#56662: WP_REST_Users_Controller::update_item(): 'rest_user_invalid_id ' error will
never be thrown as `$user` will never be falsey.
--------------------------+------------------------------
 Reporter:  jrf           |       Owner:  hellofromTonya
     Type:  defect (bug)  |      Status:  accepted
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Users         |     Version:  4.7
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------
Changes (by hellofromTonya):

 * owner:  (none) => hellofromTonya
 * status:  new => accepted


Comment:

 Additional contextual information:

 * The conditional check was introduced in [38832]:
 {{{#!php
 $user = get_userdata( $id );
 if ( ! $user ) {
         return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource
 id.' ), array( 'status' => 404 ) );
 }
 }}}
 Notice that it was checking what's returned from `get_userdata()` which
 returns `WP_User` on success or `false` on failure.

 * Changeset [39954] added the `WP_REST_Users_Controller::get_user()`
 method (which returns `WP_Error` on failure and throws the
 'rest_user_invalid_id' error) and a `is_wp_error()` conditional check. But
 it retained the original error code from [38832].

 Looking at this history, changeset [33954] moved error code logic and
 ensured a valid `WP_User` is returned.

 I agree with @jrf that this code in
 `WP_REST_Users_Controller::update_item()` can safely be removed, as the
 conditional will never be falsey and the error will never be thrown.
 {{{#!php
 if ( ! $user ) {
         return new WP_Error(
                 'rest_user_invalid_id',
                 __( 'Invalid user ID.' ),
                 array( 'status' => 404 )
         );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56662#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list