在WordPress中创建简单的CRM:使用自定义功能

我们一直在研究如何在WordPress中创建一个简单的CRM系统。 在本系列的最后一部分中,我们探讨了如何使用角色来限制用户对WordPress管理界面的某些部分的访问。

今天,我们将介绍如何使用“自定义功能”来限制对“联系人自定义帖子类型”的访问。

自定义功能

尽管我们以前通过将WordPress用户分配给编辑者或作者角色来降低他们的访问级别,但用户仍然能够管理帖子和评论。 这些功能都不对我们有用,因此我们需要进一步限制访问权限,以使分配的用户只能访问“联系人自定义帖子类型”。

我们可以通过以下方式做到这一点:

  1. 在我们的自定义帖子类型上注册自定义功能
  2. 创建一个新的WordPress用户角色,仅将我们的新自定义功能分配给该角色
  3. 创建/编辑WordPress用户,将其分配给新的联系人角色

在我们的自定义帖子类型上注册自定义功能

注册自定义功能可以使我们稍后定义哪些WordPress角色可以访问这些功能,例如,作者是否可以创建新的联系人。

让我们编辑插件文件的register_post_type()函数调用,用以下内容替换capability_type => 'post'

'capabilities' => array(
    'edit_others_posts'		=> 'edit_others_contacts',
	'delete_others_posts'	=> 'delete_others_contacts',
	'delete_private_posts'	=> 'delete_private_contacts',
	'edit_private_posts'	=> 'edit_private_contacts',
	'read_private_posts'	=> 'read_private_contacts',
	'edit_published_posts'	=> 'edit_published_contacts',
	'publish_posts'			=> 'publish_contacts',
	'delete_published_posts'=> 'delete_published_contacts',
	'edit_posts'			=> 'edit_contacts'	,
	'delete_posts'			=> 'delete_contacts',
	'edit_post' 			=> 'edit_contact',
    'read_post' 			=> 'read_contact',
    'delete_post' 			=> 'delete_contact',
),
'map_meta_cap' => true,

我们的register_post_type()函数现在应如下所示:

/**
* Registers a Custom Post Type called contact
*/
function register_custom_post_type() {
	register_post_type( 'contact', array(
        'labels' => array(
			'name'               => _x( 'Contacts', 'post type general name', 'tuts-crm' ),
			'singular_name'      => _x( 'Contact', 'post type singular name', 'tuts-crm' ),
			'menu_name'          => _x( 'Contacts', 'admin menu', 'tuts-crm' ),
			'name_admin_bar'     => _x( 'Contact', 'add new on admin bar', 'tuts-crm' ),
			'add_new'            => _x( 'Add New', 'contact', 'tuts-crm' ),
			'add_new_item'       => __( 'Add New Contact', 'tuts-crm' ),
			'new_item'           => __( 'New Contact', 'tuts-crm' ),
			'edit_item'          => __( 'Edit Contact', 'tuts-crm' ),
			'view_item'          => __( 'View Contact', 'tuts-crm' ),
			'all_items'          => __( 'All Contacts', 'tuts-crm' ),
			'search_items'       => __( 'Search Contacts', 'tuts-crm' ),
			'parent_item_colon'  => __( 'Parent Contacts:', 'tuts-crm' ),
			'not_found'          => __( 'No contacts found.', 'tuts-crm' ),
			'not_found_in_trash' => __( 'No contacts found in Trash.', 'tuts-crm' ),
		),
        
        // Frontend
        'has_archive' => false,
        'public' => false,
        'publicly_queryable' => false,
        
        // Admin
        'capabilities' => array(
	        'edit_others_posts'		=> 'edit_others_contacts',
			'delete_others_posts'	=> 'delete_others_contacts',
			'delete_private_posts'	=> 'delete_private_contacts',
			'edit_private_posts'	=> 'edit_private_contacts',
			'read_private_posts'	=> 'read_private_contacts',
			'edit_published_posts'	=> 'edit_published_contacts',
			'publish_posts'			=> 'publish_contacts',
			'delete_published_posts'=> 'delete_published_contacts',
			'edit_posts'			=> 'edit_contacts'	,
			'delete_posts'			=> 'delete_contacts',
			'edit_post' 			=> 'edit_contact',
	        'read_post' 			=> 'read_contact',
	        'delete_post' 			=> 'delete_contact',
        ),
        'map_meta_cap' => true,
        'menu_icon' => 'dashicons-businessman',
        'menu_position' => 10,
        'query_var' => true,
        'show_in_menu' => true,
        'show_ui' => true,
        'supports' => array(
        	'title',
        	'author',
        	'comments',
        ),
    ) );	
}

