Memberpress:如何在注册时将用户添加到组

Hi Eva, I went over your code and there’s no errors that I can spot - and I totally get your frustration. If it hadn’t been for Simon Cossar I don’t know how I’d have resolved it myself.

So, taking it backwards: if the PHP is correct, then something else must be disconnected. (browniepoints for me, yay.)

But…
I don’t know enough about any of that to be able to give suggestions or help.
If you’ve got your forum parked with the boys from Discourse Hosting, you could ask Richard (his name tag won’t pop, but this is his profile: Profile - RGJ - Discourse Meta )

Or perhaps @simon might be able to provide you with the missing steps.

Keeping my fingers crossed for you.

edit:
p.s. these are all the related plug-ins i use (+ that PHP one)
(Memberpress Drip is email marketing, so not related to this)

At least the code is okay…

Just going to ask some easy questions just to be sure it’s not a stupid mistake :

On discourse :

  • Are your groups hidden/closed/opened ?
  • Is your group full name different from the name and default title ? Which one is considered by the PHP code ?
  • Have you done anything special in the parameters section concerning this ?

On wordpress :

  • Does your WP-Discourse plugin have specific settings/webhooks for this ?
  • Do you validate the user’s email when they register on wordpress or you use Discourse’s email validation ?

I’m not sure the issue is there but I always take a shot, thank you again !

On Discourse:

  • groups are any and all: closed and opened (i don’t give access to hidden groups, come to think of it)
  • these are some of the group names as displayed in ‘Groups’ - and as you can see, the “NoSpacesNames” are the ones that coincide with the PHP settings
    26
    54
  • If by parameters you mean the following, then no:
    22

nor in the actual group settings (where you create it, with name, owner, etc.), it’s all straight forward and not connected to accessing through SSO - so setting or not setting this as a person’s primary group is not relavant.

There is one webhook here on Discourse that I put in as instructed by the WP-Discourse plugin:


but I don’t actually have that box checked…

  • email verification technically goes via memberpress: they (my wp-instance) send the log-in details to the user. by clicking the log-in link the users verify their address.
    i know for a fact that it’s not through Discourse, because for a while it ALSO went through discourse (glitch somewhere) and Richard sorted that for me.

you’re welcome!

使用这种方法,是否可以在用户付费注册 Discourse 时将其信任等级设置为 2 级,并在其停止按月付费时将其降至 1 级?

我有一段时间没查看过这个了,但你需要的是你的 Discourse 代码与支付代码(在我的案例中是 MemberPress)之间的通信。

因此,如果某人停止付款,MemberPress 会更改他们的代码,而该代码对应不同的群组等级。
如果没有与支付建立关联,群组等级就不会调整。

哦,等等。
重读你的问题:我不太确定信任等级(trust levels)是如何运作的……
不过……如果其他方法都行不通,你可以轻松创建一个设置为信任等级 2 的群组,再创建一个设置为信任等级 1 的群组,然后让你的会员插件为会员分配这些群组。

希望这能帮到你。如果不行,我可能需要再深入研究一下。自从上次对话后,系统一直正常运行,我之后就没再查看过。

这个主意真棒!是的,我们可以将信任级别设置为 Discourse 群组。但我对 Memberpress 还不够熟悉。

我们能否让在 Memberpress 中付费的用户自动加入一个特殊群组?

今天在 Meta 的另一篇帖子中提到了这个问题,所以我查看了代码,以确认是否可以通过将用户添加到设置比用户当前组更低的信任等级的组来降低用户的信任等级。从代码来看,我发现这是不可能的:

  def grant
    if @user.trust_level < @trust_level
      @user.change_trust_level!(@trust_level)
      @user.save!
    end
  end

如果用户的信任等级低于组所授予的信任等级,将用户添加到该组将会更新其信任等级。否则,用户的信任等级不会发生变化。

可能还有其他会员插件也能为你实现同样的功能。

这边已经是晚上 8 点了,我今天先下班了。但如果你还需要更多帮助,我很乐意在早上继续查看。请随时告诉我。

我有点生疏了(而且时间也很晚了),但信任等级其实并不重要。我根本不使用信任等级。TL 2 组可以访问“此内容”,而 TL 1 组则不能。
实际的信任等级并未启用。任何成员都可以是信任等级 1,但仍可通过付费获得对 TL 4 组的访问权限——前提是我不是按信任等级设置权限,而是仅按组设置权限。

是的,处理这个问题的方法是使用组和分类安全权限。通常无需依赖信任等级。

