-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcamp-importer-tec.php
313 lines (247 loc) · 7.53 KB
/
wordcamp-importer-tec.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
<?php
/**
* Plugin Name: Import the WordCamp Schedule to The Events Calendar
* Plugin URI: https://github.com/bamadesigner/wordcamp-importer-tec
* Description: Imports the WordCamp schedule as events for The Events Calendar WordPress plugin.
* Version: 1.0.0
* Author: Rachel Cherry
* Author URI: https://bamadesigner.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wordcamp-importer-tec
* Domain Path: /languages
*/
// @TODO setup process to check each event to see if it needs to be deleted
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// If you define them, will they be used?
define( 'WORDCAMP_IMPORTER_TEC_VERSION', '1.0.0' );
class WordCamp_Importer_TEC {
/**
* Holds the class instance.
*
* @since 1.0.0
* @access private
* @var WordCamp_Importer_TEC
*/
private static $instance;
/**
* Returns the instance of this class.
*
* @access public
* @since 1.0.0
* @return WordCamp_Importer_TEC
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
/**
* Warming up the engines.
*
* @access public
* @since 1.0.0
*/
protected function __construct() {
// Load our textdomain
add_action( 'init', array( $this, 'textdomain' ) );
// Runs on install
register_activation_hook( __FILE__, array( $this, 'install' ) );
// Runs when the plugin is upgraded
add_action( 'upgrader_process_complete', array( $this, 'upgrader_process_complete' ), 1, 2 );
// Check to see if we need to import the WordCamp schedule
add_action( 'admin_init', array( $this, 'check_import_wordcamp_schedule' ) );
}
/**
* Method to keep our instance from being cloned.
*
* @since 1.0.0
* @access private
* @return void
*/
private function __clone() {}
/**
* Method to keep our instance from being unserialized.
*
* @since 1.0.0
* @access private
* @return void
*/
private function __wakeup() {}
/**
* Runs when the plugin is installed.
*
* @access public
* @since 1.0.0
*/
public function install() {}
/**
* Runs when the plugin is upgraded.
*
* @access public
* @since 1.0.0
*/
public function upgrader_process_complete() {}
/**
* Internationalization FTW.
* Load our textdomain.
*
* @TODO Add language files
*
* @access public
* @since 1.0.0
*/
public function textdomain() {
load_plugin_textdomain( 'wordcamp-importer-tec', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Runs a check in the admin to
* import the schedule once every 24 hours.
*
* @access public
* @since 1.0.0
*/
public function check_import_wordcamp_schedule() {
// There's no point if TEC doesn't exist
if ( ! function_exists( 'tribe_create_event' ) ) {
return false;
}
// See if we need to check the import
$check_import_transient = 'wordcamp_importer_tec_check_import';
$check_import = get_transient( $check_import_transient );
if ( false === $check_import ) {
// Import the schedule
$this->import_wordcamp_schedule();
// Only check the schedule once a day
set_transient( $check_import_transient, time(), DAY_IN_SECONDS );
}
}
/**
* Imports the schedule.
*
* @access public
* @since 1.0.0
*/
public function import_wordcamp_schedule() {
global $wpdb;
// There's no point if TEC doesn't exist
if ( ! function_exists( 'tribe_create_event' ) ) {
return false;
}
// Get the schedule
if ( ( $response = wp_remote_get( 'https://central.wordcamp.org/wp-json/posts?type=wordcamp&filter[posts_per_page]=30' ) )
&& ( $body = wp_remote_retrieve_body( $response ) )
&& ( $schedule = json_decode( $body ) ) ) {
// Get current WordCamp IDs so we don't duplicate events
$wordcamp_ids = $wpdb->get_col( "SELECT meta.meta_value FROM {$wpdb->postmeta} meta INNER JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id AND posts.post_type = 'tribe_events' WHERE meta.meta_key = '_wordcamp_id'" );
// Set datetime format
$date_format = 'Y-m-d'; // H:i:s';
foreach ( $schedule as $event ) {
// Get post ID
$event_post_id = in_array( $event->ID, $wordcamp_ids ) ? $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wordcamp_id' AND meta_value = %s", $event->ID ) ) : false;
// Setup event args
$event_args = array(
'post_title' => $event->title,
'post_content' => $event->content,
'post_status' => 'publish',
'EventAllDay' => true,
'EventTimezone' => $event->date_tz,
);
// Set timezone
$event_timezone = new DateTimeZone( $event->date_tz );
// Will hold venue info
$event_venue = array();
// Process all the meta
foreach ( $event->post_meta as $meta ) {
// Get meta value
$meta_value = $meta->value;
// Get start date
if ( preg_match( '/^Start\sDate/i', $meta->key ) ) {
// We must have a value
if ( $meta_value ) {
// Create DateTime
$start_date = new DateTime( date( 'Y-m-d', $meta_value ), $event_timezone );
// Store for event
$event_args['EventStartDate'] = $start_date->format( $date_format );
} else {
// Change status to draft since no start date
$event_args['post_status'] = 'draft';
// No start date
$event_args['EventStartDate'] = null;
}
} elseif ( preg_match( '/^End\sDate/i', $meta->key ) ) {
// We must have a value
if ( $meta_value ) {
// Create DateTime
$end_date = new DateTime( date( 'Y-m-d', $meta_value ), $event_timezone );
// Store for event
$event_args['EventEndDate'] = $end_date->format( $date_format );
}
} elseif ( $meta_value ) {
switch ( $meta->key ) {
case 'URL':
$event_args['_EventURL'] = $meta_value;
break;
case 'Venue Name':
$event_venue['Venue'] = $meta_value;
break;
}
}
}
// If we have event info
if ( ! empty( $event_venue ) ) {
$event_args['Venue'] = $event_venue;
}
// If we have event info...
if ( ! empty( $event_args ) ) {
// Make sure we have an end date
if ( empty( $event_args['EventEndDate'] ) ) {
$event_args['EventEndDate'] = $event_args['EventStartDate'];
}
// Update or create the event
if ( in_array( $event->ID, $wordcamp_ids ) ) {
// Update the event
if ( $event_post_id > 0 ) {
/*
* @TODO:
* Don't update the post content until
* I figure out how to not override changes
*/
unset( $event_args['post_content'] );
tribe_update_event( $event_post_id, $event_args );
}
} elseif ( $event_post_id = tribe_create_event( $event_args ) ) {
// Store the WordCamp ID
if ( $event_post_id > 0 ) {
add_post_meta( $event_post_id, '_wordcamp_id', $event->ID, true );
}
}
// Make sure the category is set
if ( $event_post_id > 0 ) {
wp_set_object_terms( $event_post_id, 'wordcamps', 'tribe_events_cat', true );
}
}
}
}
}
}
/**
* Returns the instance of our main WordCamp_Importer_TEC class.
*
* Will come in handy when we need to access the
* class to retrieve data throughout the plugin.
*
* @since 1.0.0
* @access public
* @return WordCamp_Importer_TEC
*/
function wordcamp_importer_tec() {
return WordCamp_Importer_TEC::instance();
}
// Let's get this show on the road
wordcamp_importer_tec();