| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for Import Events into Events Manager |
| 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_EM { |
| 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 |
/** |
| 26 |
* Initialize the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
|
| 32 |
if ( defined( 'EM_POST_TYPE_EVENT' ) ) { |
| 33 |
$this->event_posttype = EM_POST_TYPE_EVENT; |
| 34 |
} else { |
| 35 |
$this->event_posttype = 'event'; |
| 36 |
} |
| 37 |
if ( defined( 'EM_TAXONOMY_CATEGORY' ) ) { |
| 38 |
$this->taxonomy = EM_TAXONOMY_CATEGORY; |
| 39 |
} else { |
| 40 |
$this->taxonomy = 'event-categories'; |
| 41 |
} |
| 42 |
if ( defined( 'EM_POST_TYPE_LOCATION' ) ) { |
| 43 |
$this->venue_posttype = EM_POST_TYPE_LOCATION; |
| 44 |
} else { |
| 45 |
$this->venue_posttype = 'location'; |
| 46 |
} |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Get Posttype and Taxonomy Functions |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function get_event_posttype(){ |
| 57 |
return $this->event_posttype; |
| 58 |
} |
| 59 |
public function get_venue_posttype(){ |
| 60 |
return $this->venue_posttype; |
| 61 |
} |
| 62 |
public function get_taxonomy(){ |
| 63 |
return $this->taxonomy; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* import event into TEC |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @param array $centralize event array. |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function import_event( $centralize_array, $event_args ){ |
| 74 |
global $wpdb, $ife_events; |
| 75 |
|
| 76 |
if( empty( $centralize_array ) || !isset( $centralize_array['ID'] ) ){ |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$is_exitsing_event = $ife_events->common->get_event_by_event_id( $this->event_posttype, $centralize_array['ID'] ); |
| 81 |
|
| 82 |
if ( $is_exitsing_event ) { |
| 83 |
// Update event or not? |
| 84 |
$options = ife_get_import_options( $centralize_array['origin'] ); |
| 85 |
$update_events = isset( $options['update_events'] ) ? $options['update_events'] : 'no'; |
| 86 |
if ( 'yes' != $update_events ) { |
| 87 |
return array( 'status'=> 'skipped' ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$origin_event_id = $centralize_array['ID']; |
| 92 |
$post_title = isset( $centralize_array['name'] ) ? $centralize_array['name'] : ''; |
| 93 |
$post_description = isset( $centralize_array['description'] ) ? $centralize_array['description'] : ''; |
| 94 |
$start_time = $centralize_array['starttime_local']; |
| 95 |
$end_time = $centralize_array['endtime_local']; |
| 96 |
$ticket_uri = $centralize_array['url']; |
| 97 |
|
| 98 |
$emeventdata = array( |
| 99 |
'post_title' => $post_title, |
| 100 |
'post_content' => $post_description, |
| 101 |
'post_type' => $this->event_posttype, |
| 102 |
'post_status' => 'pending', |
| 103 |
); |
| 104 |
if ( $is_exitsing_event ) { |
| 105 |
$emeventdata['ID'] = $is_exitsing_event; |
| 106 |
} |
| 107 |
if( isset( $event_args['event_status'] ) && $event_args['event_status'] != '' ){ |
| 108 |
$emeventdata['post_status'] = $event_args['event_status']; |
| 109 |
} |
| 110 |
|
| 111 |
$inserted_event_id = wp_insert_post( $emeventdata, true ); |
| 112 |
|
| 113 |
if ( ! is_wp_error( $inserted_event_id ) ) { |
| 114 |
$inserted_event = get_post( $inserted_event_id ); |
| 115 |
if ( empty( $inserted_event ) ) { return '';} |
| 116 |
|
| 117 |
// Asign event category. |
| 118 |
$ife_cats = isset( $event_args['event_cats'] ) ? $event_args['event_cats'] : array(); |
| 119 |
if ( ! empty( $ife_cats ) ) { |
| 120 |
foreach ( $ife_cats as $ife_catk => $ife_catv ) { |
| 121 |
$ife_cats[ $ife_catk ] = (int) $ife_catv; |
| 122 |
} |
| 123 |
} |
| 124 |
if ( ! empty( $ife_cats ) ) { |
| 125 |
wp_set_object_terms( $inserted_event_id, $ife_cats, $this->taxonomy ); |
| 126 |
} |
| 127 |
|
| 128 |
// Assign Featured images |
| 129 |
$event_image = $centralize_array['image_url']; |
| 130 |
if( $event_image != '' ){ |
| 131 |
$ife_events->common->setup_featured_image_to_event( $inserted_event_id, $event_image ); |
| 132 |
}else{ |
| 133 |
if( $is_exitsing_event ){ |
| 134 |
delete_post_thumbnail( $inserted_event_id ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if ( $is_exitsing_event ) { |
| 139 |
$location_id = $this->get_location_args( $centralize_array['location'], $inserted_event_id ); |
| 140 |
}else{ |
| 141 |
$location_id = $this->get_location_args( $centralize_array['location'], false ); |
| 142 |
} |
| 143 |
|
| 144 |
$event_status = null; |
| 145 |
if ( $inserted_event->post_status == 'publish' ) { $event_status = 1;} |
| 146 |
if ( $inserted_event->post_status == 'pending' ) { $event_status = 0;} |
| 147 |
// Save Meta. |
| 148 |
update_post_meta( $inserted_event_id, '_event_start_time', date( 'H:i:s', $start_time ) ); |
| 149 |
update_post_meta( $inserted_event_id, '_event_end_time', date( 'H:i:s', $end_time ) ); |
| 150 |
update_post_meta( $inserted_event_id, '_event_all_day', 0 ); |
| 151 |
update_post_meta( $inserted_event_id, '_event_start_date', date( 'Y-m-d', $start_time ) ); |
| 152 |
update_post_meta( $inserted_event_id, '_event_end_date', date( 'Y-m-d', $end_time ) ); |
| 153 |
update_post_meta( $inserted_event_id, '_location_id', $location_id ); |
| 154 |
update_post_meta( $inserted_event_id, '_event_status', $event_status ); |
| 155 |
update_post_meta( $inserted_event_id, '_event_private', 0 ); |
| 156 |
update_post_meta( $inserted_event_id, '_start_ts', str_pad( $start_time, 10, 0, STR_PAD_LEFT)); |
| 157 |
update_post_meta( $inserted_event_id, '_end_ts', str_pad( $end_time, 10, 0, STR_PAD_LEFT)); |
| 158 |
update_post_meta( $inserted_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); |
| 159 |
update_post_meta( $inserted_event_id, 'ife_event_link', esc_url( $ticket_uri ) ); |
| 160 |
update_post_meta( $inserted_event_id, 'ife_event_origin', $event_args['import_origin'] ); |
| 161 |
|
| 162 |
// Custom table Details |
| 163 |
$event_array = array( |
| 164 |
'post_id' => $inserted_event_id, |
| 165 |
'event_slug' => $inserted_event->post_name, |
| 166 |
'event_owner' => $inserted_event->post_author, |
| 167 |
'event_name' => $inserted_event->post_title, |
| 168 |
'event_start_time' => date( 'H:i:s', $start_time ), |
| 169 |
'event_end_time' => date( 'H:i:s', $end_time ), |
| 170 |
'event_all_day' => 0, |
| 171 |
'event_start_date' => date( 'Y-m-d', $start_time ), |
| 172 |
'event_end_date' => date( 'Y-m-d', $end_time ), |
| 173 |
'post_content' => $inserted_event->post_content, |
| 174 |
'location_id' => $location_id, |
| 175 |
'event_status' => $event_status, |
| 176 |
'event_date_created' => $inserted_event->post_date, |
| 177 |
); |
| 178 |
|
| 179 |
$event_table = ( defined( 'EM_EVENTS_TABLE' ) ? EM_EVENTS_TABLE : $wpdb->prefix . 'em_events' ); |
| 180 |
if ( $is_exitsing_event ) { |
| 181 |
$eve_id = get_post_meta( $inserted_event_id, '_event_id', true ); |
| 182 |
$where = array( 'event_id' => $eve_id ); |
| 183 |
$wpdb->update( $event_table , $event_array, $where ); |
| 184 |
}else{ |
| 185 |
if ( $wpdb->insert( $event_table , $event_array ) ) { |
| 186 |
update_post_meta( $inserted_event_id, '_event_id', $wpdb->insert_id ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if ( $is_exitsing_event ) { |
| 191 |
do_action( 'ife_after_update_em_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array ); |
| 192 |
return array( |
| 193 |
'status' => 'updated', |
| 194 |
'id' => $inserted_event_id |
| 195 |
); |
| 196 |
}else{ |
| 197 |
do_action( 'ife_after_create_em_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array ); |
| 198 |
return array( |
| 199 |
'status' => 'created', |
| 200 |
'id' => $inserted_event_id |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
}else{ |
| 205 |
return array( 'status'=> 0, 'message'=> 'Something went wrong, please try again.' ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Set Location for event |
| 211 |
* |
| 212 |
* @since 1.0.0 |
| 213 |
* @param array $venue location. |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
public function get_location_args( $venue, $event_id = false ) { |
| 217 |
global $wpdb, $ife_events; |
| 218 |
|
| 219 |
if ( !isset( $venue['ID'] ) ) { |
| 220 |
return null; |
| 221 |
} |
| 222 |
$existing_venue = $this->get_venue_by_id( $venue['ID'] ); |
| 223 |
|
| 224 |
if ( $existing_venue && is_numeric( $existing_venue ) && $existing_venue > 0 && !$event_id ) { |
| 225 |
return get_post_meta( $existing_venue, '_location_id', true ); |
| 226 |
} |
| 227 |
|
| 228 |
$locationdata = array( |
| 229 |
'post_title' => isset( $venue['name'] ) ? $venue['name'] : 'Untitled - Location', |
| 230 |
'post_content' => '', |
| 231 |
'post_type' => $this->venue_posttype, |
| 232 |
'post_status' => 'publish', |
| 233 |
); |
| 234 |
|
| 235 |
if ( $existing_venue && is_numeric( $existing_venue ) && $existing_venue > 0 ) { |
| 236 |
$locationdata['ID'] = $existing_venue; |
| 237 |
} |
| 238 |
$location_id = wp_insert_post( $locationdata, true ); |
| 239 |
|
| 240 |
if ( ! is_wp_error( $location_id ) ) { |
| 241 |
$blog_id = 0; |
| 242 |
if ( is_multisite() ) { |
| 243 |
$blog_id = get_current_blog_id(); |
| 244 |
} |
| 245 |
$location = get_post( $location_id ); |
| 246 |
if ( empty( $location ) ) { return null;} |
| 247 |
|
| 248 |
// Location information. |
| 249 |
$country = isset( $venue['country'] ) ? $venue['country'] : ''; |
| 250 |
if( strlen( $country ) > 2 && $country != '' ){ |
| 251 |
$country = $ife_events->common->ife_get_country_code( $country ); |
| 252 |
} |
| 253 |
$address = isset( $venue['full_address'] ) ? $venue['full_address'] : $venue['address_1']; |
| 254 |
$city = isset( $venue['city'] ) ? $venue['city'] : ''; |
| 255 |
$state = isset( $venue['state'] ) ? $venue['state'] : ''; |
| 256 |
$zip = isset( $venue['zip'] ) ? $venue['zip'] : ''; |
| 257 |
$lat = isset( $venue['lat'] ) ? round( $venue['lat'], 6 ) : 0.000000; |
| 258 |
$lon = isset( $venue['long'] ) ? round( $venue['long'], 6 ) : 0.000000; |
| 259 |
|
| 260 |
// Save metas. |
| 261 |
update_post_meta( $location_id, '_blog_id', $blog_id ); |
| 262 |
update_post_meta( $location_id, '_location_address', $address ); |
| 263 |
update_post_meta( $location_id, '_location_town', $city ); |
| 264 |
update_post_meta( $location_id, '_location_state', $state ); |
| 265 |
update_post_meta( $location_id, '_location_postcode', $zip ); |
| 266 |
update_post_meta( $location_id, '_location_region','' ); |
| 267 |
update_post_meta( $location_id, '_location_country', $country ); |
| 268 |
update_post_meta( $location_id, '_location_latitude', $lat ); |
| 269 |
update_post_meta( $location_id, '_location_longitude', $lon ); |
| 270 |
update_post_meta( $location_id, '_location_status', 1 ); |
| 271 |
update_post_meta( $location_id, 'ife_event_venue_id', $venue['ID'] ); |
| 272 |
|
| 273 |
global $wpdb; |
| 274 |
$location_array = array( |
| 275 |
'post_id' => $location_id, |
| 276 |
'blog_id' => $blog_id, |
| 277 |
'location_slug' => $location->post_name, |
| 278 |
'location_name' => $location->post_title, |
| 279 |
'location_owner' => $location->post_author, |
| 280 |
'location_address' => $address, |
| 281 |
'location_town' => $city, |
| 282 |
'location_state' => $state, |
| 283 |
'location_postcode' => $zip, |
| 284 |
'location_region' => $state, |
| 285 |
'location_country' => $country, |
| 286 |
'location_latitude' => $lat, |
| 287 |
'location_longitude' => $lon, |
| 288 |
'post_content' => $location->post_content, |
| 289 |
'location_status' => 1, |
| 290 |
'location_private' => 0, |
| 291 |
); |
| 292 |
$location_format = array( '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%s', '%d', '%d' ); |
| 293 |
$where_format = array( '%d' ); |
| 294 |
|
| 295 |
if( defined( 'EM_LOCATIONS_TABLE' ) ){ |
| 296 |
$event_location_table = EM_LOCATIONS_TABLE; |
| 297 |
}else{ |
| 298 |
$event_location_table = $wpdb->prefix . 'em_locations'; |
| 299 |
} |
| 300 |
|
| 301 |
if( $event_id && is_numeric( $event_id ) && $event_id > 0 ){ |
| 302 |
$loc_id = get_post_meta( $event_id, '_location_id', true ); |
| 303 |
if( $loc_id != '' ){ |
| 304 |
$where = array( 'location_id' => $loc_id ); |
| 305 |
$is_update = $wpdb->update( $event_location_table, $location_array, $where, $location_format, $where_format ); |
| 306 |
if ( false !== $is_update ) { |
| 307 |
return $loc_id; |
| 308 |
} |
| 309 |
|
| 310 |
}else{ |
| 311 |
$is_insert = $wpdb->insert( $event_location_table , $location_array, $location_format ); |
| 312 |
if ( false !== $is_insert ) { |
| 313 |
$insert_loc_id = $wpdb->insert_id; |
| 314 |
update_post_meta( $location_id, '_location_id', $insert_loc_id ); |
| 315 |
return $insert_loc_id; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
}else{ |
| 320 |
$is_insert = $wpdb->insert( $event_location_table , $location_array, $location_format ); |
| 321 |
if ( false !== $is_insert ) { |
| 322 |
$insert_loc_id = $wpdb->insert_id; |
| 323 |
update_post_meta( $location_id, '_location_id', $insert_loc_id ); |
| 324 |
return $insert_loc_id; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
return null; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Check for Existing TEC Venue |
| 333 |
* |
| 334 |
* @since 1.0.0 |
| 335 |
* @param int $venue_id Venue id. |
| 336 |
* @return int/boolean |
| 337 |
*/ |
| 338 |
public function get_venue_by_id( $venue_id ) { |
| 339 |
$existing_venue = get_posts( array( |
| 340 |
'posts_per_page' => 1, |
| 341 |
'post_type' => $this->venue_posttype, |
| 342 |
'meta_key' => 'ife_event_venue_id', |
| 343 |
'meta_value' => $venue_id, |
| 344 |
'suppress_filters' => false, |
| 345 |
) ); |
| 346 |
|
| 347 |
if ( is_array( $existing_venue ) && ! empty( $existing_venue ) ) { |
| 348 |
return $existing_venue[0]->ID; |
| 349 |
} |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|