| Server IP : 13.126.101.145 / Your IP : 216.73.216.159 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/role-management/ |
Upload File : |
<?php
/*
Plugin Name: Custom Role Manager
Description: Manage custom roles and permissions.
Version: 1.0
Author: Itrosys
*/
// For activation and deactivation of plugin
register_activation_hook( __FILE__, 'custom_role_manager_activate' );
function custom_role_manager_activate() {
create_central_admin_role();
}
// Include custom fields file
require_once plugin_dir_path( __FILE__ ) . '/custom-fields.php';
// Create custom roles on init
add_action('init', 'create_central_admin_role');
function create_central_admin_role() {
// Add Central Administrator role
add_role(
'central_administrator',
'Central Administrator',
get_role('administrator')->capabilities
);
// Add RUE Manager role
add_role(
'rue_manager',
'RUE Manager',
get_role('administrator')->capabilities
);
add_role(
'sales_representative',
'Sales Representative',
get_role('administrator')->capabilities
);
add_role(
'operation_representative',
'Operation Representative',
get_role('administrator')->capabilities
);
add_role(
'commercial_representative',
'Commercial Representative',
get_role('administrator')->capabilities
);
}
// Modify editable roles based on current user role
add_filter('editable_roles', 'modify_editable_roles');
function modify_editable_roles($roles) {
$current_user = wp_get_current_user();
// For Central Administrator
if (current_user_can('central_administrator')) {
$keep_roles = array('customer', 'rue_manager','operator', 'sales_representative','operation_representative','commercial_representative');
foreach ($roles as $role_key => $role_data) {
if (!in_array($role_key, $keep_roles)) {
unset($roles[$role_key]);
}
}
}
// For RUE Manager or BA Rentals
if (in_array('rue_manager', (array) $current_user->roles) ) {
$allowed_roles = array('customer','operator','sales_representative','operation_representative','commercial_representative');
foreach ($roles as $role_key => $role_data) {
if (!in_array($role_key, $allowed_roles)) {
unset($roles[$role_key]);
}
}
}
if (in_array('sales_representative', (array) $current_user->roles) ) {
$allowed_roles = array('customer');
foreach ($roles as $role_key => $role_data) {
if (!in_array($role_key, $allowed_roles)) {
unset($roles[$role_key]);
}
}
}
return $roles;
}
// Filter users based on role
add_action('pre_get_users', 'filter_users_by_role');
function filter_users_by_role($query) {
global $pagenow;
if (is_admin() && $pagenow == 'users.php') {
$current_user = wp_get_current_user();
if (current_user_can('central_administrator')) {
$keep_roles = array('customer', 'rue_manager',
'operator','sales_representative','operation_representative','commercial_representative');
$query->set('role__in', $keep_roles);
}
elseif (in_array('rue_manager', (array) $current_user->roles)) {
// Default roles to keep for rue_manager
$keep_roles = array('customer', 'operator', 'sales_representative', 'operation_representative', 'commercial_representative');
// Check if the user is a north rue manager
if (in_array('rue_manager', (array) $current_user->roles)) {
// Get the current user's ba_region_location
$user_region = get_user_meta($current_user->ID, 'ba_region_location', true);
// If the user's region is north, limit the roles to sales representative with a specific region
if ($user_region === 'north') {
$keep_roles = array('sales_representative');
// Add a meta query to filter by sales_rep_region_location meta key
$meta_query = array(
array(
'key' => 'sales_rep_region_location', // The usermeta key to check
'value' => 'north', // The value to match
'compare' => '=' // Comparison operator
),
);
$query->set('meta_query', $meta_query); // Set the meta query
}
}
// Set the roles in the query
$query->set('role__in', $keep_roles);
}
}
}
// Hide role dropdown for RUE Manager and BA Rentals
add_action('admin_footer', 'hide_role_dropdown_for_ba_roles');
function hide_role_dropdown_for_ba_roles() {
global $pagenow;
if (is_admin() && $pagenow == 'users.php') {
$current_user = wp_get_current_user();
if (in_array('rue_manager', (array) $current_user->roles)) {
echo '<style>#new_role, #changeit, #new_role2, #changeit2 { display: none; }</style>';
}
}
}
// Add a meta box for product region
add_action('add_meta_boxes', 'add_product_region_meta_box');
function add_product_region_meta_box() {
add_meta_box(
'product_region_meta_box',
__('Product Region', 'woocommerce'),
'display_product_region_meta_box',
'product',
'side',
'high'
);
}
// Display the product region meta box
function display_product_region_meta_box($post) {
$product_region = get_post_meta($post->ID, '_product_region', true);
?>
<p>
<label for="product_region"><?php _e('Product Region:', 'woocommerce'); ?></label>
<select id="product_region" name="product_region" required>
<option value="North" <?php selected($product_region, 'North'); ?>><?php _e('North', 'woocommerce'); ?></option>
<option value="South" <?php selected($product_region, 'South'); ?>><?php _e('South', 'woocommerce'); ?></option>
<option value="East" <?php selected($product_region, 'East'); ?>><?php _e('East', 'woocommerce'); ?></option>
<option value="West" <?php selected($product_region, 'West'); ?>><?php _e('West', 'woocommerce'); ?></option>
</select>
</p>
<?php
}
// Save the product region meta box data
add_action('save_post', 'save_product_region_meta_box');
function save_product_region_meta_box($post_id) {
if (isset($_POST['product_region'])) {
update_post_meta($post_id, '_product_region', sanitize_text_field($_POST['product_region']));
}
}
// Filter products based on user role and product region
add_action('pre_get_posts', 'filter_products_by_role_and_region');
function filter_products_by_role_and_region($query) {
if (!is_admin() || !$query->is_main_query()) {
return;
}
$user = wp_get_current_user();
if (in_array('rue_manager', (array) $user->roles) ) {
$user_region = get_user_meta($user->ID, 'ba_region_location', true);
if (!empty($user_region)) {
$meta_query = array(
array(
'key' => '_product_region',
'value' => $user_region,
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
}
}
}
// Customize user counts based on role
// add_filter('views_users', 'customize_user_counts_based_on_role');
// function customize_user_counts_based_on_role($views) {
// $current_user = wp_get_current_user();
// if (in_array('central_adminstrator', (array) $current_user->roles)) {
// $ba_rentals_count = count_users()['avail_roles']['ba_rentals'];
// unset($views['administrator']);
// unset($views['contributor']);
// unset($views['rue_manager']);
// unset($views['ba_rentals']);
// unset($views['subscriber']);
// unset($views['shop_manager']);
// unset($views['central_administrator']);
// $views['all'] = sprintf( __( 'All <span class="count">(%s)</span>' ), $ba_rentals_count );
// }
// // For RUE Manager role
// if (in_array('ba_rentals', (array) $current_user->roles)) {
// $ba_rentals_count = count_users()['avail_roles']['ba_rentals'];
// unset($views['administrator']);
// unset($views['contributor']);
// unset($views['rue_manager']);
// unset($views['ba_rentals']);
// unset($views['subscriber']);
// unset($views['shop_manager']);
// unset($views['central_administrator']);
// $views['all'] = sprintf( __( 'All <span class="count">(%s)</span>'), $ba_rentals_count );
// }
// if (in_array('rue_manager', (array) $current_user->roles)) {
// $rue_manager_count = count_users()['avail_roles']['ba_rentals'];
// unset($views['administrator']);
// unset($views['contributor']);
// unset($views['rue_manager']);
// unset($views['ba_rentals']);
// unset($views['subscriber']);
// unset($views['shop_manager']);
// unset($views['central_administrator']);
// $views['all'] = sprintf( __( 'All <span class="count">(%s)</span>' ), $rue_manager_count );
// }
// return $views;
// }
// Remove unwanted role counts from the top of the Users page
add_filter('views_users', 'filter_users_role_counts');
function filter_users_role_counts($views) {
global $current_user;
$current_screen = get_current_screen();
if ($current_screen->id === 'users') {
// Define roles to keep based on current user role
$roles_to_keep = array();
$formatted_roles = array(
'customer' => 'Customer',
'rue_manager' => 'RUE Manager',
'operator' => 'Operator'
);
if (current_user_can('central_administrator')) {
$roles_to_keep = array('customer', 'rue_manager', 'operator','sales_representative','operation_representative','commercial_representative');
} elseif (in_array('rue_manager', (array) $current_user->roles) ) {
$roles_to_keep = array('customer', 'operator','sales_representative','operation_representative','commercial_representative');
}
elseif (in_array('sales_representative', (array) $current_user->roles)) {
$roles_to_keep = array('customer');
}
if (!empty($roles_to_keep)) {
// Remove existing views
$views = array();
// Get total count of users with the roles to keep
$all_users_count = count_users();
$total_count = 0;
foreach ($roles_to_keep as $role) {
if (isset($all_users_count['avail_roles'][$role])) {
$total_count += $all_users_count['avail_roles'][$role];
}
}
// Add "All Users" count
$views['all'] = '<a href="' . esc_url(admin_url('users.php')) . '">All Users <span class="count">(' . $total_count . ')</span></a>';
// Add count for each role with formatted name
foreach ($roles_to_keep as $role) {
$role_name = isset($formatted_roles[$role]) ? $formatted_roles[$role] : ucfirst($role);
$role_count = isset($all_users_count['avail_roles'][$role]) ? $all_users_count['avail_roles'][$role] : 0;
$views[$role] = '<a href="' . esc_url(add_query_arg('role', $role, admin_url('users.php'))) . '">' . $role_name . ' <span class="count">(' . $role_count . ')</span></a>';
}
}
}
return $views;
}
// 28-12-2024
// remove send reset password from users listing for rue manager
add_filter('user_row_actions', 'hide_reset_password_action_for_rue_manager', 10, 2);
function hide_reset_password_action_for_rue_manager($actions, $user) {
$current_user = wp_get_current_user();
// Check if the logged-in user is a 'central_administrator'
if (in_array('central_administrator', $current_user->roles)) {
// Check if the user being listed has the 'rue_manager',sales_representative,operation_representative,commercial_representative role
$target_roles = ['rue_manager', 'sales_representative', 'operation_representative', 'commercial_representative'];
if (array_intersect($target_roles, $user->roles)) {
// Remove the "Send Reset Password" action
if (isset($actions['resetpassword'])) {
unset($actions['resetpassword']);
}
}
}
return $actions;
}
// remove account management section(password reset) from user edit page
add_action('admin_footer-user-edit.php', 'hide_account_management_for_rue_manager');
function hide_account_management_for_rue_manager() {
// Get the current logged-in user
$current_user = wp_get_current_user();
// Check if the logged-in user is a 'central_administrator'
if (!in_array('central_administrator', $current_user->roles)) {
return;
}
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
if ($user_id) {
$user = get_userdata($user_id);
// Check if the user being edited has the 'rue_manager',sales_representative,operation_representative,commercial_representative role
$targeted_roles = ['rue_manager', 'sales_representative', 'operation_representative', 'commercial_representative'];
if (array_intersect($targeted_roles, $user->roles)) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Hide the Account Management section
$('h2:contains("Account Management")').hide();
$('#password, .user-pass1-wrap, .user-pass2-wrap, .pw-weak, .pw-meter-message').closest('.form-table').hide();
});
</script>
<?php
}
}
}
function restrict_central_admin_login_redirect() {
if (strpos($_SERVER['REQUEST_URI'], '/my-account/') !== false) {
$current_user = wp_get_current_user();
$restricted_roles = ['central_administrator', 'rue_manager','sales_representative','operation_representative','commercial_representative','operator'];
if (array_intersect($restricted_roles, (array) $current_user->roles)) {
wp_redirect(admin_url());
exit;
}
}
}
add_action('template_redirect', 'restrict_central_admin_login_redirect');
function prevent_my_account_login_for_central_admin($user, $username, $password) {
if (strpos($_SERVER['REQUEST_URI'], '/my-account/') !== false) {
$restricted_roles = ['central_administrator', 'rue_manager','sales_representative','operation_representative','commercial_representative','operator'];
if (is_wp_error($user)) {
return $user;
}
if ($user && array_intersect($restricted_roles, (array) $user->roles)) {
return new WP_Error(
'restricted_login',
__('You are not allowed to login through the customer portal.')
);
}
}
return $user;
}
add_filter('authenticate', 'prevent_my_account_login_for_central_admin', 30, 3);
function hide_personal_options_except_administrator() {
$current_user = wp_get_current_user();
if (!in_array('administrator', $current_user->roles)) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('h2:contains("Personal Options")').hide();
$('#your-profile .form-table:first').hide();
});
</script>
<?php
}
}
add_action('admin_footer', 'hide_personal_options_except_administrator');
add_action('pre_get_posts', 'filter_orders_by_ba_region');
function filter_orders_by_ba_region($query) {
if (!is_admin() || !$query->is_main_query() || get_current_screen()->id !== 'edit-shop_order') {
return;
}
$user = wp_get_current_user();
if (in_array('rue_manager', (array) $user->roles)) {
// Get the user's assigned region
$user_region = get_user_meta($user->ID, 'ba_region_location', true);
// If a region is assigned, filter orders by that region
if (!empty($user_region)) {
$meta_query = array(
array(
'key' => 'order_region',
'value' => $user_region,
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
}
}
}
?>