-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-events.php
142 lines (100 loc) · 3.48 KB
/
import-events.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
<?php
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar( $client );
$start_time = new Carbon( 'now', new DateTimeZone( 'America/New_York' ) );
$start_time->subDays( 120 );
$start_time = $start_time->format( 'c' );
$end_time = new Carbon( 'now', new DateTimeZone( 'America/New_York' ) );
$end_time->addDays( 365 );
$end_time = $end_time->format( 'c' );
$sync_token = get_option( 'google_calendar_sync_token', false );
$sync_token = false;
$calendarId = '[email protected]';
if ( $sync_token ) {
$optParams = array(
'syncToken' => $sync_token,
);
} else {
$optParams = array(
'maxResults' => 100,
'singleEvents' => true,
'timeMin' => $start_time,
'timeMax' => $end_time,
'showDeleted' => true,
'calendarId' => $calendarId,
);
}
get_google_calendar_page( $optParams, $service );
function get_google_calendar_page( $optParams, $service, $calendarId ) {
echo PHP_EOL . 'NEW PAGE' . PHP_EOL;
$results = $service->events->listEvents( $calendarId, $optParams );
$google_ids = array();
foreach ( $results->getItems() as $event ) {
update_google_event( $event );
}
if ( $results->getNextPageToken() ) {
$optParams = array(
'nextPageToken' => $results->getNextPageToken(),
);
get_page( $optParams, $service, $calendarId );
} else {
update_option( 'google_calendar_sync_token', $results->getNextSyncToken() );
echo 'sync token update: ' . $results->getNextSyncToken();
dd();
}
}
function update_google_event( $event ) {
$status = $event->status;
$status = $status == 'cancelled' ? 'trash' : 'publish';
$google_ids[] = $event->getId();
$start = $event->start->dateTime;
if ( empty( $start ) ) {
$start = $event->start->date . ' 00:00:00';
}
$start = new Carbon( $start, new DateTimeZone( 'America/New_York' ) );
$start = $start->format( 'Y-m-d H:i:s' );
$end = $event->end->dateTime;
if ( empty( $end ) ) {
$end = $event->end->date . ' 23:59:59';
}
$end = new Carbon( $end, new DateTimeZone( 'America/New_York' ) );
$end->subDay( 1 );
$end = $end->format( 'Y-m-d H:i:s' );
$location = $event->getLocation();
$wp_event = array();
$wp_event['event_title_google'] = $event->getSummary();
$wp_event['event_description_google'] = $event->getDescription();
$wp_event['event_start_time_google'] = $start;
$wp_event['event_end_time_google'] = $end;
$wp_event['event_location_google'] = $event->getLocation();
$wp_event['google_id'] = $event->getId();
$wp_event['google_url'] = $event->htmlLink;
$args = array(
'meta_query' => array(
array(
'key' => 'google_id',
'value' => $wp_event['google_id'],
'compare' => '=',
),
),
'post_type' => 'event',
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'trash' ),
);
$posts = get_posts( $args );
$id = count( $posts ) > 0 ? $posts[0]->ID : 0;
echo $wp_event['google_id'] . '|' . $id . PHP_EOL;
$args = array(
'ID' => $id,
'post_date' => $wp_event['event_start_time_google'],
'post_title' => $wp_event['event_title_google'],
'post_status' => $status,
'post_type' => 'event',
);
$id = wp_insert_post( $args, true );
foreach ( $wp_event as $key => $value ) {
update_field( $key, $value, $id );
}
post_process_event( $id );
echo $id . ' | ' . $wp_event['event_title_google'] . ' | ' . $status . PHP_EOL;
}