PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.1.1
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.1.1
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 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / customer_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 1 year 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 1 year ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 1 year ago customer_helper.php 1 year ago database_helper.php 1 year ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 1 year ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 1 year ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 1 year 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 1 year ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 1 year ago order_intent_helper.php 1 year ago orders_helper.php 1 year 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 1 year ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 1 year ago shortcodes_helper.php 1 year ago sms_helper.php 1 year ago steps_helper.php 1 year ago stripe_connect_helper.php 1 year ago styles_helper.php 1 year ago support_topics_helper.php 1 year ago time_helper.php 1 year ago timeline_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 1 year ago version_specific_updates_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
customer_helper.php
264 lines
1 <?php
2
3 class OsCustomerHelper {
4
5
6 public static function quick_customer_btn_html( $customer_id = false, $params = array() ) {
7 $html = '';
8 if ( $customer_id ) {
9 $params['customer_id'] = $customer_id;
10 }
11 $route = OsRouterHelper::build_route_name( 'customers', !empty($customer_id) ? 'quick_edit' : 'quick_new' );
12
13 $params_str = http_build_query( $params );
14 $html = 'data-os-params="' . esc_attr($params_str) . '"
15 data-os-action="' . esc_attr($route) . '"
16 data-os-output-target="side-panel"
17 data-os-after-call="latepoint_init_quick_customer_form"';
18
19 return $html;
20 }
21
22 public static function generate_summary_for_customer( OsCustomerModel $customer ): void {
23 ?>
24 <div class="summary-box summary-box-customer-info">
25 <div class="summary-box-heading">
26 <div class="sbh-item"><?php esc_html_e( 'Customer', 'latepoint' ) ?></div>
27 <div class="sbh-line"></div>
28 </div>
29 <div class="summary-box-content with-media">
30 <div class="os-avatar-w">
31 <div class="os-avatar"><span><?php echo esc_html( $customer->get_initials() ); ?></span></div>
32 </div>
33 <div class="sbc-content-i">
34 <div class="sbc-main-item"><?php echo esc_html( $customer->full_name ); ?></div>
35 <div class="sbc-sub-item"><?php echo esc_html( $customer->email ); ?></div>
36 </div>
37 </div>
38 <?php
39 $customer_attributes = [];
40 $customer_attributes = apply_filters( 'latepoint_booking_summary_customer_attributes', $customer_attributes, $customer );
41 if ( $customer_attributes ) {
42 echo '<div class="summary-attributes sa-clean sa-hidden">';
43 foreach ( $customer_attributes as $attribute ) {
44 echo '<span>' . esc_html( $attribute['label'] ) . ': <strong>' . esc_html( $attribute['value'] ) . '</strong></span>';
45 }
46 echo '</div>';
47 }
48 ?>
49 </div>
50 <?php
51 }
52
53 public static function get_customers_for_select() {
54 $customers = new OsCustomerModel();
55 $customers = $customers->set_limit( 100 )->get_results_as_models();
56 $customers_options = [];
57 foreach ( $customers as $customer ) {
58 $customers_options[] = [ 'value' => $customer->id, 'label' => esc_html( $customer->full_name ) ];
59 }
60
61 return $customers_options;
62 }
63
64 public static function get_full_name( $customer ) {
65 return join( ' ', array( $customer->first_name, $customer->last_name ) );
66 }
67
68 public static function get_avatar_url( $customer ) {
69 $default_avatar = LATEPOINT_IMAGES_URL . 'default-avatar.jpg';
70 if ( OsAuthHelper::wp_users_as_customers() && $customer->wordpress_user_id && empty( $customer->avatar_image_id ) ) {
71 // try to get gravatar with WP function
72 $avatar_url = get_avatar_url( $customer->wordpress_user_id );
73 } else {
74 $avatar_url = false;
75 }
76 if ( ! $avatar_url ) {
77 $avatar_url = OsImageHelper::get_image_url_by_id( $customer->avatar_image_id, 'thumbnail', $default_avatar );
78 }
79
80 return $avatar_url;
81 }
82
83
84 public static function get_avatar_image( $customer ) {
85 return '<img src="' . self::get_avatar_url( $customer ) . '"/>';
86 }
87
88
89 public static function total_new_customers_for_date( $date ) {
90 $customers = new OsCustomerModel();
91 $customers = $customers->where( array( 'DATE(created_at)' => $date ) );
92
93 return $customers->count();
94 }
95
96 public static function can_cancel_booking( OsBookingModel $booking ): bool {
97 if ( OsSettingsHelper::is_on( 'allow_customer_booking_cancellation' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
98 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_cancel' ) ) {
99 // check if there is a limit on when they can cancel
100 $limit_value = OsSettingsHelper::get_settings_value( 'cancellation_limit_value' );
101 $limit_unit = OsSettingsHelper::get_settings_value( 'cancellation_limit_unit' );
102 if ( $limit_value && $limit_unit ) {
103 $now = new OsWpDateTime( 'now' );
104 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
105 return true;
106 }
107 }
108 } else {
109 return true;
110 }
111 }
112
113 return false;
114 }
115
116 public static function can_reschedule_booking( OsBookingModel $booking ): bool {
117 if ( OsSettingsHelper::is_on( 'allow_customer_booking_reschedule' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
118 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_reschedule' ) ) {
119 // check if there is a limit on when they can reschedule
120 $limit_value = OsSettingsHelper::get_settings_value( 'reschedule_limit_value' );
121 $limit_unit = OsSettingsHelper::get_settings_value( 'reschedule_limit_unit' );
122 if ( $limit_value && $limit_unit ) {
123 $now = new OsWpDateTime( 'now' );
124 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
125 return true;
126 }
127 }
128 } else {
129 return true;
130 }
131 }
132
133 return false;
134 }
135
136
137 public static function get_customer_for_wp_user( $wp_user ) {
138 $customer = new OsCustomerModel();
139 $customer = $customer->where( [ 'wordpress_user_id' => $wp_user->ID ] )->set_limit( 1 )->get_results_as_models();
140 if ( $customer ) {
141 if ( $customer->email != $wp_user->user_email ) {
142 // TODO: check if other customer already has this email
143 $customer->update_attributes( [ 'email' => $wp_user->user_email ] );
144 }
145
146 return $customer;
147 } else {
148 // check if customer with this email exists
149 $customer = new OsCustomerModel();
150 $customer = $customer->where( [ 'email' => $wp_user->user_email ] )->set_limit( 1 )->get_results_as_models();
151 if ( $customer ) {
152 $old_customer_data = $customer->get_data_vars();
153 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user->ID ] );
154 do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
155 } else {
156 // create new customer
157 $customer = new OsCustomerModel();
158 $customer->first_name = $wp_user->first_name;
159 $customer->last_name = $wp_user->last_name;
160 $customer->email = $wp_user->user_email;
161 $customer->password = $wp_user->user_pass;
162 $customer->is_guest = false;
163 $customer->save( true );
164 do_action( 'latepoint_customer_created', $customer );
165 }
166 }
167
168 return $customer;
169 }
170
171 public static function count_customers_not_connected_to_wp_users() {
172 $customers = new OsCustomerModel();
173
174 return $customers->where( [ 'wordpress_user_id' => [ 'OR' => [ 0, 'IS NULL' ] ] ] )->count();
175 }
176
177 public static function get_by_account_nonse( $account_nonse ) {
178 if ( empty( $account_nonse ) ) {
179 return false;
180 }
181 $account_nonse = sanitize_text_field( $account_nonse );
182 $customer = new OsCustomerModel();
183
184 return $customer->where( [ 'account_nonse' => $account_nonse ] )->set_limit( 1 )->get_results_as_models();
185 }
186
187 public static function create_wp_user_for_customer( $customer ) {
188 // NO connected wp user, create one
189 // check if wp user with this customer email already exists
190 $wp_user_id = email_exists( $customer->email );
191 if ( ! $wp_user_id ) {
192 $wp_user_id = username_exists( $customer->email );
193 }
194 if ( $wp_user_id ) {
195 // wp user with this email or username exists - check if its linked to another customer already - if not link it to current customer
196 $linked_customer = new OsCustomerModel();
197 $linked_customer = $linked_customer->where( [ 'wordpress_user_id' => $wp_user_id ] )->set_limit( 1 )->get_results_as_models();
198 if ( $linked_customer ) {
199 // wp user with this email exists and is linked already to a different latepoint customer
200 $customer->add_error( 'customer_exists', __( 'Customer with this email already exists', 'latepoint' ) );
201 } else {
202 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id, 'is_guest' => false ] );
203 }
204 } else {
205 $userdata = [
206 'user_email' => $customer->email,
207 'first_name' => $customer->first_name,
208 'last_name' => $customer->last_name,
209 'user_login' => $customer->email,
210 'user_pass' => $customer->password
211 ];
212 $wp_user_id = wp_insert_user( $userdata );
213 if ( ! is_wp_error( $wp_user_id ) ) {
214 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id, 'is_guest' => false ] );
215 // update password directly in database because we already hashed it in latepoint customer
216 global $wpdb;
217 $wpdb->update(
218 $wpdb->users,
219 array(
220 'user_pass' => $customer->password,
221 'user_activation_key' => '',
222 ),
223 array( 'ID' => $wp_user_id )
224 );
225 } else {
226 OsDebugHelper::log( 'Error creating WP User for customer', 'registration_error', [ 'errors' => $wp_user_id->get_error_messages() ] );
227 }
228 }
229
230 return ( ! is_wp_error( $wp_user_id ) ) ? $wp_user_id : false;
231 }
232
233 public static function generate_booking_summary_preview_btn( int $booking_id ): string {
234 $html = 'data-os-after-call="latepoint_init_booking_summary_lightbox"
235 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'booking_id' => $booking_id ] )) . '"
236 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_booking_summary_in_lightbox' )) . '"
237 data-os-output-target="lightbox"
238 data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"';
239
240 return $html;
241 }
242
243
244 public static function generate_bundle_scheduling_btn( int $order_item_id ): string {
245 $html = 'data-os-after-call="latepoint_init_bundle_scheduling_summary"
246 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_item_id' => $order_item_id ] )) . '"
247 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'scheduling_summary_for_bundle' )) . '"
248 data-os-output-target="lightbox"
249 data-os-lightbox-classes="width-500 customer-dashboard-bundle-scheduling-summary"';
250
251 return $html;
252 }
253
254 public static function generate_order_summary_btn( int $order_id ): string {
255 $html = 'data-os-after-call="latepoint_init_order_summary_lightbox"
256 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_id' => $order_id ] )) . '"
257 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_order_summary_in_lightbox' )) . '"
258 data-os-output-target="lightbox"
259 data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"';
260
261 return $html;
262 }
263
264 }