Disable comments on assignments post type

// Example filter to disable comments on LearnDash Assignment
add_filter( 'comments_open', function( $open, $post_id ) {

	// Check that $post_id is not empty. This filter is called sometimes with empty $post_id
	if ( !empty( $post_id ) ) {
		
		// Get the post from $post_id and check that it is valid WP_Post and an Assignment
		$post = get_post( $post_id );
		if ( ( $post ) && ( $post instanceof WP_Post ) && ( $post->post_type == 'sfwd-assignment' ) ) {

			// Now check the user capability. Here we are checking if the current user is 
			// administrator. But can be checked in for other atrributes. 
			if ( !current_user_can( 'administrator' ) ) {

				// If the user is not an admin we set the open to false.
				$open = false;
			}
		}
	}
	
	// Always return $open	
	return $open;
}, 10, 2);