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
9 months 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
9 months 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
762 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 | if ( self::can_wp_users_login_as_customers() ) { |
| 93 | // if WP users enabled - customers can only login using existing password of the wordpress account |
| 94 | $wp_user_id = false; |
| 95 | if ( $contact_type == 'email' ) { |
| 96 | $email = sanitize_email( $contact_value ); |
| 97 | $wp_user_id = email_exists( $email ); |
| 98 | } elseif ( $contact_type == 'phone' ) { |
| 99 | $phone = OsUtilHelper::sanitize_phone_number( $contact_value ); |
| 100 | // find latepoint customer by phone because wp users don't have a phone field |
| 101 | $customer = new OsCustomerModel(); |
| 102 | $customer = $customer->where( array( 'phone' => $phone ) )->set_limit( 1 )->get_results_as_models(); |
| 103 | if ( $customer ) { |
| 104 | $email = $customer->email; |
| 105 | if ( $customer->wordpress_user_id ) { |
| 106 | $wp_user_id = $customer->wordpress_user_id; |
| 107 | } else { |
| 108 | $wp_user_id = email_exists( $customer->email ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if ( $wp_user_id && ! empty( $email ) && ! empty( $password ) ) { |
| 113 | $wp_user = wp_signon( [ 'user_login' => $email, 'user_password' => $password ] ); |
| 114 | if ( ! is_wp_error( $wp_user ) ) { |
| 115 | // successfully logged into wp user |
| 116 | // check if latepoint customer exists in db for this wp user |
| 117 | wp_set_current_user( $wp_user->ID ); |
| 118 | $customer = OsCustomerHelper::get_customer_for_wp_user( $wp_user ); |
| 119 | if ( $customer->id ) { |
| 120 | return $customer; |
| 121 | } else { |
| 122 | OsDebugHelper::log( 'Can not login because can not create LatePoint Customer from WP User', 'customer_login_error', $customer->get_error_messages() ); |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | } else { |
| 127 | return false; |
| 128 | } |
| 129 | } else { |
| 130 | return false; |
| 131 | } |
| 132 | } else { |
| 133 | $customer = new OsCustomerModel(); |
| 134 | if ( $contact_type == 'email' ) { |
| 135 | $email = sanitize_email( $contact_value ); |
| 136 | $customer = $customer->where( array( 'email' => $email ) )->set_limit( 1 )->get_results_as_models(); |
| 137 | } elseif ( $contact_type == 'phone' ) { |
| 138 | $phone = OsUtilHelper::sanitize_phone_number( $contact_value ); |
| 139 | $customer = $customer->where( array( 'phone' => $phone ) )->set_limit( 1 )->get_results_as_models(); |
| 140 | } |
| 141 | if ( $customer && OsAuthHelper::verify_password( $password, $customer->password ) ) { |
| 142 | OsAuthHelper::authorize_customer( $customer->id ); |
| 143 | |
| 144 | return $customer; |
| 145 | } else { |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public static function can_wp_users_login_as_customers(): bool { |
| 152 | return OsSettingsHelper::is_on( 'wp_users_as_customers', false ); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | // CUSTOMERS |
| 157 | // --------------- |
| 158 | |
| 159 | public static function logout_customer() { |
| 160 | if ( self::can_wp_users_login_as_customers() ) { |
| 161 | wp_logout(); |
| 162 | } else { |
| 163 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public static function authorize_customer( $customer_id ): bool { |
| 168 | $customer = new OsCustomerModel(); |
| 169 | $customer = $customer->where( [ 'id' => $customer_id ] )->set_limit( 1 )->get_results_as_models(); |
| 170 | if ( empty( $customer ) ) { |
| 171 | OsDebugHelper::log( 'Tried to authorize customer with invalid ID', 'customer_authorization', [ 'customer_id' => $customer_id ] ); |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if ( self::can_wp_users_login_as_customers() ) { |
| 177 | |
| 178 | if ( $customer->wordpress_user_id ) { |
| 179 | $wp_user = get_user_by( 'id', $customer->wordpress_user_id ); |
| 180 | // check if WP User exists, if not - create new one and get ID, otherwise get ID from customer record, since its valid |
| 181 | $wordpress_user_id = ( $wp_user ) ? $customer->wordpress_user_id : OsCustomerHelper::create_wp_user_for_customer( $customer ); |
| 182 | } else { |
| 183 | $wordpress_user_id = OsCustomerHelper::create_wp_user_for_customer( $customer ); |
| 184 | } |
| 185 | |
| 186 | if ( $wordpress_user_id ) { |
| 187 | $wp_user = get_user_by( 'id', $wordpress_user_id ); |
| 188 | if ( $wp_user ) { |
| 189 | self::login_wp_user( $wp_user ); |
| 190 | } else { |
| 191 | OsDebugHelper::log( 'WordPress user ID for customer is not found or can not be created.', 'customer_create_error', [ |
| 192 | 'customer_id' => $customer_id, |
| 193 | 'wordpress_user_id' => $wordpress_user_id |
| 194 | ] ); |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | } else { |
| 199 | OsDebugHelper::log( 'WordPress user ID for customer is not found or can not be created.', 'customer_create_error', [ 'customer_id' => $customer_id ] ); |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | OsSessionsHelper::start_or_use_session_for_customer( $customer_id ); |
| 205 | OsStepsHelper::$customer_object = $customer; |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | public static function get_logged_in_customer_uuid() { |
| 211 | $customer = self::get_logged_in_customer(); |
| 212 | if ( $customer ) { |
| 213 | return $customer->get_uuid(); |
| 214 | } else { |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public static function get_logged_in_customer_id() { |
| 220 | if ( OsAuthHelper::is_customer_auth_disabled() ) { |
| 221 | return false; |
| 222 | } |
| 223 | if ( self::can_wp_users_login_as_customers() ) { |
| 224 | // using wp users as customers |
| 225 | if ( OsWpUserHelper::is_user_logged_in() ) { |
| 226 | $wp_user = wp_get_current_user(); |
| 227 | // search connected latepoint customer |
| 228 | $customer = OsCustomerHelper::get_customer_for_wp_user( $wp_user ); |
| 229 | if ( $customer->id ) { |
| 230 | return $customer->id; |
| 231 | } else { |
| 232 | OsDebugHelper::log( 'Can not create LatePoint Customer from WP User', 'customer_create_error', $customer->get_error_messages() ); |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | } else { |
| 237 | return false; |
| 238 | } |
| 239 | } else { |
| 240 | if ( self::$logged_in_customer_id ) { |
| 241 | return self::$logged_in_customer_id; |
| 242 | } |
| 243 | $customer_id = OsSessionsHelper::get_customer_id_from_session(); |
| 244 | // make sure customer with this ID exists in database |
| 245 | $customer = new OsCustomerModel( $customer_id ); |
| 246 | if ( ! $customer->is_new_record() ) { |
| 247 | self::$logged_in_customer_id = $customer_id; |
| 248 | |
| 249 | return self::$logged_in_customer_id; |
| 250 | } else { |
| 251 | // customer not found, destroy this invalid customer ID in session cookie |
| 252 | OsSessionsHelper::destroy_customer_session_cookie(); |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public static function is_customer_logged_in() { |
| 260 | return self::get_logged_in_customer_id(); |
| 261 | } |
| 262 | |
| 263 | public static function get_logged_in_customer() { |
| 264 | $customer = false; |
| 265 | if ( self::is_customer_logged_in() ) { |
| 266 | $customer = new OsCustomerModel( self::get_logged_in_customer_id() ); |
| 267 | if ( $customer->is_new_record() ) { |
| 268 | $customer = false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $customer; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // AGENTS |
| 277 | // ------------- |
| 278 | |
| 279 | public static function get_logged_in_agent_id() { |
| 280 | $agent_id = false; |
| 281 | if ( self::is_agent_logged_in() ) { |
| 282 | if ( self::get_current_user()->agent && self::get_current_user()->agent->id ) { |
| 283 | $agent_id = self::get_current_user()->agent->id; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return $agent_id; |
| 288 | } |
| 289 | |
| 290 | public static function is_agent_logged_in() { |
| 291 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_AGENT ); |
| 292 | } |
| 293 | |
| 294 | public static function get_logged_in_agent() { |
| 295 | $agent = false; |
| 296 | if ( self::is_agent_logged_in() ) { |
| 297 | $agent = new OsAgentModel(); |
| 298 | $agent = $agent->where( [ 'wp_user_id' => self::get_logged_in_wp_user_id() ] )->set_limit( 1 )->get_results_as_models(); |
| 299 | } |
| 300 | |
| 301 | return $agent; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | public static function is_custom_backend_user_logged_in() { |
| 306 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_CUSTOM ); |
| 307 | } |
| 308 | |
| 309 | public static function is_admin_logged_in() { |
| 310 | return ( self::get_current_user()->backend_user_type == LATEPOINT_USER_TYPE_ADMIN ); |
| 311 | } |
| 312 | |
| 313 | public static function get_logged_in_admin_user() { |
| 314 | $admin_user = false; |
| 315 | if ( self::is_admin_logged_in() ) { |
| 316 | $admin_user = self::get_logged_in_wp_user(); |
| 317 | } |
| 318 | |
| 319 | return $admin_user; |
| 320 | } |
| 321 | |
| 322 | public static function get_logged_in_admin_user_id() { |
| 323 | $admin_id = false; |
| 324 | if ( self::is_admin_logged_in() ) { |
| 325 | $admin_id = self::get_logged_in_wp_user_id(); |
| 326 | } |
| 327 | |
| 328 | return $admin_id; |
| 329 | } |
| 330 | |
| 331 | public static function get_logged_in_custom_user_id() { |
| 332 | $admin_id = false; |
| 333 | if ( self::is_custom_backend_user_logged_in() ) { |
| 334 | $admin_id = self::get_logged_in_wp_user_id(); |
| 335 | } |
| 336 | |
| 337 | return $admin_id; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | public static function get_default_customer_authentication_method(): string { |
| 342 | $method = OsSettingsHelper::get_settings_value( 'default_customer_authentication_method', 'password' ); |
| 343 | |
| 344 | $enabled_auth_methods = self::get_enabled_customer_authentication_methods(); |
| 345 | if ( empty( $enabled_auth_methods ) ) { |
| 346 | $method = ''; |
| 347 | } elseif ( count( $enabled_auth_methods ) == 1 ) { |
| 348 | $method = $enabled_auth_methods[0]; |
| 349 | } else { |
| 350 | $method = in_array( $method, $enabled_auth_methods ) ? $method : reset( $enabled_auth_methods ); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Default auth method |
| 355 | * |
| 356 | * @param {string} $method default auth method value |
| 357 | * @returns {string} The filtered auth method value |
| 358 | * |
| 359 | * @since 5.2.0 |
| 360 | * @hook latepoint_default_contact_type_for_customer_auth |
| 361 | * |
| 362 | */ |
| 363 | return apply_filters( 'latepoint_default_contact_type_for_customer_auth', $method ); |
| 364 | } |
| 365 | |
| 366 | public static function get_available_customer_authentication_methods(): array { |
| 367 | $methods = [ 'password' => __( 'Password', 'latepoint' ), 'otp' => __( 'One-time code', 'latepoint' ) ]; |
| 368 | |
| 369 | /** |
| 370 | * Enabled auth method |
| 371 | * |
| 372 | * @param {array} $methods available auth methods |
| 373 | * @returns {array} The filtered array of available auth methods |
| 374 | * |
| 375 | * @since 5.2.0 |
| 376 | * @hook latepoint_get_available_customer_authentication_methods |
| 377 | * |
| 378 | */ |
| 379 | return apply_filters( 'latepoint_get_available_customer_authentication_methods', $methods ); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | public static function get_enabled_customer_authentication_methods(): array { |
| 384 | $selected_method = OsSettingsHelper::get_settings_value( 'selected_customer_authentication_method', 'password' ); |
| 385 | |
| 386 | switch ( $selected_method ) { |
| 387 | case 'password': |
| 388 | $auth_methods = [ 'password' ]; |
| 389 | break; |
| 390 | case 'otp': |
| 391 | $auth_methods = [ 'otp' ]; |
| 392 | break; |
| 393 | case 'password_or_otp': |
| 394 | $auth_methods = [ 'password', 'otp' ]; |
| 395 | break; |
| 396 | default: |
| 397 | $auth_methods = [ 'password' ]; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Enabled auth method |
| 402 | * |
| 403 | * @param {array} $auth_methods enabled auth methods |
| 404 | * @returns {array} The filtered array of enabled auth methods |
| 405 | * |
| 406 | * @since 5.2.0 |
| 407 | * @hook latepoint_get_enabled_customer_authentication_methods |
| 408 | * |
| 409 | */ |
| 410 | return apply_filters( 'latepoint_get_enabled_customer_authentication_methods', $auth_methods ); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | public static function get_selected_customer_authentication_method(): string { |
| 415 | $via = OsSettingsHelper::get_settings_value( 'selected_customer_authentication_method', 'password' ); |
| 416 | if ( in_array( $via, self::get_enabled_customer_authentication_methods( true ) ) ) { |
| 417 | return $via; |
| 418 | } else { |
| 419 | OsSettingsHelper::save_setting_by_name( 'selected_customer_authentication_method', 'password' ); |
| 420 | |
| 421 | return 'password'; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | public static function get_customer_authentication_method_options( $keys_only = false ): array { |
| 426 | $options = [ |
| 427 | 'password' => __( 'Password', 'latepoint' ), |
| 428 | 'otp' => __( 'One-time code', 'latepoint' ) |
| 429 | ]; |
| 430 | |
| 431 | return $keys_only ? array_keys( $options ) : $options; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | public static function get_default_contact_type_for_customer_auth(): string { |
| 436 | $method = OsSettingsHelper::get_settings_value( 'default_contact_type_for_customer_auth', 'email' ); |
| 437 | |
| 438 | $enabled_contact_types = self::get_enabled_contact_types_for_customer_auth(); |
| 439 | if ( empty( $enabled_contact_types ) ) { |
| 440 | $method = ''; |
| 441 | } elseif ( count( $enabled_contact_types ) == 1 ) { |
| 442 | $method = $enabled_contact_types[0]; |
| 443 | } else { |
| 444 | $method = in_array( $method, $enabled_contact_types ) ? $method : reset( $enabled_contact_types ); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Default auth method |
| 449 | * |
| 450 | * @param {string} $method default auth method value |
| 451 | * @returns {string} The filtered auth method value |
| 452 | * |
| 453 | * @since 5.2.0 |
| 454 | * @hook latepoint_default_contact_type_for_customer_auth |
| 455 | * |
| 456 | */ |
| 457 | return apply_filters( 'latepoint_default_contact_type_for_customer_auth', $method ); |
| 458 | } |
| 459 | |
| 460 | public static function get_available_contact_types_for_customer_auth(): array { |
| 461 | $contact_types = [ 'email' => __( 'Email Address', 'latepoint' ), 'phone' => __( 'Phone Number', 'latepoint' ) ]; |
| 462 | |
| 463 | /** |
| 464 | * Enabled auth method |
| 465 | * |
| 466 | * @param {array} $contact_types available contact types |
| 467 | * @returns {array} The filtered array of available contact types |
| 468 | * |
| 469 | * @since 5.2.0 |
| 470 | * @hook latepoint_get_available_contact_types_for_customer_auth |
| 471 | * |
| 472 | */ |
| 473 | return apply_filters( 'latepoint_get_available_contact_types_for_customer_auth', $contact_types ); |
| 474 | } |
| 475 | |
| 476 | public static function get_enabled_contact_types_for_customer_auth(): array { |
| 477 | $customer_authentication_field_type = OsSettingsHelper::get_settings_value( 'selected_customer_authentication_field_type', 'email' ); |
| 478 | |
| 479 | switch ( $customer_authentication_field_type ) { |
| 480 | case 'email': |
| 481 | $contact_types = [ 'email' ]; |
| 482 | break; |
| 483 | case 'phone': |
| 484 | $contact_types = [ 'phone' ]; |
| 485 | break; |
| 486 | case 'email_or_phone': |
| 487 | $contact_types = [ 'email', 'phone' ]; |
| 488 | break; |
| 489 | case 'disabled': |
| 490 | $contact_types = []; |
| 491 | break; |
| 492 | default: |
| 493 | $contact_types = [ 'email' ]; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Enabled auth method |
| 498 | * |
| 499 | * @param {array} $contact_types enabled contact types |
| 500 | * @returns {array} The filtered array of enabled contact types |
| 501 | * |
| 502 | * @since 5.2.0 |
| 503 | * @hook latepoint_get_enabled_contact_types_for_customer_auth |
| 504 | * |
| 505 | */ |
| 506 | return apply_filters( 'latepoint_get_enabled_contact_types_for_customer_auth', $contact_types ); |
| 507 | } |
| 508 | |
| 509 | public static function is_customer_auth_enabled(): bool { |
| 510 | return ( self::get_selected_customer_authentication_field_type() != 'disabled' ); |
| 511 | } |
| 512 | |
| 513 | public static function is_customer_auth_disabled(): bool { |
| 514 | return ! self::is_customer_auth_enabled(); |
| 515 | } |
| 516 | |
| 517 | public static function count_total_customers_violating_auth_rules() : array { |
| 518 | |
| 519 | $violators = ['field' => '', 'values' => []]; |
| 520 | $field = ''; |
| 521 | |
| 522 | if(OsAuthHelper::is_customer_auth_enabled()){ |
| 523 | // auth enabled |
| 524 | $auth_field = OsAuthHelper::get_selected_customer_authentication_field_type(); |
| 525 | if($auth_field == 'email'){ |
| 526 | $field = 'email'; |
| 527 | } |
| 528 | if($auth_field == 'phone'){ |
| 529 | $field = 'phone'; |
| 530 | } |
| 531 | }else{ |
| 532 | // auth disabled |
| 533 | $merge_data = OsSettingsHelper::get_settings_value('default_contact_merge_behavior', 'email'); |
| 534 | if($merge_data == 'email'){ |
| 535 | $field = 'email'; |
| 536 | } |
| 537 | if($merge_data == 'phone'){ |
| 538 | $field = 'phone'; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if($field){ |
| 543 | $customers_model = new OsCustomerModel(); |
| 544 | $result = $customers_model->select( $field ) |
| 545 | ->where( [ $field.' !=' => '' ] ) |
| 546 | ->group_by( $field ) |
| 547 | ->having( 'COUNT(*) > 1' ) |
| 548 | ->get_results( ARRAY_A ); |
| 549 | $violators['values'] = (array_column($result, $field)); |
| 550 | $violators['field'] = $field; |
| 551 | } |
| 552 | |
| 553 | return $violators; |
| 554 | } |
| 555 | |
| 556 | public static function get_selected_customer_authentication_field_type(): string { |
| 557 | $via = OsSettingsHelper::get_settings_value( 'selected_customer_authentication_field_type' ); |
| 558 | if ( empty( $via ) ) { |
| 559 | // load from legacy setting |
| 560 | if ( OsSettingsHelper::is_on( 'steps_hide_login_register_tabs' ) ) { |
| 561 | OsSettingsHelper::save_setting_by_name( 'selected_customer_authentication_field_type', 'disabled' ); |
| 562 | |
| 563 | return 'disabled'; |
| 564 | } else { |
| 565 | OsSettingsHelper::save_setting_by_name( 'selected_customer_authentication_field_type', 'email' ); |
| 566 | |
| 567 | return 'email'; |
| 568 | } |
| 569 | } |
| 570 | if ( in_array( $via, self::get_customer_authentication_field_type_options( true ) ) ) { |
| 571 | return $via; |
| 572 | } else { |
| 573 | OsSettingsHelper::save_setting_by_name( 'selected_customer_authentication_field_type', 'email' ); |
| 574 | |
| 575 | return 'email'; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | public static function get_customer_authentication_field_type_options( $keys_only = false ): array { |
| 580 | $options = [ |
| 581 | 'email' => __( 'Email Address', 'latepoint' ), |
| 582 | 'phone' => __( 'Phone Number', 'latepoint' ), |
| 583 | 'disabled' => __( 'Disable ability to login', 'latepoint' ) |
| 584 | ]; |
| 585 | |
| 586 | return $keys_only ? array_keys( $options ) : $options; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | public static function is_classic_auth_flow(): bool { |
| 591 | return ( OsSettingsHelper::get_settings_value( 'modern_auth_flow_for_customers', LATEPOINT_VALUE_OFF ) == LATEPOINT_VALUE_OFF ); |
| 592 | } |
| 593 | |
| 594 | public static function is_otp_auth_enabled(): bool { |
| 595 | return in_array( 'otp', self::get_enabled_customer_authentication_methods() ); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | public static function get_default_delivery_method_for_customer_auth_contact_type( string $contact_type ) { |
| 600 | $delivery_method = 'email'; |
| 601 | switch ( $contact_type ) { |
| 602 | case 'email': |
| 603 | $delivery_method = 'email'; |
| 604 | break; |
| 605 | case 'phone': |
| 606 | $delivery_method = 'sms'; |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Get default delivery method for auth contact type |
| 612 | * |
| 613 | * @param {string} $delivery_method delivery method (email, sms, whatsapp) |
| 614 | * @param {string} $contact_type contact type selected for auth (email, phone) |
| 615 | * |
| 616 | * @returns {string} Filtered delivery method |
| 617 | * @since 5.2.0 |
| 618 | * @hook latepoint_get_default_delivery_method_for_customer_auth |
| 619 | * |
| 620 | */ |
| 621 | return apply_filters( 'latepoint_get_default_delivery_method_for_customer_auth', $delivery_method, $contact_type ); |
| 622 | } |
| 623 | |
| 624 | public static function auth_form_html( $is_classic, OsCustomerModel $customer, string $selected_contact_type_for_auth = '', $selected_delivery_method = '' ): string { |
| 625 | if ( empty( $selected_contact_type_for_auth ) ) { |
| 626 | $selected_contact_type_for_auth = OsAuthHelper::get_default_contact_type_for_customer_auth(); |
| 627 | } |
| 628 | if ( empty( $selected_delivery_method ) ) { |
| 629 | $selected_delivery_method = self::get_default_delivery_method_for_customer_auth_contact_type( $selected_contact_type_for_auth ); |
| 630 | } |
| 631 | $enabled_contact_types_for_customer_auth = OsAuthHelper::get_enabled_contact_types_for_customer_auth(); |
| 632 | $otp_allowed = self::is_otp_auth_enabled(); |
| 633 | $multiple_auth_methods_enabled = ( count( self::get_enabled_customer_authentication_methods() ) > 1 ); |
| 634 | $html = ''; |
| 635 | if ( ! $is_classic ) { |
| 636 | $html .= '<div class="latepoint-customer-auth-wrapper">'; |
| 637 | $html .= '<div class="latepoint-customer-otp-request-wrapper hide-when-entering-otp">'; |
| 638 | // NEW STYLE SHOWING JUST THE AUTH OPTIONS FIRST |
| 639 | if ( in_array( 'email', $enabled_contact_types_for_customer_auth ) ) { |
| 640 | $html .= '<div data-login-method="email" class="customer-login-method-wrapper ' . ( $selected_contact_type_for_auth == 'email' ? '' : 'os-hidden' ) . '">'; |
| 641 | $html .= OsFormHelper::text_field( 'auth[email]', __( 'Your Email Address', 'latepoint' ), $customer->email, array( |
| 642 | 'validate' => $customer->get_validations_for_property( 'email' ), |
| 643 | 'class' => 'required' |
| 644 | ) ); |
| 645 | $html .= '</div>'; |
| 646 | } |
| 647 | if ( in_array( 'phone', $enabled_contact_types_for_customer_auth ) ) { |
| 648 | $html .= '<div data-login-method="phone" class="customer-login-method-wrapper ' . ( $selected_contact_type_for_auth == 'phone' ? '' : 'os-hidden' ) . '">'; |
| 649 | $html .= OsFormHelper::phone_number_field( 'auth[phone]', __( 'Your Phone Number', 'latepoint' ), $customer->phone, array( |
| 650 | 'validate' => $customer->get_validations_for_property( 'phone' ), |
| 651 | 'class' => 'required', |
| 652 | 'theme' => 'simple' |
| 653 | ) ); |
| 654 | $html .= '</div>'; |
| 655 | } |
| 656 | $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>'; |
| 657 | $html .= '</div>'; |
| 658 | $html .= '</div>'; |
| 659 | } else { |
| 660 | ob_start(); |
| 661 | ?> |
| 662 | <div class="os-customer-login-w os-customer-wrapped-box os-unwrapped"> |
| 663 | <?php if ( count( $enabled_contact_types_for_customer_auth ) > 1 ) { ?> |
| 664 | <div class="login-options-wrapper"> |
| 665 | <div class="latepoint-customer-box-title"><?php _e( 'Sign in', 'latepoint' ); ?></div> |
| 666 | <?php if ( in_array( 'email', $enabled_contact_types_for_customer_auth ) && in_array( 'phone', $enabled_contact_types_for_customer_auth ) ) { ?> |
| 667 | <div class="login-options-col login-options-via"> |
| 668 | <div class="login-options-via-wrapper"> |
| 669 | <div data-login-method="email" data-is-otp-enabled="<?php echo OsOTPHelper::is_otp_enabled_for_contact_type( 'email', 'email' ) ? 'yes' : 'no'; ?>" |
| 670 | data-otp-delivery-method="email" |
| 671 | class="login-option <?php echo $selected_contact_type_for_auth == 'email' ? 'os-selected os-default' : ''; ?>"><?php _e( 'Email', 'latepoint' ); ?></div> |
| 672 | <div data-login-method="phone" data-is-otp-enabled="<?php echo OsOTPHelper::is_otp_enabled_for_contact_type( 'phone', 'sms' ) ? 'yes' : 'no'; ?>" |
| 673 | data-otp-delivery-method="sms" |
| 674 | class="login-option <?php echo $selected_contact_type_for_auth == 'phone' ? 'os-selected os-default' : ''; ?>"><?php _e( 'Phone', 'latepoint' ); ?></div> |
| 675 | </div> |
| 676 | </div> |
| 677 | <?php } ?> |
| 678 | </div> |
| 679 | <?php } ?> |
| 680 | <?php |
| 681 | if ( in_array( 'email', $enabled_contact_types_for_customer_auth ) ) { |
| 682 | echo '<div data-login-method="email" class="customer-login-method-wrapper ' . ( $selected_contact_type_for_auth == 'email' ? '' : 'os-hidden' ) . '">'; |
| 683 | echo OsFormHelper::text_field( 'auth[email]', __( 'Your Email Address', 'latepoint' ), '' ); |
| 684 | echo '</div>'; |
| 685 | } |
| 686 | ?> |
| 687 | <?php |
| 688 | if ( in_array( 'phone', $enabled_contact_types_for_customer_auth ) ) { |
| 689 | echo '<div data-login-method="phone" class="customer-login-method-wrapper ' . ( $selected_contact_type_for_auth == 'phone' ? '' : 'os-hidden' ) . '">'; |
| 690 | echo OsFormHelper::phone_number_field( 'auth[phone]', __( 'Your Phone Number', 'latepoint' ), '' ); |
| 691 | echo '</div>'; |
| 692 | } |
| 693 | ?> |
| 694 | <div class="os-customer-login-password-fields-w" <?php echo ( OsAuthHelper::get_default_customer_authentication_method() == 'otp' ) ? 'style="display:none;"' : ''; ?>> |
| 695 | <?php echo OsFormHelper::password_field( 'auth[password]', __( 'Your Password', 'latepoint' ), '', array( 'class' => 'required' ) ); ?> |
| 696 | <a href="#" class="latepoint-btn latepoint-btn-primary latepoint-btn-link step-forgot-password-btn" |
| 697 | data-os-action="<?php echo esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'request_password_reset_token' ) ); ?>" |
| 698 | data-os-output-target=".os-password-reset-form-holder" |
| 699 | data-os-after-call="latepoint_reset_password_from_booking_init" |
| 700 | data-os-params="<?php echo esc_attr( OsUtilHelper::build_os_params( [ 'from_booking' => true ] ) ); ?>"><span><?php esc_html_e( 'Forgot?', 'latepoint' ); ?></span></a> |
| 701 | </div> |
| 702 | |
| 703 | <?php if ( $otp_allowed ) { ?> |
| 704 | <div class="os-customer-otp-notice" <?php echo ( OsAuthHelper::get_default_customer_authentication_method() == 'otp' ) ? '' : 'style="display:none;"'; ?>> |
| 705 | <?php _e( 'You will receive a 6-digit code to log in.', 'latepoint' ); ?> |
| 706 | </div> |
| 707 | <?php } ?> |
| 708 | <div class="os-customer-login-buttons"> |
| 709 | <?php if ( $multiple_auth_methods_enabled ) { ?> |
| 710 | <div class="login-options-col"> |
| 711 | <div class="latepoint-customer-otp-option"> |
| 712 | <?php if ( OsAuthHelper::get_default_customer_authentication_method() == 'otp' ) { ?> |
| 713 | <label><input type="checkbox" name="auth[via]" value="password" |
| 714 | class="login-with-password-toggle os-opposite"><span><?php _e( 'Use password instead', 'latepoint' ); ?></span></label> |
| 715 | <?php } else { ?> |
| 716 | <label><input type="checkbox" name="auth[via]" value="otp" |
| 717 | class="login-with-password-toggle"><span><?php _e( 'Send me a sign-in code', 'latepoint' ); ?></span></label> |
| 718 | <?php } ?> |
| 719 | </div> |
| 720 | </div> |
| 721 | <?php } else { |
| 722 | echo OsFormHelper::hidden_field( 'auth[via]', OsAuthHelper::get_default_customer_authentication_method() ); |
| 723 | } ?> |
| 724 | <a data-otp-request-route="<?php echo esc_attr( OsRouterHelper::build_route_name( 'auth', 'request_otp' ) ); ?>" |
| 725 | data-password-login-route="<?php echo esc_attr( OsRouterHelper::build_route_name( 'auth', 'login_customer' ) ); ?>" href="#" |
| 726 | 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> |
| 727 | </div> |
| 728 | </div> |
| 729 | <?php |
| 730 | $html = ob_get_clean(); |
| 731 | } |
| 732 | $html .= OsFormHelper::hidden_field( 'auth[contact_type]', $selected_contact_type_for_auth ); |
| 733 | $html .= OsFormHelper::hidden_field( 'auth[delivery_method]', $selected_delivery_method ); |
| 734 | $html .= OsFormHelper::hidden_field( 'auth[action]', 'register' ); |
| 735 | $html .= wp_nonce_field( 'auth_nonce', 'auth[nonce]', true, false ); |
| 736 | |
| 737 | |
| 738 | return $html; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | // WP USER |
| 743 | public static function get_logged_in_wp_user_id() { |
| 744 | return OsWpUserHelper::get_current_user_id(); |
| 745 | } |
| 746 | |
| 747 | public static function get_logged_in_wp_user() { |
| 748 | return OsWpUserHelper::get_current_user(); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | // UTILS |
| 753 | |
| 754 | public static function hash_password( $password ) { |
| 755 | return wp_hash_password( $password, PASSWORD_DEFAULT ); |
| 756 | } |
| 757 | |
| 758 | public static function verify_password( $password, $hash ) { |
| 759 | return wp_check_password( $password, $hash ); |
| 760 | } |
| 761 | |
| 762 | } |