-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronjob.php
445 lines (361 loc) · 12.3 KB
/
cronjob.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
use OpenAgenda\TEC\The_Event_Calendar;
use OpenAgendaAPI\OpenAgendaApi;
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly.
/**
* Create a file with the date to avoid launching cron twice
*/
function wp_openagenda_create_pid() {
$date = date( 'd F Y @ H\hi:s' );
$file = fopen( THFO_OPENWP_PLUGIN_PATH . 'pid.txt', 'w+' );
fwrite( $file, $date );
fclose( $file );
}
function wp_openagenda_delete_pid() {
if ( file_exists( THFO_OPENWP_PLUGIN_PATH . 'pid.txt' ) ) {
unlink( THFO_OPENWP_PLUGIN_PATH . 'pid.txt' );
}
}
if ( openagenda_fs()->is__premium_only() ) {
add_action( 'openagenda_hourly_event', 'register_venue__premium_only', 10 );
add_action( 'openagenda_hourly_event', 'import_oa_events__premium_only', 20 );
add_action( 'openagenda_hourly_event', 'export_event__premium_only', 30 );
}
/**
* Register Venue from OpenAgenda
*/
function register_venue__premium_only() {
$openagenda = new OpenAgendaApi();
$url_oa = $openagenda->get_agenda_list__premium_only();
/**
* Get UID for each
*/
foreach ( $url_oa as $url ) {
$uid = $openagenda->openwp_get_uid( $url );
$json = wp_remote_get( 'https://openagenda.com/agendas/' . $uid . '/locations.json' );
if ( 200 === (int) wp_remote_retrieve_response_code( $json ) ) {
$body = wp_remote_retrieve_body( $json );
$decoded_body = json_decode( $body, true );
/**
* get all venue to update if exists
*/
foreach ( $decoded_body['items'] as $location ) {
$venues = $openagenda->get_venue__premium_only( $location['uid'] );
$name = implode( ' - ', array(
$location['name'],
$location['city'],
$location['countryCode'],
$location['uid'],
) );
if ( empty( $venues ) ) {
$insert = wp_insert_term( $name, 'openagenda_venue' );
if ( is_wp_error( $insert ) ) {
$error = 'Fatal Error -- Import OpenAgenda: ' . $insert->get_error_message();
error_log( $error );
} else {
update_term_meta( $insert['term_id'], '_oa_location_uid', $location['uid'] );
}
} else {
foreach ( $venues as $venue ) {
$locationuid = get_term_meta( $venue->term_id, '_oa_location_uid' );
$args = array(
'name' => $name,
);
// si $locationuid existe alors update
$locationuid = intval( $locationuid[0] );
if ( $location['uid'] === $locationuid ) {
wp_update_term( $venue->term_id, 'openagenda_venue', $args );
}
}
}
}
}
}
}
/**
* Import OA events from OpenAgenda to WordPress
*/
if ( ! empty( $_GET['test'] ) && 'ok' === $_GET['test'] ) {
add_action( 'admin_init', 'import_oa_events__premium_only' );
}
function import_oa_events__premium_only( $url_oa = '' ) {
$openagenda = new OpenAgendaApi();
if ( empty( $url_oa ) ) {
$url_oa = $openagenda->get_agenda_list__premium_only();
foreach ( $url_oa as $url ) {
$agendas[ $url ] = $openagenda->thfo_openwp_retrieve_data( $url, 999, 'current' );
}
} else {
$agendas[ $url_oa ] = $openagenda->thfo_openwp_retrieve_data( $url_oa, 999, 'current' );
}
foreach ( $agendas as $agenda ) {
foreach ( $agenda['events'] as $events ) {
if ( is_null( $events['longDescription']['fr'] ) ) {
$events['longDescription']['fr'] = $events['description']['fr'];
}
$args = array(
'post_type' => 'openagenda-events',
'meta_key' => '_oa_event_uid',
'meta_value' => $events['uid'],
'post_status' => 'publish',
);
$openagenda_events = get_posts(
$args
);
if ( ! empty( $openagenda_events ) ) {
$id = $openagenda_events[0]->ID;
}
// Date Formating
$start = array_pop( array_reverse( $events['timings'] ) );
$start1 = $start['start'];
$start_firstday = strtotime( $start1 );
$start2 = array_pop( $events['timings'] );
$start_lastday = $start2['start'];
$start_lastday = strtotime( $start_lastday );
/**
* Add support to TEC
*/
if ( The_Event_Calendar::$tec_activated ) {
$args = The_Event_Calendar::prepare_data( $id, $start_firstday, $end_lastday, $events );
} else {
$args = array(
'ID' => $id,
'post_content' => $events['longDescription']['fr'],
'post_title' => $events['title']['fr'],
'post_excerpt' => $events['description']['fr'],
'post_status' => 'publish',
'post_type' => $post_type,
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'_oa_conditions' => $events['conditions']['fr'],
'_oa_event_uid' => $events['uid'],
'_oa_tools' => $events['registrationUrl'],
'_oa_min_age' => $events['age']['min'],
'_oa_max_age' => $events['age']['max'],
),
);
}
$insert = wp_insert_post( $args );
carbon_set_post_meta( $insert, 'oa_start', $start_firstday );
carbon_set_post_meta( $insert, 'oa_end', $start_lastday );
//handicap
$i = 0;
foreach ( $events['accessibility'] as $accessibility ) {
add_post_meta( $insert, "_oa_a11y|||$i|value", $accessibility );
$i ++;
}
unset( $i );
// Insert Post Term venue
$venues = $openagenda->get_venue__premium_only( $events['location']['uid'] );
$venues_id = array();
foreach ( $venues as $venue ) {
array_push( $venues_id, $venue->term_id );
}
if ( ! empty( $venues_id ) ) {
wp_set_post_terms( $insert, $venues_id, 'openagenda_venue' );
}
// insert origin Agenda
$agendas = get_term_by( 'name', 'https://openagenda.com/' . $events['origin']['slug'], 'openagenda_agenda' );
if ( ! empty( $agendas ) ) {
wp_set_post_terms( $insert, $agendas->term_id, 'openagenda_agenda' );
}
// insert Keywords
wp_set_post_terms( $insert, $events['keywords']['fr'], 'openagenda_keyword' );
// insert post thumbnail
// Gives us access to the download_url() and wp_handle_sideload() functions
require_once( ABSPATH . 'wp-admin/includes/file.php' );
// Download file to temp dir
$timeout_seconds = 5;
$url = $events['originalImage'];
$filename = basename( $url );
$file_exists = MediaFileAlreadyExists( $filename );
// Download file to temp dir.
$temp_file = download_url( $url, $timeout_seconds );
if ( ! is_wp_error( $temp_file ) && false === $file_exists ) {
// Array based on $_FILE as seen in PHP file uploads.
$file = array(
'name' => basename( $url ), // ex: wp-header-logo.png
'type' => 'image/png',
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize( $temp_file ),
);
$overrides = array(
/*
* Tells WordPress to not look for the POST form fields that would
* normally be present, default is true, we downloaded the file from
* a remote server, so there will be no form fields.
*/
'test_form' => false,
// Setting this to false lets WordPress allow empty files, not recommended.
'test_size' => true,
// A properly uploaded file will pass this test. There should be no reason to override this one.
'test_upload' => true,
);
// Move the temporary file into the uploads directory.
$results = wp_handle_sideload( $file, $overrides );
$wp_upload_dir = wp_upload_dir();
if ( empty( $results['error'] ) ) {
$filename = $results['file']; // Full path to the file.
$filetype = wp_check_filetype( $filename, null );
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $insert );
update_post_meta( $attachment_id, '_wp_attachment_image_alt', $events['title']['fr'] );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once ABSPATH . 'wp-admin/includes/image.php';
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $insert, $attachment_id );
}
}
}
}
}
add_action( 'save_post_openagenda-events', 'export_event__premium_only' );
function export_event__premium_only() {
$locale = get_locale();
$locale = explode( '_', $locale );
$locale = $locale[0];
$openagenda = new OpenAgendaApi();
$options = array( 'lang' => 'fr' );
$agendas = $openagenda->get_agenda_list__premium_only();
$accessToken = $openagenda->get_acces_token();
foreach ( $agendas as $agenda ) {
$agendaUid = $openagenda->openwp_get_uid( $agenda );
// Get Post openagenda-events
$events = get_posts(
array(
'post_type' => 'openagenda-events',
)
);
foreach ( $events as $event ) {
$eventuid = carbon_get_post_meta( $event->ID, 'oa_event_uid' );
if ( empty( $eventuid ) ) {
//create
$route = "https://api.openagenda.com/v2/agendas/$agendaUid/events";
} else {
//update
$route = "https://api.openagenda.com/v2/agendas/$agendaUid/events/$eventuid";
}
extract( array_merge( array(
'lang' => 'fr'
), $options ) );
// retrieve event keywords
$keywords = wp_get_post_terms( $event->ID, 'openagenda_keyword' );
if ( ! empty( $keywords ) ) {
$keys = array();
foreach ( $keywords as $keyword ) {
array_push( $keys, $keyword->name );
}
$keywords = implode( ', ', $keys );
}
// get min age
$min_age = get_post_meta( $event->ID, '_oa_min_age' );
// get max age
$max_age = get_post_meta( $event->ID, '_oa_max_age' );
$age = array(
'min' => $min_age[0],
'max' => $max_age[0],
);
// get conditions
$conditions = get_post_meta( $event->ID, '_oa_conditions' );
//get registration
$registrations = get_post_meta( $event->ID, '_oa_tools' );
// retrieve locationUID
$locationuid = wp_get_post_terms( $event->ID, 'openagenda_venue' );
$locationuid = get_term_meta( $locationuid[0]->term_id, '_oa_location_uid' );
// get start date
$debut = carbon_get_post_meta( $event->ID, 'oa_start' );
//get end date
$fin = carbon_get_post_meta( $event->ID, 'oa_end' );
// day number between start and en of the events
$diff = $fin - $debut;
$diff = ceil( $diff / 86400 );
$i = 0;
$dates = [];
$date = [];
while ( $i < $diff ) {
$debut = intval( $debut );
$end = ( $debut + 86400 * $i ) + 7200;
$date = array(
'begin' => date( 'Y-m-d\Th:i:00+0200', $debut + 86400 * $i ),
'end' => date( 'Y-m-d\Th:i:00+0200', $end ),
);
array_push( $dates, $date );
$i ++;
}
//a11y
$a11y = carbon_get_post_meta( $event->ID, 'oa_a11y' );
}
if ( empty( $event->post_excerpt ) ) {
$excerpt = __( 'No data found', 'wp-openagenda' );
} else {
$excerpt = $event->post_excerpt;
}
$data = array(
'slug' => "$event->post_name-" . rand(),
'title' =>
[
$locale => $event->post_title,
],
'description' =>
[
$locale => $excerpt,
],
'longDescription' =>
[
$locale => $event->post_content,
],
'keywords' =>
[
$locale => $keywords,
],
'age' => $age,
'accessibility' => $a11y,
'conditions' =>
[
$locale => $conditions[0],
],
'registration' => $registrations,
'locationUid' => $locationuid[0],
'timings' => $dates,
);
$imageLocalPath = null;
if ( isset( $data['image'] ) && isset( $data['image']['file'] ) ) {
$imageLocalPath = $data['image']['file'];
unset( $data['image']['file'] );
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $route );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
$posted = array(
'access_token' => $accessToken,
'nonce' => rand(),
'data' => json_encode( $data ),
'lang' => 'fr',
);
if ( $imageLocalPath ) {
$posted['image'] = $imageLocalPath;
}
curl_setopt( $ch, CURLOPT_POSTFIELDS, $posted );
$received_content = curl_exec( $ch );
$decode = json_decode( $received_content, true );
// update event uid
$uid = intval( $decode['event']['uid'] );
if ( $uid ) {
add_post_meta( $event->ID, '_oa_event_uid', $uid );
}
return $decode;
}
}