benjaminmedia/wp-wa-oauth

本包的最新版本(1.5.0)没有提供许可证信息。

从WA平台登录用户

安装: 201

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 14

分支: 1

开放问题: 0

类型:wordpress-plugin

1.5.0 2020-02-27 11:47 UTC

README

此插件可以使您的WordPress站点与WA用户基础集成。它通过集成WA OAuth API,并提供一系列您可以在主题中调用的api函数来实现,以便有条件地锁定未经授权用户的内容。

要求

  • WordPress 4.4或更高版本
  • WP REST API必须安装并激活2.0或更高版本 http://v2.wp-api.org/
  • 语言支持(可选)必须安装并激活Polylang插件1.8.4或更高版本
  • PHP 5.6或更高版本

安装/配置

通过composer安装

composer require benjaminmedia/wp-wa-oauth

从以下位置下载最新版本: https://github.com/BenjaminMedia/wp-wa-oauth/releases 解压缩并将文件放置在您的 /wp-content/plugins 目录中。

获取凭证和配置

安装并激活插件后,请确保您有一组API凭证。如果您没有一组凭证,请联系Bonnier以获取它们。您需要为站点上的每个域名设置一组凭证。在生成凭证之前,您必须通知Bonnier应与凭证一起工作的重定向URI。这必须是以下格式 http://|https://[your-domain].[your-country]/wp-json/bp-wa-oauth/v1/oauth/login

示例: http://digitalfoto.dk/wp-json/bp-wa-oauth/v1/oauth/login

除了重定向URI之外,您还应告知他们您想要验证的WA用户库,例如: http://woman.dk 或另一个WA网站。

获取您的凭证后,登录到您的WP管理面板,转到菜单设置->Wp Wa Oauth:在这里,您必须输入您收到的每套凭证。

在这里,您还可以选择为每个域名全局启用或禁用登录。此外,您还可以选择全局所需用户角色级别。

如果您有网站的多个语言版本,请确保您使用Polylang插件。

如果您希望特定的页面可以解锁或需要不同于全局设置的另一个用户角色,则可以访问每个页面/帖子的编辑页面,在这里您将找到一个名为“WA OAuth锁定内容”的元数据框。您在这里所做的任何设置都将覆盖您设置的全球设置。

创建本地用户

从版本1.1.0开始,此插件现在支持创建本地用户。这意味着您可以从插件设置页面切换是否要在用户登录后创建WordPress用户。

如果您启用此功能,您还可以选择一个选项,即WordPress用户是否应在通过WA验证后自动登录到WordPress。

用户将被赋予从WA提供的角色创建的自定义角色:目前这包括两个角色 userssubscribers。为了避免与现有WordPress角色的冲突,角色名称将使用前缀 bp_wa_,这意味着最终的角色名称将是 bp_wa_usersbp_wa_subscribers

这两个角色都只有读取权限,这将允许用户编辑/查看他们的个人资料,但在WordPress中没有任何其他权限。

自版本 1.2.2 开始,插件现在将劫持与 WA 登录用户具有相同电子邮件地址的现有用户。插件将使用 WA 用户的字段更新本地用户字段。

自定义角色能力

您可能需要覆盖您创建的角色的默认能力,您应该将以下过滤器添加到插件或您的 functions.php 文件中。

// To override the default capabilities you should implement a filter like so

add_filter('bp_wa_users_capabilities', function($default) {
	return array_merge($default, ['edit_posts' => false]);
});

// you can either extend the default capabilities by doing an array merge or,
// you can override the capabilities completely by returning a new array like so

add_filter('bp_wa_subscribers_capabilities', function($default) {
	return ['edit_posts' => true];
});

// note the filter follows a [role_name].[_capabilities] format
挂钩到本地用户更新

自插件版本 1.2.0 及以上版本,您现在可以挂钩到本地用户的更新调用。这是通过使用 add_filter 方法添加 WordPress 过滤器来完成的,以下是一个示例:

add_filter('bp_wa_oauth_on_user_update', function($users){

	$localUser = $users['wp']; // Local user object and Instance of the WP_User class
	$waUser = $users['wa']; // WhiteAlbum user object an instance of stdClass

	// Set the user_url property to the profile_image url
	$localUser->user_url = $waUser->profile_image->url;

	// You can also save custom_user meta
	update_user_meta($localUser->ID, 'user_birth_date', $waUser->birthdate);

	// it is important to remember to return the $localUser variable otherwise the changes made will not be saved.
	return $localUser;

});

