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_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
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
customer_helper.php
269 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 | |
| 143 | $email_already_assigned = new OsCustomerModel(); |
| 144 | $email_already_assigned = $email_already_assigned->where([ 'email' => $wp_user->user_email, 'id !=' => $customer->id ])->set_limit( 1 )->get_results_as_models(); |
| 145 | |
| 146 | if (!$email_already_assigned) { |
| 147 | $customer->update_attributes( [ 'email' => $wp_user->user_email ] ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return $customer; |
| 152 | } else { |
| 153 | // check if customer with this email exists |
| 154 | $customer = new OsCustomerModel(); |
| 155 | $customer = $customer->where( [ 'email' => $wp_user->user_email ] )->set_limit( 1 )->get_results_as_models(); |
| 156 | if ( $customer ) { |
| 157 | $old_customer_data = $customer->get_data_vars(); |
| 158 | $customer->update_attributes( [ 'wordpress_user_id' => $wp_user->ID ] ); |
| 159 | do_action( 'latepoint_customer_updated', $customer, $old_customer_data ); |
| 160 | } else { |
| 161 | // create new customer |
| 162 | $customer = new OsCustomerModel(); |
| 163 | $customer->first_name = $wp_user->first_name; |
| 164 | $customer->last_name = $wp_user->last_name; |
| 165 | $customer->email = $wp_user->user_email; |
| 166 | $customer->password = $wp_user->user_pass; |
| 167 | $customer->is_guest = false; |
| 168 | $customer->save( true ); |
| 169 | do_action( 'latepoint_customer_created', $customer ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $customer; |
| 174 | } |
| 175 | |
| 176 | public static function count_customers_not_connected_to_wp_users() { |
| 177 | $customers = new OsCustomerModel(); |
| 178 | |
| 179 | return $customers->where( [ 'wordpress_user_id' => [ 'OR' => [ 0, 'IS NULL' ] ] ] )->count(); |
| 180 | } |
| 181 | |
| 182 | public static function get_by_account_nonse( $account_nonse ) { |
| 183 | if ( empty( $account_nonse ) ) { |
| 184 | return false; |
| 185 | } |
| 186 | $account_nonse = sanitize_text_field( $account_nonse ); |
| 187 | $customer = new OsCustomerModel(); |
| 188 | |
| 189 | return $customer->where( [ 'account_nonse' => $account_nonse ] )->set_limit( 1 )->get_results_as_models(); |
| 190 | } |
| 191 | |
| 192 | public static function create_wp_user_for_customer( $customer ) { |
| 193 | // NO connected wp user, create one |
| 194 | // check if wp user with this customer email already exists |
| 195 | $wp_user_id = email_exists( $customer->email ); |
| 196 | if ( ! $wp_user_id ) { |
| 197 | $wp_user_id = username_exists( $customer->email ); |
| 198 | } |
| 199 | if ( $wp_user_id ) { |
| 200 | // wp user with this email or username exists - check if its linked to another customer already - if not link it to current customer |
| 201 | $linked_customer = new OsCustomerModel(); |
| 202 | $linked_customer = $linked_customer->where( [ 'wordpress_user_id' => $wp_user_id ] )->set_limit( 1 )->get_results_as_models(); |
| 203 | if ( $linked_customer ) { |
| 204 | // wp user with this email exists and is linked already to a different latepoint customer |
| 205 | $customer->add_error( 'customer_exists', __( 'Customer with this email already exists', 'latepoint' ) ); |
| 206 | } else { |
| 207 | $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id, 'is_guest' => false ] ); |
| 208 | } |
| 209 | } else { |
| 210 | $userdata = [ |
| 211 | 'user_email' => $customer->email, |
| 212 | 'first_name' => $customer->first_name, |
| 213 | 'last_name' => $customer->last_name, |
| 214 | 'user_login' => $customer->email, |
| 215 | 'user_pass' => $customer->password |
| 216 | ]; |
| 217 | $wp_user_id = wp_insert_user( $userdata ); |
| 218 | if ( ! is_wp_error( $wp_user_id ) ) { |
| 219 | $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id, 'is_guest' => false ] ); |
| 220 | // update password directly in database because we already hashed it in latepoint customer |
| 221 | global $wpdb; |
| 222 | $wpdb->update( |
| 223 | $wpdb->users, |
| 224 | array( |
| 225 | 'user_pass' => $customer->password, |
| 226 | 'user_activation_key' => '', |
| 227 | ), |
| 228 | array( 'ID' => $wp_user_id ) |
| 229 | ); |
| 230 | } else { |
| 231 | OsDebugHelper::log( 'Error creating WP User for customer', 'registration_error', [ 'errors' => $wp_user_id->get_error_messages() ] ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return ( ! is_wp_error( $wp_user_id ) ) ? $wp_user_id : false; |
| 236 | } |
| 237 | |
| 238 | public static function generate_booking_summary_preview_btn( int $booking_id ): string { |
| 239 | $html = 'data-os-after-call="latepoint_init_booking_summary_lightbox" |
| 240 | data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'booking_id' => $booking_id ] )) . '" |
| 241 | data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_booking_summary_in_lightbox' )) . '" |
| 242 | data-os-output-target="lightbox" |
| 243 | data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"'; |
| 244 | |
| 245 | return $html; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | public static function generate_bundle_scheduling_btn( int $order_item_id ): string { |
| 250 | $html = 'data-os-after-call="latepoint_init_bundle_scheduling_summary" |
| 251 | data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_item_id' => $order_item_id ] )) . '" |
| 252 | data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'scheduling_summary_for_bundle' )) . '" |
| 253 | data-os-output-target="lightbox" |
| 254 | data-os-lightbox-classes="width-500 customer-dashboard-bundle-scheduling-summary"'; |
| 255 | |
| 256 | return $html; |
| 257 | } |
| 258 | |
| 259 | public static function generate_order_summary_btn( int $order_id ): string { |
| 260 | $html = 'data-os-after-call="latepoint_init_order_summary_lightbox" |
| 261 | data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_id' => $order_id ] )) . '" |
| 262 | data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_order_summary_in_lightbox' )) . '" |
| 263 | data-os-output-target="lightbox" |
| 264 | data-os-lightbox-classes="width-500 customer-dashboard-order-summary-lightbox"'; |
| 265 | |
| 266 | return $html; |
| 267 | } |
| 268 | |
| 269 | } |