Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Multiple Roles by default setting? #2

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
1bcd450
Add suport of checkboxes roles on add user form
Mahjouba91 Sep 24, 2016
b6563d9
Bump version to 1.2
Mahjouba91 Sep 24, 2016
a713681
Add gitignore for PHP storm file
Mahjouba91 Sep 26, 2016
58d7814
Prepare i18n (add textdomain)
Mahjouba91 Sep 26, 2016
9941488
Update changelog
Mahjouba91 Sep 26, 2016
169f29b
Remove textdomain from constant
Mahjouba91 Oct 6, 2016
2768c68
Security : Sanitize, escape
Mahjouba91 Oct 6, 2016
cf2d37e
Enhance form with better UX; Avoid conflicts
Mahjouba91 Oct 7, 2016
6b06b4c
rever is user role
Mahjouba91 Oct 7, 2016
14ebab5
Check if role exist before updating
Mahjouba91 Oct 9, 2016
f4c68b2
Properly handle multisite add new user
Mahjouba91 Oct 11, 2016
e0c0454
get_editable_roles to be sure a little user role can add admin user
Mahjouba91 Oct 11, 2016
9c9ba9b
Add some filters
Mahjouba91 Oct 11, 2016
dbd41c1
Update readme changelog
Mahjouba91 Oct 11, 2016
d4037f5
Update readme
Mahjouba91 Oct 11, 2016
4a30fb8
Update readme
Mahjouba91 Oct 23, 2016
e8e8565
Update gitignore
Mahjouba91 Oct 23, 2016
61ca8b1
Add Human made hooks
Mahjouba91 Oct 23, 2016
c3b4a8a
Better consisten with hook names
Mahjouba91 Oct 23, 2016
cbbebd1
Add version of JS script
Mahjouba91 Oct 23, 2016
d7dca7d
Remove closure to ensure BC with PHP < 5.3
Mahjouba91 Nov 2, 2016
36bf332
Code refactoring, update changelog
Mahjouba91 Nov 2, 2016
19e340c
Fix multisite bug with subsciption 1.1.2
Mahjouba91 Dec 21, 2016
1fde764
Bump version
Mahjouba91 Dec 21, 2016
212a34e
Fix overwrite off readme
Mahjouba91 Dec 21, 2016
2d69328
Fix changelog
Mahjouba91 Dec 21, 2016
a20c026
Fix error when updating user
Mahjouba91 Dec 22, 2016
04e6398
1.1.4 : Fix all bugs introduced in 1.1.2, handle mulitisite properly
Mahjouba91 Dec 23, 2016
3e368ea
Bump version 1.2.0 : enhancements
Mahjouba91 Aug 21, 2017
7f33f3d
Only remove get_editable_roles() roles on update
thomasfw Apr 12, 2018
ace2835
Fixed bug preventing us from unsetting a user's roles by unchecking a…
thomasfw Apr 12, 2018
e7939da
Use 'promote_users' cap
thomasfw Apr 12, 2018
5bea79c
Update readme.md
thomasfw Apr 12, 2018
3104610
Merge pull request #6 from thomasfw/fix-#3
Mahjouba91 Apr 12, 2018
ae3b76b
Merge pull request #7 from thomasfw/fix-#4
Mahjouba91 Apr 12, 2018
39c9e9f
Merge pull request #8 from thomasfw/fix-#5
Mahjouba91 Apr 12, 2018
fa74d9c
Bump version to 1.3.0
Apr 12, 2018
e6244fd
Merge remote-tracking branch 'Mahjouba91/master'
thomasfw Jun 11, 2018
3c1b661
Check for existence of the checklist in process_checklist()
thomasfw Jun 11, 2018
71eb9a2
Add minimum PHP version
Mahjouba91 Jul 1, 2020
c99b9b9
Merge pull request #11 from thomasfw/fix#9
Mahjouba91 Jul 1, 2020
6a6b6c8
Fix coding standards
Mahjouba91 Jul 1, 2020
580faac
Fix indentation
Mahjouba91 Jul 1, 2020
6f77373
Update readme text with new version and changelog
Mahjouba91 Jul 1, 2020
b167b82
Add WordPress deploy plugin to wordpress.org repo
Mahjouba91 Jul 1, 2020
f7e826a
Delete main.yml
Mahjouba91 Jul 1, 2020
8500213
Improve github action
Mahjouba91 Jul 1, 2020
40d52cb
Update wp_plugin_release.yml
Mahjouba91 Jul 1, 2020
ef1499c
Improve workflow
Mahjouba91 Jul 2, 2020
6376667
Fix misuses of nonces
Mahjouba91 Jun 30, 2021
32483b1
Bump readme.txt to 1.3.2
Mahjouba91 Jun 30, 2021
4ccf89a
Bump main file to 1.3.2
Mahjouba91 Jun 30, 2021
165b62f
Add the new plugin maintainer
Mahjouba91 May 25, 2022
a5b1c66
Update readme.txt
Mahjouba91 May 25, 2022
d95f590
Bump plugin version
Mahjouba91 May 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/wp_plugin_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Deploy to WordPress.org
on:
push:
tags:
- "*"
jobs:
tag:
name: New tag
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: WordPress Plugin Deploy
uses: 10up/action-wordpress-plugin-deploy@stable
env:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### WordPress template
*.log
.htaccess

# PHP Storm files
.idea

