仅将数组中的前两个相加(_CAP)

时间:2014-02-13 作者:LPH

在尝试跟踪代码中的一个问题无数天后,发现add\\u cap只处理数组中的前两个问题。第一个(编辑器)只会将delete\\u others\\u页面和delete\\u others\\u帖子添加到数据库中。如果我使用第二种重复add\\u cap的方法,那么我可以将所有功能都放到数据库中。

有人能看看这段代码,看看出了什么问题吗?为什么只有数组中的前两项进入数据库?

        if( $new_roles == \'editor\' ) {

            $user = new WP_User( $user_id );

            $user->add_cap(
                \'delete_others_pages\',\'delete_others_posts\',\'delete_pages\',\'delete_posts\',\'delete_private_pages\',\'delete_private_posts\',\'delete_published_pages\',\'delete_published_posts\',\'edit_others_pages\',\'edit_others_posts\',\'edit_pages\',\'edit_posts\',\'edit_private_pages\',\'edit_private_posts\',\'edit_published_pages\',\'edit_published_posts\',\'manage_categories\',\'manage_links\',\'moderate_comments\',\'publish_pages\',\'publish_posts\',\'read\',\'read_private_pages\',\'read_private_posts\');

        }

        elseif( $new_roles == \'author\' ) {

            $user_id = $XF->visitor->get(\'user_id\');

            $user = new WP_User( $user_id );

            $user->add_cap(
                \'delete_posts\', \'delete_published_posts\'
            );

            $user->add_cap(
                \'edit_posts\', \'edit_published_posts\'
            );

            $user->add_cap(
                \'publish_posts\', \'read\', \'upload_files\'
            );

            $user->add_cap(
                \'upload_files\'
            );

        }
更新代码以放置完整的函数:现在添加数组,以便回答我的初始问题,但它们添加到了错误的用户。

public static function set_user_roles( &$classObj, $user_group_id, $secondary_group_ids ) {
    /**
     * Function set_user_roles(&$classObj, $user_group_id, $secondary_group_ids)
     * 
     * This function sets the current user role based on the secondary group
     */
    global $XF, $wp_roles;
    $user_groups = explode( \',\', $secondary_group_ids );
    $user_groups[] = $user_group_id;

    /**
     * Loop through the user_roles and find the WP role equivalent for each
     */
    $user_roles = array();
    foreach( $user_groups as $user_group ) {
        $user_roles[] = $XF->options[\'xf_user_role\'][$user_group];

        /**
         * wp_die( \'This is the role:\' . var_dump( $XF->options[\'xf_user_role\'] ) );
         * 
         * Returns:
         *
         * array(7) { [3]=> string(13) "administrator" [4]=> string(6) "author" [2]=> string(10) "subscriber" [1]=> string(10) "subscriber" [5]=> string(6) "author" [7]=> string(11) "contributor" [6]=> string(6) "editor" }
         */

        /**
         * wp_die( var_dump( $XF->options[\'xf_user_role\'][$user_group] ) ); 
         *
         * Returns for user Ellie who has secondary group related to author
         * string(6) "author"
         * 
         * Returns Albert who has secondary group set to editor
         * string(6) "editor"
         *
         * Returns Pascal who has secondary group related to contributor
         * string(11) "contributor"
         *
         * Returns Gracie NO secondary group for registered members.
         * NULL
         *           
         */


        $new_roles =  $XF->options[\'xf_user_role\'][$user_group];

        if( $new_roles == \'\' || $new_roles == null) {

            $user_id = $XF->visitor->get(\'user_id\');

            $caps = array( \'read\' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == \'subscriber\' ) {

            $user_id = $XF->visitor->get(\'user_id\');

            $caps = array( \'read\' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == \'contributor\' ) {

            $user_id = $XF->visitor->get(\'user_id\');

            $caps= array( \'delete_posts\', \'edit_posts\', \'read\' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == \'author\' ) {          

            $user_id = $XF->visitor->get(\'user_id\');

            $caps = array(
                \'delete_posts\', \'delete_published_posts\',\'edit_posts\', \'edit_published_posts\',\'publish_posts\', \'read\', \'upload_files\',\'upload_files\'
                );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }           

        elseif( $new_roles == \'editor\' ) {

            $user_id = $XF->visitor->get(\'user_id\');

            $caps = array ( 
                \'delete_others_pages\',\'delete_others_posts\',\'delete_pages\',\'delete_post\',\'delete_private_pages\',\'delete_private_posts\',\'delete_published_pages\',\'delete_published_posts\',\'edit_others_pages\',\'edit_others_posts\',\'edit_pages\',\'edit_posts\', \'edit_private_pages\',\'edit_private_posts\',\'edit_published_pages\',\'edit_published_posts\',\'manage_categories\',\'manage_links\',\'moderate_comments\',\'publish_pages\',\'publish_posts\',\'read\',\'read_private_pages\',\'read_private_posts\'
            );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == \'administrator\' ) {

            $user_id = $XF->visitor->get(\'user_id\');

            $caps = array ( 
                \'activate_plugins\', \'delete_others_pages\',\'delete_others_posts\',\'delete_pages\',\'delete_plugins\',\'delete_posts\',\'delete_private_pages\',\'delete_private_posts\',\'delete_published_pages\',\'delete_published_posts\',\'edit_dashboard\',\'edit_files\',\'edit_others_pages\',\'edit_others_posts\',\'edit_pages\',\'edit_posts\',\'edit_private_pages\',\'edit_private_posts\',\'edit_published_pages\',\'edit_published_posts\',\'edit_theme_options\',\'export\',\'import\',\'list_users\',\'manage_categories\',\'manage_links\',\'manage_options\',\'moderate_comments\',\'promote_users\',\'publish_pages\',\'publish_posts\',\'read_private_pages\',\'read_private_posts\',\'read\',\'remove_users\',\'switch_themes\',\'upload_files\',\'create_product\'
            );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }


    }

}
更新:正在添加功能,但现在所有帐户都获得了管理功能。现在有人问:Editor and contributor roles not correct after adding function

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

查看您的代码,我认为您正在尝试修改现有角色的功能。我说得对吗?

为此,必须为角色对象而不是用户对象运行add\\u cap。此外,您必须知道,add\\u cap只接受一个功能作为字符串:

 $role = get_role( \'editor\' );
 $role->add_cap( \'the_capability\' );
如果要添加多个功能,可以执行循环。例如:

$capabilities = array( \'cap_1\', \'cap_2\', \'cap_3\' );
$role = get_role( \'editor\' );

foreach( $capabilities as $cap ) {
        $role->add_cap( $cap );
}
这同样适用于对用户对象执行add\\u cap:

//example of user ID
$user_id = 25
$capabilities = array( \'cap_1\', \'cap_2\', \'cap_3\' );
$user = new WP_User( $user_id );

foreach( $capabilities as $cap ) {
        $user->add_cap( $cap );
}

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?