| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handling Staff Related Hooks |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Staffs; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
use Timetics\Core\Staffs\Onboard; |
| 12 |
use Timetics\Utils\Singleton; |
| 13 |
|
| 14 |
class Hooks { |
| 15 |
use Singleton; |
| 16 |
|
| 17 |
/** |
| 18 |
* Intialie |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function init() { |
| 23 |
add_filter( 'retrieve_password_notification_email', [$this, 'reset_email_content'], 10, 4 ); |
| 24 |
add_filter( 'retrieve_password_title', [$this, 'reset_email_title'] ); |
| 25 |
|
| 26 |
add_action( 'after_password_reset', [$this, 'update_staff_status'] ); |
| 27 |
|
| 28 |
add_filter( 'user_row_actions', [$this, 'user_row_action'], 10, 2 ); |
| 29 |
|
| 30 |
add_action( 'admin_init', [$this, 'make_staff'] ); |
| 31 |
|
| 32 |
add_action( 'init', [$this, 'setup_staff_onboard'] ); |
| 33 |
|
| 34 |
add_filter( 'login_redirect', [$this, 'login_redirect'], 10, 3 ); |
| 35 |
|
| 36 |
add_action( 'wp_ajax_timetics_staff_onboard_skip', [$this, 'timetics_staff_onboard_skip'] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Reset user activation and reset password link |
| 41 |
* |
| 42 |
* @param array $config [$config description] |
| 43 |
* @param string $key [$key description] |
| 44 |
* @param [type] $user_login [$user_login description] |
| 45 |
* @param [type] $user_data [$user_data description] |
| 46 |
* |
| 47 |
* @return [type] [return description] |
| 48 |
*/ |
| 49 |
public function reset_email_content( $config, $key, $user_login, $user_data ) { |
| 50 |
$headers = 'MIME-Version: 1.0' . "\r\n"; |
| 51 |
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; |
| 52 |
|
| 53 |
$local = get_locale(); |
| 54 |
$timetics_reset_url = site_url( "wp-login.php?action=rp&key={$key}&login={$user_login}&wp_lang={$local}" ); |
| 55 |
|
| 56 |
$timetics_reset_url = apply_filters( 'timetics_staff_password_reset_url', $timetics_reset_url, $key, $user_login, $local ); |
| 57 |
|
| 58 |
ob_start(); |
| 59 |
include TIMETICS_PLUGIN_DIR . '/templates/emails/email-header.php'; |
| 60 |
include TIMETICS_PLUGIN_DIR . '/templates/emails/new-staff.php'; |
| 61 |
include TIMETICS_PLUGIN_DIR . '/templates/emails/email-footer.php'; |
| 62 |
$message = ob_get_clean(); |
| 63 |
|
| 64 |
$config['message'] = $message; |
| 65 |
$config['headers'] = $headers; |
| 66 |
|
| 67 |
return $config; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Update staff status after updating password |
| 72 |
* |
| 73 |
* @param Object $user |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function update_staff_status( $user ) { |
| 78 |
$staff = new Staff( $user->ID ); |
| 79 |
|
| 80 |
if ( ! $staff->is_staff() ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$staff->update( |
| 85 |
[ |
| 86 |
'status' => 1, |
| 87 |
] |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Show row actions for make staff |
| 93 |
* |
| 94 |
* @param array $actions |
| 95 |
* @param Object $user_object |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function user_row_action( $actions, $user_object ) { |
| 100 |
$is_staff = user_can( $user_object, 'timetics-staff' ); |
| 101 |
$button_text = $is_staff ? esc_html__( 'Remove from staff', 'timetics' ) : esc_html__( 'Make staff', 'timetics' ); |
| 102 |
$action = $is_staff ? 'removestaff' : 'makestaff'; |
| 103 |
|
| 104 |
$actions['staff'] = "<a class='staff' href='" . wp_nonce_url( "users.php?action=$action&users=$user_object->ID", 'bulk-users' ) . "'>" . $button_text . '</a>'; |
| 105 |
|
| 106 |
return $actions; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Make staff |
| 111 |
* |
| 112 |
* @return mixed |
| 113 |
*/ |
| 114 |
public function make_staff() { |
| 115 |
|
| 116 |
$action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 117 |
$user_id = isset( $_GET['users'] ) ? intval( $_GET['users'] ) : 0; |
| 118 |
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; |
| 119 |
$user = get_userdata( $user_id ); |
| 120 |
|
| 121 |
if ( 'makestaff' === $action ) { |
| 122 |
|
| 123 |
if ( !wp_verify_nonce( $nonce, 'bulk-users' ) ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$capability = current_user_can( 'manage_options' ); |
| 128 |
if( !$capability) return; |
| 129 |
|
| 130 |
$user->add_role( 'timetics-staff' ); |
| 131 |
$staff = new Staff( $user_id ); |
| 132 |
$staff->update( |
| 133 |
[ |
| 134 |
'status' => 1, |
| 135 |
] |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
if ( 'removestaff' === $action ) { |
| 140 |
|
| 141 |
if ( !wp_verify_nonce( $nonce, 'bulk-users' ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$capability = current_user_can( 'manage_options' ); |
| 146 |
if( !$capability ) return; |
| 147 |
|
| 148 |
$user->remove_role( 'timetics-staff' ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Setup staff onboard page |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function setup_staff_onboard() { |
| 158 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only page parameter check, no form processing |
| 159 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 160 |
|
| 161 |
if ( 'staff-onboard' === $page ) { |
| 162 |
Onboard::instance()->init(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Setup redirect uri |
| 168 |
* |
| 169 |
* @param string $reidrect_to |
| 170 |
* @param string $requeted_redirect |
| 171 |
* @param WP_User $user Login user |
| 172 |
* |
| 173 |
* @return void |
| 174 |
*/ |
| 175 |
public function login_redirect( $reidrect_to, $requeted_redirect, $user ) { |
| 176 |
global $user; |
| 177 |
|
| 178 |
$roles = isset( $user->roles ) ? $user->roles : []; |
| 179 |
|
| 180 |
if ( is_wp_error( $user ) ) { |
| 181 |
return $reidrect_to; |
| 182 |
} |
| 183 |
|
| 184 |
$onboard_skip = get_user_meta( $user->ID, 'timetics_staff_onboard_skip', true ); |
| 185 |
|
| 186 |
if ( $onboard_skip ) { |
| 187 |
return $reidrect_to; |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! in_array( 'timetics-staff', $roles ) ) { |
| 191 |
return $reidrect_to; |
| 192 |
} |
| 193 |
|
| 194 |
$integrations = timetics_get_staff_integrations( $user->ID ); |
| 195 |
|
| 196 |
if ( ! $integrations[0]['connected'] ) { |
| 197 |
$reidrect_to = admin_url( 'admin.php?page=staff-onboard' ); |
| 198 |
} |
| 199 |
|
| 200 |
return $reidrect_to; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Update password reset email title |
| 205 |
* |
| 206 |
* @param string $title |
| 207 |
* |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
public function reset_email_title( $title ) { |
| 211 |
/* translators: %s: Site name */ |
| 212 |
$title = sprintf( esc_html__( 'Welcome to %s onboarding!', 'timetics' ), get_bloginfo( 'name' ) ); |
| 213 |
|
| 214 |
return $title; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Staff onboard skip |
| 219 |
* |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public function timetics_staff_onboard_skip() { |
| 223 |
update_user_meta( get_current_user_id(), 'timetics_staff_onboard_skip', true ); |
| 224 |
|
| 225 |
wp_send_json_success( __( 'Staff onboard skipped successfully.', 'timetics' ) ); |
| 226 |
} |
| 227 |
} |