release.sh
136 changes: 125 additions & 11 deletions controllers/checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MDMR_Checklist_Controller {
/**
* The model object.
*
* @var object
* @var \MDMR_Model $model
*/
var $model;

Expand All @@ -27,9 +27,10 @@ public function __construct( $model ) {
* @param string $hook The current admin screen.
*/
public function remove_dropdown( $hook ) {
if ( $hook != 'user-edit.php' )
if ( 'user-edit.php' !== $hook && 'user-new.php' !== $hook) {
return;
wp_enqueue_script( 'md-multiple-roles', MDMR_URL . 'views/js/scripts.js', array( 'jquery' ) );
}
wp_enqueue_script( 'md-multiple-roles', MDMR_URL . 'views/js/scripts.js', array( 'jquery' ), '1.0' );
}

/**
Expand All @@ -40,15 +41,16 @@ public function remove_dropdown( $hook ) {
*/
public function output_checklist( $user ) {

if ( !$this->model->can_update_roles() )
if ( ! $this->model->can_update_roles() ) {
return;
}

wp_nonce_field( 'update-md-multiple-roles', 'md_multiple_roles_nonce' );

$roles = $this->model->get_roles();
$user_roles = $user->roles;
$roles = $this->model->get_editable_roles();
$user_roles = ( isset( $user->roles ) ) ? $user->roles : null;

include( MDMR_PATH . 'views/checklist.html.php' );
include( apply_filters( 'mdmr_checklist_template', MDMR_PATH . 'views/checklist.html.php' ) );

}

Expand All @@ -60,16 +62,128 @@ public function output_checklist( $user ) {
*/
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' ) )
// The checklist is not always rendered when this method is triggered on 'profile_update' (i.e. when updating a profile programmatically),
// First check that the 'md_multiple_roles_nonce' is available, else bail. If we continue to process and update_roles(), all user roles will be lost.
// We check for 'md_multiple_roles_nonce' rather than 'md_multiple_roles' as this input/variable will be empty if all role inputs are left unchecked.
if ( ! isset( $_POST['md_multiple_roles_nonce'] ) ) {
return;
}

if ( !$this->model->can_update_roles() )
if ( ! wp_verify_nonce( $_POST['md_multiple_roles_nonce'], 'update-md-multiple-roles' ) ) {
return;
}

$new_roles = isset( $_POST['md_multiple_roles'] ) ? $_POST['md_multiple_roles'] : array();
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();

$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
*
* @return void|WP_Error
*/
public function mu_add_roles_in_signup_meta( $user, $user_email, $key, $meta ) {
if ( ! 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 ( ! 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'] );
}
}

}
}
9 changes: 5 additions & 4 deletions controllers/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MDMR_Column_Controller {
/**
* The model object.
*
* @var object
* @var \MDMR_Model $model
*/
var $model;

Expand All @@ -29,7 +29,7 @@ public function __construct( $model ) {
*/
public function replace_column( $columns ) {
unset( $columns['role'] );
$columns['md_multiple_roles_column'] = 'Roles';
$columns['md_multiple_roles_column'] = __( 'Roles', 'multiple-roles' );
return $columns;
}

Expand All @@ -43,13 +43,14 @@ public function replace_column( $columns ) {
*/
public function output_column_content( $output, $column, $user_id ) {

if ( $column !== 'md_multiple_roles_column' )
if ( 'md_multiple_roles_column' !== $column ) {
return $output;
}

$roles = $this->model->get_user_roles( $user_id );

ob_start();
include( MDMR_PATH . 'views/column.html.php' );
include( apply_filters( 'mdmr_column_template', MDMR_PATH . 'views/column.html.php' ) );
return ob_get_clean();

}
Expand Down
34 changes: 34 additions & 0 deletions languages/multiple-roles.pot
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>\n"
"Language-Team: Florian TIAR <[email protected]>\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 ""
62 changes: 46 additions & 16 deletions model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ class MDMR_Model {
*/
public function get_roles() {
global $wp_roles;
return $wp_roles->role_names;
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 );
}

/**
Expand All @@ -22,41 +37,55 @@ public function get_roles() {
*/
public function get_user_roles( $user = 0 ) {

if ( $user && is_int( $user ) )
$user = get_user_by( 'id', $user );
if ( ! $user ) {
return array();
}

if ( !$user )
$user = get_user_by( 'id', (int) $user );
if ( empty( $user->roles ) ) {
return array();
}

global $wp_roles;
$all_roles = $this->get_roles();
$roles = array();

foreach( $user->roles as $role ) {
$roles[$role] = $wp_roles->role_names[$role];
$roles[$role] = $all_roles[$role];
}

return $roles;

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 );

$roles = array_map( 'sanitize_key', (array) $roles );
$user = get_user_by( 'id', $user_id );
$roles = array_filter( (array) $roles, 'get_role' );

$user = get_user_by( 'id', (int) $user_id );

// remove all roles
$user->set_role( '' );
// Remove all editable roles
$editable = get_editable_roles();
$editable_roles = is_array($editable) ? array_keys($editable) : array();
foreach( $editable_roles as $role ) {
$user->remove_role( $role );
}

foreach( $roles as $role ) {
$user->add_role( $role );
}

do_action( 'mdmr_after_update_roles', $user_id, $roles, $user->roles );

return true;
}

/**
Expand All @@ -68,10 +97,11 @@ public function update_roles( $user_id = 0, $roles = array() ) {
*/
public function 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;
do_action( 'mdmr_before_can_update_roles' );

if ( is_network_admin() || ! current_user_can( 'promote_users' ) || ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE && ! current_user_can( 'manage_sites' ) ) ) {
return false;
}

return true;

Expand Down
Loading