| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles custom functionality on the new user screen, such as multiple user roles. |
| 4 |
* |
| 5 |
* @package Members |
| 6 |
* @subpackage Admin |
| 7 |
* @author The MemberPress Team |
| 8 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 9 |
* @link https://members-plugin.com/ |
| 10 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
namespace Members\Admin; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Edit user screen class. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class User_New { |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the instances of this class. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access private |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
private static $instance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor method. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access private |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
private function __construct() {} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sets up needed actions/filters for the admin to initialize. |
| 43 |
* |
| 44 |
* @since 2.0.0 |
| 45 |
* @access private |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
private function setup_actions() { |
| 49 |
|
| 50 |
// If multiple roles per user is not enabled, bail. |
| 51 |
// |
| 52 |
// @since 2.0.1 Added a check to not run on multisite. |
| 53 |
if ( ! members_multiple_user_roles_enabled() || is_multisite() ) |
| 54 |
return; |
| 55 |
|
| 56 |
// Only run our customization on the 'user-edit.php' page in the admin. |
| 57 |
add_action( 'load-user-new.php', array( $this, 'load' ) ); |
| 58 |
|
| 59 |
// Sets the new user's roles. |
| 60 |
add_action( 'user_register', array( $this, 'user_register' ), 5 ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Adds actions/filters on load. |
| 65 |
* |
| 66 |
* @since 2.0.0 |
| 67 |
* @access public |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function load() { |
| 71 |
|
| 72 |
// Adds the profile fields. |
| 73 |
add_action( 'user_new_form', array( $this, 'profile_fields' ) ); |
| 74 |
|
| 75 |
// Handle scripts. |
| 76 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 77 |
add_action( 'admin_footer', array( $this, 'print_scripts' ), 25 ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Adds custom profile fields. |
| 82 |
* |
| 83 |
* @since 2.0.0 |
| 84 |
* @access public |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function profile_fields() { |
| 88 |
|
| 89 |
if ( ! current_user_can( 'promote_users' ) ) |
| 90 |
return; |
| 91 |
|
| 92 |
// Get the default user roles. |
| 93 |
$new_user_roles = apply_filters( 'members_default_user_roles', array( get_option( 'default_role' ) ) ); |
| 94 |
|
| 95 |
// If the form was submitted but didn't go through, get the posted roles. |
| 96 |
if ( isset( $_POST['createuser'] ) && ! empty( $_POST['members_user_roles'] ) ) |
| 97 |
$new_user_roles = array_map( 'members_sanitize_role', $_POST['members_user_roles'] ); |
| 98 |
|
| 99 |
$roles = members_get_roles(); |
| 100 |
|
| 101 |
ksort( $roles ); |
| 102 |
|
| 103 |
wp_nonce_field( 'new_user_roles', 'members_new_user_roles_nonce' ); ?> |
| 104 |
|
| 105 |
<table class="form-table"> |
| 106 |
|
| 107 |
<tr> |
| 108 |
<th><?php esc_html_e( 'User Roles', 'members' ); ?></th> |
| 109 |
|
| 110 |
<td> |
| 111 |
<div class="wp-tab-panel"> |
| 112 |
<ul> |
| 113 |
<?php foreach ( $roles as $role ) : ?> |
| 114 |
|
| 115 |
<?php if ( members_is_role_editable( $role->name ) ) :?> |
| 116 |
<li> |
| 117 |
<label> |
| 118 |
<input type="checkbox" name="members_user_roles[]" value="<?php echo esc_attr( $role->name ); ?>" <?php checked( in_array( $role->name, $new_user_roles ) ); ?> /> |
| 119 |
<?php echo esc_html( $role->get( 'label' ) ); ?> |
| 120 |
</label> |
| 121 |
</li> |
| 122 |
<?php endif; ?> |
| 123 |
|
| 124 |
<?php endforeach; ?> |
| 125 |
</ul> |
| 126 |
</div> |
| 127 |
</td> |
| 128 |
</tr> |
| 129 |
|
| 130 |
</table> |
| 131 |
<?php } |
| 132 |
|
| 133 |
/** |
| 134 |
* Handles the new user's roles once the form has been submitted. |
| 135 |
* |
| 136 |
* @since 2.0.0 |
| 137 |
* @access public |
| 138 |
* @param int $user_id |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
public function user_register( $user_id ) { |
| 142 |
|
| 143 |
// If the current user can't promote users or edit this particular user, bail. |
| 144 |
if ( ! current_user_can( 'promote_users' ) ) |
| 145 |
return; |
| 146 |
|
| 147 |
// Is this a role change? |
| 148 |
if ( ! isset( $_POST['members_new_user_roles_nonce'] ) || ! wp_verify_nonce( $_POST['members_new_user_roles_nonce'], 'new_user_roles' ) ) |
| 149 |
return; |
| 150 |
|
| 151 |
// Create a new user object. |
| 152 |
$user = new \WP_User( $user_id ); |
| 153 |
|
| 154 |
// If we have an array of roles. |
| 155 |
if ( ! empty( $_POST['members_user_roles'] ) ) { |
| 156 |
|
| 157 |
// Get the current user roles. |
| 158 |
$old_roles = (array) $user->roles; |
| 159 |
|
| 160 |
// Sanitize the posted roles. |
| 161 |
$new_roles = array_map( 'members_sanitize_role', $_POST['members_user_roles'] ); |
| 162 |
|
| 163 |
// Loop through the posted roles. |
| 164 |
foreach ( $new_roles as $new_role ) { |
| 165 |
|
| 166 |
// If the user doesn't already have the role, add it. |
| 167 |
if ( members_is_role_editable( $new_role ) && ! in_array( $new_role, (array) $user->roles ) ) |
| 168 |
$user->add_role( $new_role ); |
| 169 |
} |
| 170 |
|
| 171 |
// Loop through the current user roles. |
| 172 |
foreach ( $old_roles as $old_role ) { |
| 173 |
|
| 174 |
// If the role is editable and not in the new roles array, remove it. |
| 175 |
if ( members_is_role_editable( $old_role ) && ! in_array( $old_role, $new_roles ) ) |
| 176 |
$user->remove_role( $old_role ); |
| 177 |
} |
| 178 |
|
| 179 |
// If the posted roles are empty. |
| 180 |
} else { |
| 181 |
|
| 182 |
// Loop through the current user roles. |
| 183 |
foreach ( (array) $user->roles as $old_role ) { |
| 184 |
|
| 185 |
// Remove the role if it is editable. |
| 186 |
if ( members_is_role_editable( $old_role ) ) |
| 187 |
$user->remove_role( $old_role ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Enqueue scripts. |
| 194 |
* |
| 195 |
* @since 2.0.0 |
| 196 |
* @access public |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public function enqueue() { |
| 200 |
|
| 201 |
wp_enqueue_script( 'jquery' ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Enqueue the plugin admin CSS. |
| 206 |
* |
| 207 |
* @since 2.0.0 |
| 208 |
* @access public |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function print_scripts() { ?> |
| 212 |
|
| 213 |
<script> |
| 214 |
jQuery( document ).ready( function() { |
| 215 |
|
| 216 |
var roles_dropdown = jQuery('select#role'); |
| 217 |
roles_dropdown.closest( 'tr' ).remove(); |
| 218 |
} ); |
| 219 |
</script> |
| 220 |
|
| 221 |
<?php } |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the instance. |
| 225 |
* |
| 226 |
* @since 2.0.0 |
| 227 |
* @access public |
| 228 |
* @return object |
| 229 |
*/ |
| 230 |
public static function get_instance() { |
| 231 |
|
| 232 |
if ( ! self::$instance ) { |
| 233 |
self::$instance = new self; |
| 234 |
|
| 235 |
self::$instance->setup_actions(); |
| 236 |
} |
| 237 |
|
| 238 |
return self::$instance; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
User_New::get_instance(); |
| 243 |
|