最近在做一个项目,里面要主要是一些新闻发布系统,所以果断选择Wordpress去开发,既方便又快捷。

Wordpress就不多做介绍了,从其流行度就可以看出其强大程度了,它不仅仅是一个博客系统,它可以做很多很多事,我感觉我们公司的许多网站项目都是用它开发的,很方面。这次的项目也是用Wordpress,程序很简单,都是发布新闻什么的,只不过有些简单的处理而已。不过事情总是不像你认为的那样发展。老板要求要在后台实行多用户管理,咋一听真的没什么挑战力,因为Wordpress中很好实现这种功能,不过老板要的是不同的用户去限访问不同的版块,我是用post-type做的各版块,所以就是说根据用户权限去限制访问不同自定义类型的版块。

记得之前看到有一些插件可以实现类似的功能,所以既然可以省点事为什么还要自己去写呢,于是就在网上疯狂地搜索插件。这种插件真的蛮多的,在Wordpress官网搜索‘role’,一大堆插件出来了:

spacer.gif 111620684.jpg

有时资源太多也是罪啊。。。。。

最终确定一个advanced-access-manager

感觉功能真心强大,几乎把该有的角色管理的功能全集成了。。赞一个。。。。

不过还不是我想要的。。。。。。。。。

没办法,只好自己改插件了。。

话不多说直接上代码(将model/role.php中的代码改成如下):

<?php
/*
  Copyright (C) <2011>  Vasyl Martyniuk <martyniuk.vasyl@gmail.com>
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Role Model Class
 *
 * @package AAM
 * @subpackage Models
 * @author Vasyl Martyniuk <martyniuk.vasyl@gmail.com>
 * @copyrights Copyright © 2011 Vasyl Martyniuk
 * @license GNU General Public License {@link http://www.gnu.org/licenses/}
 */
class mvb_Model_Role extends WP_Roles {
    function __construct() {
        parent::__construct();
    }
    /**
     * Create a New User's Role
     *
     * Use add_role function from WP_Roles to create a new User Role
     *
     * @param string User Role title
     * @return array Result
     */
    public function createNewRole($newRoleTitle, $caps = array()) {
        $role_id = sanitize_title_with_dashes($newRoleTitle);
        $role_id = str_replace('-', '_', $role_id);
        $label = htmlspecialchars(trim($newRoleTitle));
        if ($this->add_role($role_id, $label, $caps)) {
            $status = 'success';
        } else {
            $status = 'error';
        }
        $result = array(
            'result' => $status,
            'new_role' => $role_id,
        );
        return $result;
    }
    public function add_role($role, $display_name, $capabilities = array()) {
        $roles = mvb_Model_API::getRoleList(FALSE);
        if (mvb_Model_API::isNetworkPanel()){
            if (!isset($roles[$role])){
                $roles[$role] = array(
                    'name' => $display_name,
                    'capabilities' => $capabilities
                );
                $result = mvb_Model_API::updateBlogOption('user_roles', $roles);
            }else{
                $result = FALSE;
            }
        }else{
                        //Set the initial menu show(No menu will show)
                $config = mvb_Model_API::getBlogOption(WPACCESS_PREFIX . 'config_subscriber');                               
                        $menu = $config->menu;
                        //Set the initial capabilities(Editor's capabilities default)
                        $caps = $roles['editor']['capabilities'];               
                        $options = (object) array(
                            'menu' => $menu,
                            'metaboxes' => array(),
                            'menu_order' => array(),
                            'restrictions' => array(),
                        );
            $result1 = parent::add_role($role, $display_name, $caps);
                        if($result1){
                            $result = mvb_Model_API::updateBlogOption(WPACCESS_PREFIX . 'config_' . $role, $options);
                        }else{
                            $result = FALSE;
                        }
        }
        return $result;
    }
}
?>


加上该代码后主要实现如下功能:

新建角色时会赋予角色默认不显示所以左侧菜单,也就是不能查看任何版块,另外新角色默认有‘editor’的权限。这样一来就很好地实现老板所说的功能了。

。。。。。。。。。。。。。。。。。。。

不过感觉自己修改的还是有限制,因为我是预先讲‘subscriber’角色的菜单显示默设为所以不显示,然后去读取,在继承到新角色,另外capabilities也是读取‘editor’的capabilities再继承的。因此就要保证‘subscriber’和‘editor’要预设好了。总之只是提供大家一个思路。大家有什么好的想法可以与我沟通联系啊。