| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\HRM\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* Loads HR users admin area |
| 7 |
* |
| 8 |
* @package WP-ERP\HR |
| 9 |
* @subpackage Administration |
| 10 |
*/ |
| 11 |
class User_Profile { |
| 12 |
|
| 13 |
/** |
| 14 |
* The HR users admin loader |
| 15 |
* |
| 16 |
* @package WP-ERP\HR |
| 17 |
* @subpackage Administration |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
$this->setup_actions(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Setup the admin hooks, actions and filters |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
function setup_actions() { |
| 29 |
// Bail if in network admin |
| 30 |
if ( is_network_admin() ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// User profile edit/display actions |
| 35 |
add_action( 'erp_user_profile_role', array( $this, 'role' ) ); |
| 36 |
add_action( 'erp_update_user', array( $this, 'update_user' ), 10, 2 ); |
| 37 |
|
| 38 |
//notification disable checkbox |
| 39 |
add_action( 'edit_user_profile', [ $this, 'profile_settings' ] ); |
| 40 |
add_action( 'show_user_profile', [ $this, 'profile_settings' ] ); |
| 41 |
|
| 42 |
add_action( 'erp_update_user', [ $this, 'update_profile_settings' ], 10, 2 ); |
| 43 |
} |
| 44 |
|
| 45 |
function update_user( $user_id, $post ) { |
| 46 |
|
| 47 |
// HR role we want the user to have |
| 48 |
$new_hr_manager_role = isset( $post['hr_manager'] ) ? sanitize_text_field( $post['hr_manager'] ) : false; |
| 49 |
|
| 50 |
if ( ! $new_hr_manager_role ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// Bail if current user cannot promote the passing user |
| 55 |
if ( ! current_user_can( 'promote_user', $user_id ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Set the new HR role |
| 60 |
$user = get_user_by( 'id', $user_id ); |
| 61 |
|
| 62 |
if ( $new_hr_manager_role ) { |
| 63 |
$user->add_role( $new_hr_manager_role ); |
| 64 |
} else { |
| 65 |
$user->remove_role( erp_hr_get_manager_role() ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function role( $profileuser ) { |
| 70 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$checked = in_array( erp_hr_get_manager_role(), $profileuser->roles ) ? 'checked' : ''; |
| 75 |
?> |
| 76 |
<label for="erp-hr-manager"> |
| 77 |
<input type="checkbox" id="erp-hr-manager" <?php echo $checked; ?> name="hr_manager" |
| 78 |
value="<?php echo erp_hr_get_manager_role(); ?>"> |
| 79 |
<span class="description"><?php _e( 'HR Manager', 'erp' ); ?></span> |
| 80 |
</label> |
| 81 |
<?php |
| 82 |
} |
| 83 |
|
| 84 |
function profile_settings( $profileuser ) { |
| 85 |
$notification = get_user_meta( $profileuser->ID, 'erp_hr_disable_notification', true ); |
| 86 |
$checked = ! empty( $notification ) ? 'checked' : ''; |
| 87 |
?> |
| 88 |
<h3><?php _e( 'ERP Profile Settings', 'erp' ); ?></h3> |
| 89 |
<table class="form-table"> |
| 90 |
<tbody> |
| 91 |
<tr> |
| 92 |
<th><label for="erp-hr-disable-notification"><?php esc_html_e( 'Notification', 'erp' ); ?></label></th> |
| 93 |
<td> |
| 94 |
<input type="checkbox" id="erp-hr-disable-notification" <?php echo $checked; ?> |
| 95 |
name="erp_hr_disable_notification"> |
| 96 |
<span class="description"><?php _e( 'Disable WP ERP email notifications', 'erp' ); ?></span> |
| 97 |
</td> |
| 98 |
</tr> |
| 99 |
</tbody> |
| 100 |
</table> |
| 101 |
<?php |
| 102 |
} |
| 103 |
|
| 104 |
function update_profile_settings( $user_id, $posted ) { |
| 105 |
|
| 106 |
if ( current_user_can( 'edit_user', $user_id ) ) { |
| 107 |
$erp_hr_disable_notification = isset($posted['erp_hr_disable_notification'])? $posted['erp_hr_disable_notification'] : ''; |
| 108 |
update_user_meta( $user_id, 'erp_hr_disable_notification', $erp_hr_disable_notification ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|