diff --git a/includes/admin/core/packages/2.0-beta1.php b/includes/admin/core/packages/2.0-beta1.php index 45454cb6..61d47981 100644 --- a/includes/admin/core/packages/2.0-beta1.php +++ b/includes/admin/core/packages/2.0-beta1.php @@ -945,7 +945,20 @@ $roles_associations = array(); $all_wp_roles = array_keys( get_editable_roles() ); if ( ! empty( $um_roles ) ) { foreach ( $um_roles as $um_role ) { - $role_key = sanitize_title( $um_role->post_title ); + + //old role key which inserted for each user to usermeta "role" + $key_in_meta = $um_role->post_name; + + if ( preg_match( "/[a-z0-9]+$/i", $um_role->post_title ) ) { + $role_key = sanitize_title( $um_role->post_title ); + } else { + $auto_increment = UM()->options()->get( 'custom_roles_increment' ); + $auto_increment = ! empty( $auto_increment ) ? $auto_increment : 1; + $role_key = 'custom_role_' . $auto_increment; + + $auto_increment++; + UM()->options()->update( 'custom_roles_increment', $auto_increment ); + } if ( ! in_array( $role_key, $all_wp_roles ) ) { $role_keys[] = $role_key; @@ -963,7 +976,7 @@ if ( ! empty( $um_roles ) ) { $role_metadata = array(); if ( ! empty( $all_role_metadata ) ) { - foreach( $all_role_metadata as $metadata ) { + foreach ( $all_role_metadata as $metadata ) { if ( '_um_can_edit_roles' == $metadata['meta_key'] || '_um_can_delete_roles' == $metadata['meta_key'] || '_um_can_view_roles' == $metadata['meta_key'] || '_um_can_follow_roles' == $metadata['meta_key'] @@ -985,20 +998,21 @@ if ( ! empty( $um_roles ) ) { $role_meta = $role_metadata; } - $old_key = ! empty( $role_meta['_um_core'] ) ? $role_meta['_um_core'] : $role_key; + //$old_key = ! empty( $role_meta['_um_core'] ) ? $role_meta['_um_core'] : $role_key; if ( ! in_array( $role_key, $all_wp_roles ) ) { - $roles_associations[ $old_key ] = 'um_' . $role_key; + $roles_associations[ $key_in_meta ] = 'um_' . $role_key; } else { - $roles_associations[ $old_key ] = $role_key; + $roles_associations[ $key_in_meta ] = $role_key; } - $r_key = ! empty( $role_meta['_um_core'] ) ? $role_meta['_um_core'] : $role_key; + + //$r_key = ! empty( $role_meta['_um_core'] ) ? $role_meta['_um_core'] : $role_key; //get all users with UM role $args = array( 'meta_query' => array( array( 'key' => 'role', - 'value' => $r_key + 'value' => $key_in_meta ) ), 'number' => '', @@ -1025,8 +1039,14 @@ if ( ! empty( $um_roles ) ) { update_option( "um_role_{$role_key}_meta", $role_meta ); } + //update user role meta where role keys stored foreach ( $um_roles as $um_role ) { - $role_key = sanitize_title( $um_role->post_title ); + + $key_in_meta = $um_role->post_name; + + $role_key = $roles_associations[ $key_in_meta ]; + if ( strpos( $role_key, 'um_' ) === 0 ) + $role_key = substr( $role_key, 3 ); $role_meta = get_option( "um_role_{$role_key}_meta" ); diff --git a/includes/admin/templates/role/role-edit.php b/includes/admin/templates/role/role-edit.php index 8a2dc3bc..597746a8 100644 --- a/includes/admin/templates/role/role-edit.php +++ b/includes/admin/templates/role/role-edit.php @@ -36,7 +36,15 @@ if ( ! empty( $_POST['role'] ) ) { } else { if ( 'add' == $_GET['tab'] ) { - $id = sanitize_title( $data['name'] ); + + if ( preg_match( "/[a-z0-9]+$/i", $data['name'] ) ) { + $id = sanitize_title( $data['name'] ); + } else { + $auto_increment = UM()->options()->get( 'custom_roles_increment' ); + $auto_increment = ! empty( $auto_increment ) ? $auto_increment : 1; + $id = 'custom_role_' . $auto_increment; + } + $redirect = add_query_arg( array( 'page'=>'um_roles', 'tab'=>'edit', 'id'=>$id, 'msg'=>'a' ), admin_url( 'admin.php' ) ); } elseif ( 'edit' == $_GET['tab'] && ! empty( $_GET['id'] ) ) { $id = $_GET['id']; @@ -58,6 +66,11 @@ if ( ! empty( $_POST['role'] ) ) { $roles[] = $id; update_option( 'um_roles', $roles ); + + if ( isset( $auto_increment ) ) { + $auto_increment++; + UM()->options()->update( 'custom_roles_increment', $auto_increment ); + } } $role_meta = $data; diff --git a/includes/class-config.php b/includes/class-config.php index 20b2d49c..50b3eec1 100644 --- a/includes/class-config.php +++ b/includes/class-config.php @@ -368,6 +368,7 @@ if ( ! class_exists( 'um\Config' ) ) { 'profile_photo_max_size' => 999999999, 'cover_photo_max_size' => 999999999, 'menu_item_workaround' => 0, + 'custom_roles_increment' => 1, ); $tabs = UM()->profile()->tabs_primary(); diff --git a/includes/class-dependencies.php b/includes/class-dependencies.php index 05323977..1dfeb5b2 100644 --- a/includes/class-dependencies.php +++ b/includes/class-dependencies.php @@ -20,29 +20,31 @@ if ( ! class_exists( 'um\Dependencies' ) ) { * @var array */ public $ext_required_version = array( - 'bbpress' => '2.0', - 'followers' => '2.0', - 'friends' => '2.0', + 'bbpress' => '2.0.1', + 'followers' => '2.0.1', + 'friends' => '2.0.1', 'groups' => '2.0', 'instagram' => '2.0', 'invitations' => '2.0', - 'mailchimp' => '2.0', - 'messaging' => '2.0', + 'mailchimp' => '2.0.1', + 'messaging' => '2.0.1', 'mycred' => '2.0', - 'notices' => '2.0', + 'notices' => '2.0.1', 'notifications' => '2.0', 'online' => '2.0', 'private-content' => '2.0', - 'profile-completeness' => '2.0', + 'profile-completeness' => '2.0.1', 'recaptcha' => '2.0', - 'reviews' => '2.0', + 'reviews' => '2.0.1', 'social-activity' => '2.0', - 'social-login' => '2.0', + 'social-login' => '2.0.1', 'terms-conditions' => '2.0', 'user-location' => '2.0', 'user-tags' => '2.0', 'verified-users' => '2.0', 'woocommerce' => '2.0', + 'restrict-content' => '2.0', + 'beaver-builder' => '2.0', ); /** diff --git a/includes/core/class-roles-capabilities.php b/includes/core/class-roles-capabilities.php index 8813547d..f9b181dd 100644 --- a/includes/core/class-roles-capabilities.php +++ b/includes/core/class-roles-capabilities.php @@ -243,6 +243,69 @@ if ( ! class_exists( 'Roles_Capabilities' ) ) { } + /** + * @return array + */ + function get_editable_user_roles() { + // User has roles so look for a UM Role one + $um_roles_keys = get_option( 'um_roles' ); + + if ( ! empty( $um_roles_keys ) ) { + $um_roles_keys = array_map( function( $item ) { + return 'um_' . $item; + }, $um_roles_keys ); + } + + return array_merge( $um_roles_keys, array( 'subscriber' ) ); + } + + + /** + * @param $user_id + * + * @return bool|mixed + */ + function get_editable_priority_user_role( $user_id ) { + $user = get_userdata( $user_id ); + + if ( empty( $user->roles ) ) + return false; + + // User has roles so look for a UM Role one + $um_roles_keys = get_option( 'um_roles' ); + + if ( ! empty( $um_roles_keys ) ) { + $um_roles_keys = array_map( function( $item ) { + return 'um_' . $item; + }, $um_roles_keys ); + } + + $orders = array(); + foreach ( array_values( $user->roles ) as $userrole ) { + if ( ! empty( $um_roles_keys ) && in_array( $userrole, $um_roles_keys ) ) { + $userrole_metakey = substr( $userrole, 3 ); + } else { + $userrole_metakey = $userrole; + } + + $rolemeta = get_option( "um_role_{$userrole_metakey}_meta", false ); + + if ( ! $rolemeta ) { + $orders[ $userrole ] = 0; + continue; + } + + $orders[ $userrole ] = ! empty( $rolemeta['_um_priority'] ) ? $rolemeta['_um_priority'] : 0; + } + + arsort( $orders ); + $roles_in_priority = array_keys( $orders ); + $roles_in_priority = array_intersect( $roles_in_priority, $this->get_editable_user_roles() ); + + return array_shift( $roles_in_priority ); + } + + /** * @param $user_id * diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 65e2d036..f835a892 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -386,7 +386,6 @@ if ( ! class_exists( 'User' ) ) { if ( ! current_user_can( 'edit_user', $userdata->ID ) ) return $content; - //$user_role = UM()->roles()->um_get_user_role( $userdata->ID ); $user_role = UM()->roles()->get_um_user_role( $userdata->ID ); if ( $user_role && ! empty( $userdata->roles ) && count( $userdata->roles ) == 1 ) $style = 'style="display:none;"'; @@ -589,9 +588,7 @@ if ( ! class_exists( 'User' ) ) { } // add permissions - //$user_role = UM()->roles()->um_get_user_role( $this->id ); $user_role = UM()->roles()->get_priority_user_role( $this->id ); - //$this->profile['role'] = ( strpos( $user_role, 'um_' ) === 0 ) ? str_replace( 'um_', '', $user_role ) : $user_role; $this->profile['role'] = $user_role; $this->profile['roles'] = UM()->roles()->get_all_user_roles( $this->id ); diff --git a/includes/core/um-actions-wpadmin.php b/includes/core/um-actions-wpadmin.php index 3a847567..1cc06348 100644 --- a/includes/core/um-actions-wpadmin.php +++ b/includes/core/um-actions-wpadmin.php @@ -3,38 +3,41 @@ if ( ! defined( 'ABSPATH' ) ) exit; - /*** - *** @checks if user can access the backend - ***/ - function um_block_wpadmin_by_user_role() { - if ( is_admin() && ! defined('DOING_AJAX') && um_user( 'ID' ) && ! um_user( 'can_access_wpadmin' ) && ! is_super_admin( um_user('ID') ) ) { - um_redirect_home(); - } +/** + * Checks if user can access the backend + */ +function um_block_wpadmin_by_user_role() { + if ( is_admin() && ! defined( 'DOING_AJAX' ) && um_user( 'ID' ) && ! um_user( 'can_access_wpadmin' ) && ! is_super_admin( um_user( 'ID' ) ) ) { + um_redirect_home(); } - add_action( 'init', 'um_block_wpadmin_by_user_role', 99 ); - - /*** - *** @hide admin bar appropriately - ***/ - function um_control_admin_bar( $content ){ - - if( is_user_logged_in() ){ - - if ( um_user('can_not_see_adminbar') ){ - return false; - } +} +add_action( 'init', 'um_block_wpadmin_by_user_role', 99 ); - return true; - } - return $content; +/** + * Hide admin bar appropriately + * + * @param $content + * + * @return bool + */ +function um_control_admin_bar( $content ) { + if ( is_user_logged_in() ) { + if ( um_user( 'can_not_see_adminbar' ) ) { + return false; + } + return true; } - add_filter( 'show_admin_bar' , 'um_control_admin_bar', 9999, 1 ); - - /*** - *** @fix permission for admin bar - ***/ - function um_force_admin_bar() { - um_reset_user(); - } - add_action( 'wp_footer', 'um_force_admin_bar' ); + + return $content; +} +add_filter( 'show_admin_bar' , 'um_control_admin_bar', 9999, 1 ); + + +/** + * Fix permission for admin bar + */ +function um_force_admin_bar() { + um_reset_user(); +} +add_action( 'wp_footer', 'um_force_admin_bar' ); \ No newline at end of file diff --git a/includes/um-short-functions.php b/includes/um-short-functions.php index edee0170..a8335d2d 100644 --- a/includes/um-short-functions.php +++ b/includes/um-short-functions.php @@ -1646,7 +1646,8 @@ function um_user( $data, $attrs = null ) { case 'role_select': case 'role_radio': - return UM()->roles()->get_role_name( um_user( 'role' ) ); + + return UM()->roles()->get_role_name( UM()->roles()->get_editable_priority_user_role( um_user( 'ID' ) ) ); break; case 'submitted':