q2a / question2answer

Question2Answer is a free and open source platform for Q&A sites, running on PHP/MySQL.
http://www.question2answer.org/
GNU General Public License v3.0
1.63k stars 629 forks source link

Check the handle and return at the very beginning if it is null in #739

Closed q2amarket closed 5 years ago

q2amarket commented 5 years ago

The issue I came up when was working on one plugin to render handle html. The function was returning Anonymous when the handle is NULL

svivian commented 5 years ago

What should it be displaying instead? If the username is NULL that means it is an anonymous user (or one that was deleted).

q2amarket commented 5 years ago

What should it be displaying instead? If the username is NULL that means it is an anonymous user (or one that was deleted).

Well, within the core it may make sense as it is calling only when the users is exists or not. However, in some cases it returns Anonymous when the data returns null as a handle value. For instance, I am using this $handles = qa_userids_to_handles($user_ids); and using qa_get_one_user_html($handle) in foreach loop returning Anonymous if array is null.

For now I have unset the value in my own code but it would be better to check within the core function.

svivian commented 5 years ago

Where are you getting the $user_ids from? Surely wherever you get those from should be fetching valid user IDs only. If that's not possible, then you can check in your foreach loop rather than unset, for example:

$handles = qa_userids_to_handles($user_ids);
foreach ($handles as $handle) {
    if ($handle === null) {
        $html = ''; // or whatever you need to do if null
    } else {
        $html = qa_get_one_user_html($handle);
    }
}

I think qa_get_one_user_html is working as intended, changing it would break a bunch of other code that relies on displaying "anonymous".

q2amarket commented 5 years ago

Okay let me try once again to check as you mentioned. Actually, I have a bit different scenario and it is creating an issue. I have to unset the element.

Let me have a look and get back to you.