From 54016fdaab170d30c973bc79f9b5f9679e5b4ecd Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Thu, 8 Feb 2018 15:27:01 +0200 Subject: [PATCH] - 2.0.1 version; - small code optimization; - fix for update user profile role change; --- includes/admin/class-admin.php | 23 ++++++++ includes/admin/core/class-admin-enqueue.php | 27 +-------- includes/admin/core/class-admin-functions.php | 37 +----------- includes/admin/core/class-admin-metabox.php | 59 +------------------ includes/core/um-actions-profile.php | 41 ++++++++++++- ultimate-member.php | 2 +- 6 files changed, 67 insertions(+), 122 deletions(-) diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php index 93533b09..9ad80460 100644 --- a/includes/admin/class-admin.php +++ b/includes/admin/class-admin.php @@ -21,6 +21,29 @@ if ( ! class_exists( 'Admin' ) ) { } + /** + * Check if current page load UM post type + * + * + * @return bool + */ + function is_plugin_post_type() { + if ( isset( $_REQUEST['post_type'] ) ) { + $post_type = $_REQUEST['post_type']; + if ( in_array( $post_type, array( 'um_form','um_directory' ) ) ) { + return true; + } + } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'edit' ) { + $post_type = get_post_type(); + if ( in_array( $post_type, array( 'um_form', 'um_directory' ) ) ) { + return true; + } + } + + return false; + } + + /** * Check if plugin is installed with correct folder */ diff --git a/includes/admin/core/class-admin-enqueue.php b/includes/admin/core/class-admin-enqueue.php index 74e818ce..4b1f561a 100644 --- a/includes/admin/core/class-admin-enqueue.php +++ b/includes/admin/core/class-admin-enqueue.php @@ -51,26 +51,24 @@ if ( ! class_exists( 'Admin_Enqueue' ) ) { /*** *** @enter title placeholder ***/ - function enter_title_here( $title ){ + function enter_title_here( $title ) { $screen = get_current_screen(); if ( 'um_directory' == $screen->post_type ){ $title = 'e.g. Member Directory'; } - if ( 'um_role' == $screen->post_type ){ - $title = 'e.g. Community Member'; - } if ( 'um_form' == $screen->post_type ){ $title = 'e.g. New Registration Form'; } return $title; } + /*** *** @Runs on admin head ***/ function admin_head(){ - if ( $this->is_plugin_post_type() ){ + if ( UM()->admin()->is_plugin_post_type() ){ ?> @@ -87,25 +85,6 @@ if ( ! class_exists( 'Admin_Enqueue' ) ) { } - /*** - *** @check that we're on a custom post type supported by UM - ***/ - function is_plugin_post_type(){ - if (isset($_REQUEST['post_type'])){ - $post_type = $_REQUEST['post_type']; - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } else if ( isset($_REQUEST['action'] ) && $_REQUEST['action'] == 'edit') { - $post_type = get_post_type(); - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } - return false; - } - - /*** *** @Load Form ***/ diff --git a/includes/admin/core/class-admin-functions.php b/includes/admin/core/class-admin-functions.php index 0e5b2b80..ae37c426 100644 --- a/includes/admin/core/class-admin-functions.php +++ b/includes/admin/core/class-admin-functions.php @@ -44,44 +44,9 @@ if ( ! class_exists( 'Admin_Functions' ) ) { } - if ($post_type == 'um_role') { - - $messages['um_role'] = array( - 0 => '', - 1 => __('Role updated.'), - 2 => __('Custom field updated.'), - 3 => __('Custom field deleted.'), - 4 => __('Role updated.'), - 5 => isset($_GET['revision']) ? __('Role restored to revision.') : false, - 6 => __('Role created.'), - 7 => __('Role saved.'), - 8 => __('Role submitted.'), - 9 => __('Role scheduled.'), - 10=> __('Role draft updated.'), - ); - - } - return $messages; } - /*** - *** @check that we're on a custom post type supported by UM - ***/ - function is_plugin_post_type(){ - if (isset($_REQUEST['post_type'])){ - $post_type = $_REQUEST['post_type']; - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } else if ( isset($_REQUEST['action'] ) && $_REQUEST['action'] == 'edit') { - $post_type = get_post_type(); - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } - return false; - } /*** *** @gettext filters @@ -89,7 +54,7 @@ if ( ! class_exists( 'Admin_Functions' ) ) { function gettext($translation, $text, $domain) { global $post; //$screen = get_current_screen(); - if ( isset( $post->post_type ) && $this->is_plugin_post_type() ) { + if ( isset( $post->post_type ) && UM()->admin()->is_plugin_post_type() ) { $translations = get_translations_for_domain( $domain); if ( $text == 'Publish') { return $translations->translate( 'Create' ); diff --git a/includes/admin/core/class-admin-metabox.php b/includes/admin/core/class-admin-metabox.php index 3fe2bf4a..abeb7eba 100644 --- a/includes/admin/core/class-admin-metabox.php +++ b/includes/admin/core/class-admin-metabox.php @@ -39,23 +39,6 @@ if ( ! class_exists( 'Admin_Metabox' ) ) { return false; } - /*** - *** @check that we're on a custom post type supported by UM - ***/ - function is_plugin_post_type() { - if (isset($_REQUEST['post_type'])){ - $post_type = $_REQUEST['post_type']; - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } else if ( isset($_REQUEST['action'] ) && $_REQUEST['action'] == 'edit') { - $post_type = get_post_type(); - if ( in_array($post_type, array('um_form','um_role','um_directory'))){ - return true; - } - } - return false; - } /*** *** @Gets the role meta @@ -76,7 +59,7 @@ if ( ! class_exists( 'Admin_Metabox' ) ) { ***/ function admin_head(){ global $post; - if ( $this->is_plugin_post_type() && isset($post->ID) ){ + if ( UM()->admin()->is_plugin_post_type() && isset($post->ID) ){ $this->postmeta = $this->get_custom_post_meta($post->ID); } } @@ -767,46 +750,6 @@ if ( ! class_exists( 'Admin_Metabox' ) ) { } - /*** - *** @save role metabox - ***/ - /* function save_metabox_role( $post_id, $post ) { - global $wpdb; - - // validate nonce - if ( !isset( $_POST['um_admin_save_metabox_role_nonce'] ) || !wp_verify_nonce( $_POST['um_admin_save_metabox_role_nonce'], basename( __FILE__ ) ) ) return $post_id; - - // validate post type - if ( $post->post_type != 'um_role' ) return $post_id; - - // validate user - $post_type = get_post_type_object( $post->post_type ); - if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; - - $where = array( 'ID' => $post_id ); - if (empty($_POST['post_title'])) $_POST['post_title'] = 'Role #'.$post_id; - $wpdb->update( $wpdb->posts, array( 'post_title' => $_POST['post_title'], 'post_name' => sanitize_title( $_POST['post_title'] ) ), $where ); - - // save - delete_post_meta( $post_id, '_um_can_view_roles' ); - delete_post_meta( $post_id, '_um_can_edit_roles' ); - delete_post_meta( $post_id, '_um_can_delete_roles' ); - - do_action('um_admin_before_saving_role_meta', $post_id ); - - do_action('um_admin_before_save_role', $post_id, $post ); - - foreach( $_POST as $k => $v ) { - if (strstr($k, '_um_')){ - update_post_meta( $post_id, $k, $v); - } - } - - do_action('um_admin_after_editing_role', $post_id, $post); - - do_action('um_admin_after_save_role', $post_id, $post ); - - }*/ /*** *** @save form metabox diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php index 9cc32218..243d279c 100644 --- a/includes/core/um-actions-profile.php +++ b/includes/core/um-actions-profile.php @@ -114,16 +114,18 @@ if ( ! defined( 'ABSPATH' ) ) exit; if ( ! in_array( $args['submitted']['role'], $exclude_roles ) ) { $to_update['role'] = $args['submitted']['role']; } + + $args['roles_before_upgrade'] = UM()->roles()->get_all_user_roles( um_user( 'ID' ) ); } do_action( 'um_user_pre_updating_profile', $to_update ); $to_update = apply_filters( 'um_user_pre_updating_profile_array', $to_update ); - if (is_array( $to_update )) { - UM()->user()->update_profile( $to_update ); - do_action( 'um_after_user_updated', um_user( 'ID' ) ); + if ( is_array( $to_update ) ) { + UM()->user()->update_profile( $to_update ); + do_action( 'um_after_user_updated', um_user( 'ID' ), $args, $to_update ); } $files = apply_filters( 'um_user_pre_updating_files_array', $files ); @@ -145,6 +147,39 @@ if ( ! defined( 'ABSPATH' ) ) exit; } + + /** + * Leave roles for User, which are not in the list of update profile (are default WP or 3rd plugins roles) + * + * @param $user_id + * @param $args + * @param $to_update + */ + function um_restore_default_roles( $user_id, $args, $to_update ) { + if ( ! empty( $args['submitted']['role'] ) ) { + $wp_user = new WP_User( $user_id ); + + $role_keys = array_map( function( $item ) { + return 'um_' . $item; + }, get_option( 'um_roles' ) ); + + $leave_roles = array_diff( $args['roles_before_upgrade'], array_merge( $role_keys, array( 'subscriber' ) ) ); + + if ( UM()->roles()->is_role_custom( $to_update['role'] ) ) { + $wp_user->remove_role( $to_update['role'] ); + $roles = array_merge( $leave_roles, array( $to_update['role'] ) ); + } else { + $roles = array_merge( array( $to_update['role'] ), $leave_roles ); + } + + foreach ( $roles as $role_k ) { + $wp_user->add_role( $role_k ); + } + } + } + add_action( 'um_after_user_updated', 'um_restore_default_roles', 10, 3 ); + + /*** *** @if editing another user ***/ diff --git a/ultimate-member.php b/ultimate-member.php index 60d30255..8596e403 100644 --- a/ultimate-member.php +++ b/ultimate-member.php @@ -3,7 +3,7 @@ Plugin Name: Ultimate Member Plugin URI: http://ultimatemember.com/ Description: The easiest way to create powerful online communities and beautiful user profiles with WordPress -Version: 2.0 +Version: 2.0.1 Author: Ultimate Member Author URI: http://ultimatemember.com/ Text Domain: ultimate-member