Site icon Hip-Hop Website Design and Development

Show content if user left comment / non-logged visitors?

im trying to hide content/show content based on comments.
i tried this to achieve what i want. but i want to hide/show content from non-logged visitors also..

this is my final code:

// the user may have commented on *any* post
define( 'CHECK_GLOBAL_FOR_COMMENTS', FALSE );
 
//
// some more code
//
add_shortcode( 'membervip', 'memberviparea' );
function memberviparea( $atts, $content = null ) {
 
    $post_id = 0;
 
    // if the user have to left a comment explicit on this post, get the post ID
    if( defined( 'CHECK_GLOBAL_FOR_COMMENTS' ) && FALSE === CHECK_GLOBAL_FOR_COMMENTS ) {
        global $post;
 
        $post_id = ( is_object( $post ) && isset( $post->ID ) ) ?
            $post->ID : 0;
    }
 
    if( is_user_logged_in() && user_has_left_comment( $post_id ) || current_user_can( 'administrator' ) )
        return '<p>' . $content . '</p>';
    elseif( !is_user_logged_in() && user_has_left_comment( $post_id ) || current_user_can( 'administrator' ) )
        return '<p>' . $content . '</p>';
    else
        return '<p>Bu alanı görmek için yorum yapmalısınız.</p>';
 
}
 
/**
 * Check if the user has left a comment
 *
 * If a post ID is set, the function checks if
 * the user has just left a comment in this post.
 * Otherwise it check if the user has left a comment on
 * any post.
 * If no user ID is set, the ID of the current logged in user is used.
 * If no user is currently logged in, the fuction returns null.
 *
 * @param int $post_id ID of the post (optional)
 * @param int $user_id User ID (required)
 * @return null|bool Null if no user is logged in and no user ID is set, else true if the user has left a comment, false if not
 */
function user_has_left_comment( $post_id = 0, $user_id = 0 ) {
 
    if( is_user_logged_in() && 0 === $user_id )
    return NULL;
    elseif( 0 === $user_id )
        $user_id = wp_get_current_user()->ID;
 
    $args = array( 'user_id' => $user_id );
 
    if ( 0 !== $post_id )
        $args['post_id'] = $post_id;
 
    $comments = get_comments( $args );
 
    return ! empty( $comments );
}


i think the problem start from that line:

if( !is_user_logged_in() && 0 === $user_id )
    return NULL;

if i change this;

is_user_logged_in()

-> it shows the content who is non-logged users
it i use it that way;

is_user_logged_in()

-> even non-logged visitors left comment, they can not see the content.

can you help me to figure it out please.