| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Accounting\Includes\Classes; |
| 4 |
|
| 5 |
/** |
| 6 |
* Loads Accounting users in admin area |
| 7 |
*/ |
| 8 |
class User_Profile { |
| 9 |
|
| 10 |
/** |
| 11 |
* The HR users admin loader |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
$this->setup_actions(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Setup the admin hooks, actions and filters |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function setup_actions() { |
| 23 |
|
| 24 |
// Bail if in network admin |
| 25 |
if ( is_network_admin() ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// User profile edit/display actions |
| 30 |
add_action( 'erp_user_profile_role', [ $this, 'role' ] ); |
| 31 |
add_action( 'erp_update_user', [ $this, 'update_user' ], 10, 1 ); |
| 32 |
} |
| 33 |
|
| 34 |
public function update_user( $user_id ) { |
| 35 |
// verify nonce |
| 36 |
if ( ! isset( $_REQUEST['_erp_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_erp_nonce'] ), 'user_profile_update_role' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// AC role we want the user to have |
| 41 |
$new_role = isset( $_POST['ac_manager'] ) ? sanitize_text_field( wp_unslash( $_POST['ac_manager'] ) ) : false; |
| 42 |
|
| 43 |
if ( ! $new_role ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Bail if current user cannot promote the passing user |
| 48 |
if ( ! current_user_can( 'promote_user', $user_id ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Set the new HR role |
| 53 |
$user = get_user_by( 'id', $user_id ); |
| 54 |
|
| 55 |
if ( $new_role ) { |
| 56 |
$user->add_role( $new_role ); |
| 57 |
} else { |
| 58 |
$user->remove_role( erp_ac_get_manager_role() ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public function role( $profileuser ) { |
| 63 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
$checked = in_array( erp_ac_get_manager_role(), $profileuser->roles, true ) ? 'checked' : ''; ?> |
| 67 |
<label for="erp-ac-manager"> |
| 68 |
<input type="checkbox" id="erp-ac-manager" <?php echo esc_attr( $checked ); ?> name="ac_manager" value="<?php echo esc_attr( erp_ac_get_manager_role() ); ?>"> |
| 69 |
<span class="description"><?php esc_html_e( 'Accounting Manager', 'erp' ); ?></span> |
| 70 |
</label> |
| 71 |
|
| 72 |
<?php |
| 73 |
} |
| 74 |
} |
| 75 |
|