以下是 Wa 登录服务返回的数据示例

{
	id: 621905,
	url: "http://woman.dk/users/test-account",
	path: "/users/test-account",
	username: "test-account",
	first_name: "Test",
	last_name: "Account",
	gender: "male",
	birthdate: "1925-06-12",
	address: "",
	zip: "",
	city: "",
	email: "aa@attmatr.com",
	roles: [
		"users"
	],
	banned: false,
	unread_message_count: 0,
	profile_image: {
		id: 1291263,
		url: "http://bonnier.imgix.net/ladygaga-z6VF3a4XTgSQEraVN-qmRQ.jpg",
		copyright: null,
		title: null,
		description: null,
		uuid: "ladygaga-z6VF3a4XTgSQEraVN-qmRQ.jpg",
		crops: [ ],
		alt_text: null,
		width: 400,
		height: 300
	},
	cover_image: null
}

用户更新回调

如果您启用了创建本地用户选项,那么可能发生的情况是用户将在 Main WhiteAlbum 网站上更新他们的个人资料。这可能会意味着您在应用程序中拥有的本地用户信息可能会与同步。为了解决这个问题,WhiteAlbum 具有回调功能,这意味着每次有用户在您的应用程序中登录时,它们都可以调用您的应用程序。

该插件自动设置回调路由,并也会处理用户信息的更新,但在 WhiteAlbum 可以调用您的应用程序之前,他们需要知道您的回调 URL。回调 URI 是从以下格式生成的:http://|https://[你的域名].[你的国家]/wp-json/bp-wa-oauth/v1/oauth/callback

示例 URL: http://test.komputer.dk/wp-json/bp-wa-oauth/v1/oauth/callback

您应该在请求凭证时将此 URL 提供给 Bonnier,然后我们将设置回调。

注意:回调只能在您的用户在过去 24 小时内登录应用程序的情况下工作。否则,本地用户信息将不会更新,直到他们下次登录您的应用程序。

用法示例

以下代码片段显示了在您的主题模板文件中可能使用的 php 示例。

<!-- 	
	First we get an instance of the bp-wa-oauth plugin by calling bp_wa_oauth(),
	once we have an instance we can call the is_authenticated() function.
	This function checks if the user is logged in,
	and if they have access to the current page/post.
	You may pass an optional $postId variable to the function,
	if you wish to check against a specific page/post.
-->

<?php if( bp_wa_oauth()->is_authenticated() ) { ?>

	<!-- 	
		If we are authenticated;
		then we can get the current user info by calling: get_user()
		get_user() returns either a WP_User object or a stdClass if the create
		local user option is disabled.
	-->

		<?php echo bp_wa_oauth()->get_user()->first_name; ?>

		<!-- 	
			You can add logout buttons to trigger a logout, note this will not destroy
			your WhiteAlbum session. It will only log you out of the current site.
		-->
		<button class="bp-wa-oauth-logout">login and return here</button>
		<button class="bp-wa-oauth-logout" data-bp-wa-oauth-redirect="/some/url" >login and redirect to specific url</button>

		<!-- 	
			If we are not authenticated;
			then we can check wether to display the login buttons or not,
			by checking if the current page is locked.
			This i done by calling the is_locked function, like the is_authenticated()
			the function will attempt to check the current page/post, but also accepts
			an optional parameter $postId if you wish to check against a specific page/post.
		-->

<?php } elseif( bp_wa_oauth()->is_locked() ) { ?>

		<!-- 	
			On clicking this button the user will be redirected to login screen;
			and returned to the same page on login completion
		-->
		<button class="bp-wa-oauth-login" >login current page</button>

		<!-- 	
			On clicking this button the user will be redirected to login screen;
			and returned to the url provided in the attr: 'data-bp-wa-oauth-redirect'
		-->
		<button class="bp-wa-oauth-login" data-bp-wa-oauth-redirect="/test/tasker/akvis/tag-ny-test-dk-artikel-test-om-den-kommer">login and redirect to specific url</button>

		<!-- 	
			Notice how each button has a class "bp-wa-oauth-login",
			this class is what triggers the login JavaScript,
			any element that has this class attached will be clickable;
			and will once clicked, trigger the login flow.
		-->

<?php } ?>