在这里,发生两件事:

  1. 我们已经使用capabilities参数定义了我们自己的capabilities ,并将它们映射到它们的Post等效项。 这样可以确保WordPress准确理解功能的含义(即edit_contact行为与edit_post功能相同,只不过它适用于我们的Contacts Custom Post Type)。
  2. 我们已经告诉WordPress使用map_meta_cap将上述功能映射到WordPress的原始功能,以便将其强制执行。

以任何用户身份重新加载WordPress管理,您会看到我们的联系人自定义帖子类型已从WordPress管理菜单中消失:

联系人自定义帖子类型丢失

发生这种情况是因为我们现在需要告诉WordPress哪些角色具有我们新的联系人功能( edit_contactedit_contacts等)。

创建一个新的WordPress用户角色,为其分配我们的新自定义功能

使用add_role() ,我们可以创建一个新的WordPress用户角色并为其分配联系人功能。 该角色存储在WordPress Options数据中,因此我们只需要调用一次此函数。

为此,请在插件的__construct()函数末尾下方添加以下函数:

/**
* Activation hook to register a new Role and assign it our Contact Capabilities
*/
function plugin_activation() {
	
	// Define our custom capabilities
	$customCaps = array(
		'edit_others_contacts'			=> true,
		'delete_others_contacts'		=> true,
		'delete_private_contacts'		=> true,
		'edit_private_contacts'			=> true,
		'read_private_contacts'			=> true,
		'edit_published_contacts'		=> true,
		'publish_contacts'			=> true,
		'delete_published_contacts'		=> true,
		'edit_contacts'				=> true,
		'delete_contacts'			=> true,
		'edit_contact'				=> true,
		'read_contact'				=> true,
		'delete_contact'			=> true,
		'read'					=> true,
	);
	
	// Create our CRM role and assign the custom capabilities to it
	add_role( 'crm', __( 'CRM', 'tuts-crm'), $customCaps );
	
}

此功能将在WordPress中添加一个称为CRM的新角色。 分配给该角色的用户将只能访问联系人功能。 因此,他们将只能访问联系人自定义帖子类型。

注意,我们还将read功能分配给此角色。 这是允许用户编辑其个人资料(名称,密码等)所必需的。 我们需要允许用户执行此操作,因为当他们登录时,WordPress会自动将其重定向到“个人资料”屏幕。

如果我们未分配read功能,则用户登录时将发生以下情况:

未将读取功能分配给角色时的错误消息

要一次运行我们的plugin_activation()函数,让我们将以下代码添加到插件文件的末尾:

register_activation_hook( __FILE__, array( &$wpTutsCRM, 'plugin_activation' ) );

这告诉WordPress,在激活插件时,它需要在WPTutsCRM类中调用plugin_activation()函数。

创建/编辑WordPress用户,将其分配给新联系人角色

接下来,停用并重新激活您的插件,然后在WordPress管理界面中导航到“ 用户”>“添加新的”。

如果一切顺利,您将在下拉列表中看到新的CRM角色:

我们新的WordPress角色

让我们继续创建一个名为crm的新用户,然后以该新用户身份登录。 现在,我们应该看到Contacts ,其中DashboardProfile是唯一的其他菜单选项:


将自定义功能分配给其他角色

如果我们注销并以WordPress管理员,编辑者或作者身份重新登录,您会注意到WordPress管理菜单中仍缺少联系人自定义帖子类型:


这是因为我们只为CRM角色分配了“联系人自定义帖子类型”的功能。 因此,所有其他用户角色仍然无权访问此帖子类型。

要解决此问题,让我们通过在plugin_activation()函数的末尾添加以下代码,将自定义功能分配给管理员和编辑者角色:

// Add custom capabilities to Admin and Editor Roles
$roles = array( 'administrator', 'editor' );
foreach ( $roles as $roleName ) {
	// Get role
	$role = get_role( $roleName );
	
	// Check role exists
	if ( is_null( $role) ) {
		continue;
	}
	
	// Iterate through our custom capabilities, adding them
	// to this role if they are enabled
	foreach ( $customCaps as $capability => $enabled ) {
		if ( $enabled ) {
			// Add capability
			$role->add_cap( $capability );
		}
	}
}

