Uncaught Error Call to a member function has_cap() on bool

Solved3.49K viewsIssues
0

Hi,
I received a PHP error when viewing an answer, and not logged in, and option in ‘User Access Control’ for ‘Who can read comments’ set to ‘ap_read_comment capability’.
The error was
Fatal error: Uncaught Error: Call to a member function has_cap() on bool in /usr/home/wordpress/practicecoach/htdocs/wp-content/plugins/anspress-question-answer/includes/class/roles-cap.php:1515
The solution was to add is_user_logged_in() to line 1515, so it becomes :

if( 'have_cap' === $option && is_user_logged_in() && get_user_by( 'ID', $user_id )->has_cap( 'ap_read_comment' ) ) {
     return true;
 }


instead of

if( 'have_cap' === $option && get_user_by( 'ID', $user_id )->has_cap( 'ap_read_comment' ) ) {
     return true;
 }

Someone may have a better solution, or I may be mistaken.

Question is closed for new answers.
Selected answer as best
0

Hello
Thanks for reporting the issue. Your solution seems completely legit. I will run few checks before adding your suggestion.
BTW, you can send a PR if you have a github account. You can find our repo here: github.com/anspress/anspress
Thanks

Answered question