-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkap-specific-date-gcal-sync.php
375 lines (289 loc) · 13.1 KB
/
bkap-specific-date-gcal-sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* Plugin Name: Export Specific Dates to Google Calendar
* Description: Exporting the specific dates of the product to Google Calendar
* Version: 1.0
* Author: Tyche Softwares
* Author URI: http://www.tychesoftwares.com/
* Requires PHP: 5.6
* WC requires at least: 3.0.0
* WC tested up to: 3.3.4
* Text Domain: bkap-specific-date-gcal-sync
*/
if( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'BKAP_Specific_Date_Gcal_Sync' ) ) :
include_once( 'bkap-sg-class.php' );
/**
* Booking & Appointment Plugin Specific Dates Google Calendar Class
*
* @class BKAP_Specific_Date_Gcal_Sync
*/
class BKAP_Specific_Date_Gcal_Sync {
/**
* Default constructor
*
* @since 1.0
*/
public function __construct() {
if ( ! defined( 'BKAPSG_PLUGIN_PATH' ) ) {
define( 'BKAPSG_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
}
add_action( 'admin_init', array( &$this, 'bkapsg_include_files' ) );
add_action( 'bkap_after_import_events_settings', array( $this, 'bkap_after_import_events_settings_callback' ) );
//add_filter( 'admin_init', array( $this, 'bkap_additional_data_after_timeslots_calculator_callback' ), 10, 3 );
add_action( 'bkap_delete_timeslot', array( $this, 'bkap_delete_timeslot_callback' ), 10, 4 );
//add_action( 'admin_init', array( $this, 'bkapsg_admin_init' ) );
add_action( 'woocommerce_bkap_import_events', array( $this, 'bkapsg_import_events_cron' ) );
add_action( 'woocommerce_bkap_import_events'/* 'admin_init' */, array( $this, 'bkap_admin_insert_event_callback' ) );
}
public static function bkap_admin_insert_event_callback( $product_id = 0 ) {
$product_id = 0;
if ( $product_id == 0 ) {
$all_products = bkap_common::get_woocommerce_product_list( false );
$product_list = array();
if ( isset( $all_products ) && count( $all_products ) > 0 ) {
foreach ( $all_products as $a_key => $a_value ) {
$pro_id = $a_value[ 1 ];
$bookable = bkap_common::bkap_get_bookable_status( $a_value[ 1 ] );
// if the product is bookable
if ( $bookable ) {
$sgcal_info = get_post_meta( $pro_id, '_bkap_date_gcal_ids', array( '123', '456' ) );
$bkap_time_settings = get_post_meta( $pro_id, '_bkap_time_settings', array() );
if ( $sgcal_info === '' ) {
$sgcal_info = array( '123' => '456' );
}
if ( count( $bkap_time_settings ) > 0 ) {
foreach ( $bkap_time_settings as $key => $value ) {
if ( ! empty( $value ) ) {
foreach( $value as $k => $v ) {
if ( strpos( $k, '-' ) !== false ) {
if ( ! empty( $v ) ) {
foreach ( $v as $timeslot_k => $timeslot_v ) { // aLL TIMESLOTS FOR PERTICULAR DATE
$from = $timeslot_v[ 'from_slot_hrs' ] . $timeslot_v[ 'from_slot_min' ];
$to = $timeslot_v[ 'to_slot_hrs' ] . $timeslot_v[ 'to_slot_min' ];
$from_time = $timeslot_v[ 'from_slot_hrs' ] . ":" . $timeslot_v[ 'from_slot_min' ];
$to_time = $timeslot_v[ 'to_slot_hrs' ] . ":" .$timeslot_v[ 'to_slot_min' ];
$specific_key = $k . ':' . $from . '-' . $to;
$event_details = array();
if ( ! isset( $sgcal_info[ $specific_key ] ) ) {
$uid = '';
$event_details[ 'hidden_booking_date' ] = $k;
$event_details[ 'product_id' ] = $pro_id;
$event_details[ 'time_slot' ] = $from_time . " - ". $to_time;
$product_post = get_post( $pro_id );
$recent_author = get_user_by( 'ID', $product_post->post_author );
$event_details[ 'author_name' ] = $recent_author->display_name;
$uid = self::bkapsg_insert_event_to_gcal( $event_details );
if ( $uid ) {
$sgcal_info[ $specific_key ] = $uid;
}
}
}
}
}
}
update_post_meta( $pro_id, '_bkap_date_gcal_ids', $sgcal_info );
}
}
}
}
}
}
}
}
public static function bkapsg_import_events_cron() {
$bkapsg_delete_events_uids = get_option( 'bkapsg_delete_events_uids', array() );
if ( count( $bkapsg_delete_events_uids ) > 0 ) {
$key = get_option( 'bkap_specific_date_key_file' );
$service = get_option( 'bkap_specific_date_service_account' );
$calendar = get_option( 'bkap_specific_date_calendar_used' );
$gcal = new BKAP_Gcal( $key, $service, $calendar );
// $gcal->bkapsg_init();
$pro_id = 0;
$user_id = get_current_user_id(); // user ID
foreach( $bkapsg_delete_events_uids as $key => $event_id ) {
unset( $bkapsg_delete_events_uids[$key] );
$gcal->delete_event( 0, $user_id, $pro_id, $event_id );
update_option( 'bkapsg_delete_events_uids', $bkapsg_delete_events_uids );
}
}
}
public static function bkapsg_admin_init() {
$bkapsg_delete_events_uids = get_option( 'bkapsg_delete_events_uids', array() );
if ( count( $bkapsg_delete_events_uids ) > 0 ) {
$gcal = new BKAP_Gcal( );
// $gcal->bkapsg_init();
$pro_id = 0;
$user_id = get_current_user_id(); // user ID
foreach( $bkapsg_delete_events_uids as $key => $event_id ) {
unset( $bkapsg_delete_events_uids[$key] );
$gcal->delete_event( 0, $user_id, $pro_id, $event_id );
//update_option( 'bkapsg_delete_events_uids', $bkapsg_delete_events_uids );
}
}
}
public function bkapsg_include_files() {
global $wpdb;
$sgbkap_data = get_option( 'sgbkap_data_deleted', 'no' );
if ( $sgbkap_data == 'no' ) {
$select_ids = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key LIKE '_bkap_date_gcal_ids';";
$check_weekday = $wpdb->get_results( $select_ids );
$delete_ids = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key LIKE '_bkap_date_gcal_ids';";
$wpdb->query( $delete_ids );
delete_option( 'bkapsg_delete_events_uids' );
update_option( 'sgbkap_data_deleted', 'yes' );
}
include_once( /* BKAPSG_PLUGIN_PATH . '/ */'bkap-sg-functions.php' );
include_once( /* BKAPSG_PLUGIN_PATH . '/ */'bkap-sg-class.php' );
}
public static function bkap_additional_data_after_timeslots_calculator_callback( /* $settings_data, $product_id, $clean_settings_data */ ) {
$product_ids = bkap_common::get_woocommerce_product_list( false, 'on', 'yes' );
foreach ( $product_ids as $pkey => $pvalue ) {
$sgcal_info = get_post_meta( $product_id, '_bkap_date_gcal_ids', array() );
if ( $sgcal_info == "" ) {
$sgcal_info = array();
}
$bkap_time_settings = $settings_data[ '_bkap_time_settings' ];
foreach ( $bkap_time_settings as $key => $value ) {
foreach ( $value as $k => $v ) {
$from = $v[ 'from_slot_hrs' ] . $v[ 'from_slot_min' ];
$to = $v[ 'to_slot_hrs' ] . $v[ 'to_slot_min' ];
$from_time = $v[ 'from_slot_hrs' ] . ":" . $v[ 'from_slot_min' ];
$to_time = $v[ 'to_slot_hrs' ] . ":" .$v[ 'to_slot_min' ];
$specific_key = $key . ':' . $from . '-' . $to;
$event_details = array();
if ( ! isset( $sgcal_info[ $specific_key ] ) ) {
$uid = '';
$event_details[ 'hidden_booking_date' ] = $key;
$event_details[ 'product_id' ] = $product_id;
$event_details[ 'time_slot' ] = $from_time . " - ". $to_time;
$uid = self::bkapsg_insert_event_to_gcal( $event_details );
if ( $uid ) {
$sgcal_info[ $specific_key ] = $uid;
}
}
}
}
update_post_meta( $product_id, '_bkap_date_gcal_ids', $sgcal_info );
}
//return $settings_data;
}
public static function bkapsg_insert_event_to_gcal( $event_details, $product_id = 0 ) {
$uid = '';
$user_id = get_current_user_id();
$gcal = new BKAP_Specific_Gcal( "extra" );
$gcal->bkapsg_init();
if ( $gcal->get_api_mode( $user_id, $product_id ) == "directly" ) {
$uid = $gcal->insert_event( $event_details, 0, $user_id, $product_id, false );
}
return $uid;
}
public static function bkap_delete_timeslot_callback( $product_id, $day_value, $from_time, $to_time ) {
$pro_id = $product_id;
$user_id = get_current_user_id(); // user ID
//$gcal = new BKAP_Gcal( "extra" );
//$gcal->bkapsg_init();
include_once( BKAPSG_PLUGIN_PATH . '/bkap-sg-class.php' );
$bkap_date_gcal_ids = get_post_meta( $product_id, '_bkap_date_gcal_ids', true );
//delete_option( 'bkapsg_delete_events_uids' );
if ( '' !== $bkap_date_gcal_ids && count( $bkap_date_gcal_ids ) ){
//if ( $gcal->get_api_mode( $user_id, $product_id ) == "directly" ) {
$from_time = str_replace( ":", "", $from_time );
$to_time = str_replace( ":", "", $to_time );
$specific_key = $day_value . ':' . $from_time . '-' . $to_time;
if ( isset( $bkap_date_gcal_ids[ $specific_key ] ) ) {
$bkapsg_delete_events_uids = get_option( 'bkapsg_delete_events_uids', array() );
$event_id = $bkap_date_gcal_ids[ $specific_key ];
if ( in_array( $event_id, $bkapsg_delete_events_uids ) ) {
} else {
$bkapsg_delete_events_uids[] = $event_id;
}
update_option( 'bkapsg_delete_events_uids', $bkapsg_delete_events_uids );
//$gcal->delete_event( 0, $user_id, $pro_id, $event_id );
}
//}
}
}
public static function bkap_after_import_events_settings_callback(){
add_settings_section(
'bkap_google_calendar_sync_section',
__( 'Specific Dates and Google Calendar Sync', 'woocommerce-booking' ),
array( 'BKAP_Specific_Date_Gcal_Sync', 'bkap_google_calendar_sync_section_callback' ),
'bkap_gcal_sync_settings_page'
);
add_settings_field(
'bkap_specific_date_key_file',
__( 'Key File:', 'woocommerce-booking' ),
array( 'BKAP_Specific_Date_Gcal_Sync', 'bkap_specific_date_key_file_callback' ),
'bkap_gcal_sync_settings_page',
'bkap_google_calendar_sync_section',
array( '<br>Enter key file name here without extention, e.g. ab12345678901234567890-privatekey.', 'woocommerce-booking' )
);
add_settings_field(
'bkap_specific_date_service_account',
__( 'Service account email address:', 'woocommerce-booking' ),
array( 'BKAP_Specific_Date_Gcal_Sync', 'bkap_specific_date_service_account_callback' ),
'bkap_gcal_sync_settings_page',
'bkap_google_calendar_sync_section',
array( '<br>Enter Service account email address here, e.g. [email protected].', 'woocommerce-booking' )
);
add_settings_field(
'bkap_specific_date_calendar_used',
__( 'Calendar to be used :', 'woocommerce-booking' ),
array( 'BKAP_Specific_Date_Gcal_Sync', 'bkap_specific_date_calendar_used_callback' ),
'bkap_gcal_sync_settings_page',
'bkap_google_calendar_sync_section',
array( '<br>Enter the ID of the calendar in which your bookings will be saved, e.g. [email protected].', 'woocommerce-booking' )
);
register_setting(
'bkap_gcal_sync_settings',
'bkap_specific_date_key_file'
);
register_setting(
'bkap_gcal_sync_settings',
'bkap_specific_date_service_account'
);
register_setting(
'bkap_gcal_sync_settings',
'bkap_specific_date_calendar_used'
);
}
public static function bkap_google_calendar_sync_section_callback(){}
public static function bkap_specific_date_key_file_callback( $args ){
$bkap_specific_date_key_file = '';
if( get_option( 'bkap_specific_date_key_file' ) == "" ) {
$bkap_specific_date_key_file = "";
} else {
$bkap_specific_date_key_file = get_option( 'bkap_specific_date_key_file' );
}
echo '<input type="text" size="50" name="bkap_specific_date_key_file" id="bkap_specific_date_key_file" value="' . esc_attr( $bkap_specific_date_key_file ) . '" />';
$html = '<label for="bkap_specific_date_key_file"> ' . $args[0] . '</label>';
echo $html;
}
public static function bkap_specific_date_service_account_callback( $args ){
$bkap_specific_date_service_account = '';
if( get_option( 'bkap_specific_date_service_account' ) == "" ) {
$bkap_specific_date_service_account = "";
} else {
$bkap_specific_date_service_account = get_option( 'bkap_specific_date_service_account' );
}
echo '<input type="text" size="50" name="bkap_specific_date_service_account" id="bkap_specific_date_service_account" value="' . esc_attr( $bkap_specific_date_service_account ) . '" />';
$html = '<label for="bkap_specific_date_service_account"> ' . $args[0] . '</label>';
echo $html;
}
public static function bkap_specific_date_calendar_used_callback( $args ){
$bkap_specific_date_calendar_used = '';
if( get_option( 'bkap_specific_date_calendar_used' ) == "" ) {
$bkap_specific_date_calendar_used = "";
} else {
$bkap_specific_date_calendar_used = get_option( 'bkap_specific_date_calendar_used' );
}
echo '<input type="text" size="50" name="bkap_specific_date_calendar_used" id="bkap_specific_date_calendar_used" value="' . esc_attr( $bkap_specific_date_calendar_used ) . '" />';
$html = '<label for="bkap_specific_date_calendar_used"> ' . $args[0] . '</label>';
echo $html;
}
}
$bkap_specific_date_gcal_sync = new BKAP_Specific_Date_Gcal_Sync();
endif;