gasap-users.php 4.89 KB
Newer Older
agnez's avatar
start  
agnez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * @package gasap-users
 * @version 1.0
 */
/*
Plugin Name: GASAP users
Description: Plugin for the GASAP network, managing user roles and capabilities
Author: Lore Mipsum
Text Domain: gasap-users
Domain Path: /languages
Version: 1.0
Author URI: http://mipsum.be/
*/
defined( 'ABSPATH' ) or die();

add_action( 'plugins_loaded', 'gasap_users_load_plugin_textdomain' );
function gasap_users_load_plugin_textdomain() {
    load_plugin_textdomain( 'gasap-users', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

/*
agnez's avatar
agnez committed
23
 * class for moving admin role out of editor's reach http://wordpress.stackexchange.com/questions/4479/editor-can-create-any-new-user-except-administrator
agnez's avatar
start  
agnez committed
24
 */
agnez's avatar
agnez committed
25
class GASAP_User_Caps {
agnez's avatar
start  
agnez committed
26 27

  // Add our filters
agnez's avatar
agnez committed
28
  function GASAP_User_Caps(){
agnez's avatar
start  
agnez committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    add_filter( 'editable_roles', array(&$this, 'editable_roles'));
    add_filter( 'map_meta_cap', array(&$this, 'map_meta_cap'),10,4);
  }

  // Remove 'Administrator' from the list of roles if the current user is not an admin
  function editable_roles( $roles ){
    if( isset( $roles['administrator'] ) && !current_user_can('administrator') ){
      unset( $roles['administrator']);
    }
    return $roles;
  }

  // If someone is trying to edit or delete and admin and that user isn't an admin, don't allow it
  function map_meta_cap( $caps, $cap, $user_id, $args ){

    switch( $cap ){
        case 'edit_user':
        case 'remove_user':
        case 'promote_user':
            if( isset($args[0]) && $args[0] == $user_id )
                break;
            elseif( !isset($args[0]) )
                $caps[] = 'do_not_allow';
            $other = new WP_User( absint($args[0]) );
            if( $other->has_cap( 'administrator' ) ){
                if(!current_user_can('administrator')){
                    $caps[] = 'do_not_allow';
                }
            }
            break;
        case 'delete_user':
        case 'delete_users':
            if( !isset($args[0]) )
                break;
            $other = new WP_User( absint($args[0]) );
            if( $other->has_cap( 'administrator' ) ){
                if(!current_user_can('administrator')){
                    $caps[] = 'do_not_allow';
                }
            }
            break;
        default:
            break;
    }
    return $caps;
  }

}
agnez's avatar
agnez committed
77
$GASAP_user_caps = new GASAP_User_Caps();
agnez's avatar
start  
agnez committed
78 79 80 81 82 83


/* 
 * user roles and capabilities
 */

agnez's avatar
agnez committed
84 85 86 87 88 89 90
//check if role exist before removing it
if( get_role('subscriber') ){
      remove_role( 'subscriber' );
}
if( get_role('contributor') ){
      remove_role( 'contributor' );
}
agnez's avatar
start  
agnez committed
91 92

/**
agnez's avatar
agnez committed
93
 * Remove capabilities from authors and referents
agnez's avatar
start  
agnez committed
94 95 96 97 98 99
 */
add_action( 'init', 'gasap_remove_capabilities' );
function gasap_remove_capabilities() {
    $author = get_role( 'author' );
    $caps = array(
        'edit_pages',
agnez's avatar
agnez committed
100
        'publish_pages',
agnez's avatar
start  
agnez committed
101 102 103 104 105 106 107
        'delete_pages',
        'edit_contentpieces',
        'publish_contentpieces',
        'delete_contentpieces',
    );
    foreach ( $caps as $cap ) {
        $author->remove_cap( $cap );
agnez's avatar
agnez committed
108
    } 
agnez's avatar
start  
agnez committed
109 110 111 112 113 114 115 116 117 118 119 120
}

// hide from admin menu http://www.wpmayor.com/how-to-remove-menu-items-in-admin-depending-on-user-role/
add_action( 'admin_init', 'gasap_remove_menu_pages' );
function gasap_remove_menu_pages() {
    global $user_ID;
 
    if ( current_user_can( 'author' ) ) {
        remove_menu_page('edit-comments.php'); // Comments
        remove_menu_page('tools.php'); // tools
    }
    if ( current_user_can( 'editor' ) ) {
agnez's avatar
agnez committed
121 122 123 124
        remove_submenu_page( 'themes.php', 'themes.php' ); // hide the theme selection submenu
        remove_submenu_page( 'themes.php', 'widgets.php' ); // hide the widgets submenu
        remove_submenu_page( 'themes.php', 'customize.php' ); //not working
        remove_submenu_page( 'themes.php', 'background_image' );  //not working
agnez's avatar
start  
agnez committed
125 126 127
    }    
}

agnez's avatar
agnez committed
128 129 130 131 132 133 134
// remove toolbar https://developer.wordpress.org/reference/classes/wp_admin_bar/remove_node/
add_action( 'admin_bar_menu', 'gasap_remove_wp_nodes', 999 );
function gasap_remove_wp_nodes($wp_admin_bar) {
    $wp_admin_bar->remove_node( 'new-page' );
    $wp_admin_bar->remove_node( 'new-contentpiece' );
    $wp_admin_bar->remove_node( 'new-media' );
    $wp_admin_bar->remove_node( 'wp-logo' );  
agnez's avatar
start  
agnez committed
135
    if ( current_user_can( 'author' ) ) {    
agnez's avatar
agnez committed
136 137 138 139 140
        $wp_admin_bar->remove_node( 'new-gasap' );
        $wp_admin_bar->remove_node( 'new-producer' );
        $wp_admin_bar->remove_node( 'new-worker' );
        $wp_admin_bar->remove_node( 'new-partner' );
        $wp_admin_bar->remove_node( 'new-user' );
agnez's avatar
start  
agnez committed
141
    }
agnez's avatar
agnez committed
142 143
}

agnez's avatar
agnez committed
144 145


agnez's avatar
agnez committed
146
//rename role
agnez's avatar
agnez committed
147 148
add_action('init', 'gasap_change_role_name');
function gasap_change_role_name() {
agnez's avatar
agnez committed
149
    global $wp_roles;
agnez's avatar
agnez committed
150
    if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
agnez's avatar
agnez committed
151

agnez's avatar
agnez committed
152 153 154 155
    $wp_roles->roles['editor']['name'] = __( 'super GASAPiens', 'gasap-users' );
    $wp_roles->role_names['editor'] = __( 'super GASAPiens', 'gasap-users' );
    $wp_roles->roles['author']['name'] = __( 'GASAPiens', 'gasap-users' );
    $wp_roles->role_names['author'] = __( 'GASAPiens', 'gasap-users' );
agnez's avatar
start  
agnez committed
156
}