WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 9 months ago

#41788 new defect (bug)

PHP Fatal error: Call to a member function has_cap() on a non-object in wp-includes/comment.php

Reported by: kirbyde Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8.1
Component: Comments Keywords: reporter-feedback
Focuses: Cc:

Description

if ( ! empty( $commentdata['user_id'] ) ) {
        $user = get_userdata( $commentdata['user_id'] );
        $post_author = $wpdb->get_var( $wpdb->prepare(
                "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
                $commentdata['comment_post_ID']
        ) );
}

if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {

If get_userdata() returns false, this code can lead to a fatal error.

Change History (2)

#1 @johnbillion
4 years ago

  • Keywords reporter-feedback added

Thanks for your report, @kirbyde, and welcome to WordPress Trac.

This is from the wp_allow_comment() function. Can you let us know under what condition this situation can occur? Do you have a plugin or theme on your site which customises the way comments are submitted?

#2 @hellofromTonya
9 months ago

The code could be further guarded by switching the isset( $user ) to ! empty( $user ), which would then check if isset and not false.

That said, I'm not seeing other reports of this issue.

Note: See TracTickets for help on using tickets.