Uname: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

403WebShell
403Webshell
Server IP : 13.126.101.145  /  Your IP : 216.73.217.37
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 :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/rentals_updated/wp-content/plugins/role-management/custom-fields.php
<?php
function add_custom_user_fields($user) {
    // Fetch the saved value from user_meta for the user being edited
    $saved_location = get_user_meta($user->ID, 'ba_region_location', true);
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // Only show this for users with the administrator or central_administrator role
    if (in_array('administrator', $user_roles) || user_has_role('central_administrator')) {
        ?>
        <script>
            jQuery(document).ready(function($) {
                $('#role').change(function() {
                    var selectedRole = $(this).val();
                    if (selectedRole === 'rue_manager' || selectedRole === 'sales_representative' || selectedRole === 'commercial_representative' || selectedRole === 'operation_representative') {
                        $('#admin_location_field').show();
                    } else {
                        $('#admin_location_field').hide();
                    }
                });

                // On page load, check initial value
                var initialRole = $('#role').val();
                if (initialRole === 'rue_manager' || initialRole === 'sales_representative' || initialRole === 'commercial_representative' || initialRole === 'operation_representative') {
                    $('#admin_location_field').show();
                } else {
                    $('#admin_location_field').hide();
                }
            });
        </script>

        <table class="form-table">
            <tr id="admin_location_field" style="display: none;">
                <th><label for="admin_location">Regional Location</label></th>
                <td>
                    <select name="admin_location" id="admin_location">
                        <option value="north" <?php selected($saved_location, 'north'); ?>>North</option>
                        <option value="south" <?php selected($saved_location, 'south'); ?>>South</option>
                        <option value="east" <?php selected($saved_location, 'east'); ?>>East</option>
                        <option value="west" <?php selected($saved_location, 'west'); ?>>West</option>
                    </select>
                </td>
            </tr>
        </table>
        <?php
    }
}

function save_custom_user_fields($user_id) {
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    if (isset($_POST['admin_location'])) {
        update_user_meta($user_id, 'ba_region_location', sanitize_text_field($_POST['admin_location']));
    }

    // Save Employee ID
    if (isset($_POST['employee_id'])) {
        update_user_meta($user_id, 'employee_id', sanitize_text_field($_POST['employee_id']));
    }
}

// Save custom fields for new user registration
function save_custom_user_fields_for_new_user($user_id) {
    if (isset($_POST['admin_location'])) {
        update_user_meta($user_id, 'ba_region_location', sanitize_text_field($_POST['admin_location']));
    }
}

// Add custom fields to new user form
function add_custom_user_fields_for_new_user() {
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // Only show this for users with the administrator or central_administrator role
    if (in_array('administrator', $user_roles) || user_has_role('central_administrator')) {
        ?>
        <script>
            jQuery(document).ready(function($) {
                $('#role').change(function() {
                    var selectedRole = $(this).val();
                    if (selectedRole === 'rue_manager' || selectedRole === 'sales_representative' || selectedRole === 'commercial_representative'  || selectedRole === 'operation_representative') {
                        $('#admin_location_field').show();
                    } else {
                        $('#admin_location_field').hide();
                    }
                    // Show/Hide employee ID field if role is sales_representative
                    if (selectedRole === 'sales_representative') {
                        $('#employee_id_field').show();
                    } else {
                        $('#employee_id_field').hide();
                    }
                });

                // Initially hide the field
                $('#admin_location_field').hide();
                $('#employee_id_field').hide();
            });
        </script>
        <table class="form-table">
            <tr id="admin_location_field" style="display: none;">
                <th><label for="admin_location">Regional Location</label></th>
                <td>
                    <select name="admin_location" id="admin_location">
                        <option value="north">North</option>
                        <option value="south">South</option>
                        <option value="east">East</option>
                        <option value="west">West</option>
                    </select>
                </td>
            </tr>
            <!-- Employee ID Field -->
            <tr id="employee_id_field" style="display: none;">
                <th><label for="employee_id">Employee ID</label></th>
                <td>
                    <input type="text" name="employee_id" id="employee_id" class="regular-text" />
                </td>
            </tr>
        </table>
        <?php
    }
}

