| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for Import Events into The Events Calendar |
| 4 |
* |
| 5 |
* @link http://xylusthemes.com/ |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Import_Facebook_Events |
| 9 |
* @subpackage Import_Facebook_Events/includes |
| 10 |
*/ |
| 11 |
// Exit if accessed directly |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
class Import_Facebook_Events_TEC { |
| 15 |
|
| 16 |
// The Events Calendar Event Taxonomy |
| 17 |
protected $taxonomy; |
| 18 |
|
| 19 |
// The Events Calendar Event Posttype |
| 20 |
protected $event_posttype; |
| 21 |
|
| 22 |
// The Events Calendar Venue Posttype |
| 23 |
protected $venue_posttype; |
| 24 |
|
| 25 |
// The Events Calendar Oraganizer Posttype |
| 26 |
protected $oraganizer_posttype; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize the class and set its properties. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
|
| 35 |
$this->taxonomy = 'tribe_events_cat'; |
| 36 |
if ( class_exists( 'Tribe__Events__Main' ) ) { |
| 37 |
$this->event_posttype = Tribe__Events__Main::POSTTYPE; |
| 38 |
}else{ |
| 39 |
$this->event_posttype = 'tribe_events'; |
| 40 |
} |
| 41 |
|
| 42 |
if ( class_exists( 'Tribe__Events__Organizer' ) ) { |
| 43 |
$this->oraganizer_posttype = Tribe__Events__Organizer::POSTTYPE; |
| 44 |
}else{ |
| 45 |
$this->oraganizer_posttype = 'tribe_organizer'; |
| 46 |
} |
| 47 |
|
| 48 |
if ( class_exists( 'Tribe__Events__Venue' ) ) { |
| 49 |
$this->venue_posttype = Tribe__Events__Venue::POSTTYPE; |
| 50 |
}else{ |
| 51 |
$this->venue_posttype = 'tribe_venue'; |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
/** |
| 56 |
* Get Posttype and Taxonomy Functions |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get_event_posttype(){ |
| 61 |
return $this->event_posttype; |
| 62 |
} |
| 63 |
public function get_oraganizer_posttype(){ |
| 64 |
return $this->oraganizer_posttype; |
| 65 |
} |
| 66 |
public function get_venue_posttype(){ |
| 67 |
return $this->venue_posttype; |
| 68 |
} |
| 69 |
public function get_taxonomy(){ |
| 70 |
return $this->taxonomy; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* import event into TEC |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* @param array $centralize event array. |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function import_event( $centralize_array, $event_args ){ |
| 81 |
global $ife_events; |
| 82 |
|
| 83 |
$is_exitsing_event = $ife_events->common->get_event_by_event_id( $this->event_posttype, $centralize_array['ID'] ); |
| 84 |
$formated_args = $this->format_event_args_for_tec( $centralize_array ); |
| 85 |
if( isset( $event_args['event_status'] ) && $event_args['event_status'] != '' ){ |
| 86 |
$formated_args['post_status'] = $event_args['event_status']; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $is_exitsing_event && is_numeric( $is_exitsing_event ) && $is_exitsing_event > 0 ) { |
| 90 |
$options = ife_get_import_options( $centralize_array['origin'] ); |
| 91 |
$update_events = isset( $options['update_events'] ) ? $options['update_events'] : 'no'; |
| 92 |
if ( 'yes' == $update_events ) { |
| 93 |
return $this->update_event( $is_exitsing_event, $centralize_array, $formated_args, $event_args ); |
| 94 |
}else{ |
| 95 |
return array( 'status'=> 'skipped' ); |
| 96 |
} |
| 97 |
} else { |
| 98 |
return $this->create_event( $centralize_array, $formated_args, $event_args ); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Create New TEC event. |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* @param array $eventbrite_event Eventbrite event. |
| 108 |
* @param array $formated_args Formated arguments for eventbrite event. |
| 109 |
* @param int $post_id Post id. |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function create_event( $centralize_array = array(), $formated_args = array(), $event_args = array() ) { |
| 113 |
// Create event using TEC advanced functions. |
| 114 |
global $ife_events; |
| 115 |
$new_event_id = tribe_create_event( $formated_args ); |
| 116 |
if ( $new_event_id ) { |
| 117 |
update_post_meta( $new_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); |
| 118 |
update_post_meta( $new_event_id, 'ife_event_origin', $event_args['import_origin'] ); |
| 119 |
update_post_meta( $new_event_id, 'ife_event_link', esc_url( $centralize_array['url'] ) ); |
| 120 |
|
| 121 |
// Asign event category. |
| 122 |
$ife_cats = isset( $event_args['event_cats'] ) ? $event_args['event_cats'] : array(); |
| 123 |
if ( ! empty( $ife_cats ) ) { |
| 124 |
foreach ( $ife_cats as $ife_catk => $ife_catv ) { |
| 125 |
$ife_cats[ $ife_catk ] = (int) $ife_catv; |
| 126 |
} |
| 127 |
} |
| 128 |
if ( ! empty( $ife_cats ) ) { |
| 129 |
wp_set_object_terms( $new_event_id, $ife_cats, $this->taxonomy ); |
| 130 |
} |
| 131 |
|
| 132 |
$event_featured_image = $centralize_array['image_url']; |
| 133 |
if( $event_featured_image != '' ){ |
| 134 |
$ife_events->common->setup_featured_image_to_event( $new_event_id, $event_featured_image ); |
| 135 |
} |
| 136 |
|
| 137 |
do_action( 'ife_after_create_tec_'.$centralize_array["origin"].'_event', $new_event_id, $formated_args, $centralize_array ); |
| 138 |
return array( |
| 139 |
'status' => 'created', |
| 140 |
'id' => $new_event_id |
| 141 |
); |
| 142 |
|
| 143 |
}else{ |
| 144 |
$ife_errors[] = __( 'Something went wrong, please try again.', 'import-facebook-events' ); |
| 145 |
return; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Update eventbrite event. |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* @param array $centralize_array Eventbrite event. |
| 155 |
* @param array $formated_args Formated arguments for eventbrite event. |
| 156 |
* @param int $post_id Post id. |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function update_event( $event_id, $centralize_array, $formated_args = array(), $event_args = array() ) { |
| 160 |
// Update event using TEC advanced functions. |
| 161 |
global $ife_events; |
| 162 |
|
| 163 |
$update_event_id = tribe_update_event( $event_id, $formated_args ); |
| 164 |
if ( $update_event_id ) { |
| 165 |
update_post_meta( $update_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); |
| 166 |
update_post_meta( $update_event_id, 'ife_event_origin', $event_args['import_origin'] ); |
| 167 |
update_post_meta( $update_event_id, 'ife_event_link', esc_url( $centralize_array['url'] ) ); |
| 168 |
|
| 169 |
// Asign event category. |
| 170 |
$ife_cats = isset( $event_args['event_cats'] ) ? (array) $event_args['event_cats'] : array(); |
| 171 |
if ( ! empty( $ife_cats ) ) { |
| 172 |
foreach ( $ife_cats as $ife_catk => $ife_catv ) { |
| 173 |
$ife_cats[ $ife_catk ] = (int) $ife_catv; |
| 174 |
} |
| 175 |
} |
| 176 |
if ( ! empty( $ife_cats ) ) { |
| 177 |
wp_set_object_terms( $update_event_id, $ife_cats, $this->taxonomy ); |
| 178 |
} |
| 179 |
|
| 180 |
$event_featured_image = $centralize_array['image_url']; |
| 181 |
if( $event_featured_image != '' ){ |
| 182 |
$ife_events->common->setup_featured_image_to_event( $update_event_id, $event_featured_image ); |
| 183 |
}else{ |
| 184 |
delete_post_thumbnail( $update_event_id ); |
| 185 |
} |
| 186 |
|
| 187 |
do_action( 'ife_after_update_tec_'.$centralize_array["origin"].'_event', $update_event_id, $formated_args, $centralize_array ); |
| 188 |
return array( |
| 189 |
'status' => 'updated', |
| 190 |
'id' => $update_event_id |
| 191 |
); |
| 192 |
}else{ |
| 193 |
$ife_errors[] = __( 'Something went wrong, please try again.', 'import-facebook-events' ); |
| 194 |
return; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Format events arguments as per TEC |
| 201 |
* |
| 202 |
* @since 1.0.0 |
| 203 |
* @param array $centralize_array Eventbrite event. |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function format_event_args_for_tec( $centralize_array ) { |
| 207 |
|
| 208 |
if( empty( $centralize_array ) ){ |
| 209 |
return; |
| 210 |
} |
| 211 |
$start_time = $centralize_array['starttime_local']; |
| 212 |
$end_time = $centralize_array['endtime_local']; |
| 213 |
$event_args = array( |
| 214 |
'post_type' => $this->event_posttype, |
| 215 |
'post_title' => $centralize_array['name'], |
| 216 |
'post_status' => 'pending', |
| 217 |
'post_content' => $centralize_array['description'], |
| 218 |
'EventStartDate' => date( 'Y-m-d', $start_time ), |
| 219 |
'EventStartHour' => date( 'h', $start_time ), |
| 220 |
'EventStartMinute' => date( 'i', $start_time ), |
| 221 |
'EventStartMeridian' => date( 'a', $start_time ), |
| 222 |
'EventEndDate' => date( 'Y-m-d', $end_time ), |
| 223 |
'EventEndHour' => date( 'h', $end_time ), |
| 224 |
'EventEndMinute' => date( 'i', $end_time ), |
| 225 |
'EventEndMeridian' => date( 'a', $end_time ), |
| 226 |
'EventStartDateUTC' => !empty( $centralize_array['startime_utc'] ) ? date( 'Y-m-d H:i:s', $centralize_array['startime_utc'] ) : '', |
| 227 |
'EventEndDateUTC' => !empty( $centralize_array['endtime_utc'] ) ? date( 'Y-m-d H:i:s', $centralize_array['endtime_utc'] ) : '', |
| 228 |
'EventURL' => $centralize_array['url'], |
| 229 |
'EventShowMap' => 1, |
| 230 |
'EventShowMapLink' => 1, |
| 231 |
); |
| 232 |
|
| 233 |
if ( array_key_exists( 'organizer', $centralize_array ) ) { |
| 234 |
$event_args['organizer'] = $this->get_organizer_args( $centralize_array['organizer'] ); |
| 235 |
} |
| 236 |
|
| 237 |
if ( array_key_exists( 'location', $centralize_array ) ) { |
| 238 |
$event_args['venue'] = $this->get_venue_args( $centralize_array['location'] ); |
| 239 |
} |
| 240 |
return $event_args; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get organizer args for event. |
| 245 |
* |
| 246 |
* @since 1.0.0 |
| 247 |
* @param array $centralize_org_array Location array. |
| 248 |
* @return array |
| 249 |
*/ |
| 250 |
public function get_organizer_args( $centralize_org_array ) { |
| 251 |
|
| 252 |
if ( !isset( $centralize_org_array['ID'] ) ) { |
| 253 |
return null; |
| 254 |
} |
| 255 |
$existing_organizer = $this->get_organizer_by_id( $centralize_org_array['ID'] ); |
| 256 |
if ( $existing_organizer && is_numeric( $existing_organizer ) && $existing_organizer > 0 ) { |
| 257 |
return array( |
| 258 |
'OrganizerID' => $existing_organizer, |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
$create_organizer = tribe_create_organizer( array( |
| 263 |
'Organizer' => isset( $centralize_org_array['name'] ) ? $centralize_org_array['name'] : '', |
| 264 |
'Phone' => isset( $centralize_org_array['phone'] ) ? $centralize_org_array['phone'] : '', |
| 265 |
'Email' => isset( $centralize_org_array['email'] ) ? $centralize_org_array['email'] : '', |
| 266 |
'Website' => isset( $centralize_org_array['url'] ) ? $centralize_org_array['url'] : '', |
| 267 |
) ); |
| 268 |
|
| 269 |
if ( $create_organizer ) { |
| 270 |
update_post_meta( $create_organizer, 'ife_event_organizer_id', $centralize_org_array['ID'] ); |
| 271 |
return array( |
| 272 |
'OrganizerID' => $create_organizer, |
| 273 |
); |
| 274 |
} |
| 275 |
return null; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Get venue args for event |
| 280 |
* |
| 281 |
* @since 1.0.0 |
| 282 |
* @param array $venue venue array. |
| 283 |
* @return array |
| 284 |
*/ |
| 285 |
public function get_venue_args( $venue ) { |
| 286 |
global $ife_events; |
| 287 |
|
| 288 |
if ( !isset( $venue['ID'] ) ) { |
| 289 |
return null; |
| 290 |
} |
| 291 |
$existing_venue = $this->get_venue_by_id( $venue['ID'] ); |
| 292 |
|
| 293 |
if ( $existing_venue && is_numeric( $existing_venue ) && $existing_venue > 0 ) { |
| 294 |
return array( |
| 295 |
'VenueID' => $existing_venue, |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
$country = isset( $venue['country'] ) ? $venue['country'] : ''; |
| 300 |
if( strlen( $country ) > 2 && $country != '' ){ |
| 301 |
$country = $ife_events->common->ife_get_country_code( $country ); |
| 302 |
} |
| 303 |
$create_venue = tribe_create_venue( array( |
| 304 |
'Venue' => isset( $venue['name'] ) ? $venue['name'] : '', |
| 305 |
'Address' => isset( $venue['full_address'] ) ? $venue['full_address'] : $venue['address_1'], |
| 306 |
'City' => isset( $venue['city'] ) ? $venue['city'] : '', |
| 307 |
'State' => isset( $venue['state'] ) ? $venue['state'] : '', |
| 308 |
'Country' => $country, |
| 309 |
'Zip' => isset( $venue['zip'] ) ? $venue['zip'] : '', |
| 310 |
'Phone' => isset( $venue['phone'] ) ? $venue['phone'] : '', |
| 311 |
'ShowMap' => true, |
| 312 |
'ShowMapLink' => true, |
| 313 |
) ); |
| 314 |
|
| 315 |
if ( $create_venue ) { |
| 316 |
update_post_meta( $create_venue, 'ife_event_venue_id', $venue['ID'] ); |
| 317 |
return array( |
| 318 |
'VenueID' => $create_venue, |
| 319 |
); |
| 320 |
} |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Check for Existing TEC Organizer |
| 326 |
* |
| 327 |
* @since 1.0.0 |
| 328 |
* @param int $organizer_id Organizer id. |
| 329 |
* @return int/boolean |
| 330 |
*/ |
| 331 |
public function get_organizer_by_id( $organizer_id ) { |
| 332 |
$existing_organizer = get_posts( array( |
| 333 |
'posts_per_page' => 1, |
| 334 |
'post_type' => $this->oraganizer_posttype, |
| 335 |
'meta_key' => 'ife_event_organizer_id', |
| 336 |
'meta_value' => $organizer_id, |
| 337 |
'suppress_filters' => false, |
| 338 |
) ); |
| 339 |
|
| 340 |
if ( is_array( $existing_organizer ) && ! empty( $existing_organizer ) ) { |
| 341 |
return $existing_organizer[0]->ID; |
| 342 |
} |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Check for Existing TEC Venue |
| 348 |
* |
| 349 |
* @since 1.0.0 |
| 350 |
* @param int $venue_id Venue id. |
| 351 |
* @return int/boolean |
| 352 |
*/ |
| 353 |
public function get_venue_by_id( $venue_id ) { |
| 354 |
$existing_organizer = get_posts( array( |
| 355 |
'posts_per_page' => 1, |
| 356 |
'post_type' => $this->venue_posttype, |
| 357 |
'meta_key' => 'ife_event_venue_id', |
| 358 |
'meta_value' => $venue_id, |
| 359 |
'suppress_filters' => false, |
| 360 |
) ); |
| 361 |
|
| 362 |
if ( is_array( $existing_organizer ) && ! empty( $existing_organizer ) ) { |
| 363 |
return $existing_organizer[0]->ID; |
| 364 |
} |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
} |
| 369 |
|