Added login request

This commit is contained in:
Nedim Uka
2018-06-20 18:03:43 +02:00
parent 4e52521fae
commit 593b445a21
4716 changed files with 1218265 additions and 57 deletions

View File

@@ -0,0 +1,182 @@
<?php
/**
* A set of action functions which handle the behavior of the roles checklist
* on user edit screens.
*/
class MDMR_Checklist_Controller {
/**
* The model object.
*
* @var \MDMR_Model $model
*/
var $model;
/**
* Constructor. Define properties.
*
* @param object $model The model object.
*/
public function __construct( $model ) {
$this->model = $model;
}
/**
* Remove the default WordPress role dropdown from the DOM.
*
* @param string $hook The current admin screen.
*/
public function remove_dropdown( $hook ) {
if ( 'user-edit.php' !== $hook && 'user-new.php' !== $hook) {
return;
}
wp_enqueue_script( 'md-multiple-roles', MDMR_URL . 'views/js/scripts.js', array( 'jquery' ), '1.0' );
}
/**
* Output the checklist view. If the user is not allowed to edit roles,
* nothing will appear.
*
* @param object $user The current user object.
*/
public function output_checklist( $user ) {
if ( ! $this->model->can_update_roles() ) {
return;
}
wp_nonce_field( 'update-md-multiple-roles', 'md_multiple_roles_nonce' );
$roles = $this->model->get_editable_roles();
$user_roles = ( isset( $user->roles ) ) ? $user->roles : null;
include( apply_filters( 'mdmr_checklist_template', MDMR_PATH . 'views/checklist.html.php' ) );
}
/**
* Update the given user's roles as long as we've passed the nonce
* and permissions checks.
*
* @param int $user_id The user ID whose roles might get updated.
*/
public function process_checklist( $user_id ) {
if ( isset( $_POST['md_multiple_roles_nonce'] ) && ! wp_verify_nonce( $_POST['md_multiple_roles_nonce'], 'update-md-multiple-roles' ) ) {
return;
}
if ( ! $this->model->can_update_roles() ) {
return;
}
$new_roles = ( isset( $_POST['md_multiple_roles'] ) && is_array( $_POST['md_multiple_roles'] ) ) ? $_POST['md_multiple_roles'] : array();
if ( empty( $new_roles ) ) {
return;
}
$this->model->update_roles( $user_id, $new_roles );
}
/**
* Add multiple roles in the $meta array in wp_signups db table
*
* @since 1.1.4
*
* @param $user
* @param $user_email
* @param $key
* @param $meta
*/
public function mu_add_roles_in_signup_meta( $user, $user_email, $key, $meta ) {
if ( isset( $_POST['md_multiple_roles_nonce'] ) && ! wp_verify_nonce( $_POST['md_multiple_roles_nonce'], 'update-md-multiple-roles' ) ) {
return;
}
if ( ! $this->model->can_update_roles() ) {
return;
}
$new_roles = ( isset( $_POST['md_multiple_roles'] ) && is_array( $_POST['md_multiple_roles'] ) ) ? $_POST['md_multiple_roles'] : array();
if ( empty( $new_roles ) ) {
return;
}
global $wpdb;
// Get user signup
// Suppress errors in case the table doesn't exist
$suppress = $wpdb->suppress_errors();
$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->signups} WHERE user_email = %s", $user_email ) );
$wpdb->suppress_errors( $suppress );
if ( empty( $signup ) || is_wp_error( $signup ) ) {
return new WP_Error( 'md_get_user_signups_failed' );
}
// Add multiple roles to a new array in meta var
$meta = maybe_unserialize( $meta );
$meta['md_roles'] = $new_roles;
$meta = maybe_serialize( $meta );
// Update user signup with good meta
$where = array( 'signup_id' => (int) $signup->signup_id );
$where_format = array( '%d' );
$formats = array( '%s' );
$fields = array( 'meta' => $meta );
$result = $wpdb->update( $wpdb->signups, $fields, $where, $formats, $where_format );
// Check for errors
if ( empty( $result ) && ! empty( $wpdb->last_error ) ) {
return new WP_Error( 'md_update_user_signups_failed' );
}
}
/**
* Add roles in signup meta with WP 4.8 filter : better method
*
* @since 1.2.0
*
* @param $meta
* @param $domain
* @param $path
* @param $title
* @param $user
* @param $user_email
* @param $key
*/
public function mu_add_roles_in_signup_meta_recently( $meta, $domain, $path, $title, $user, $user_email, $key ) {
if ( isset( $_POST['md_multiple_roles_nonce'] ) && ! wp_verify_nonce( $_POST['md_multiple_roles_nonce'], 'update-md-multiple-roles' ) ) {
return;
}
if ( ! $this->model->can_update_roles() ) {
return;
}
$new_roles = ( isset( $_POST['md_multiple_roles'] ) && is_array( $_POST['md_multiple_roles'] ) ) ? $_POST['md_multiple_roles'] : array();
if ( empty( $new_roles ) ) {
return;
}
$meta['md_roles'] = $new_roles;
return $meta;
}
/**
* Add multiple roles after user activation
*
* @since 1.1.4
*
* @param $user_id
* @param $password
* @param $meta
*/
public function mu_add_roles_after_activation( $user_id, $password, $meta ) {
if ( ! empty( $meta['md_roles'] ) ) {
$this->model->update_roles( $user_id, $meta['md_roles'] );
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* A set of filter functions which handle the behavior of the roles column in
* user list tables.
*/
class MDMR_Column_Controller {
/**
* The model object.
*
* @var \MDMR_Model $model
*/
var $model;
/**
* Constructor. Define properties.
*
* @param object $model The model object.
*/
public function __construct( $model ) {
$this->model = $model;
}
/**
* Remove the default role column and replace it with a custom version.
*
* @param array $columns Existing columns in name => label pairs.
* @return array An updated list of columns.
*/
public function replace_column( $columns ) {
unset( $columns['role'] );
$columns['md_multiple_roles_column'] = __( 'Roles', 'multiple-roles' );
return $columns;
}
/**
* Output the content of the Roles column.
*
* @param string $output The existing HTML to display. Should be empty.
* @param string $column The name of the current column.
* @param int $user_id The user ID whose roles are about to be displayed.
* @return string The new HTML output.
*/
public function output_column_content( $output, $column, $user_id ) {
if ( 'md_multiple_roles_column' !== $column ) {
return $output;
}
$roles = $this->model->get_user_roles( $user_id );
ob_start();
include( apply_filters( 'mdmr_column_template', MDMR_PATH . 'views/column.html.php' ) );
return ob_get_clean();
}
}

View File

@@ -0,0 +1,34 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Multiple Roles\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-09-26 14:09+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Florian TIAR <contact@tiar-florian.fr>\n"
"Language-Team: Florian TIAR <contact@tiar-florian.fr>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1;\n"
"X-Generator: Poedit 1.8.6\n"
"X-Poedit-KeywordsList: _e;__;_x;esc_html__;esc_html_e;_n;_ex;esc_html_x;"
"esc_attr_x;translate;esc_attr__;esc_attr_e\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPathExcluded-0: node_modules\n"
"X-Poedit-SearchPathExcluded-1: assets\n"
"X-Poedit-SearchPathExcluded-2: fields\n"
#: controllers/column.php:32 views/checklist.html.php:11
msgid "Roles"
msgstr ""
#: views/checklist.html.php:8
msgid "Permissions"
msgstr ""
#: views/column.html.php:14
msgid "None"
msgstr ""

View File

@@ -0,0 +1,110 @@
<?php
/**
* Role logic: retrieving, updating, and checking permission.
*/
class MDMR_Model {
/**
* Grab all WordPress roles.
*
* @return array Roles in name => label pairs.
*/
public function get_roles() {
global $wp_roles;
return apply_filters( 'mdmr_get_roles', $wp_roles->role_names );
}
/**
* Get all editable roles by the current user
*
* @return array editable roles
*/
public function get_editable_roles() {
$editable_roles = get_editable_roles();
$final_roles = array();
foreach ( $editable_roles as $key => $role ) {
$final_roles[$key] = $role['name'];
}
return apply_filters( 'mdmr_get_editable_roles', (array) $final_roles );
}
/**
* Grab a particular user's roles.
*
* @param object|int $user The user object or ID.
* @return array Roles in name => label pairs.
*/
public function get_user_roles( $user = 0 ) {
if ( ! $user ) {
return array();
}
$user = get_user_by( 'id', (int) $user );
if ( empty( $user->roles ) ) {
return array();
}
$all_roles = $this->get_roles();
$roles = array();
foreach( $user->roles as $role ) {
$roles[$role] = $all_roles[$role];
}
return apply_filters( 'mdmr_get_user_roles', $roles );
}
/**
* Erase the user's existing roles and replace them with the new array.
*
* @param integer $user_id The WordPress user ID.
* @param array $roles The new array of roles for the user.
*
* @return bool
*/
public function update_roles( $user_id = 0, $roles = array() ) {
do_action( 'mdmr_before_update_roles', $user_id, $roles );
if ( empty( $roles ) ) {
return false;
}
$roles = array_map( 'sanitize_key', (array) $roles );
$roles = array_filter( (array) $roles, 'get_role' );
$user = get_user_by( 'id', (int) $user_id );
// remove all roles
$user->set_role( '' );
foreach( $roles as $role ) {
$user->add_role( $role );
}
do_action( 'mdmr_after_update_roles', $user_id, $roles, $user->roles );
return true;
}
/**
* Check whether or not a user can edit roles. User must have the edit_roles cap and
* must be on a specific site (and not in the network admin area). Users also can't
* edit their own roles unless they're a network admin.
*
* @return bool True if current user can update roles, false if not.
*/
public function can_update_roles() {
do_action( 'mdmr_before_can_update_roles' );
if ( is_network_admin() || ! current_user_can( 'edit_users' ) || ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE && ! current_user_can( 'manage_sites' ) ) ) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
Plugin Name: Multiple Roles
Description: Allow users to have multiple roles on one site.
Version: 1.2.0
Author: Florian TIAR
Author URI: http://tiar-florian.fr
Plugin URI: https://wordpress.org/plugins/multiple-roles/
Github URI: https://github.com/Mahjouba91/multiple-roles
Text Domain: multiple-roles
*/
define( 'MDMR_PATH', plugin_dir_path( __FILE__ ) );
define( 'MDMR_URL', plugin_dir_url( __FILE__ ) );
/**
* Load files and add hooks to get things rolling.
*/
require_once( MDMR_PATH . 'model.php' );
require_once( MDMR_PATH . 'controllers/checklist.php' );
require_once( MDMR_PATH . 'controllers/column.php' );
$model = new MDMR_Model();
$checklist = new MDMR_Checklist_Controller( $model );
add_action( 'admin_enqueue_scripts', array( $checklist, 'remove_dropdown' ) );
add_action( 'show_user_profile', array( $checklist, 'output_checklist' ) );
add_action( 'edit_user_profile', array( $checklist, 'output_checklist' ) );
add_action( 'user_new_form', array( $checklist, 'output_checklist' ) );
add_action( 'profile_update', array( $checklist, 'process_checklist' ) );
// For new user form (in Backoffice)
// In multisite, user_register hook is too early so wp_mu_activate_user add user role after
if ( is_multisite() ) {
if ( version_compare( get_bloginfo( 'version' ), '4.8', '>=' ) ) {
add_filter( 'signup_site_meta', array( $checklist, 'mu_add_roles_in_signup_meta_recently' ), 10, 7 );
} else {
add_action( 'after_signup_user', array( $checklist, 'mu_add_roles_in_signup_meta' ), 10, 4 );
}
add_action( 'wpmu_activate_user', array( $checklist, 'mu_add_roles_after_activation' ), 10, 3 );
} else {
add_action( 'user_register', array( $checklist, 'process_checklist' ) );
}
$column = new MDMR_Column_Controller( $model );
add_filter( 'manage_users_columns', array( $column, 'replace_column' ), 11 );
add_filter( 'manage_users_custom_column', array( $column, 'output_column_content' ), 10, 3 );
add_action( 'init', 'load_translation' );
function load_translation() {
load_plugin_textdomain( 'multiple-roles', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

View File

@@ -0,0 +1,102 @@
=== Multiple Roles ===
Contributors: SeventhSteel, mista-flo
Tags: multiple roles, multiple roles per user, user roles, edit user roles, edit roles, more than one role, more than one role per user, more than one role for each user, many roles per user, unlimited roles
Requires at least: 3.1
Tested up to: 4.9
Stable tag: 1.2.0
Donate link: https://www.paypal.me/FlorianTIAR/5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Allow users to have multiple roles on one site.
== Description ==
This plugin allows you to select multiple roles for a user - something that WordPress already supports "under the hood", but doesn't provide a user interface for.
User edit and Add new user screens will display a checklist of roles instead of the default role dropdown. The main user list screen will also display all roles a user has.
It also supports well Multisite mode.
That's it. No extra settings.
If you want to contribute to this plugin, feel free to check the Github repository : https://github.com/Mahjouba91/multiple-roles
== Installation ==
= Automatic Install =
1. Log into your WordPress dashboard and go to Plugins &rarr; Add New
2. Search for "Multiple Roles"
3. Click "Install Now" under the Multiple Roles plugin
4. Click "Activate Now"
= Manual Install =
1. Download the plugin from the download button on this page
2. Unzip the file, and upload the resulting `multiple-roles` folder to your `/wp-content/plugins` directory
3. Log into your WordPress dashboard and go to Plugins
4. Click "Activate" under the Multiple Roles plugin
== Frequently Asked Questions ==
= Who can edit users roles? =
Anyone with the `edit_users` capability. By default, that means only administrators and network administrators on multi-site.
= Can you edit your own roles? =
If you're a network administrator on a multi-site setup, yes, you can edit your roles in sites of that network. Otherwise, no. This is how WordPress works normally too.
= I'm on the user edit screen - where's the checklist of roles? =
It's underneath the default profile stuff, under the heading "Permissions". If you still can't find it, you might be on your own profile page, or you might not have the `edit_users` capability.
= Can you remove all roles from a user? =
Sure. The user will still be able to log in and out, but won't be able to access any admin screens or see private pages. However, the user will still be able to see the WP Toolbar by default, which displays links to the Dashboard and Profile screens, so clicking on those will result in seeing a permission error.
== Screenshots ==
1. The roles checklist on Edit User screens
2. The Users screen with the enhanced Roles column
== Changelog ==
= 1.2.0 =
* 21 august 2017
* Check compatibilty with WP 4.8.1
* Translation of roles names : thanks to <a href="https://profiles.wordpress.org/benjaminniess/">Benjamin Niess</a>
* Mutlisite enhancement : Use a WP 4.8 filter to easier edit signup user meta
= 1.1.4 =
* 23 december 2016
* Fix fatal error in new user in single site : After adding an user, a wp_die error was shown "You cant give users that role", it was due to changes in 1.1.2
* Workaround to handle multisite support without breaking single site features
= 1.1.3 =
* 22 december 2016
* Fix fatal error in user update : After updating an user, a wp_die error was shown "You cant give users that role", it was due to changes in 1.1.2
= 1.1.2 =
* 21 december 2016
* Fix bug in multisite : After adding a new user with email confirmation, the multiple roles were not set, so the user did not have any roles on the site
= 1.1.1 =
* 3 november 2016
* Remove PHP closure to ensure Backward Compatibility with PHP versions < 5.3
= 1.1 =
* 24 october 2016
* New maintainer : Florian TIAR, you're strongly encouraged to update this plugin
* Add support of role checkbox in new user form (admin)
* Add Multisite support (for new user form)
* Add i18n support (text domain, translatable strings and pot file)
* Add some hooks (actions and filters)
* Fix issue where some low level users could add admin users
* Sanitize and escape all data
* Enhance UX of the form
= 1.0 =
* 2015
* Initial release

View File

@@ -0,0 +1,36 @@
<?php
/**
* Output the roles checklist.
*
* @var $roles array All WordPress roles in name => label pairs.
* @var $user_roles array An array of role names belonging to the current user.
*/
$creating = isset( $_POST['createuser'] );
$selected_roles = $creating && isset( $_POST['md_multiple_roles'] ) ? wp_unslash( $_POST['md_multiple_roles'] ) : '';
?>
<h3><?php _e( 'Permissions', 'multiple-roles' ); ?></h3>
<table class="form-table">
<tr>
<th><?php _e( 'Roles', 'multiple-roles' ); ?></th>
<td>
<?php foreach( $roles as $name => $label ) :
$input_uniq_id = uniqid(); ?>
<label for="md-multiple-roles-<?php echo esc_attr( $name ) . '-' . $input_uniq_id; ?>">
<input
id="md-multiple-roles-<?php echo esc_attr( $name ) . '-' . $input_uniq_id; ?>"
type="checkbox"
name="md_multiple_roles[]"
value="<?php echo esc_attr( $name ); ?>"
<?php if ( ! is_null( $user_roles ) ) : // Edit user page
checked( in_array( $name, $user_roles ) );
elseif ( ! empty( $selected_roles ) ) : // Add new user page
checked( in_array( $name, $selected_roles ) );
endif; ?>
/>
<?php echo esc_html( translate_user_role( $label ) ); ?>
</label>
<br />
<?php endforeach; ?>
</td>
</tr>
</table>

View File

@@ -0,0 +1,16 @@
<?php
/**
* Output a list of roles belonging to the current user.
*
* @var $roles array All applicable roles in name => label pairs.
*/
?><div class="md-multiple-roles">
<?php if ( ! empty( $roles ) ) :
foreach( $roles as $name => $label ) :
$roles[$name] = '<a href="users.php?role=' . esc_attr( $name ) . '">' . esc_html( translate_user_role( $label ) ) . '</a>';
endforeach;
echo implode( ', ', $roles );
else : ?>
<span class="md-multiple-roles-no-role"><?php _e( 'None', 'multiple-roles' ); ?></span>
<?php endif; ?>
</div><!-- .md-multiple-roles -->

View File

@@ -0,0 +1,6 @@
/**
* Remove the default WP role dropdown from the DOM.
*/
jQuery( document ).ready( function( $ ) {
$( 'select[name="role"]' ).closest( 'tr' ).remove();
} );