From 224b869541ed7bff3e452c85b5437399d2001599 Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Fri, 25 May 2018 18:22:38 +0300 Subject: [PATCH] - fixed user status setup on registration process; --- includes/core/class-form.php | 3 ++- includes/core/class-user.php | 30 ++++++++++++++++----------- includes/core/um-actions-register.php | 28 +++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/includes/core/class-form.php b/includes/core/class-form.php index 9b5ff609..7c0c1db6 100644 --- a/includes/core/class-form.php +++ b/includes/core/class-form.php @@ -465,8 +465,9 @@ if ( ! class_exists( 'um\core\Form' ) ) { $global_role = get_option( 'default_role' ); // WP Global settings $um_global_role = UM()->options()->get( 'register_role' ); // UM Settings Global settings - if ( ! empty( $um_global_role ) ) + if ( ! empty( $um_global_role ) ) { $global_role = $um_global_role; // Form Global settings + } $mode = $this->form_type( $post_id ); diff --git a/includes/core/class-user.php b/includes/core/class-user.php index a449366c..28cff8f4 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -1661,23 +1661,29 @@ if ( ! class_exists( 'um\core\User' ) ) { } } + + // update user if ( count( $args ) > 1 ) { - global $wp_roles; - $um_roles = get_option( 'um_roles' ); - if ( ! empty( $um_roles ) ) { - $role_keys = array_map( function( $item ) { - return 'um_' . $item; - }, get_option( 'um_roles' ) ); - } else { - $role_keys = array(); - } + //if isset roles argument validate role to properly for security reasons + if ( isset( $args['role'] ) ) { + global $wp_roles; + $um_roles = get_option( 'um_roles' ); - $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); + if ( ! empty( $um_roles ) ) { + $role_keys = array_map( function( $item ) { + return 'um_' . $item; + }, get_option( 'um_roles' ) ); + } else { + $role_keys = array(); + } - if ( isset( $args['role'] ) && in_array( $args['role'], $exclude_roles ) ) { - unset( $args['role'] ); + $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); + + if ( in_array( $args['role'], $exclude_roles ) ) { + unset( $args['role'] ); + } } wp_update_user( $args ); diff --git a/includes/core/um-actions-register.php b/includes/core/um-actions-register.php index e59f6040..addab9b4 100644 --- a/includes/core/um-actions-register.php +++ b/includes/core/um-actions-register.php @@ -55,7 +55,7 @@ function um_after_insert_user( $user_id, $args ) { UM()->user()->remove_cached_queue(); um_fetch_user( $user_id ); - UM()->user()->set_status( um_user('status') ); + UM()->user()->set_status( um_user( 'status' ) ); if ( ! empty( $args['submitted'] ) ) { UM()->user()->set_registration_details( $args['submitted'] ); } @@ -369,6 +369,30 @@ function um_submit_form_register( $args ) { $args['submitted'] = array_merge( $args['submitted'], $credentials ); $args = array_merge( $args, $credentials ); + //get user role from global or form's settings + $user_role = UM()->form()->assigned_role( UM()->form()->form_id ); + + //get user role from field Role dropdown or radio + if ( isset( $args['role'] ) ) { + global $wp_roles; + $um_roles = get_option( 'um_roles' ); + + if ( ! empty( $um_roles ) ) { + $role_keys = array_map( function( $item ) { + return 'um_' . $item; + }, get_option( 'um_roles' ) ); + } else { + $role_keys = array(); + } + + $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); + + //if role is properly set it + if ( ! in_array( $args['role'], $exclude_roles ) ) { + $user_role = $args['role']; + } + } + /** * UM hook * @@ -391,7 +415,7 @@ function um_submit_form_register( $args ) { * } * ?> */ - $user_role = apply_filters( 'um_registration_user_role', UM()->form()->assigned_role( UM()->form()->form_id ), $args ); + $user_role = apply_filters( 'um_registration_user_role', $user_role, $args ); $userdata = array( 'user_login' => $user_login,