PluginProbe
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.1.92
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.1.92
5.6.11 5.6.10 5.6.9 5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 All 48 releases
latepoint / lib / controllers / auth_controller.php
auth_controller.php
136 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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;