在这里,我们迭代要分配自定义功能的角色,检查角色是否存在。 如果是这样,我们将遍历之前定义的自定义功能,并将其添加到角色中。

您会注意到我们尚未向“作者角色”添加任何自定义功能; 这是因为我们不想分配所有功能,因为“作者角色”传统上只允许对该用户自己的帖子进行写访问。

让我们通过为作者角色添加一些功能来继续构建我们的plugin_activation()函数:

// Add some of our custom capabilities to the Author Role
$role = get_role( 'author' );
$role->add_cap( 'edit_contact' );
$role->add_cap( 'edit_contacts' );
$role->add_cap( 'publish_contacts' );
$role->add_cap( 'read_contact' );
$role->add_cap( 'delete_contact' );
unset( $role );

现在,我们的整个函数应如下所示:

/**
* Activation hook to register a new Role and assign it our Contact Capabilities
*/
function plugin_activation() {
	
	// Define our custom capabilities
	$customCaps = array(
		'edit_others_contacts'			=> true,
		'delete_others_contacts'		=> true,
		'delete_private_contacts'		=> true,
		'edit_private_contacts'			=> true,
		'read_private_contacts'			=> true,
		'edit_published_contacts'		=> true,
		'publish_contacts'				=> true,
		'delete_published_contacts'		=> true,
		'edit_contacts'					=> true,
		'delete_contacts'				=> true,
		'edit_contact'					=> true,
		'read_contact'					=> true,
		'delete_contact'				=> true,
		'read'							=> true,
	);
	
	// Create our CRM role and assign the custom capabilities to it
	add_role( 'crm', __( 'CRM', 'tuts-crm'), $customCaps );
	
	// Add custom capabilities to Admin and Editor Roles
	$roles = array( 'administrator', 'editor' );
	foreach ( $roles as $roleName ) {
		// Get role
		$role = get_role( $roleName );
		
		// Check role exists
		if ( is_null( $role) ) {
			continue;
		}
		
		// Iterate through our custom capabilities, adding them
		// to this role if they are enabled
		foreach ( $customCaps as $capability => $enabled ) {
			if ( $enabled ) {
				// Add capability
				$role->add_cap( $capability );
			}
		}
	}
			
	// Add some of our custom capabilities to the Author Role
	$role = get_role( 'author' );
	$role->add_cap( 'edit_contact' );
	$role->add_cap( 'edit_contacts' );
	$role->add_cap( 'publish_contacts' );
	$role->add_cap( 'read_contact' );
	$role->add_cap( 'delete_contact' );
	unset( $role );
	
}

现在,以管理员,编辑者或作者身份登录将在WordPress管理菜单中显示“联系人”选项:


清理我们的角色

如果WordPress管理员停用了我们的插件,则CRM角色将保留在WordPress中。 但是,鉴于没有其他插件或WordPress的一部分使用此角色,因此这是多余的-因此我们需要确保在停用插件时删除CRM角色。

为此,请在plugin_activation()函数下面添加以下函数:

/**
* Deactivation hook to unregister our existing Contacts Role
*/
function plugin_deactivation() {
	
	remove_role( 'crm' );
	
}

与我们在激活插件时使用register_activation_hook()函数的方式相同,在我们的插件停用时,我们可以使用register_deactivation_hook() 。 让我们在register_activation_hook调用下面添加以下内容:

register_deactivation_hook( __FILE__, array( &$wpTutsCRM, 'plugin_deactivation' ) );

停用插件后,我们的CRM角色将不再可用。

摘要

我们已经成功地在WordPress中创建了一个简单的CRM系统,探索了使用自定义帖子类型,帖子元字段和第三方插件集成来存储有关我们的客户和潜在客户的信息。

本教程还介绍了WordPress的一些更高级方面,包括通过WP_List_Table列显示高级自定义字段数据,过滤我们的Post Query以搜索我们的高级自定义字段数据,以及通过角色和功能管理用户访问权限以将访问权限限制为仅对我们的CRM系统。

翻译自: https://code.tutsplus.com/tutorials/create-a-simple-crm-in-wordpress-using-custom-capabilities--cms-22985

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值