Fix user regsiter form & user roles

This commit is contained in:
Hoang Huu 2020-02-29 13:30:32 +07:00
parent 2e48bafe16
commit 8ef01f3c21
11 changed files with 244 additions and 141 deletions

View File

@ -1,3 +1,6 @@
= 1.3.6 - 2020-02-29 =
* Fixes - User register form & User roles
= 1.3.5 - 2020-02-28 =
* Added - Time format setting
* Added - Hide unset amenities settings

View File

@ -20,13 +20,11 @@ if ( ! defined( 'ABSPATH' ) ) {
class OpalEstate_Admin_User {
/**
*
* OpalEstate_Admin_User constructor.
*/
public function __construct() {
add_action( 'cmb2_admin_init', array( $this, 'register_user_profile_metabox') );
add_action( 'personal_options', array( $this, 'show_message_user_profile' ) );
add_action( 'cmb2_admin_init', [ $this, 'register_user_profile_metabox' ] );
add_action( 'personal_options', [ $this, 'show_message_user_profile' ] );
}
/**
@ -76,61 +74,74 @@ class OpalEstate_Admin_User{
public function register_user_profile_metabox() {
global $pagenow;
if( $pagenow == 'profile.php' || $pagenow == 'user-new.php' || 'user-edit.php' ){
if ( $pagenow == 'profile.php' || $pagenow == 'user-new.php' || $pagenow == 'user-edit.php' ) {
if ( $pagenow == 'profile.php' && ! opalestate_current_user_can_access_dashboard_page() ) {
return;
}
if ( $pagenow == 'user-edit.php' ) {
$user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0;
if ( ! $user_id ) {
return;
}
if ( ! opalestate_user_has_estate_roles( $user_id ) ) {
return;
}
}
$prefix = OPALESTATE_USER_PROFILE_PREFIX;
$metabox = new Opalestate_User_MetaBox();
$box_options = array(
$box_options = [
'id' => $prefix . 'edit',
'title' => esc_html__( 'Metabox', 'opalestate-pro' ),
'object_types' => array( 'user' ),
'object_types' => [ 'user' ],
'show_names' => true,
);
];
$cmb = new_cmb2_box( $box_options );
// Setting tabs
$tabs_setting = array(
$tabs_setting = [
'config' => $box_options,
'layout' => 'vertical', // Default : horizontal
'tabs' => array()
);
'tabs' => [],
];
$tabs_setting['tabs'][] = array(
$tabs_setting['tabs'][] = [
'id' => 'p-general',
'icon' => 'dashicons-admin-home',
'title' => esc_html__( 'General', 'opalestate-pro' ),
'fields' => $this->get_base_fields()
);
'fields' => $this->get_base_fields(),
];
$tabs_setting['tabs'][] = array(
$tabs_setting['tabs'][] = [
'id' => 'p-socials',
'icon' => 'dashicons-share',
'title' => esc_html__( 'Socials', 'opalestate-pro' ),
'fields' => $metabox->get_social_fields( $prefix ),
);
];
// Set tabs
$cmb->add_field( array(
$cmb->add_field( [
'id' => '__tabs',
'type' => 'tabs',
'tabs' => $tabs_setting
) );
'tabs' => $tabs_setting,
] );
/**
* Metabox for the user profile screen
*/
$cmb_user = new_cmb2_box( array(
$cmb_user = new_cmb2_box( [
'id' => $prefix . 'edit',
'title' => esc_html__( 'User Profile Metabox', 'cmb2' ), // Doesn't output for user boxes
'object_types' => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
'object_types' => [ 'user' ], // Tells CMB2 to use user_meta vs post_meta
'show_names' => true,
'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
) );
] );
$fields = $this->extra_info_fields();
foreach ( $fields as $field ) {
@ -151,6 +162,7 @@ class OpalEstate_Admin_User{
return $fields;
}
/**
*
*/
@ -159,24 +171,24 @@ class OpalEstate_Admin_User{
$prefix = OPALESTATE_USER_PROFILE_PREFIX;
$management = array();
$management = [];
$admin_fields = array();
$admin_fields[] = array(
$admin_fields = [];
$admin_fields[] = [
'id' => "{$prefix}block_submission",
'name' => esc_html__( 'Block Submssion', 'opalestate-pro' ),
'type' => 'checkbox',
'description' => esc_html__( 'Disable Submssion Functions to not allow submit property', 'opalestate-pro' ),
'before_row' => '<hr>'
'before_row' => '<hr>',
);
$admin_fields[] = array(
];
$admin_fields[] = [
'id' => "{$prefix}block_submission_msg",
'name' => esc_html__( 'Block Submssion Message', 'opalestate-pro' ),
'type' => 'textarea',
'description' => esc_html__( 'Show message for disabled user', 'opalestate-pro' ),
);
];
$management = array_merge_recursive( $admin_fields, $management );

View File

@ -34,7 +34,6 @@ class Opalestate_User_MetaBox {
'type' => 'uploader',
'single' => 1,
'limit' => 1,
],
[
'id' => 'first_name',
@ -65,7 +64,7 @@ class Opalestate_User_MetaBox {
}
public function get_avatar_fields( $prefix ) {
return [
return apply_filters( 'opalestate_get_user_matabox_avatar_fields', [
[
'name' => esc_html__( 'Avatar Picture', 'opalestate-pro' ),
'desc' => esc_html__( 'This image will display in user detail and profile box information', 'opalestate-pro' ),
@ -74,7 +73,7 @@ class Opalestate_User_MetaBox {
'avatar' => true,
],
];
] );
}
public function get_address_fields( $prefix ) {

View File

@ -188,7 +188,7 @@ class OpalEstate_User_Search {
*
*/
public function dashboard_menu( $menu ) {
if ( 'on' === opalestate_get_option( 'enable_dashboard_savedsearch', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'savedsearch' ) && 'on' === opalestate_get_option( 'enable_dashboard_savedsearch', 'on' ) ) {
$menu['savedsearch'] = [
'icon' => 'fa fa-search',
'link' => 'saved_search',

View File

@ -63,7 +63,6 @@ class OpalEstate_User {
}
$this->enable_extra_profile = opalestate_options( 'enable_extra_profile', 'on' );
add_action( 'init', [ $this, 'process_frontend_submit' ], 99999 );
add_action( 'cmb2_render_text_password', [ $this, 'cmb2_render_text_password' ], 10, 5 );
@ -160,9 +159,9 @@ class OpalEstate_User {
public function disable() {
if ( ! current_user_can( 'manage_options' ) ) {
add_action( 'wp_before_admin_bar_render', [ $this, 'disable_profile_page' ] );
add_action( 'admin_init', [ $this, 'disable_profile_page' ] );
add_filter( 'show_admin_bar', '__return_false' );
// add_action( 'wp_before_admin_bar_render', [ $this, 'disable_profile_page' ] );
// add_action( 'admin_init', [ $this, 'disable_profile_page' ] );
add_filter( 'show_admin_bar', [ $this, 'disable_admin_bar' ] );
}
}
@ -170,6 +169,19 @@ class OpalEstate_User {
add_action( 'opalestate_user_content_profile_page', [ $this, 'user_profile' ] );
}
public function disable_admin_bar( $show_admin_bar ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$roles = $current_user->roles;
if ( in_array( 'opalestate_agent', $roles ) || in_array( 'opalestate_agency', $roles ) ) {
return false;
}
}
return $show_admin_bar;
}
/**
*
*/
@ -303,7 +315,7 @@ class OpalEstate_User {
public static function get_user_types() {
return apply_filters( 'opalestate_usertypes', [
'none' => esc_html__( 'Subscriber', 'opalestate-pro' ),
'subscriber' => esc_html__( 'Subscriber', 'opalestate-pro' ),
'opalestate_agent' => esc_html__( 'Agent', 'opalestate-pro' ),
'opalestate_agency' => esc_html__( 'Agency', 'opalestate-pro' ),
] );

View File

@ -70,6 +70,13 @@ function opalestate_get_user_tab_uri( $tab ) {
function opalestate_management_show_content_page_tab() {
$tab = isset( $_GET['tab'] ) && $_GET['tab'] ? sanitize_text_field( $_GET['tab'] ) : 'dashboard';
if ( ! opalestate_current_user_can_access_dashboard_page( $tab ) ) {
echo opalestate_load_template_path( 'user/error' );
return;
}
$tab_hook = $tab;
$tab_hook = apply_filters( 'opalestate_user_content_tab_hook', $tab_hook, $tab );
$fnc = 'opalestate_user_content_' . $tab_hook . '_page';
@ -122,9 +129,8 @@ function opalestate_my_account_page( $id = false, $args = [] ) {
}
function opalestate_submssion_page( $id = false, $args = [] ) {
$page = get_permalink( opalestate_get_option( 'submission_page', '/' ) );
if ( $id ) {
$edit_page_id = opalestate_get_option( 'submission_edit_page' );
$page = $edit_page_id ? get_permalink( $edit_page_id ) : $page;
@ -177,12 +183,12 @@ function opalestate_get_user_dashboard_menus() {
$menu['dashboard'] = [
'icon' => 'fas fa-chart-line',
'link' => 'dashboard',
'link' => opalestate_current_user_can_access_dashboard_page( 'dashboard' ) ? 'dashboard' : get_dashboard_url(),
'title' => esc_html__( 'Dashboard', 'opalestate-pro' ),
'id' => isset( $opalestate_options['profile_page'] ) ? $opalestate_options['profile_page'] : 0,
];
if ( 'on' === opalestate_get_option( 'enable_dashboard_profile', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'profile' ) && 'on' === opalestate_get_option( 'enable_dashboard_profile', 'on' ) ) {
$menu['profile'] = [
'icon' => 'far fa-user',
'link' => 'profile',
@ -191,7 +197,7 @@ function opalestate_get_user_dashboard_menus() {
];
}
if ( 'on' === opalestate_get_option( 'enable_dashboard_favorite', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'favorite' ) && 'on' === opalestate_get_option( 'enable_dashboard_favorite', 'on' ) ) {
$menu['favorite'] = [
'icon' => 'far fa-heart',
'link' => 'favorite',
@ -200,7 +206,7 @@ function opalestate_get_user_dashboard_menus() {
];
}
if ( 'on' === opalestate_get_option( 'enable_dashboard_reviews', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'reviews' ) && 'on' === opalestate_get_option( 'enable_dashboard_reviews', 'on' ) ) {
$menu['reviews'] = [
'icon' => 'far fa-star',
'link' => 'reviews',
@ -209,7 +215,7 @@ function opalestate_get_user_dashboard_menus() {
];
}
if ( 'on' === opalestate_get_option( 'message_log', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'messages' ) && 'on' === opalestate_get_option( 'message_log', 'on' ) ) {
$menu['messages'] = [
'icon' => 'fa fa-envelope',
'link' => 'messages',
@ -218,7 +224,7 @@ function opalestate_get_user_dashboard_menus() {
];
}
if ( 'on' === opalestate_get_option( 'enable_dashboard_submission', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'submission' ) && 'on' === opalestate_get_option( 'enable_dashboard_submission', 'on' ) ) {
$menu['submission'] = [
'icon' => 'fa fa-upload',
'link' => 'submission',
@ -227,7 +233,7 @@ function opalestate_get_user_dashboard_menus() {
];
}
if ( 'on' === opalestate_get_option( 'enable_dashboard_properties', 'on' ) ) {
if ( opalestate_current_user_can_access_dashboard_page( 'myproperties' ) && 'on' === opalestate_get_option( 'enable_dashboard_properties', 'on' ) ) {
$statistics = new OpalEstate_User_Statistics();
$menu['myproperties'] = [
@ -308,3 +314,59 @@ if ( ! function_exists( 'opalestate_create_user' ) ) {
function opalestate_get_user_meta( $user_id, $key, $single = true ) {
return get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $key, $single );
}
/**
* Current user can access dashboard page?
*
* @param $page
* @return bool
*/
function opalestate_current_user_can_access_dashboard_page( $page = '' ) {
if ( ! is_user_logged_in() ) {
return false;
}
$current_user = wp_get_current_user();
$roles = $current_user->roles;
$allowd_roles = opalestate_get_allowed_roles();
foreach ( $roles as $role ) {
if ( in_array( $role, $allowd_roles ) ) {
return apply_filters( 'opalestate_opalestate_user_can_access', true, $role, $page );
}
}
return false;
}
/**
* User has estate roles?
*
* @param $user_id
* @return bool
*/
function opalestate_user_has_estate_roles( $user_id ) {
$user_meta = get_userdata( $user_id );
$roles = $user_meta->roles;
$allowd_roles = opalestate_get_allowed_roles();
foreach ( $roles as $role ) {
if ( in_array( $role, $allowd_roles ) ) {
return true;
}
}
return false;
}
/**
* Get allowed roles for dashboard page.
*/
function opalestate_get_allowed_roles() {
return apply_filters( 'opalestate_get_allowed_roles', [
'opalestate_agent',
'opalestate_agency',
'opalestate_manager',
'administrator',
] );
}

View File

@ -290,20 +290,26 @@ class OpalEstate_Membership {
public static function membership_menu( $menu ) {
if ( function_exists( "opalmembership_get_dashdoard_page_uri" ) ) {
global $opalmembership_options;
if ( opalestate_current_user_can_access_dashboard_page( 'membership' ) ) {
$menu['membership'] = [
'icon' => 'fa fa-user',
'link' => opalmembership_get_dashdoard_page_uri(),
'title' => esc_html__( 'My Membership', 'opalestate-pro' ),
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
}
if ( opalestate_current_user_can_access_dashboard_page( 'membership_history' ) ) {
$menu['membership_history'] = [
'icon' => 'fa fa-user',
'link' => opalmembership_get_payment_history_page_uri(),
'title' => esc_html__( 'My Invoices', 'opalestate-pro' ),
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
}
if ( opalestate_current_user_can_access_dashboard_page( 'packages' ) ) {
$menu['packages'] = [
'icon' => 'fa fa-certificate',
'link' => opalmembership_get_membership_page_uri(),
@ -311,6 +317,7 @@ class OpalEstate_Membership {
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
}
}
return $menu;
}

View File

@ -3,7 +3,7 @@
* Plugin Name: Opal Estate Pro
* Plugin URI: https://wpdocs.gitbook.io/opal-estate/
* Description: Opal Real Estate Plugin is an ideal solution and brilliant choice for you to set up a professional estate website.
* Version: 1.3.5
* Version: 1.3.6
* Author: WPOPAL
* Author URI: http://www.wpopal.com
* Requires at least: 4.9
@ -150,7 +150,7 @@ if ( ! class_exists( 'OpalEstate' ) ) {
*/
public function __clone() {
// Cloning instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'opalestate-pro' ), '1.3.5' );
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'opalestate-pro' ), '1.3.6' );
}
/**
@ -159,7 +159,7 @@ if ( ! class_exists( 'OpalEstate' ) ) {
public function setup_constants() {
// Plugin version
if ( ! defined( 'OPALESTATE_VERSION' ) ) {
define( 'OPALESTATE_VERSION', '1.3.5' );
define( 'OPALESTATE_VERSION', '1.3.6' );
}
// Plugin Folder Path

View File

@ -4,7 +4,7 @@ Donate link: https://wpdocs.gitbook.io/opal-estate/
Tags: estate, property, opalestate, house for rent, agency for lease, estate submission, agents estate property, property marketplace
Requires at least: 4.9
Tested up to: 5.3.2
Stable tag: 1.3.5
Stable tag: 1.3.6
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
@ -156,6 +156,9 @@ This section describes how to install the plugin and get it working.
* System tickets support 24/7 available : [free support](https://wpopal.ticksy.com/ "Visit the Plugin support Page")
== Changelog ==
= 1.3.6 - 2020-02-29 =
* Fixes - User register form & User roles
= 1.3.5 - 2020-02-28 =
* Added - Time format setting
* Added - Hide unset amenities settings

4
templates/user/error.php Normal file
View File

@ -0,0 +1,4 @@
<div class="opalestate-user-dashboard-error">
<h4><?php esc_html_e( 'You need a higher level of permission or this page is denied with your user role.', 'opalestate-pro' ) ?></h4>
<p><?php esc_html_e( 'Sorry, you are not allowed to this page.', 'opalestate-pro' ); ?></p>
</div>

View File

@ -25,6 +25,7 @@ if ( is_user_logged_in() ) {
}
$types = OpalEstate_User::get_user_types();
$type_default = apply_filters( 'opalestate_register_form_default_type', 'subscriber' );
?>
<div class="opalesate-form">
<?php if ( $hide_title === false ) : ?>
@ -69,7 +70,7 @@ $types = OpalEstate_User::get_user_types();
<label for="userrole"><?php esc_html_e( 'Type', 'opalestate-pro' ); ?> <span class="required">*</span></label>
<select name="role" id="userrole" class="form-control">
<?php foreach ( $types as $type => $label ): ?>
<option value="<?php echo $type; ?>"><?php echo $label; ?></option>
<option value="<?php echo $type; ?>" <?php selected( $type, $type_default, true ); ?>><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
</p>