activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
9 months ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
9 months ago
carts_helper.php
1 year ago
connector_helper.php
1 year ago
csv_helper.php
9 months ago
customer_helper.php
9 months ago
customer_import_helper.php
9 months ago
database_helper.php
9 months ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
9 months ago
encrypt_helper.php
1 year ago
events_helper.php
1 year ago
form_helper.php
9 months ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
9 months ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
9 months ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
9 months ago
order_intent_helper.php
9 months ago
orders_helper.php
1 year ago
otp_helper.php
9 months ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
9 months ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
9 months ago
short_links_systems_helper.php
9 months ago
shortcodes_helper.php
9 months ago
sms_helper.php
1 year ago
steps_helper.php
9 months ago
stripe_connect_helper.php
9 months ago
styles_helper.php
9 months ago
support_topics_helper.php
1 year ago
time_helper.php
9 months ago
timeline_helper.php
9 months ago
transaction_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
9 months ago
version_specific_updates_helper.php
9 months ago
whatsapp_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
auth_helper.php
690 lines
| 1 | <?php |
| 2 | |
| 3 | class OsAuthHelper { |
| 4 | |
| 5 | public static ?\LatePoint\Misc\User $current_user = null; |
| 6 | public static $logged_in_customer_id = false; |
| 7 | |
| 8 | public static function set_current_user() { |
| 9 | if ( \OsWpUserHelper::is_user_logged_in() ) { |
| 10 | // if wp user is logged in - load from it |
| 11 | self::$current_user = \LatePoint\Misc\User::load_from_wp_user( \OsWpUserHelper::get_current_user() ); |
| 12 | } else { |
| 13 | self::$current_user = new \LatePoint\Misc\User(); |
| 14 | } |
| 15 | $customer = self::get_logged_in_customer(); |
| 16 | if ( $customer ) { |
| 17 | self::$current_user->customer = $customer; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | public static function get_current_user(): \LatePoint\Misc\User { |
| 22 | if ( ! self::$current_user ) { |
| 23 | self::set_current_user(); |
| 24 | } |
| 25 | |
| 26 | return self::$current_user; |
| 27 | } |
| 28 | |
| 29 | public static function get_highest_current_user_id() { |
| 30 | $user_id = false; |
| 31 | switch ( self::get_highest_current_user_type() ) { |
| 32 | case LATEPOINT_USER_TYPE_ADMIN: |
| 33 | case LATEPOINT_USER_TYPE_CUSTOM: |
| 34 | $user_id = self::get_logged_in_wp_user_id(); |
| 35 | break; |
| 36 | case LATEPOINT_USER_TYPE_AGENT: |
| 37 | $user_id = self::get_logged_in_agent_id(); |
| 38 | break; |
| 39 | case LATEPOINT_USER_TYPE_CUSTOMER: |
| 40 | $user_id = self::get_logged_in_customer_id(); |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | return $user_id; |
| 45 | } |
| 46 | |
| 47 | public static function get_admin_or_agent_avatar_url() { |
| 48 | $avatar_url = LATEPOINT_DEFAULT_AVATAR_URL; |
| 49 | if ( self::is_agent_logged_in() ) { |
| 50 | $agent = self::get_logged_in_agent(); |
| 51 | $avatar_url = $agent->get_avatar_url(); |
| 52 | } elseif ( self::get_logged_in_wp_user_id() ) { |
| 53 | $wp_user = self::get_logged_in_wp_user(); |
| 54 | $avatar_url = get_avatar_url( $wp_user->user_email ); |
| 55 | } |
| 56 | |
| 57 | return $avatar_url; |
| 58 | } |
| 59 | |
| 60 | public static function get_highest_current_user_type() { |
| 61 | // check if WP admin is logged in |
| 62 | $user_type = false; |
| 63 | if ( self::get_current_user()->backend_user_type ) { |
| 64 | // backend user, admin, agent or custom role |
| 65 | return self::get_current_user()->backend_user_type; |
| 66 | } elseif ( self::get_current_user()->customer ) { |
| 67 | // customer |
| 68 | return LATEPOINT_USER_TYPE_CUSTOMER; |
| 69 | } |
| 70 | |
| 71 | return $user_type; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | public static function login_wp_user( $user ) { |
| 76 | clean_user_cache( $user->ID ); |
| 77 | wp_set_current_user( $user->ID ); |
| 78 | wp_set_auth_cookie( $user->ID ); |
| 79 | update_user_caches( $user ); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | public static function login_customer( $contact_value, $password, $contact_type = 'email' ) { |
| 84 | if ( empty( $contact_value ) || empty( $password ) || !in_array($contact_type, self::get_enabled_contact_types_for_customer_auth()) ) { |
| 85 | return false; |
| 86 | } |
| 87 | $available_contact_types = OsAuthHelper::get_available_contact_types_for_customer_auth(); |
| 88 | if(!isset($available_contact_types[$contact_type])){ |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | if ( self::can_wp_users_login_as_customers() ) { |
| 94 | // if WP users enabled - customers can only login using existing password of the wordpress account |
| 95 | $wp_user_id = false; |
| 96 | if($contact_type == 'email'){ |
| 97 | $email = sanitize_email( $contact_value ); |
| 98 | $wp_user_id = email_exists( $email ); |
| 99 | }elseif($contact_type == 'phone'){ |
| 100 | $phone = OsUtilHelper::sanitize_phone_number( $contact_value ); |
| 101 | // find latepoint customer by phone because wp users don't have a phone field |
| 102 | $customer = new OsCustomerModel(); |
| 103 | $customer = $customer->where( array( 'phone' => $phone ) )->set_limit( 1 )->get_results_as_models(); |
| 104 | if($customer){ |
| 105 | $email = $customer->email; |
| 106 | if($customer->wordpress_user_id){ |
| 107 | $wp_user_id = $customer->wordpress_user_id; |
| 108 | }else{ |
| 109 | $wp_user_id = email_exists( $customer->email ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | if($wp_user_id && !empty($email) && !empty($password)){ |
| 114 | $wp_user = wp_signon( [ 'user_login' => $email, 'user_password' => $password ] ); |
| 115 | if ( ! is_wp_error( $wp_user ) ) { |
| 116 | // successfully logged into wp user |
| 117 | // check if latepoint customer exists in db for this wp user |
| 118 | wp_set_current_user( $wp_user->ID ); |
| 119 | $customer = OsCustomerHelper::get_customer_for_wp_user( $wp_user ); |
| 120 | if ( $customer->id ) { |
| 121 | return $customer; |
| 122 | } else { |
| 123 | OsDebugHelper::log( 'Can not login because can not create LatePoint Customer from WP User', 'customer_login_error', $customer->get_error_messages() ); |
| 124 | |
| 125 | return false; |
| 126 | } |
| 127 | } else { |
| 128 | return false; |
| 129 | } |
| 130 | }else{ |
| 131 | return false; |
| 132 | } |
| 133 | } else { |
| 134 | $customer = new OsCustomerModel(); |
| 135 | if($contact_type == 'email'){ |
| 136 | $email = sanitize_email( $contact_value ); |
| 137 | $customer = $customer->where( array( 'email' => $email ) )->set_limit( 1 )->get_results_as_models(); |
| 138 | }elseif($contact_type == 'phone'){ |
| 139 | $phone = OsUtilHelper::sanitize_phone_number( $contact_value ); |
| 140 | $customer = $customer->where( array( 'phone' => $phone ) )->set_limit( 1 )->get_results_as_models(); |
| 141 | } |
| 142 | if ( $customer && OsAuthHelper::verify_password( $password, $customer->password ) ) { |
| 143 | OsAuthHelper::authorize_customer( $customer->id ); |
| 144 | |
| 145 | return $customer; |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public static function can_wp_users_login_as_customers() : bool { |
| 153 | return OsSettingsHelper::is_on( 'wp_users_as_customers', false ); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // CUSTOMERS |
| 158 | // --------------- |
| 159 | |
| 160 | public static function logout_customer() { |
| 161 | if ( self::can_wp_users_login_as_customers() ) { |
| 162 | wp_logout(); |
| 163 | } else { |
| 164 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public static function authorize_customer( $customer_id ) : bool { |
| 169 | $customer = new OsCustomerModel(); |
| 170 | $customer = $customer->where( [ 'id' => $customer_id ] )->set_limit( 1 )->get_results_as_models(); |
| 171 | if ( empty( $customer ) ) { |
| 172 | OsDebugHelper::log( 'Tried to authorize customer with invalid ID', 'customer_authorization', [ 'customer_id' => $customer_id ] ); |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | if ( self::can_wp_users_login_as_customers() ) { |
| 178 | |
| 179 | if ( $customer->wordpress_user_id ) { |
| 180 | $wp_user = get_user_by( 'id', $customer->wordpress_user_id ); |
| 181 | // check if WP User exists, if not - create new one and get ID, otherwise get ID from customer record, since its valid |
| 182 | $wordpress_user_id = ( $wp_user ) ? $customer->wordpress_user_id : OsCustomerHelper::create_wp_user_for_customer( $customer ); |
| 183 | } else { |
| 184 | $wordpress_user_id = OsCustomerHelper::create_wp_user_for_customer( $customer ); |
| 185 | } |
| 186 | |
| 187 | if ( $wordpress_user_id ) { |
| 188 | $wp_user = get_user_by( 'id', $wordpress_user_id ); |
| 189 | if ( $wp_user ) { |
| 190 | self::login_wp_user( $wp_user ); |
| 191 | } else { |
| 192 | OsDebugHelper::log( 'WordPress user ID for customer is not found or can not be created.', 'customer_create_error', [ 'customer_id' => $customer_id, |
| 193 | 'wordpress_user_id' => $wordpress_user_id |
| 194 | ] ); |
| 195 | return false; |
| 196 | } |
| 197 | } else { |
| 198 | OsDebugHelper::log( 'WordPress user ID for customer is not found or can not be created.', 'customer_create_error', [ 'customer_id' => $customer_id ] ); |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | OsSessionsHelper::start_or_use_session_for_customer( $customer_id ); |
| 203 | OsStepsHelper::$customer_object = $customer; |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | public static function get_logged_in_customer_uuid() { |
| 208 | $customer = self::get_logged_in_customer(); |
| 209 | if($customer){ |
| 210 | return $customer->get_uuid(); |
| 211 | }else{ |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | public static function get_logged_in_customer_id() { |
| 217 | if ( OsAuthHelper::is_customer_auth_disabled() ) { |
| 218 | return false; |
| 219 | } |
| 220 | if ( self::can_wp_users_login_as_customers() ) { |
| 221 | // using wp users as customers |
| 222 | if ( OsWpUserHelper::is_user_logged_in() ) { |
| 223 | $wp_user = wp_get_current_user(); |
| 224 | // search connected latepoint customer |
| 225 | $customer = OsCustomerHelper::get_customer_for_wp_user( $wp_user ); |
| 226 | if ( $customer->id ) { |
| 227 | return $customer->id; |
| 228 | } else { |
| 229 | OsDebugHelper::log( 'Can not create LatePoint Customer from WP User', 'customer_create_error', $customer->get_error_messages() ); |
| 230 | |
| 231 | return false; |
| 232 | } |
| 233 | } else { |
| 234 | return false; |
| 235 | } |
| 236 | } else { |
| 237 | if ( self::$logged_in_customer_id ) { |
| 238 | return self::$logged_in_customer_id; |
| 239 | } |
| 240 | $customer_id = OsSessionsHelper::get_customer_id_from_session(); |
| 241 | // make sure customer with this ID exists in database |
| 242 | $customer = new OsCustomerModel( $customer_id ); |
| 243 | if ( ! $customer->is_new_record() ) { |
| 244 | self::$logged_in_customer_id = $customer_id; |
| 245 | |
| 246 | return self::$logged_in_customer_id; |
| 247 | } else { |
| 248 | // customer not found, destroy this invalid customer ID in session cookie |
| 249 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public static function is_customer_logged_in() { |
| 257 | return self::get_logged_in_customer_id(); |
| 258 | } |
| 259 | |
| 260 | public static function get_logged_in_customer() { |
| 261 | $customer = false; |
| 262 | if ( self::is_customer_logged_in() ) { |
| 263 | $customer = new OsCustomerModel( self::get_logged_in_customer_id() ); |
| 264 | if ( $customer->is_new_record() ) { |
| 265 | $customer = false; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return $customer; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | // AGENTS |
| 274 | // ------------- |
| 275 | |
| 276 | public static function get_logged_in_agent_id() { |
| 277 | $agent_id = false; |
| 278 | if ( self::is_agent_logged_in() ) { |
| 279 | if ( self::get_current_user()->agent && self::get_current_user()->agent->id ) { |
| 280 | $agent_id = self::get_current_user()->agent->id; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return $agent_id; |
| 285 | } |
| 286 | |
| 287 | public static function is_agent_logged_in() { |
| 288 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_AGENT ); |
| 289 | } |
| 290 | |
| 291 | public static function get_logged_in_agent() { |
| 292 | $agent = false; |
| 293 | if ( self::is_agent_logged_in() ) { |
| 294 | $agent = new OsAgentModel(); |
| 295 | $agent = $agent->where( [ 'wp_user_id' => self::get_logged_in_wp_user_id() ] )->set_limit( 1 )->get_results_as_models(); |
| 296 | } |
| 297 | |
| 298 | return $agent; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | public static function is_custom_backend_user_logged_in() { |
| 303 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_CUSTOM ); |
| 304 | } |
| 305 | |
| 306 | public static function is_admin_logged_in() { |
| 307 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_ADMIN ); |
| 308 | } |
| 309 | |
| 310 | public static function get_logged_in_admin_user() { |
| 311 | $admin_user = false; |
| 312 | if ( self::is_admin_logged_in() ) { |
| 313 | $admin_user = self::get_logged_in_wp_user(); |
| 314 | } |
| 315 | |
| 316 | return $admin_user; |
| 317 | } |
| 318 | |
| 319 | public static function get_logged_in_admin_user_id() { |
| 320 | $admin_id = false; |
| 321 | if ( self::is_admin_logged_in() ) { |
| 322 | $admin_id = self::get_logged_in_wp_user_id(); |
| 323 | } |
| 324 | |
| 325 | return $admin_id; |
| 326 | } |
| 327 | |
| 328 | public static function get_logged_in_custom_user_id() { |
| 329 | $admin_id = false; |
| 330 | if ( self::is_custom_backend_user_logged_in() ) { |
| 331 | $admin_id = self::get_logged_in_wp_user_id(); |
| 332 | } |
| 333 | |
| 334 | return $admin_id; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | public static function get_default_customer_authentication_method() : string { |
| 339 | $method = OsSettingsHelper::get_settings_value( 'default_customer_authentication_method', 'password' ); |
| 340 | |
| 341 | $enabled_auth_methods = self::get_enabled_customer_authentication_methods(); |
| 342 | if(empty($enabled_auth_methods)){ |
| 343 | $method = ''; |
| 344 | }elseif(count($enabled_auth_methods) == 1){ |
| 345 | $method = $enabled_auth_methods[0]; |
| 346 | }else{ |
| 347 | $method = in_array($method, $enabled_auth_methods) ? $method : reset($enabled_auth_methods); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Default auth method |
| 352 | * |
| 353 | * @since 5.2.0 |
| 354 | * @hook latepoint_default_contact_type_for_customer_auth |
| 355 | * |
| 356 | * @param {string} $method default auth method value |
| 357 | * @returns {string} The filtered auth method value |
| 358 | */ |
| 359 | return apply_filters( 'latepoint_default_contact_type_for_customer_auth', $method ); |
| 360 | } |
| 361 | |
| 362 | public static function get_available_customer_authentication_methods() : array { |
| 363 | $methods = ['password' => __('Password', 'latepoint'), 'otp' => __('One-time code', 'latepoint')]; |
| 364 | /** |
| 365 | * Enabled auth method |
| 366 | * |
| 367 | * @since 5.2.0 |
| 368 | * @hook latepoint_get_available_customer_authentication_methods |
| 369 | * |
| 370 | * @param {array} $methods available auth methods |
| 371 | * @returns {array} The filtered array of available auth methods |
| 372 | */ |
| 373 | return apply_filters( 'latepoint_get_available_customer_authentication_methods', $methods ); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | public static function get_enabled_customer_authentication_methods() : array { |
| 378 | $selected_method = OsSettingsHelper::get_settings_value('selected_customer_authentication_method', 'password'); |
| 379 | |
| 380 | switch($selected_method){ |
| 381 | case 'password': |
| 382 | $auth_methods = ['password']; |
| 383 | break; |
| 384 | case 'otp': |
| 385 | $auth_methods = ['otp']; |
| 386 | break; |
| 387 | case 'password_or_otp': |
| 388 | $auth_methods = ['password', 'otp']; |
| 389 | break; |
| 390 | default: |
| 391 | $auth_methods = ['password']; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Enabled auth method |
| 396 | * |
| 397 | * @since 5.2.0 |
| 398 | * @hook latepoint_get_enabled_customer_authentication_methods |
| 399 | * |
| 400 | * @param {array} $auth_methods enabled auth methods |
| 401 | * @returns {array} The filtered array of enabled auth methods |
| 402 | */ |
| 403 | return apply_filters( 'latepoint_get_enabled_customer_authentication_methods', $auth_methods ); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | public static function get_selected_customer_authentication_method() : string { |
| 408 | $via = OsSettingsHelper::get_settings_value('selected_customer_authentication_method', 'password'); |
| 409 | if(in_array($via, self::get_enabled_customer_authentication_methods(true))){ |
| 410 | return $via; |
| 411 | }else{ |
| 412 | OsSettingsHelper::save_setting_by_name('selected_customer_authentication_method', 'password'); |
| 413 | return 'password'; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | public static function get_customer_authentication_method_options($keys_only = false) : array{ |
| 418 | $options = [ |
| 419 | 'password' => __('Password', 'latepoint'), |
| 420 | 'otp' => __('One-time code', 'latepoint')]; |
| 421 | return $keys_only ? array_keys( $options ) : $options; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | public static function get_default_contact_type_for_customer_auth() : string { |
| 426 | $method = OsSettingsHelper::get_settings_value( 'default_contact_type_for_customer_auth', 'email' ); |
| 427 | |
| 428 | $enabled_contact_types = self::get_enabled_contact_types_for_customer_auth(); |
| 429 | if(empty($enabled_contact_types)){ |
| 430 | $method = ''; |
| 431 | }elseif(count($enabled_contact_types) == 1){ |
| 432 | $method = $enabled_contact_types[0]; |
| 433 | }else{ |
| 434 | $method = in_array($method, $enabled_contact_types) ? $method : reset($enabled_contact_types); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Default auth method |
| 439 | * |
| 440 | * @since 5.2.0 |
| 441 | * @hook latepoint_default_contact_type_for_customer_auth |
| 442 | * |
| 443 | * @param {string} $method default auth method value |
| 444 | * @returns {string} The filtered auth method value |
| 445 | */ |
| 446 | return apply_filters( 'latepoint_default_contact_type_for_customer_auth', $method ); |
| 447 | } |
| 448 | |
| 449 | public static function get_available_contact_types_for_customer_auth() : array { |
| 450 | $contact_types = ['email' => __('Email Address', 'latepoint'), 'phone' => __('Phone Number', 'latepoint')]; |
| 451 | /** |
| 452 | * Enabled auth method |
| 453 | * |
| 454 | * @since 5.2.0 |
| 455 | * @hook latepoint_get_available_contact_types_for_customer_auth |
| 456 | * |
| 457 | * @param {array} $contact_types available contact types |
| 458 | * @returns {array} The filtered array of available contact types |
| 459 | */ |
| 460 | return apply_filters( 'latepoint_get_available_contact_types_for_customer_auth', $contact_types ); |
| 461 | } |
| 462 | |
| 463 | public static function get_enabled_contact_types_for_customer_auth() : array { |
| 464 | $customer_authentication_field_type = OsSettingsHelper::get_settings_value('selected_customer_authentication_field_type', 'email'); |
| 465 | |
| 466 | switch($customer_authentication_field_type){ |
| 467 | case 'email': |
| 468 | $contact_types = ['email']; |
| 469 | break; |
| 470 | case 'phone': |
| 471 | $contact_types = ['phone']; |
| 472 | break; |
| 473 | case 'email_or_phone': |
| 474 | $contact_types = ['email', 'phone']; |
| 475 | break; |
| 476 | case 'disabled': |
| 477 | $contact_types = []; |
| 478 | break; |
| 479 | default: |
| 480 | $contact_types = ['email']; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Enabled auth method |
| 485 | * |
| 486 | * @since 5.2.0 |
| 487 | * @hook latepoint_get_enabled_contact_types_for_customer_auth |
| 488 | * |
| 489 | * @param {array} $contact_types enabled contact types |
| 490 | * @returns {array} The filtered array of enabled contact types |
| 491 | */ |
| 492 | return apply_filters( 'latepoint_get_enabled_contact_types_for_customer_auth', $contact_types ); |
| 493 | } |
| 494 | |
| 495 | public static function is_customer_auth_enabled() : bool{ |
| 496 | return (self::get_selected_customer_authentication_field_type() != 'disabled'); |
| 497 | } |
| 498 | |
| 499 | public static function is_customer_auth_disabled() : bool{ |
| 500 | return !self::is_customer_auth_enabled(); |
| 501 | } |
| 502 | |
| 503 | public static function get_selected_customer_authentication_field_type() : string { |
| 504 | $via = OsSettingsHelper::get_settings_value('selected_customer_authentication_field_type'); |
| 505 | if(empty($via)){ |
| 506 | // load from legacy setting |
| 507 | if(OsSettingsHelper::is_on('steps_hide_login_register_tabs')){ |
| 508 | OsSettingsHelper::save_setting_by_name('selected_customer_authentication_field_type', 'disabled'); |
| 509 | return 'disabled'; |
| 510 | }else{ |
| 511 | OsSettingsHelper::save_setting_by_name('selected_customer_authentication_field_type', 'email'); |
| 512 | return 'email'; |
| 513 | } |
| 514 | } |
| 515 | if(in_array($via, self::get_customer_authentication_field_type_options(true))){ |
| 516 | return $via; |
| 517 | }else{ |
| 518 | OsSettingsHelper::save_setting_by_name('selected_customer_authentication_field_type', 'email'); |
| 519 | return 'email'; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | public static function get_customer_authentication_field_type_options($keys_only = false) : array{ |
| 524 | $options = [ |
| 525 | 'email' => __('Email Address', 'latepoint'), |
| 526 | 'phone' => __('Phone Number', 'latepoint'), |
| 527 | 'disabled' => __('Disable ability to login', 'latepoint')]; |
| 528 | return $keys_only ? array_keys( $options ) : $options; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | public static function is_classic_auth_flow() : bool{ |
| 533 | return (OsSettingsHelper::get_settings_value('modern_auth_flow_for_customers', LATEPOINT_VALUE_OFF) == LATEPOINT_VALUE_OFF); |
| 534 | } |
| 535 | public static function is_otp_auth_enabled() : bool{ |
| 536 | return in_array('otp', self::get_enabled_customer_authentication_methods()); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | public static function get_default_delivery_method_for_customer_auth_contact_type(string $contact_type){ |
| 541 | $delivery_method = 'email'; |
| 542 | switch($contact_type){ |
| 543 | case 'email': |
| 544 | $delivery_method = 'email'; |
| 545 | break; |
| 546 | case 'phone': |
| 547 | $delivery_method = 'sms'; |
| 548 | break; |
| 549 | } |
| 550 | /** |
| 551 | * Get default delivery method for auth contact type |
| 552 | * |
| 553 | * @since 5.2.0 |
| 554 | * @hook latepoint_get_default_delivery_method_for_customer_auth |
| 555 | * |
| 556 | * @param {string} $delivery_method delivery method (email, sms, whatsapp) |
| 557 | * @param {string} $contact_type contact type selected for auth (email, phone) |
| 558 | * |
| 559 | * @returns {string} Filtered delivery method |
| 560 | */ |
| 561 | return apply_filters('latepoint_get_default_delivery_method_for_customer_auth', $delivery_method, $contact_type); |
| 562 | } |
| 563 | |
| 564 | public static function auth_form_html($is_classic, OsCustomerModel $customer, string $selected_contact_type_for_auth = '', $selected_delivery_method = '') : string{ |
| 565 | if(empty($selected_contact_type_for_auth)){ |
| 566 | $selected_contact_type_for_auth = OsAuthHelper::get_default_contact_type_for_customer_auth(); |
| 567 | } |
| 568 | if(empty($selected_delivery_method)){ |
| 569 | $selected_delivery_method = self::get_default_delivery_method_for_customer_auth_contact_type($selected_contact_type_for_auth); |
| 570 | } |
| 571 | $enabled_contact_types_for_customer_auth = OsAuthHelper::get_enabled_contact_types_for_customer_auth(); |
| 572 | $otp_allowed = self::is_otp_auth_enabled(); |
| 573 | $multiple_auth_methods_enabled = (count(self::get_enabled_customer_authentication_methods()) > 1); |
| 574 | $html = ''; |
| 575 | if(!$is_classic){ |
| 576 | $html.='<div class="latepoint-customer-auth-wrapper">'; |
| 577 | $html.='<div class="latepoint-customer-otp-request-wrapper hide-when-entering-otp">'; |
| 578 | // NEW STYLE SHOWING JUST THE AUTH OPTIONS FIRST |
| 579 | if(in_array('email', $enabled_contact_types_for_customer_auth)){ |
| 580 | $html.='<div data-login-method="email" class="customer-login-method-wrapper '.($selected_contact_type_for_auth == 'email' ? '' : 'os-hidden').'">'; |
| 581 | $html.=OsFormHelper::text_field('auth[email]', __('Your Email Address', 'latepoint'), $customer->email, array('validate' => $customer->get_validations_for_property('email'), 'class' => 'required')); |
| 582 | $html.='</div>'; |
| 583 | } |
| 584 | if(in_array('phone', $enabled_contact_types_for_customer_auth)){ |
| 585 | $html.='<div data-login-method="phone" class="customer-login-method-wrapper '.($selected_contact_type_for_auth == 'phone' ? '' : 'os-hidden').'">'; |
| 586 | $html.=OsFormHelper::phone_number_field('auth[phone]', __('Your Phone Number', 'latepoint'), $customer->phone, array('validate' => $customer->get_validations_for_property('phone'), 'class' => 'required', 'theme' => 'simple')); |
| 587 | $html.='</div>'; |
| 588 | } |
| 589 | $html.='<a tabindex="0" class="latepoint-btn latepoint-btn-block latepoint-btn-primary latepoint-request-otp-button" data-otp-request-route="'.OsRouterHelper::build_route_name('auth', 'request_otp').'"><span>'.__('Continue', 'latepoint').'</span></a>'; |
| 590 | $html.='</div>'; |
| 591 | $html.='</div>'; |
| 592 | }else{ |
| 593 | ob_start(); |
| 594 | ?> |
| 595 | <div class="os-customer-login-w os-customer-wrapped-box os-unwrapped"> |
| 596 | <?php if(count($enabled_contact_types_for_customer_auth) > 1){ ?> |
| 597 | <div class="login-options-wrapper"> |
| 598 | <div class="latepoint-customer-box-title"><?php _e('Sign in', 'latepoint'); ?></div> |
| 599 | <?php if(in_array('email', $enabled_contact_types_for_customer_auth) && in_array('phone', $enabled_contact_types_for_customer_auth)){ ?> |
| 600 | <div class="login-options-col login-options-via"> |
| 601 | <div class="login-options-via-wrapper"> |
| 602 | <div data-login-method="email" data-is-otp-enabled="<?php echo OsOTPHelper::is_otp_enabled_for_contact_type('email', 'email') ? 'yes' : 'no'; ?>" data-otp-delivery-method="email" class="login-option <?php echo $selected_contact_type_for_auth == 'email' ? 'os-selected os-default' : ''; ?>"><?php _e('Email', 'latepoint'); ?></div> |
| 603 | <div data-login-method="phone" data-is-otp-enabled="<?php echo OsOTPHelper::is_otp_enabled_for_contact_type('phone', 'sms') ? 'yes' : 'no'; ?>" data-otp-delivery-method="sms" class="login-option <?php echo $selected_contact_type_for_auth == 'phone' ? 'os-selected os-default' : ''; ?>"><?php _e('Phone', 'latepoint'); ?></div> |
| 604 | </div> |
| 605 | </div> |
| 606 | <?php } ?> |
| 607 | </div> |
| 608 | <?php } ?> |
| 609 | <?php |
| 610 | if(in_array('email', $enabled_contact_types_for_customer_auth)){ |
| 611 | echo '<div data-login-method="email" class="customer-login-method-wrapper '.($selected_contact_type_for_auth == 'email' ? '' : 'os-hidden').'">'; |
| 612 | echo OsFormHelper::text_field('auth[email]', __('Your Email Address', 'latepoint'), ''); |
| 613 | echo '</div>'; |
| 614 | } |
| 615 | ?> |
| 616 | <?php |
| 617 | if(in_array('phone', $enabled_contact_types_for_customer_auth)){ |
| 618 | echo '<div data-login-method="phone" class="customer-login-method-wrapper '.($selected_contact_type_for_auth == 'phone' ? '' : 'os-hidden').'">'; |
| 619 | echo OsFormHelper::phone_number_field('auth[phone]', __('Your Phone Number', 'latepoint'), ''); |
| 620 | echo '</div>'; |
| 621 | } |
| 622 | ?> |
| 623 | <div class="os-customer-login-password-fields-w" <?php echo (OsAuthHelper::get_default_customer_authentication_method() == 'otp') ? 'style="display:none;"' : ''; ?>> |
| 624 | <?php echo OsFormHelper::password_field('auth[password]', __('Your Password', 'latepoint'), '', array('class' => 'required')); ?> |
| 625 | <a href="#" class="latepoint-btn latepoint-btn-primary latepoint-btn-link step-forgot-password-btn" |
| 626 | data-os-action="<?php echo esc_attr(OsRouterHelper::build_route_name('customer_cabinet', 'request_password_reset_token')); ?>" |
| 627 | data-os-output-target=".os-password-reset-form-holder" |
| 628 | data-os-after-call="latepoint_reset_password_from_booking_init" |
| 629 | data-os-params="<?php echo esc_attr(OsUtilHelper::build_os_params(['from_booking' => true])); ?>"><span><?php esc_html_e('Forgot?', 'latepoint'); ?></span></a> |
| 630 | </div> |
| 631 | |
| 632 | <?php if($otp_allowed){ ?> |
| 633 | <div class="os-customer-otp-notice" <?php echo (OsAuthHelper::get_default_customer_authentication_method() == 'otp') ? '' : 'style="display:none;"'; ?>> |
| 634 | <?php _e('You will receive a 6-digit code to log in.', 'latepoint'); ?> |
| 635 | </div> |
| 636 | <?php } ?> |
| 637 | <div class="os-customer-login-buttons"> |
| 638 | <?php if($multiple_auth_methods_enabled){ ?> |
| 639 | <div class="login-options-col"> |
| 640 | <div class="latepoint-customer-otp-option"> |
| 641 | <?php if(OsAuthHelper::get_default_customer_authentication_method() == 'otp'){ ?> |
| 642 | <label><input type="checkbox" name="auth[via]" value="password" class="login-with-password-toggle os-opposite"><span><?php _e('Use password instead', 'latepoint'); ?></span></label> |
| 643 | <?php }else{ ?> |
| 644 | <label><input type="checkbox" name="auth[via]" value="otp" class="login-with-password-toggle"><span><?php _e('Send me a sign-in code', 'latepoint'); ?></span></label> |
| 645 | <?php } ?> |
| 646 | </div> |
| 647 | </div> |
| 648 | <?php }else{ |
| 649 | echo OsFormHelper::hidden_field('auth[via]', OsAuthHelper::get_default_customer_authentication_method()); |
| 650 | }?> |
| 651 | <a data-otp-request-route="<?php echo esc_attr(OsRouterHelper::build_route_name('auth', 'request_otp')); ?>" data-password-login-route="<?php echo esc_attr(OsRouterHelper::build_route_name('auth', 'login_customer')); ?>" href="#" |
| 652 | class="latepoint-btn latepoint-btn-primary step-login-existing-customer-btn <?php echo $multiple_auth_methods_enabled ? '' : 'latepoint-btn-block'; ?>"><span><?php esc_html_e('Continue', 'latepoint'); ?></span></a> |
| 653 | </div> |
| 654 | </div> |
| 655 | <?php |
| 656 | $html = ob_get_clean(); |
| 657 | } |
| 658 | $html.= OsFormHelper::hidden_field( 'auth[contact_type]', $selected_contact_type_for_auth ); |
| 659 | $html.= OsFormHelper::hidden_field( 'auth[delivery_method]', $selected_delivery_method ); |
| 660 | $html.= OsFormHelper::hidden_field( 'auth[action]', 'register' ); |
| 661 | $html.= wp_nonce_field( 'auth_nonce', 'auth[nonce]', true, false ); |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | return $html; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | // WP USER |
| 671 | public static function get_logged_in_wp_user_id() { |
| 672 | return OsWpUserHelper::get_current_user_id(); |
| 673 | } |
| 674 | |
| 675 | public static function get_logged_in_wp_user() { |
| 676 | return OsWpUserHelper::get_current_user(); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | // UTILS |
| 681 | |
| 682 | public static function hash_password( $password ) { |
| 683 | return password_hash( $password, PASSWORD_DEFAULT ); |
| 684 | } |
| 685 | |
| 686 | public static function verify_password( $password, $hash ) { |
| 687 | return password_verify( $password, $hash ); |
| 688 | } |
| 689 | |
| 690 | } |