@Dani1,感谢您在此分享您的经验与教训,这对像我一样使用 MemberPress 的用户非常有帮助。

这确实合理,但我很好奇,如果会员的订阅过期,而他们从未退出登录,从而继续访问群组,会发生什么情况?您是否遇到过这个问题?

如果是这样,我想知道是否有办法强制刷新或强制退出登录……

谢谢!
Ray

您需要捕获 Memberpress 事件 并相应地调整群组权限。

另见 Bring over permission level from WordPress MemberPress - #2 by simonManage group membership in Discourse with WP Discourse SSO

@RGJ 上面标记为解决方案的代码确实会从群组中移除成员,但正如 Dani1 提到的,她指出该功能要等到用户注销后才会生效。

如果用户通过正常的 SSO 登录流程进行更新,则更新要等到他们注销并重新登录后才会生效。上面的代码示例是解决该问题最简单的方法,但可能并不是管理组成员身份的最佳方式。

当 WordPress 作为 Discourse 的 SSO 提供商站点时,WP Discourse 插件提供了一些辅助函数。这些函数允许您更新组成员身份,而无需用户注销 Discourse。这些函数位于 Richard 链接的主题中:https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse-sso/74724。

Richard 链接的帖子详细介绍了这两种不同的方法。我将在下周将这些细节添加到 howto 主题中。目前,最好先阅读该帖子。

啊,我想我明白了。我困惑的一部分原因在于 Bring over permission level from WordPress MemberPress - #2 by simon 这个帖子,它在底部指向了这个帖子作为解决方案,所以让人有些混淆。

这对我来说还是有点太难了,但我期待你关于这个主题的操作指南。

感谢大家的帮助!
Ray

那就把他们登出吧 :slight_smile:
类似这样的代码应该可以:

  $url = sprintf('https://%s/users/by-external/%d.json', $discourseHostname, $wpUserID);
  $options = [
    'headers' => [
      'Api-Key' => $apiKey,
      'Api-Username' => 'system'
    ]
  ];
  $request = wp_remote_get($url, $options);
  if (wp_remote_retrieve_response_code($request) != 200) {
	return; // 用户未找到,退出
  }
  
  $userData = json_decode(wp_remote_retrieve_body($request), true);
  if (!isset($userData['user']['id'])) {
	return false; // 响应不正确,退出
  }
  
  $discourseUserID = $userData['user']['id'];
  
  $url = sprintf('https://%s/admin/users/%d/log_out', $discourseHostname, $discourseUserID);
  $options = [
    'headers' => [
      'Api-Key' => $apiKey,
      'Api-Username' => 'system'
    ]
  ];
  $request = wp_remote_post($url, $options);

你可以代表他们登出。只需模拟他们的身份即可。

事实上,只需进入他们的用户详情页面,就可以让他们登出。

(显然,这一切都非常手动。我只有几个人,而且清楚每个人在做什么。我还没有尝试 Richard 的解决方案,但知道有这个方法很好!)

届时能否在这里添加一个链接?谢谢!

当然可以,相关主题链接如下:https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse-sso/74724。该主题介绍了如何使用 WP Discourseadd_user_to_discourse_groupremove_user_from_discourse_group 函数。假设您的 WordPress 站点已配置为 Discourse 的 SSO 提供商站点,那么您应该使用这些函数来管理 Discourse 上的群组成员资格。

该主题中的示例使用的是 PaidMembershipsPro 插件,但类似的方法应该适用于任何制作精良的 WordPress 会员插件。

我将在该主题中补充一些关于如何使用 add_groupsremove_groups SSO 参数来管理群组成员资格的细节。在大多数情况下,通过在 SSO 载荷中添加这些参数来管理群组成员资格并不是最佳方案,因为这需要用户退出并重新登录 Discourse,其群组成员资格才会更新。

我希望在不久的将来,会出现一种开箱即用的解决方案,将 Discourse 与特定的 WordPress 会员插件进行关联。目前,这只能通过向您的 WordPress 站点添加少量自定义代码来实现。如果您通常不从事此类工作,可能需要聘请开发人员来协助完成。

感谢提供这些细节。我看看能不能把它们记下来。

我同意——我宁愿让群组的添加/删除实时完成,而不是等到用户登出再登录时处理。

如果我自己搞不定,我会在这里雇一位开发者来做,并分享代码。我想市场板块里有几位开发者已经做过类似的功能了。

Ray

嘿,雷,你最后在这里做什么?