Guest User

WP Change The Role

a guest
Oct 31st, 2014
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Change The Role
  4. Plugin URI: https://ja.forums.wordpress.org/topic/141608
  5. Version: 0.1a.20141031
  6. Author: mizube
  7. Author URI: https://ja.forums.wordpress.org/profile/419438
  8. Text Domain: change-the-role
  9. Domain Path: /languages/
  10. Description: Enables users to change the user role in some roles.
  11. */
  12.  
  13. add_action( 'plugins_loaded', array( 'ChangeTheRole', '__boot' ) );
  14. register_deactivation_hook( __FILE__, array( 'ChangeTheRole', 'deactivated' ) );
  15.  
  16. class ChangeTheRole {
  17.  
  18.     public $textdomain = 'change-the-role';
  19.  
  20.     public static function __boot() {
  21.         $class = __CLASS__;
  22.         new $class();
  23.     }
  24.  
  25.     public function __construct() {
  26.         load_plugin_textdomain( $this->textdomain, false, basename( dirname( __FILE__ ) ) . '/languages/' );
  27.  
  28.         add_action( 'personal_options_update', array( $this, 'action_personal_options_update' ) );
  29.         add_action( 'personal_options', array( $this, 'action_personal_options' ) );
  30.         add_action( 'admin_init', array( $this, 'action_admin_init' ) );
  31.     }
  32.  
  33.     public function action_personal_options_update( $user_id ) {
  34.         if ( !$user_id || !isset( $_POST['erc_role'] ) || !current_user_can( 'edit_user', $user_id ) )
  35.             return;
  36.  
  37.         $erc_roles = get_option( 'erc_changeable_roles', array() );
  38.         $erc_role  = sanitize_text_field( $_POST['erc_role'] );
  39.  
  40.         if ( !in_array( $erc_role, $erc_roles ) )
  41.             return;
  42.  
  43.         $user_roles = get_userdata( $user_id )->roles;
  44.         $has_roles  = false;
  45.  
  46.         foreach ( $user_roles as $user_role ) {
  47.             if ( in_array( $user_role, $erc_roles ) ) {
  48.                 $has_roles = true;
  49.                 break;
  50.             }
  51.         }
  52.  
  53.         if ( !$has_roles ) return;
  54.  
  55.         $wp_user = new WP_User( $user_id );
  56.         $wp_user->set_role( $erc_role );
  57.         unset( $wp_user );
  58.     }
  59.  
  60.     public function action_personal_options( $profileuser ) {
  61.         global $wp_roles;
  62.  
  63.         $role_names = $wp_roles->get_names();
  64.         $erc_roles  = get_option( 'erc_changeable_roles', array() );
  65.         $has_roles  = false;
  66.  
  67.         foreach ( $profileuser->roles as $user_role ) {
  68.             if ( in_array( $user_role, $erc_roles ) ) {
  69.                 $has_roles = true;
  70.                 break;
  71.             }
  72.         }
  73.  
  74.         if ( !empty( $erc_roles ) && $has_roles && !current_user_can( 'edit_users' ) ) {
  75.             echo '<table class="form-table"><tr><th scope="row"><label for="erc_role">' . __( 'Role' ) . '</label></th><td>';
  76.             echo '<select name="erc_role" id="erc_role">';
  77.             foreach ( $erc_roles as $role ) {
  78.                 echo '<option value="' . $role . '"'
  79.                     . selected( in_array( $role, $profileuser->roles ), true, false ) . '>'
  80.                     . translate_user_role( $role_names[$role] ) . '</option>';
  81.             }
  82.             echo '</select>';
  83.             echo '</td></tr></table>';
  84.         }
  85.     }
  86.  
  87.     public function action_admin_init() {
  88.         add_settings_field(
  89.             'erc_changeable_roles',
  90.             __( 'Changeable Roles', $this->textdomain ),
  91.             array( $this, 'callback_render_roles_list' ),
  92.             'general',
  93.             'default'
  94.         );
  95.         register_setting( 'general', 'erc_changeable_roles' );
  96.     }
  97.  
  98.     public function callback_render_roles_list() {
  99.         global $wp_roles;
  100.         $roles  = $wp_roles->get_names();
  101.         $option = get_option( 'erc_changeable_roles', array() );
  102.         echo '<ul>';
  103.         foreach ( $roles as $role => $label ) {
  104.             if( isset( $wp_roles->roles[$role]['capabilities']['manage_options'] ) ) continue;
  105.             echo '<li><label><input type="checkbox" name="erc_changeable_roles[]" value="' . $role . '" '
  106.                 . checked( in_array( $role, $option ), true, false ) . ' /> '
  107.                 . translate_user_role( $label ) . '</label></li>';
  108.         }
  109.         echo '</ul>';
  110.     }
  111.  
  112.     public static function deactivated() {
  113.         delete_option( 'erc_changeable_roles' );
  114.     }
  115. }
RAW Paste Data