// Hook functions to appropriate actions
add_action('show_user_profile', 'add_custom_user_fields');
add_action('edit_user_profile', 'add_custom_user_fields');
add_action('personal_options_update', 'save_custom_user_fields');
add_action('edit_user_profile_update', 'save_custom_user_fields');
add_action('user_new_form', 'add_custom_user_fields_for_new_user');
add_action('user_register', 'save_custom_user_fields_for_new_user');


// add role field
function add_create_role_button() {
    $screen = get_current_screen();
    if ($screen->id !== 'users') {
        return;
    }

    // Check if the current user has the 'administrator' or 'centraladministrator' role
    if (!current_user_can('administrator') && !user_has_role('central_administrator')) {
        return;
    }

    ?>
    <style>
        .create-role-button {
            margin-left: 10px;
            background-color: #007cba;
            border-color: #007cba;
            color: #ffffff;
            text-decoration: none;
            padding: 5px 10px;
            border-radius: 3px;
            font-size: 13px;
        }
        /* Modal styles */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgb(0,0,0);
            background-color: rgba(0,0,0,0.4);
            padding-top: 60px;
        }
        .modal-content {
            background-color: #fefefe;
            margin: 5% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 400px;
            border-radius: 5px;
            box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-body {
            margin-top: 20px;
        }
        .modal-footer {
            text-align: left;
        }
        #createRoleForm label {
            color: #000;
            font-size: 16px;
        }
        #createRoleForm #roleName {
            margin: 10px 0px 20px 0px;
            width: 100%;
            box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
            border: 1px solid #ffbd2b;
        }
        .message {
            margin-top: 10px;
            padding: 10px;
            border-radius: 3px;
        }
        .message.success {
            background-color: #dff0d8;
            border-color: #d6e9c6;
            color: #3c763d;
        }
        .message.error {
            background-color: #f2dede;
            border-color: #ebccd1;
            color: #a94442;
        }
    </style>
    <script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function() {
    var addUserButton = document.querySelector('.page-title-action');
    if (addUserButton) {
        var createRoleButton = document.createElement('a');
        createRoleButton.href = '#';
        createRoleButton.className = 'page-title-action create-role-button';
        createRoleButton.style.display = 'none';
        createRoleButton.textContent = 'Create Role';
        createRoleButton.onclick = function(e) {
            e.preventDefault();
            document.getElementById('createRoleModal').style.display = 'block';
        };
        addUserButton.parentNode.insertBefore(createRoleButton, addUserButton.nextSibling);
    }

    var modal = document.getElementById('createRoleModal');
    var span = document.getElementsByClassName('close')[0];

    function closeModal() {
        modal.style.display = 'none';
        // Clear the form field and message when modal is closed
        var form = document.getElementById('createRoleForm');
        form.reset();
        document.getElementById('roleMessage').innerHTML = '';
    }

    span.onclick = function() {
        closeModal();
    }

    window.onclick = function(event) {
        if (event.target == modal) {
            closeModal();
        }
    }

    document.getElementById('createRoleForm').onsubmit = function(e) {
        e.preventDefault();
        var roleName = document.getElementById('roleName').value;
        var messageDiv = document.getElementById('roleMessage');
        messageDiv.innerHTML = ''; // Clear previous messages
        if (roleName) {
            var data = {
                'action': 'create_role',
                'roleName': roleName,
            };

            jQuery.post(ajaxurl, data, function(response) {
                if (response.success) {
                    messageDiv.className = 'message success';
                    messageDiv.textContent = response.data;
                } else {
                    messageDiv.className = 'message error';
                    messageDiv.textContent = 'Error: ' + response.data;
                }
                // Keep the modal open for a few seconds to show the message, then close it and reload the page
                setTimeout(function() {
                    closeModal();
                    window.location.reload(); // Reload the page after closing the modal
                }, 3000);
            });
        }
    }
});


    </script>
    <!-- The Modal -->
    <div id="createRoleModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Create New Role</h2>
                <span class="close">&times;</span>
            </div>
            <div class="modal-body">
                <form id="createRoleForm">
                    <label for="roleName">Role Name:</label><br>
                    <input type="text" id="roleName" name="roleName" required>
                    <div id="roleMessage"></div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="submit" class="button button-primary" form="createRoleForm">Create Role</button>
            </div>
        </div>
    </div>
    <?php
}

