PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / admin / CustomersController.php

CustomersController.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/admin/CustomersController.php

327 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes\Admin;
4
5 use Bookit\Classes\Base\User;
6 use Bookit\Classes\Database\Appointments;
7 use Bookit\Classes\Database\Customers;
8 use Bookit\Classes\Template;
9 use Bookit\Helpers\CleanHelper;
10
11 class CustomersController extends DashboardController {
12
13 private static $sortFields = array( 'id', 'full_name', 'email', 'phone' );
14
15 private static function getCleanRules() {
16 return array(
17 'id' => array( 'type' => 'intval' ),
18 'limit' => array( 'type' => 'intval' ),
19 'offset' => array( 'type' => 'intval' ),
20 'sort' => array( 'type' => 'strval' ),
21 'order' => array( 'type' => 'strval' ),
22 'search' => array( 'type' => 'strval' ),
23 'full_name' => array( 'type' => 'strval' ),
24 'phone' => array(
25 'function' => array(
26 'custom' => true,
27 'name' => 'custom_sanitize_phone',
28 ),
29 ),
30 'email' => array(
31 'type' => 'strval',
32 'function' => array(
33 'custom' => false,
34 'name' => 'sanitize_email',
35 ),
36 ),
37 );
38 }
39
40 /**
41 * Display Rendered Template
42 * @return bool|string
43 */
44 public static function render() {
45 self::enqueue_styles_scripts();
46
47 $user_args = array(
48 'fields' => array( 'ID', 'display_name' ),
49 );
50 $wp_users = get_users( $user_args );
51
52 return Template::load_template(
53 'dashboard/bookit-customers',
54 array(
55 'wp_users' => $wp_users,
56 'page' => __( 'Customers', 'bookit' ),
57 ),
58 true
59 );
60 }
61
62 /**
63 * Whether the account-lookup endpoint should respond. It supports the
64 * logged-out registered-booking flow, so limit availability to that case.
65 *
66 * @since 2.6.0
67 *
68 * @return bool
69 */
70 private static function account_lookup_available() {
71 return is_user_logged_in()
72 || 'registered' === get_option_by_path( 'bookit_settings.booking_type' );
73 }
74
75 /**
76 * Tell the registered-booking form whether an account already exists for
77 * an email. Limited to the registered-booking flow.
78 *
79 * @since 2.6.0
80 */
81 public static function get_wp_user_by_email() {
82 check_ajax_referer( 'bookit_get_wp_user_by_email', 'nonce' );
83
84 if ( ! self::account_lookup_available() ) {
85 wp_send_json_error( array( 'message' => __( 'Not available.', 'bookit' ) ) );
86 }
87
88 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
89 if ( empty( $email ) ) {
90 wp_send_json_success( array( 'exist' => false ) );
91 }
92
93 $exist = get_user_by( 'email', $email ) instanceof \WP_User;
94
95 wp_send_json_success( array( 'exist' => $exist ) );
96 }
97
98 /**
99 * Get Customers with Pagination
100 */
101 public static function get_customers() {
102 check_ajax_referer( 'bookit_get_customers', 'nonce' );
103
104 if ( ! current_user_can( 'manage_options' ) ) {
105 return false;
106 }
107
108 global $wpdb;
109 $data = CleanHelper::cleanData( $_GET, self::getCleanRules() );
110
111 if ( ! empty( $data['limit'] ) ) {
112 $search_where = '';
113 $search_values = array();
114 if ( ! empty( $data['search'] ) ) {
115 $like = '%' . $wpdb->esc_like( $data['search'] ) . '%';
116 $search_where = 'WHERE full_name LIKE %s OR email LIKE %s OR phone LIKE %s';
117 $search_values = array( $like, $like, $like );
118 }
119 $response['customers'] = Customers::get_paged(
120 $data['limit'],
121 $data['offset'],
122 $search_where,
123 ( isset( $data['sort'] ) && in_array( $data['sort'], self::$sortFields ) ) ? $data['sort'] : '',
124 ( isset( $data['order'] ) && in_array( $data['order'], array( 'asc', 'desc' ) ) ) ? $data['order'] : '',
125 $search_values
126 );
127 $response['total'] = ( ! empty( $search_where ) ) ? count( $response['customers'] ) : Customers::get_count();
128
129 array_walk(
130 $response['customers'],
131 function ( &$value, $key ) {
132 $value['full_name'] = stripslashes( $value['full_name'] );
133 }
134 );
135
136 wp_send_json_success( $response );
137 }
138
139 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
140 }
141
142 /**
143 * Validate post fields
144 */
145 public static function validate( $data ) {
146 $errors = array();
147
148 if ( $data['phone'] || false === $data['phone'] ) {
149 if ( ! preg_match( '/^((\+)?[0-9]{9,14})$/', $data['phone'] ) ) {
150 $errors['phone'] = esc_html__( 'Please enter a valid phone number', 'bookit' );
151 }
152 }
153
154 if ( ! $data['email'] ) {
155 $errors['email'] = esc_html__( 'Email is required', 'bookit' );
156 }
157
158 if ( $data['email'] && ! is_email( $data['email'] ) ) {
159 $errors['email'] = __( 'Not valid Email', 'bookit' );
160 }
161
162 $customer = Customers::get( 'email', $data['email'] );
163 if ( null != $customer && ! isset( $data['id'] ) ) {
164 $errors['email'] = __( 'Customer with such email already exist', 'bookit' );
165 }
166
167 if ( $data['full_name'] ) {
168 if ( strlen( $data['full_name'] ) < 3 || strlen( $data['full_name'] ) > 25 ) {
169 $errors['full_name'] = __( 'Full name must be between 3 and 25 characters long', 'bookit' );
170 }
171 } else {
172 $errors['full_name'] = __( 'Full Name is required.', 'bookit' );
173 }
174 $settings_booking_type = get_option_by_path( 'bookit_settings.booking_type' );
175 if ( isset( $data['from'] ) && 'calendar' == $data['from'] && 'registered' == $settings_booking_type ) {
176
177 if ( empty( $data['password'] ) ) {
178 $errors['password'] = __( 'Please enter a password', 'bookit' );
179 }
180
181 if ( false !== strpos( wp_unslash( $data['password'] ), '\\' ) ) {
182 $errors['password'] = __( "Passwords may not contain the character '\\'", 'bookit' );
183 }
184
185 if ( ( ! empty( $data['password'] ) ) && $data['password'] != $data['password_confirmation'] ) {
186 $errors['password_confirmation'] = __( 'Please enter the same password in both password fields', 'bookit' );
187 }
188 }
189
190 if ( count( $errors ) > 0 ) {
191 wp_send_json_error(
192 array(
193 'errors' => $errors,
194 'message' => __( 'Error occurred!', 'bookit' ),
195 )
196 );
197 }
198 }
199
200 /**
201 * Create Customer from appointment
202 */
203 public static function create() {
204 check_ajax_referer( 'bookit_save_item', 'nonce' );
205
206 if ( ! current_user_can( 'manage_options' ) ) {
207 return false;
208 }
209
210 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
211 self::validate( $data );
212 unset( $data['id'] );
213
214 $exist = Customers::get( 'email', $data['email'] );
215 if ( $exist ) {
216 wp_send_json_error(
217 array(
218 'errors' => array( 'email' => __( 'Customer with such email already exist.', 'bookit' ) ),
219 'message' => __( 'Error occurred!', 'bookit' ),
220 )
221 );
222 }
223
224 /** save in WordPress users based on booking type value */
225 $settings_booking_type = get_option_by_path( 'bookit_settings.booking_type' );
226 if ( 'registered' == $settings_booking_type ) {
227 $data['role'] = User::$customer_role;
228 $data['wp_user_id'] = Customers::save_or_get_wp_user( $data );
229 }
230
231 unset( $data['role'] );
232 unset( $data['password'] );
233 unset( $data['password_confirmation'] );
234 unset( $data['from'] );
235
236 Customers::insert( $data );
237 $customer = Customers::get( 'id', Customers::insert_id() );
238
239 wp_send_json_success(
240 array(
241 'customer' => $customer,
242 'message' => __( 'Customer Saved!', 'bookit' ),
243 )
244 );
245 }
246
247 /**
248 * Save Customer
249 */
250 public static function save() {
251 check_ajax_referer( 'bookit_save_item', 'nonce' );
252
253 if ( ! current_user_can( 'manage_options' ) ) {
254 return false;
255 }
256
257 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
258 self::validate( $data );
259
260 if ( ! empty( $data ) ) {
261 if ( ! empty( $data['id'] ) ) {
262 Customers::update( $data, array( 'id' => $data['id'] ) );
263 } else {
264 Customers::insert( $data );
265 $data['id'] = Customers::insert_id();
266 }
267
268 do_action( 'bookit_customer_saved', $data['id'] );
269
270 $customer = Customers::get( 'id', $data['id'] );
271
272 wp_send_json_success(
273 array(
274 'id' => $data['id'],
275 'customer' => $customer,
276 'message' => __( 'Customer Saved!', 'bookit' ),
277 )
278 );
279 }
280
281 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
282 }
283
284 /** Get Service Assosiated data by id **/
285 public static function get_assosiated_total_data_by_id() {
286 check_ajax_referer( 'bookit_get_customer_assosiated_total_data', 'nonce' );
287
288 if ( ! current_user_can( 'manage_options' ) ) {
289 return false;
290 }
291
292 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
293
294 if ( empty( $data['id'] ) ) {
295 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
296 }
297
298 $appointments = Appointments::get_total_active_assosiated_appointments( '', '', $data['id'] );
299
300 $response = array( 'total' => array( 'appointments' => $appointments ) );
301 wp_send_json_success( $response );
302 }
303
304 /**
305 * Delete the Customer
306 */
307 public static function delete() {
308 check_ajax_referer( 'bookit_delete_item', 'nonce' );
309
310 if ( ! current_user_can( 'manage_options' ) ) {
311 return false;
312 }
313
314 $data = CleanHelper::cleanData( $_GET, self::getCleanRules() );
315
316 if ( isset( $data['id'] ) ) {
317
318 $id = $data['id'];
319 Customers::deleteCustomer( $id );
320 do_action( 'bookit_customer_deleted', $id );
321 wp_send_json_success( array( 'message' => __( 'Customer Deleted!', 'bookit' ) ) );
322 }
323
324 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
325 }
326 }
327