public static function is_user_non_contact_blocked($recipient, $sender = null) {
        debugging('\core_message\api::is_user_non_contact_blocked() is deprecated', DEBUG_DEVELOPER);

        global $USER, $CFG;

        if (is_null($sender)) {
            // The message is from the logged in user, unless otherwise specified.
            $sender = $USER;
        }

        $privacypreference = self::get_user_privacy_messaging_preference($recipient->id);
        switch ($privacypreference) {
            case self::MESSAGE_PRIVACY_SITE:
                if (!empty($CFG->messagingallusers)) {
                    // Users can be messaged without being contacts or members of the same course.
                    break;
                }
                // When the $CFG->messagingallusers privacy setting is disabled, continue with the next
                // case, because MESSAGE_PRIVACY_SITE is replaced to MESSAGE_PRIVACY_COURSEMEMBER.
            case self::MESSAGE_PRIVACY_COURSEMEMBER:
                // Confirm the sender and the recipient are both members of the same course.
                if (enrol_sharing_course($recipient, $sender)) {
                    // All good, the recipient and the sender are members of the same course.
                    return false;
                }
            case self::MESSAGE_PRIVACY_ONLYCONTACTS:
                // True if they aren't contacts (they can't send a message because of the privacy settings), false otherwise.
                return !self::is_contact($sender->id, $recipient->id);
        }

        return false;
    }