function user_has_role($role) {
    $user = wp_get_current_user();
    return in_array($role, $user->roles);
}

add_action('admin_footer', 'add_create_role_button');


function handle_create_role() {
    if (!current_user_can('manage_options')) {
        wp_send_json_error('Permission denied');
    }

    if (!isset($_POST['roleName'])) {
        wp_send_json_error('Role name is required');
    }

    $role_name = sanitize_text_field($_POST['roleName']);
    
    // Check if the role already exists
    if (get_role($role_name)) {
        wp_send_json_error('Role already exists');
    }

    if (add_role($role_name, ucfirst($role_name), array(
        'read' => true,
    ))) {
        wp_send_json_success('Role created successfully');
    } else {
        wp_send_json_error('Role creation failed');
    }
}
add_action('wp_ajax_create_role', 'handle_create_role');

// region wise sales rep
// 1. Add region field for new sales_representative user, but automatically set based on rue_manager's region
function add_custom_user_fields_for_new_sales_rep() {
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // Only show this for users with the rue_manager role
    if (in_array('rue_manager', $user_roles)) {
        $rue_manager_region = get_user_meta($current_user->ID, 'ba_region_location', true);

        ?>
        <script>
            jQuery(document).ready(function($) {
                var selectedRole = $('#role').val();
                
                if (selectedRole === 'sales_representative') {
                    // Auto-set the region and hide the dropdown
                    $('#sales_rep_region_location').val('<?php echo esc_js($rue_manager_region); ?>').prop('disabled', true);
                    $('#sales_rep_location_field').show();
                } else {
                    $('#sales_rep_location_field').hide();
                }
                
                $('#role').change(function() {
                    if ($(this).val() === 'sales_representative') {
                        $('#sales_rep_region_location').val('<?php echo esc_js($rue_manager_region); ?>').prop('disabled', true);
                        $('#sales_rep_location_field').show();
                    } else {
                        $('#sales_rep_location_field').hide();
                    }
                });
            });
        </script>
        <table class="form-table">
            <tr id="sales_rep_location_field" style="display: none;">
                <th><label for="sales_rep_region_location">Regional Location</label></th>
                <td>
                    <select name="sales_rep_region_location" id="sales_rep_region_location">
                        <option value="north">North</option>
                        <option value="south">South</option>
                        <option value="east">East</option>
                        <option value="west">West</option>
                    </select>
                </td>
            </tr>
        </table>
        <?php
    }
}

// 2. Hook the function for new user creation form
add_action('user_new_form', 'add_custom_user_fields_for_new_sales_rep');

// 3. Save the region field for sales_representative during user registration
function save_custom_user_fields_for_new_sales_rep($user_id) {
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // Only enforce this when a rue_manager creates a sales_representative
    if (in_array('rue_manager', $user_roles)) {
        if (isset($_POST['role']) && $_POST['role'] === 'sales_representative') {
            $rue_manager_region = get_user_meta($current_user->ID, 'ba_region_location', true);
            update_user_meta($user_id, 'sales_rep_region_location', sanitize_text_field($rue_manager_region));
        }
    }
}

add_action('user_register', 'save_custom_user_fields_for_new_sales_rep');

// 4. Ensure that the regional location is saved when editing a sales_representative
function restrict_region_assignment_for_sales_rep($user_id) {
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // If the current user is a rue_manager and is editing a sales_representative
    if (in_array('rue_manager', $user_roles) && isset($_POST['role']) && $_POST['role'] === 'sales_representative') {
        $rue_manager_region = get_user_meta($current_user->ID, 'ba_region_location', true);
        update_user_meta($user_id, 'sales_rep_region_location', sanitize_text_field($rue_manager_region));
    }
}

add_action('edit_user_profile_update', 'restrict_region_assignment_for_sales_rep');


Youez - 2016 - github.com/yon3zu
LinuXploit