activities_controller.php
1 year ago
auth_controller.php
1 year ago
booking_form_settings_controller.php
1 year ago
bookings_controller.php
1 year ago
calendars_controller.php
1 year ago
carts_controller.php
1 year ago
controller.php
1 year ago
customer_cabinet_controller.php
1 year ago
customers_controller.php
1 year ago
dashboard_controller.php
1 year ago
default_agent_controller.php
1 year ago
events_controller.php
1 year ago
form_fields_controller.php
1 year ago
integrations_controller.php
1 year ago
invoices_controller.php
1 year ago
manage_booking_by_key_controller.php
1 year ago
manage_order_by_key_controller.php
1 year ago
notifications_controller.php
1 year ago
orders_controller.php
1 year ago
pro_controller.php
1 year ago
process_jobs_controller.php
1 year ago
processes_controller.php
1 year ago
search_controller.php
1 year ago
services_controller.php
1 year ago
settings_controller.php
1 year ago
steps_controller.php
1 year ago
stripe_connect_controller.php
1 year ago
support_topics_controller.php
1 year ago
todos_controller.php
1 year ago
transactions_controller.php
1 year ago
wizard_controller.php
1 year ago
auth_controller.php
136 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if ( ! class_exists( 'OsAuthController' ) ) : |
| 8 | |
| 9 | |
| 10 | class OsAuthController extends OsController { |
| 11 | |
| 12 | function __construct() { |
| 13 | parent::__construct(); |
| 14 | $this->action_access['public'] = array_merge( $this->action_access['public'], [ |
| 15 | 'logout_customer', |
| 16 | 'login_customer', |
| 17 | 'login_customer_using_social_data', |
| 18 | 'login_customer_using_google_token', |
| 19 | 'login_customer_using_facebook_token' |
| 20 | ] ); |
| 21 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'auth/'; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | // Logs out customer and shows blank contact step |
| 26 | public function logout_customer() { |
| 27 | OsAuthHelper::logout_customer(); |
| 28 | |
| 29 | if ( $this->get_return_format() == 'json' ) { |
| 30 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => __( 'You have been logged out of your account.', 'latepoint' ) ) ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Login customer and show contact step with prefilled info |
| 35 | public function login_customer() { |
| 36 | $customer = OsAuthHelper::login_customer( $this->params['email'], $this->params['password'] ); |
| 37 | if ( $customer ) { |
| 38 | $status = LATEPOINT_STATUS_SUCCESS; |
| 39 | $customer_id = $customer->id; |
| 40 | $response_html = __( 'Welcome back', 'latepoint' ); |
| 41 | } else { |
| 42 | $status = LATEPOINT_STATUS_ERROR; |
| 43 | $response_html = __( 'Sorry, that email or password didn\'t work.', 'latepoint' ); |
| 44 | $customer_id = ''; |
| 45 | } |
| 46 | if ( $this->get_return_format() == 'json' ) { |
| 47 | $this->send_json( array( 'status' => $status, 'message' => $response_html, 'customer_id' => $customer_id ) ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function login_customer_using_social_data( $network, $social_user ) { |
| 52 | $customer_id = ''; |
| 53 | if ( isset( $social_user['social_id'] ) ) { |
| 54 | $customer_was_updated = false; |
| 55 | $old_customer_data = []; |
| 56 | $social_id_field_name = $network . '_user_id'; |
| 57 | $status = LATEPOINT_STATUS_SUCCESS; |
| 58 | $response_html = $social_user['social_id']; |
| 59 | // Search for existing customer with email that google provided |
| 60 | $customer = new OsCustomerModel(); |
| 61 | $customer = $customer->where( array( 'email' => $social_user['email'] ) )->set_limit( 1 )->get_results_as_models(); |
| 62 | if ( OsAuthHelper::wp_users_as_customers() ) { |
| 63 | if ( $customer->wordpress_user_id != email_exists( $social_user['email'] ) ) { |
| 64 | $old_customer_data = $customer->get_data_vars(); |
| 65 | $customer->update_attributes( [ 'wordpress_user_id' => null ] ); |
| 66 | $wp_user_id = OsCustomerHelper::create_wp_user_for_customer( $customer ); |
| 67 | $customer_was_updated = true; |
| 68 | if ( ! $wp_user_id ) { |
| 69 | $status = LATEPOINT_STATUS_ERROR; |
| 70 | $response_html = __( 'Error creating wp user', 'latepoint' ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | // Create customer if its not found |
| 75 | if ( ! $customer ) { |
| 76 | $customer = new OsCustomerModel(); |
| 77 | $customer->first_name = $social_user['first_name']; |
| 78 | $customer->last_name = $social_user['last_name']; |
| 79 | $customer->email = $social_user['email']; |
| 80 | $customer->$social_id_field_name = $social_user['social_id']; |
| 81 | if ( ! $customer->save( true ) ) { |
| 82 | $response_html = $customer->get_error_messages(); |
| 83 | $status = LATEPOINT_STATUS_ERROR; |
| 84 | } else { |
| 85 | do_action( 'latepoint_customer_created', $customer ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if ( ( $status == LATEPOINT_STATUS_SUCCESS ) && $customer->id ) { |
| 90 | $customer_id = $customer->id; |
| 91 | // Update customer google user id if its not set yet |
| 92 | if ( $customer->$social_id_field_name != $social_user['social_id'] ) { |
| 93 | $old_customer_data = $customer->get_data_vars(); |
| 94 | $customer->$social_id_field_name = $social_user['social_id']; |
| 95 | $customer->save(); |
| 96 | $customer_was_updated = true; |
| 97 | } |
| 98 | OsAuthHelper::authorize_customer( $customer->id ); |
| 99 | $response_html = __( 'Welcome back', 'latepoint' ); |
| 100 | } |
| 101 | if ( $customer_was_updated && $old_customer_data ) { |
| 102 | do_action( 'latepoint_customer_updated', $customer, $old_customer_data ); |
| 103 | } |
| 104 | } else { |
| 105 | // ERROR WITH GOOGLE LOGIN |
| 106 | $status = LATEPOINT_STATUS_ERROR; |
| 107 | $response_html = $social_user['error']; |
| 108 | } |
| 109 | if ( $this->get_return_format() == 'json' ) { |
| 110 | $this->send_json( array( 'status' => $status, 'message' => $response_html, 'customer_id' => $customer_id ) ); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | public function login_customer_using_google_token() { |
| 117 | $social_user = []; |
| 118 | $token = sanitize_text_field( $this->params['token'] ); |
| 119 | $social_user = apply_filters( 'latepoint_get_social_user_by_token', $social_user, 'google', $token ); |
| 120 | if ( !empty($social_user) ) { |
| 121 | $this->login_customer_using_social_data( 'google', $social_user ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public function login_customer_using_facebook_token() { |
| 126 | $social_user = []; |
| 127 | $token = sanitize_text_field( $this->params['token'] ); |
| 128 | $social_user = apply_filters( 'latepoint_get_social_user_by_token', $social_user, 'facebook', $token ); |
| 129 | if ( !empty($social_user) ) { |
| 130 | $this->login_customer_using_social_data( 'facebook', $social_user ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | } |
| 136 | endif; |