-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-events-class.php
246 lines (192 loc) · 6.43 KB
/
import-events-class.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
<?php
use Carbon\Carbon;
class ImportEvents {
protected $developer_key = 'AIzaSyAqezaiecr8sKISZv7BV0sS2SnNXXKFav0';
protected $client;
protected $service;
protected $application_name = 'Grace Calendar Sync';
protected $scopes;
protected $calendarId = '[email protected]';
protected $start_time;
protected $end_time;
public function __construct() {
$date = date( 'm/d/Y h:i:s a', time() );
echo 'beginning import at ' . $date . ' ' . PHP_EOL;
$this->bootstrap();
$this->setScopes();
$this->setGoogleClientAndSerice();
$this->setTimes();
$this->import();
echo 'FINISHED' . PHP_EOL;
}
protected function bootstrap() {
echo 'CONFIRMING ON CLI' . PHP_EOL;
if ( php_sapi_name() != 'cli' ) {
throw new Exception( 'This application must be run on the command line.' );
}
echo 'SETTING MEMORY LIMIT' . PHP_EOL;
ini_set( 'memory_limit', '750M' );
echo 'autoloading' . PHP_EOL;
require_once 'vendor/autoload.php';
// echo 'CHECKING IF WordPress IS LOADED';
// if ( ! defined( 'ABSPATH' ) ) {
// echo 'LOADING WordPress';
// require_once 'public_html/wp-load.php';
// }
echo 'SETTING TIMEZONE' . PHP_EOL;
date_default_timezone_set( 'America/New_York' );
echo 'CHECKING THAT SERVER NAME IS SET';
if ( empty( $_SERVER['SERVER_NAME'] ) ) {
echo 'SETTING SERVER NAME';
$_SERVER['SERVER_NAME'] = 'CLI';
}
echo 'PURGING CACHE';
sg_cachepress_purge_everything();
echo 'BOOTSTRAPPING COMPLETE' . PHP_EOL;
}
protected function setGoogleClientAndSerice() {
echo 'CONFIGURING GOOGLE CLIENT' . PHP_EOL;
$client = new Google_Client();
$client->setDeveloperKey( $this->developer_key );
$client->setApplicationName( $this->application_name );
$client->setScopes( $this->scopes );
$client->setAccessType( 'offline' );
$this->client = $client;
$this->service = new Google_Service_Calendar( $client );
echo 'FINISHED CONFIGURING GOOGLE CLIENT' . PHP_EOL;
}
protected function setScopes() {
$this->scopes = implode( ' ', array( Google_Service_Calendar::CALENDAR_READONLY ) );
}
protected function update_event( $event ) {
echo 'UPDATING EVENT' . PHP_EOL;
echo $event->visibility;
echo $event->summary;
echo $event->getSummary();
$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' );
$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;
$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['google_id'] . ' | ' . $wp_event['event_title_google'] . ' | ' . $status . PHP_EOL;
}
public function get_page( $optParams ) {
echo 'STARTING PAGE' . PHP_EOL;
try {
$results = $this->service->events->listEvents( $this->calendarId, $optParams );
} catch ( \Google_Service_Exception $e ) {
$code = $e->getCode();
if ( $code == 410 ) {
echo PHP_EOL . 'NEED TO RESYNC' . PHP_EOL;
$optParams = array(
'maxResults' => 250,
'singleEvents' => true,
'timeMin' => $this->start_time,
'timeMax' => $this->end_time,
'showDeleted' => true,
);
return $this->get_page( $optParams );
}
}
foreach ( $results->getItems() as $event ) {
try {
$this->update_event( $event );
} catch ( Exception $e ) {
print( $e );
die();
}
}
if ( $results->getNextPageToken() ) {
$optParams = array(
'pageToken' => $results->getNextPageToken(),
'maxResults' => 250,
'singleEvents' => true,
'timeMin' => $this->start_time,
'timeMax' => $this->end_time,
'showDeleted' => true,
);
return $this->get_page( $optParams );
}
update_option( 'google_calendar_sync_token', $results->getNextSyncToken() );
echo 'FINISHED IMPORT sync token: ' . $results->getNextSyncToken();
}
protected function setTimes() {
echo 'SETTING TIMES' . PHP_EOL;
$start_time = new Carbon( 'now', new DateTimeZone( 'America/New_York' ) );
$start_time->subDays( 15 );
$this->start_time = $start_time->format( 'c' );
$end_time = new Carbon( 'now', new DateTimeZone( 'America/New_York' ) );
$end_time->addDays( 90 );
$this->end_time = $end_time->format( 'c' );
echo 'FINISHING TIMES' . PHP_EOL;
}
public function import() {
echo 'STARTING IMPORT' . PHP_EOL;
$sync_token = get_option( 'google_calendar_sync_token', false );
$sync_token = false; # disabling sync tokne for now
print( $sync_token );
if ( $sync_token ) {
$optParams = array(
'syncToken' => $sync_token,
);
} else {
$optParams = array(
'maxResults' => 250,
'singleEvents' => true,
'timeMin' => $this->start_time,
'timeMax' => $this->end_time,
'showDeleted' => true,
);
}
$this->get_page( $optParams );
echo 'PURGING CACHE';
sg_cachepress_purge_everything();
$date = date( 'm/d/Y h:i:s a', time() );
echo 'ending import at ' . $date . ' ' . PHP_EOL;
}
}
new ImportEvents();