Skip to content

PHP Warning in notify_logout method #24

@hidetaka-dc

Description

@hidetaka-dc

Description:

PHP Warning occurs when attempting to read property "user_login" on a boolean value in the Shifter WordPress MU plugin.

Error Details:

PHP Warning: Attempt to read property "user_login" on bool in /var/www/html/web/wp/mu-plugins/shifter-wp-mu/api/class-shifter-api.php on line 172  

Root Cause:
The issue occurs in the notify_logout method where get_user_by('ID', $user_id) returns false when an invalid user ID is passed, but the code attempts to access the user_login property without checking if the user object is valid.

Steps to Reproduce:

Trigger a WordPress logout event with an invalid or non-existent user ID
The wp_logout hook calls the notify_logout method
get_user_by() returns false for invalid user IDs
Code attempts to access $user->user_login on the boolean false value
Expected Behavior:
The method should handle cases where get_user_by() returns false gracefully without throwing PHP warnings.

Suggested Fix:

Add validation to check if $user is a valid user object before accessing its properties:

public function notify_logout( $user_id ) {  
    $user = get_user_by( 'ID', $user_id );  
    if ( $user && isset( $user->user_login ) ) {  
        $result = $this->call_update_active_user( false, $user->user_login );  
    }  
}

Environment:

Plugin: Shifter WordPress MU shifter.php:15-18
File: api/class-shifter-api.php
Line: 172 (in the notify_logout method)

Notes

This error typically occurs during user session management when invalid user IDs are passed to the logout notification system. The Shifter API's update_active_user functionality tracks active users on WordPress sites and notifies the Shifter platform of login/logout events.

Wiki pages you might want to explore:

Development Workflow (getshifter/shifter-wp-mu)
Coding Standards (getshifter/shifter-wp-mu)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions