· 5 years ago · Sep 14, 2020, 02:26 PM
1<?php
2// Exit if accessed directly.
3defined( 'ABSPATH' ) || exit;
4
5/**
6 * WC_Appointments_Admin_Settings
7 */
8class WC_Appointments_Admin_Settings extends WC_Settings_Page {
9
10 // Gcal instance.
11 public $gcal = null;
12
13 /**
14 * Setup settings class
15 *
16 * @since 1.0
17 */
18 public function __construct() {
19 // Appointments Settings ID.
20 $this->id = 'appointments';
21 $this->label = __( 'Appointments', 'woocommerce-appointments' );
22
23 add_action( 'woocommerce_admin_field_gcal_authorization', array( $this, 'gcal_authorization_setting' ) );
24 add_action( 'woocommerce_admin_field_gcal_calendar_id', array( $this, 'gcal_calendar_id_setting' ) );
25 add_action( 'woocommerce_admin_field_manual_sync', array( $this, 'manual_sync_setting' ) );
26
27 parent::__construct();
28 }
29
30 /**
31 * Get sections
32 *
33 * @return array
34 */
35 public function get_sections() {
36 $sections = array(
37 '' => __( 'Global Availability', 'woocommerce-appointments' ),
38 'gcal' => __( 'Google Calendar', 'woocommerce-appointments' ),
39 );
40
41 return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
42 }
43
44 /**
45 * Output the settings
46 *
47 * @since 1.0
48 */
49 public function output() {
50 global $current_section;
51
52 if ( '' == $current_section ) {
53 include 'views/html-settings-global-availability.php';
54 } else {
55 wp_enqueue_script( 'wc_appointments_writepanel_js' );
56 $settings = $this->get_settings( $current_section );
57 WC_Admin_Settings::output_fields( $settings );
58 }
59 }
60
61 /**
62 * Save settings
63 */
64 public function save() {
65 global $current_section;
66
67 if ( '' == $current_section ) {
68 $this->save_global_availability();
69 } else {
70 $settings = $this->get_settings( $current_section );
71 WC_Admin_Settings::save_fields( $settings );
72 }
73
74 if ( $current_section ) {
75 do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
76 }
77 }
78
79 /**
80 * Save global availability
81 */
82 public function save_global_availability() {
83 // Save the field values
84 if ( ! empty( $_POST['appointments_availability_submitted'] ) ) {
85
86 // Delete.
87 if ( ! empty( $_POST['wc_appointment_availability_deleted'] ) ) {
88 $deleted_ids = array_filter( explode( ',', wc_clean( wp_unslash( $_POST['wc_appointment_availability_deleted'] ) ) ) );
89
90 foreach ( $deleted_ids as $delete_id ) {
91 $availability_object = get_wc_appointments_availability( $delete_id );
92 $availability_object->delete();
93 }
94 }
95
96 // Save.
97 $types = isset( $_POST['wc_appointment_availability_type'] ) ? wc_clean( wp_unslash( $_POST['wc_appointment_availability_type'] ) ) : array();
98 $row_size = count( $types );
99
100 for ( $i = 0; $i < $row_size; $i ++ ) {
101 if ( isset( $_POST['wc_appointment_availability_id'][ $i ] ) ) {
102 $current_id = intval( $_POST['wc_appointment_availability_id'][ $i ] );
103 } else {
104 $current_id = 0;
105 }
106
107 $availability = get_wc_appointments_availability( $current_id );
108 $availability->set_ordering( $i );
109 $availability->set_range_type( $types[ $i ] );
110 $availability->set_kind( 'availability#global' );
111
112 if ( isset( $_POST['wc_appointment_availability_appointable'][ $i ] ) ) {
113 $availability->set_appointable( wc_clean( wp_unslash( $_POST['wc_appointment_availability_appointable'][ $i ] ) ) );
114 }
115
116 if ( isset( $_POST['wc_appointment_availability_title'][ $i ] ) ) {
117 $availability->set_title( sanitize_text_field( wp_unslash( $_POST['wc_appointment_availability_title'][ $i ] ) ) );
118 }
119
120 if ( isset( $_POST['wc_appointment_availability_qty'][ $i ] ) ) {
121 $availability->set_qty( intval( $_POST['wc_appointment_availability_qty'][ $i ] ) );
122 }
123
124 if ( isset( $_POST['wc_appointment_availability_priority'][ $i ] ) ) {
125 $availability->set_priority( intval( $_POST['wc_appointment_availability_priority'][ $i ] ) );
126 }
127
128 switch ( $availability->get_range_type() ) {
129 case 'custom':
130 if ( isset( $_POST['wc_appointment_availability_from_date'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_date'][ $i ] ) ) {
131 $availability->set_from_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_from_date'][ $i ] ) ) );
132 $availability->set_to_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_to_date'][ $i ] ) ) );
133 }
134 break;
135 case 'months':
136 if ( isset( $_POST['wc_appointment_availability_from_month'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_month'][ $i ] ) ) {
137 $availability->set_from_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_from_month'][ $i ] ) ) );
138 $availability->set_to_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_to_month'][ $i ] ) ) );
139 }
140 break;
141 case 'weeks':
142 if ( isset( $_POST['wc_appointment_availability_from_week'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_week'][ $i ] ) ) {
143 $availability->set_from_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_from_week'][ $i ] ) ) );
144 $availability->set_to_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_to_week'][ $i ] ) ) );
145 }
146 break;
147 case 'days':
148 if ( isset( $_POST['wc_appointment_availability_from_day_of_week'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_day_of_week'][ $i ] ) ) {
149 $availability->set_from_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_from_day_of_week'][ $i ] ) ) );
150 $availability->set_to_range( wc_clean( wp_unslash( $_POST['wc_appointment_availability_to_day_of_week'][ $i ] ) ) );
151 }
152 break;
153 case 'rrule':
154 // Do nothing rrules are read only for now.
155 break;
156 case 'time':
157 case 'time:1':
158 case 'time:2':
159 case 'time:3':
160 case 'time:4':
161 case 'time:5':
162 case 'time:6':
163 case 'time:7':
164 if ( isset( $_POST['wc_appointment_availability_from_time'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_time'][ $i ] ) ) {
165 $availability->set_from_range( wc_appointment_sanitize_time( wp_unslash( $_POST['wc_appointment_availability_from_time'][ $i ] ) ) );
166 $availability->set_to_range( wc_appointment_sanitize_time( wp_unslash( $_POST['wc_appointment_availability_to_time'][ $i ] ) ) );
167 }
168 break;
169 case 'time:range':
170 case 'custom:daterange':
171 if ( isset( $_POST['wc_appointment_availability_from_time'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_time'][ $i ] ) ) {
172 $availability->set_from_range( wc_appointment_sanitize_time( wp_unslash( $_POST['wc_appointment_availability_from_time'][ $i ] ) ) );
173 $availability->set_to_range( wc_appointment_sanitize_time( wp_unslash( $_POST['wc_appointment_availability_to_time'][ $i ] ) ) );
174 }
175 if ( isset( $_POST['wc_appointment_availability_from_date'][ $i ] ) && isset( $_POST['wc_appointment_availability_to_date'][ $i ] ) ) {
176 $availability->set_from_date( wc_clean( wp_unslash( $_POST['wc_appointment_availability_from_date'][ $i ] ) ) );
177 $availability->set_to_date( wc_clean( wp_unslash( $_POST['wc_appointment_availability_to_date'][ $i ] ) ) );
178 }
179 break;
180 }
181
182 #update_option( 'xxx_' . $current_id, $_POST );
183
184 $availability->save();
185 }
186 do_action( 'wc_appointments_global_availability_on_save' );
187 }
188 }
189
190 /**
191 * Get settings array.
192 *
193 * @param string $current_section Current section name.
194 * @return array
195 */
196 public function get_settings( $current_section = '' ) {
197 $settings = array();
198 if ( 'gcal' === $current_section ) {
199 $settings = apply_filters(
200 'wc_appointments_gcal_settings',
201 array(
202 array(
203 'title' => __( 'Google Calendar Sync', 'woocommerce-appointments' ),
204 'type' => 'title',
205 /* translators: 1: link to google calendar sync tutorial */
206 'desc' => sprintf( __( 'To use this integration go through %s instructions.', 'woocommerce-appointments' ), '<a href="https://bookingwp.com/help/setup/wc-appointments/google-calendar-integration/" target="_blank">' . __( 'Google Calendar Integration', 'woocommerce-appointments' ) . '</a>' ),
207 'id' => 'wc_appointments_gcal_options',
208 ),
209 array(
210 'title' => __( 'Client ID', 'woocommerce-appointments' ),
211 'desc' => __( 'Your Google Client ID.', 'woocommerce-appointments' ),
212 'id' => 'wc_appointments_gcal_client_id',
213 'type' => 'text',
214 'default' => '',
215 'autoload' => false,
216 'desc_tip' => true,
217 ),
218 array(
219 'title' => __( 'Client Secret', 'woocommerce-appointments' ),
220 'desc' => __( 'Your Google Client Secret.', 'woocommerce-appointments' ),
221 'id' => 'wc_appointments_gcal_client_secret',
222 'type' => 'text',
223 'default' => '',
224 'autoload' => false,
225 'desc_tip' => true,
226 ),
227 array(
228 'title' => __( 'Authorization', 'woocommerce-appointments' ),
229 'type' => 'gcal_authorization',
230 ),
231 array(
232 'title' => __( 'Calendar ID', 'woocommerce-appointments' ),
233 'type' => 'gcal_calendar_id',
234 'id' => 'wc_appointments_gcal_calendar_id',
235 ),
236 'gcal_twoway' => array(
237 'title' => __( 'Sync Preference', 'woocommerce-appointments' ),
238 'desc' => __( 'Choose the sync preference.', 'woocommerce-appointments' ),
239 'options' => array(
240 'one_way' => __( 'One way - from Store to Google', 'woocommerce-appointments' ),
241 'two_way' => __( 'Two way - between Store and Google', 'woocommerce-appointments' ),
242 ),
243 'id' => 'wc_appointments_gcal_twoway',
244 'default' => 'one_way',
245 'type' => 'select',
246 'class' => 'wc-enhanced-select',
247 'desc_tip' => true,
248 ),
249 'manual_sync' => array(
250 'title' => __( 'Last Sync', 'woocommerce-appointments' ),
251 'type' => 'manual_sync',
252 ),
253 array(
254 'type' => 'sectionend',
255 'id' => 'gcal_twoway_options',
256 ),
257 array(
258 'title' => __( 'Testing', 'woocommerce-appointments' ),
259 'type' => 'title',
260 /* translators: 1: month singular, 2: log file name string */
261 'desc' => sprintf( __( 'Log Google Calendar events, such as API requests, inside %s', 'woocommerce-appointments' ), '<code>woocommerce/logs/' . $this->id . '-' . sanitize_file_name( wp_hash( $this->id ) ) . '.txt</code>' ),
262 'id' => 'wc_appointments_gcal_testing',
263 ),
264 array(
265 'title' => __( 'Debug Log', 'woocommerce-appointments' ),
266 'desc' => __( 'Enable logging', 'woocommerce-appointments' ),
267 'id' => 'wc_appointments_gcal_debug',
268 'default' => 'no',
269 'type' => 'checkbox',
270 ),
271 array(
272 'type' => 'sectionend',
273 'id' => 'gcal_debug_options',
274 ),
275 )
276 );
277
278 // Run Gcal oauth redirect.
279 $gcal_integration_class = wc_appointments_gcal();
280
281 // Get access token.
282 $access_token = $gcal_integration_class->get_access_token();
283
284 // Get calendar ID.
285 $calendar_id = get_option( 'wc_appointments_gcal_calendar_id' );
286
287 // Stop here if access token no active.
288 if ( ! $access_token || ! $calendar_id ) {
289 unset( $settings['manual_sync'] );
290 }
291 }
292
293 return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section );
294 }
295
296 /**
297 * Generate the GCal Authorization field.
298 *
299 * @param mixed $key
300 * @param array $data
301 *
302 * @return string
303 */
304 public function gcal_authorization_setting( $data ) {
305 $client_id = isset( $_POST['wc_appointments_gcal_client_id'] ) ? sanitize_text_field( $_POST['wc_appointments_gcal_client_id'] ) : get_option( 'wc_appointments_gcal_client_id' );
306 $client_secret = isset( $_POST['wc_appointments_gcal_client_secret'] ) ? sanitize_text_field( $_POST['wc_appointments_gcal_client_secret'] ) : get_option( 'wc_appointments_gcal_client_secret' );
307
308 // Run Gcal oauth redirect.
309 $gcal_integration_class = wc_appointments_gcal();
310
311 // Get access token.
312 $access_token = $gcal_integration_class->get_access_token();
313
314 ob_start();
315 ?>
316 <tr valign="top">
317 <th scope="row" class="titledesc">
318 <?php echo wp_kses_post( $data['title'] ); ?>
319 </th>
320 <td class="forminp">
321 <input type="hidden" name="wc_appointments_google_calendar_redirect" id="wc_appointments_google_calendar_redirect">
322 <?php if ( ! $access_token && ( $client_id && $client_secret ) ) : ?>
323 <button type="button" class="button oauth_redirect" data-staff="0" data-logout="0"><?php esc_html_e( 'Connect with Google', 'woocommerce-appointments' ); ?></button>
324 <?php elseif ( $access_token ) : ?>
325 <p style="color:green;"><?php esc_html_e( 'Successfully authenticated.', 'woocommerce-appointments' ); ?></p>
326 <p class="submit"><button type="button" class="button oauth_redirect" data-staff="0" data-logout="1"><?php esc_html_e( 'Disconnect', 'woocommerce-appointments' ); ?></button></p>
327 <?php else : ?>
328 <p style="color:red;"><?php esc_html_e( 'Please fill out all required fields from above.', 'woocommerce-appointments' ); ?></p>
329 <?php endif; ?>
330 </td>
331 </tr>
332 <?php
333 echo ob_get_clean();
334 }
335
336 /**
337 * Generate the GCal Authorization field.
338 *
339 * @param mixed $key
340 * @param array $data
341 *
342 * @return string
343 */
344 public function gcal_calendar_id_setting( $data ) {
345 $client_id = get_option( 'wc_appointments_gcal_client_id' );
346 $client_secret = get_option( 'wc_appointments_gcal_client_secret' );
347 $calendar_id = get_option( 'wc_appointments_gcal_calendar_id' );
348
349 // Run Gcal oauth redirect.
350 $gcal_integration_class = wc_appointments_gcal();
351
352 // Get access token.
353 $access_token = $gcal_integration_class->get_access_token();
354
355 // Get calendars array.
356 $get_calendars = $gcal_integration_class->get_calendars();
357
358 if ( ! $access_token || ! $client_id || ! $client_secret ) {
359 return;
360 }
361
362 ob_start();
363 ?>
364 <tr valign="top">
365 <th scope="row" class="titledesc">
366 <label for="wc_appointments_gcal_calendar_id">
367 <?php echo wp_kses_post( $data['title'] ); ?>
368 <?php echo wc_help_tip( esc_html__( 'Your Google Calendar ID. Leave empty if you only want to sync on staff level.', 'woocommerce-appointments' ) ); ?>
369 </label>
370 </th>
371 <td class="forminp">
372 <?php if ( $get_calendars ) : ?>
373 <select id="wc_appointments_gcal_calendar_id" name="wc_appointments_gcal_calendar_id" class="wc-enhanced-select" style="width:25em;">
374 <option value=""><?php esc_html_e( 'N/A', 'woocommerce-appointments' ); ?></option>
375 <?php
376 // Check if authorized.
377 if ( $access_token ) {
378 foreach ( $get_calendars as $cal_id => $cal_name ) {
379 ?>
380 <option value="<?php echo esc_attr( $cal_id ); ?>" <?php selected( $calendar_id, $cal_id ); ?>><?php echo esc_attr( $cal_name ); ?></option>
381 <?php
382 }
383 }
384 ?>
385 </select>
386 <?php else : ?>
387 <input type="text" name="wc_appointments_gcal_calendar_id" id="wc_appointments_gcal_calendar_id" value="<?php echo esc_attr( $calendar_id ); ?>">
388 <?php endif; ?>
389 </td>
390 </tr>
391 <?php
392 echo ob_get_clean();
393 }
394
395 /**
396 * Generate the Manual Sync field.
397 *
398 * @param mixed $key
399 * @param array $data
400 *
401 * @return string
402 */
403 public function manual_sync_setting( $data ) {
404 $client_id = isset( $_POST['wc_appointments_gcal_client_id'] ) ? sanitize_text_field( $_POST['wc_appointments_gcal_client_id'] ) : false;
405 $twoway = isset( $_POST['wc_appointments_gcal_twoway'] ) ? $_POST['wc_appointments_gcal_twoway'] : false;
406
407 $get_twoway = get_option( 'wc_appointments_gcal_twoway' );
408
409 // Two-way sync not configured yet, enable by default.
410 $twoway_enabled = ( 'two_way' !== $get_twoway ) ? false : true;
411 $twoway_enabled = $twoway ? true : $twoway_enabled;
412
413 if ( ! $twoway_enabled ) {
414 return;
415 }
416
417 ob_start();
418 ?>
419 <tr valign="top">
420 <th scope="row" class="titledesc">
421 <?php echo wp_kses_post( $data['title'] ); ?>
422 </th>
423 <td class="forminp">
424 <?php
425 $last_synced = get_option( 'wc_appointments_gcal_availability_last_synced' );
426 $last_synced = $last_synced ? $last_synced : '';
427 if ( $last_synced ) {
428 $ls_timestamp = isset( $last_synced[0] ) && $last_synced[0] ? absint( $last_synced[0] ) : absint( current_time( 'timestamp' ) );
429 /* translators: 1: date format, 2: time format */
430 $ls_message = sprintf( __( '%1$s, %2$s', 'woocommerce-appointments' ), date_i18n( wc_date_format(), $ls_timestamp ), date_i18n( wc_time_format(), $ls_timestamp ) );
431 ?>
432 <p class="last_synced"><?php echo esc_attr( $ls_message ); ?></p>
433 <?php } else { ?>
434 <p class="last_synced"><?php esc_html_e( 'No synced rules.', 'woocommerce-appointments' ); ?></p>
435 <?php } ?>
436 <p class="submit">
437 <button type="button" class="button manual_sync"><?php esc_html_e( 'Sync Manually', 'woocommerce-appointments' ); ?></button>
438 </p>
439 </td>
440 </tr>
441 <?php
442 echo ob_get_clean();
443 }
444
445}
446
447return new WC_Appointments_Admin_Settings();
448